Cant stop task.

Discussion in 'Plugin Development' started by michael566, Jul 31, 2014.

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

    michael566

    So I am trying to stop one of my tasks with the Bukkit.getserver.getscheduler.canceltask, but its not working.

    Im doing this:
    Code:java
    1. int taskid = Bukkit.getServer().getScheduler().scheduleAsyncRepeatingTask(snip){
    2. @Overrride
    3. public void run() {
    4. //blah blah
    5. };
    6. }
    7. Bukkit.getServer().getScheduler().cancelTask(taskid);
     
  2. Offline

    TheMcScavenger

    Most likely because you're not doing it correctly. The runnable isn't placed correctly.
     
  3. Offline

    xize

    michael566

    could I ask where did you put in the task?, and no the runnable isn't wrong it can also be called this way but if its called inside a event it could lead to some complications.
     
  4. Offline

    michael566

    xize

    I was trying to cancel it with a command.
     
  5. Offline

    mythbusterma

    michael566
    Are you sure the taskid is correct at the location you're calling it from?
     
  6. Offline

    michael566

  7. Offline

    xize

    michael566
    could you show how its been done in the commands?, I suspect you may start a new scheduler everytime you run the command or the int has been reseted.
     
  8. Offline

    mythbusterma

    michael566

    That's normally a cue to show your code because there's probably a mistake.
     
  9. Offline

    michael566


    xize it basically a oncommand and then after that I do this:

    Code:java
    1. Bukkit.getServer().getScheduler().cancelTask(taskid);
     
  10. Offline

    mythbusterma

    michael566

    What about the declaration of taskid and the declaration of the runnable, you can't be stingy with your code.
     
  11. Offline

    xize

    michael566

    do you have a setup smilliar to this?

    Code:
    private int taskid;
     
    public boolean onCommand(CommandSender sender, Command cmd, String commandlabel, String[] args) {
        if(cmd.getName().equalsIgnoreCase("test")) {
              if(args.length == 0) {
                    //runs scheduler here
              } else if(args.length == 1) {
                      if(args[0].equalsIgnoreCase("stop")) {
                               Bukkit.getScheduler().cancelTask(taskid);
                       }
              }
         }
    return false;
    }
    
    because that could be a issue because the taskid field is outside your command which means you override everytime the last task so if you type 2 times /test the first /test taskid will be removed and the latest will be runned, so if you use for example /test stop to cancel the task the first one has never been cancelled.
     
  12. Offline

    michael566

    xize yeah, without args though.
     
  13. Offline

    xize

    michael566
    well in that case you may want to use a BukkitTask and set the BukkitTask to null once when cancelled and only fire a new scheduler when the BukkitTask is null again, or use a new BukkitRunnable() inside the command and cancel it inside the scheduler, I recommend the first approuch though however without code I cannot help you than just geussing.

    also why are you using a async task there?
     
  14. Offline

    michael566

  15. Offline

    xize

    michael566

    yes, if the command is used 2 times the first scheduler won't cancel.

    you could do 2 things though actually 3 but thats kinda confusing:

    1. you can still cancel the scheduler when the command is used 2 times by checking if the task id is not 0.
    that means you should set ID to 0 then as follows you do inside the command:

    Code:
    if(ID > 0) {
        Bukkit.getScheduler().cancelTask(ID);
        ID = yournewscheduler;
    } else {
      ID = yournewscheduler
    }
    
    however if you want to have more multiple users at the same time this will conflict, it means that only one player can have these effects (which I guess is not what you want).

    2. in that case, you can solve it by putting the scheduler outside the command by just calling Bukkit.getScheduler().runTaskTimer(yourplugin, this, 0L, 20L); it asks you to implement the runnable outside the command (which is in my opinion safer because in events and such this could hang them when the scheduler runs inside, but not always), then as follows you can iterate through all online players and check if they contains in a HashSet and if they exist run the effects you can add those players through the command in the HashSet, note that the runTaskTimer does not give a int as task id but rather a BukkitTask object which you from there could cancel when for example the HashSet is empty.

    I hope it helps:)
     
Thread Status:
Not open for further replies.

Share This Page