Solved Cancelling a BukkitRunnable

Discussion in 'Plugin Development' started by Code0, Mar 11, 2015.

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

    Code0

    Hey,

    Let's get right to the problem. When I try to use the basic BukkitRunnable "code":
    Code:
    new BukkitRunnable(){
    @Override
    public void run(){
    this.cancel();
    System.out.println("reached");
    }
    }
    , it does print out "reached". So yea... this.cancel(); for some reason doesn't cancel my bukkitrunnables.
     
  2. Offline

    adam753

    Calling cancel doesn't magically end the run method. If you want to end it prematurely, you could use "return".
     
  3. Offline

    Code0

    I am very aware of that. However, since I'm using the BukkitRunnable definiton inside of a method, it would just exit the method, which I don't want.
     
  4. Offline

    adam753

    @Code0
    Return would only exit the innermost method.
    Although there are plenty of other logical ways around it, depending on your situation:
    Code:
    new BukkitRunnable() {
        @Override
        public void run(){
            if(IWantToCancel) {
                this.cancel();
            }
            else {
                System.out.println("reached");
            }
        }
    }
     
  5. Offline

    Code0

    @adam753 After testing this once again, it actually does exit the full method. Also, I know that there are ways around that but I was just for the easiest and fastest solution. I'll test return; once again.

    Thank you for your help :)
     
  6. @Code0
    Code:
    Bukkit.getSchedueler().cancelTask(task);
    That's one way to do it, but if you don't want to specify a task, you could just do
    Code:
    Bukkit.getSchedueler().cancelAllTasks();
     
    Code0 likes this.
  7. Offline

    Code0

    Just so you don't end up doing some stupid mistakes ;) Don't think a BukkitRunnable is a scheduler! That'll make some stuff really confusing ;)
     
  8. Offline

    1Rogue

    Using "return" inside an anonymous class's methods won't return from the method you're writing the entire anonymous class in.
     
Thread Status:
Not open for further replies.

Share This Page