Solved What is the new way to use tasks?

Discussion in 'Plugin Development' started by Deleted user, Aug 31, 2014.

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

    Deleted user

    Here is how I use tasks.
    Code:java
    1. public class Task extends BukkitRunnable {
    2. @Override
    3. public void run() {
    4. // Work goes here
    5. }
    6. }

    Code:java
    1. Scheduler.runTask(Plugin, new Task());
    2. // Or
    3. Scheduler.runTaskLater(Plugin, new Task(), Long);
    4. // Or
    5. Scheduler.runTaskTimer(Plugin, new Task(), Long, Long);

    Main problem is that I can't call this.cancel().
    hintss I found http://stackoverflow.com/a/25590842/3453226.
    So what is the new way to use tasks?
     
  2. Offline

    Flamedek

    Joiner
    Try:
    Code:java
    1. Task task = new Task();
    2. // Should be able to do
    3. task.runTaskTimer(Plugin, Long(, Long) );
    4. task.cancel();
     
  3. Offline

    1Rogue

    nothing wrong with what you're doing.
     
  4. Offline

    Deleted user

    1Rogue Thank you! But... I can't use this.cancel()...

    Flamedek Yes! Thank you very much!
    What is the difference between these two lines of code?
    Code:java
    1. BukkitTask task = new ExampleTask();

    Code:java
    1. ExampleTask task = new ExampleTask();


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
  5. Offline

    Flamedek

    Joiner Since you're ExampleTask extends BukkitTask, you can use it as a BukkitTask Object. That would give you access to the BukkitTask (inherited) methods, but not the methods you made yourself in ExampleTask.
     
  6. Offline

    Ivan

    The task has to register itself by calling .runTask... , otherwise you wont be able to call .cancel() to cancel it. You'd have to cancel it through the scheduler.
     
  7. Offline

    1Rogue


    If your anonymous runnable class extends BukkitRunnable, then you can use cancel internally. Otherwise, .runTask returns a BukkitTask object, which you can call cancel() on:

    Code:java
    1. BukkitTask task = this.plugin.getServer().runTaskTimer(this.plugin, /* some runnable */, 20L);
    2. //later...
    3. task.cancel();


    Of course if you need to cancel upon some form of context from the runnable, then you should do that in the runnable, which means you should either have a service that manages scheduling tasks or use BukkitRunnable.
     
  8. Offline

    fireblast709

    1Rogue
    Note: The last bit refers to the runTask*(...) methods provided by BukkitRunnable.
     
  9. Offline

    Deleted user

    fireblast709 Is that the "static" or the "instance" way?
     
  10. Offline

    1Rogue


    Where was I suggesting to pass a BukkitRunnable to the scheduler?
     
  11. 1Rogue You said there was nothing wrong with what he was doing, and he was passing a Runnable to the scheduler
     
  12. Offline

    1Rogue


    Ah I see that now.
     
    AdamQpzm likes this.
  13. Offline

    Deleted user

    Thank you.
     
Thread Status:
Not open for further replies.

Share This Page