Solved Cancel BukkitTimer from inside itself?

Discussion in 'Plugin Development' started by Dudemister1999, Oct 23, 2014.

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

    Dudemister1999

    Hello! I've got a problem I can't solve. Most likely an easy answer, but it's eluding me.
    I've got a Runnable / BukkitTask, and I have all the code I need. But I need to be able to cancel that task within itself. Here's what I have so far:

    Code:java
    1. class BlockUpdater implements Runnable
    2. {
    3. private final BukkitTask instance;
    4.  
    5. public BlockUpdater(BukkitTask t)
    6. {
    7. instance = t;
    8. }
    9.  
    10. @Override
    11. public void run()
    12. {
    13. if(broken)
    14. {
    15. instance.cancel();
    16. }
    17. else if(rand.nextInt(3) == 0)
    18. {
    19. update();
    20. }
    21. }
    22. }


    This is an inner-class, hence the broken & rand variables, and the update method.
     
  2. Offline

    ProtoTempus

  3. Offline

    Dudemister1999

    ProtoTempus I keep forgetting runTaskTimerAsynchronously allows for BukkitTasks. *facepalm* Thanks. I know of that function, I just didn't remember that function allows for not just Runnables.
     
  4. Offline

    ProtoTempus

  5. Offline

    Dudemister1999

    Here's the code, for anybody interested in it.

    Code:java
    1. BukkitTask r = new BukkitRunnable()
    2. {
    3. @Override
    4. public void run()
    5. {
    6. if(broken)
    7. {
    8. this.cancel();
    9. }
    10. else if(rand.nextInt(3) == 0)
    11. {
    12. update();
    13. }
    14. }
    15. }.runTaskTimerAsynchronously(CustomItemsPlugin.instance, 20L, (20 * 2));
     
Thread Status:
Not open for further replies.

Share This Page