Cancelling BukkitRunnables: cancel() and IllegalStateException

Discussion in 'Plugin Development' started by Drkmaster83, Jan 12, 2014.

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

    Drkmaster83

    When I use this method after the else block is executed, it throws an IllegalStateException that states "Not scheduled yet."
    Code:
    public void playSong(final Player p) {
    getServer().getScheduler().runTaskTimer(this, new BukkitRunnable() {
    @Override
    public void run()
    {
    if(note.hasNext()) p.playSound(p.getLocation(), Sound.NOTE_PIANO, 1f, NotePitch.getNote(note.next()));
    else {
    note = notes.iterator();
    cancel();
    }
    }
    }, 0L, 5L);
    }
    
    It throws the error pointing to the line that uses cancel() (it doesn't cancel the task). Why does it not allow me to cancel it?
     
  2. Because its the first call.
     
  3. Offline

    Zen3515

    I got this error and I still can fix it.....
    my period is 2L
    it create 320 IllegalStateException for me only 1-2 sec.
    DOES ANY ONE KNOW HOW TO FIX THIS WHY IT NOT SCHEDULED YET ?


    Code:java
    1. Bukkit.getScheduler().scheduleSyncRepeatingTask(Nanosuit, new BukkitRunnable(){
    2. @Override
    3. public void run(){
    4. try{
    5. i++;
    6. if(i <= 10){
    7. senddebugmessage("do");
    8. }
    9. else{
    10. senddebugmessage(">10");
    11. this.cancel();
    12. }
    13. }
    14. this.cancel();
    15. }
    16. }
    17. },0L,2L);
     
  4. Offline

    Drkmaster83

    That doesn't make sense, because the iterator should have made it run several times over before executing that code.
     
    Zen3515 likes this.
  5. Offline

    Rocoty

    Wrong.

    Drkmaster83 It is rather because you are using the scheduler to schedule a task with a BukkitRunnable as the Runnable. The BukkitRunnable is handy because you can cancel the task with a method, rather than having to know the ID of the task. The trouble here is that you are not actually starting a task from the BukkitRunnable, so it has nothing to cancel.

    Here is what happens in your case:
    The scheduler waits 0 ticks and executes the run method in the BukkitRunnable. It then waits 5 ticks and repeats.
    The run method runs, but the BukkitRunnable hasn't actually scheduled a task. The Bukkit Scheduler has.

    Here is what you should do:
    Do not use the scheduler. Instead, use the methods from the BukkitRunnable itself. Something like this:

    Code:java
    1. new BukkitRunnable() {
    2.  
    3. @Override
    4. public void run() {
    5. //Something. Here you can have cancel()
    6. }
    7.  
    8. }.runTaskTimer(plugin, delay, step);
     
    Europia79, Exoaria and Zen3515 like this.
  6. Offline

    Zen3515

    WoW this work thanks!!!!!!!!
     
  7. Offline

    Drkmaster83

    Ah, that makes very vague sense but I do see what's wrong. The Scheduler and BukkitRunnables run like two separate components. The issue was I was trying to cancel something which never scheduled itself, right? Just because the scheduler schedules it doesn't mean it can be cancelled from within a constructor of it if there's not one that calls its scheduling functions. I have this right, yeah?

    Thanks a bunch, man!
     
    Rocoty likes this.
  8. Offline

    Rocoty

    Drkmaster83
    Basically, you have got it right. The BukkitRunnable was never actually scheduled. It was just used as a Runnable for another scheduler.
     
  9. its just a guess... :oops:

    (i have never used bukkit runnables or similar stuff i use threads instead)
     
Thread Status:
Not open for further replies.

Share This Page