Scheduled task canceled if another player launch it.

Discussion in 'Plugin Development' started by splint, Jun 24, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    splint

    Hi again all. After fixing some bug, I try my plugin on a real server, with other people.

    When a player type a command, a Scheduled task was launched to loop some code. That work, but if another player type the same command, the Scheduled task stop looping for the first player, and work like a charm for the second player.

    This is my part of the code with the Scheduled task:
    Code:java
    1. if (command.getName().toLowerCase().equals("playloop")) {
    2. if (player.hasPermission("spouties.play")) {
    3. Urlcheck urlckeck = new Urlcheck();
    4. urlckeck.urlcheck(sender, command, commandLabel, args);
    5. if (urlckeck.urlcheck == 1) {
    6. if ((args.length > 1)) {
    7. if (args[1].matches("[0-9]+")) {
    8. if (start == 0) {
    9. start = 1;
    10. sender.sendMessage(ChatColor.RED + this.getConfig().getString("msg.playloop"));
    11. final String t = args[1];
    12. int i = Integer.parseInt(t);
    13. i = i * 20;
    14. this.getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable() {
    15. public void run() {
    16. SoundManager music = SpoutManager.getSoundManager();
    17. music.stopMusic(SpoutManager.getPlayer(player), true, 1000);
    18. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Plugin, new Runnable() {
    19. public void run() {
    20. SoundManager music = SpoutManager.getSoundManager();
    21. music.playCustomMusic(Plugin,SpoutManager.getPlayer(player),args[0],false);
    22. }
    23. }, 20);
    24. }
    25. }, 0L, i);
    26. } else {
    27. sender.sendMessage(ChatColor.RED + this.getConfig().getString("msg.loopalreadystarted"));
    28. }
    29. } else {
    30. sender.sendMessage(ChatColor.RED + this.getConfig().getString("msg.wronglooptime"));
    31. }
    32. } else {
    33. sender.sendMessage(ChatColor.RED + this.getConfig().getString("msg.nolooptime"));
    34. }
    35. }
    36. return true;
    37. }
    38.  
    39. else {
    40. sender.sendMessage(ChatColor.RED + this.getConfig().getString("msg.nopermplay"));
    41. }
    42. }


    Thanks in advance, splint33
     
  2. Offline

    The Fancy Whale

    That is because you are using the same task for two different people.
     
  3. Offline

    JEREMSPEED

    Add a boolean! If your scheduling is running put it to true.
    Then, put a test statement.

    Code:java
    1. if (!isScheduleRunning == true) {
    2. schedule.start();
    3. } else {
    4. sender.sendMessage("Already looping...");
    5. }


    You also should use a BukkitRunnable your code would be easier to manage I think.
     
  4. Offline

    splint

    How to run one taks for one player ?
    Because I can't say's "Already looping", that a ambiant region music ;)
     
  5. Offline

    JEREMSPEED

    To do that, you need to create a sub-class that extends BukkitRunnable.
    When a player casts the command, instantiate a new Thread. ;)
    Do you want an exemple?
     
  6. Offline

    splint

    JEREMSPEED
    If you can, yes, because, like some other say, I'm a beguiner at java dev', and I learn it by practice it.

    Thank you in advance ! :)
     
  7. Offline

    JEREMSPEED

    Your sub-class should look like that:

    Code:java
    1. public class myRunnable extends BukkitRunnable {
    2.  
    3. private final CommandSender sender;
    4. private final Plugin plugin;
    5.  
    6. public backup(CommandSender sender , Plugin plugin) {
    7. this.sender = sender;
    8. this.plugin = plugin;
    9. }
    10.  
    11. @Override
    12. public void run() {
    13. //Instructions
    14. }
    15.  
    16. }


    And in the onCommand method, you call it like that:

    Code:java
    1. new Thread( new myRunnable (sender , this) );


    Use "this" only if the onCommand method is located in the main class.
     
Thread Status:
Not open for further replies.

Share This Page