How to cancel a repeating class after a certain number of iterations

Discussion in 'Plugin Development' started by moo3oo3oo3, Nov 26, 2014.

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

    moo3oo3oo3

    I would like to cancel a Bukkit runnable sync repeating task after it went through a certain amout of iterations or time. How would I get about doing this?

    I tried to cancel it from within it's self using a if statement to track the number of times it went thoguh but that's doesn't seem to work
    Code:java
    1. final int stop = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
    2. public void run() {
    3. boolean stopIt = false;
    4. Bukkit.broadcastMessage("cancel");
    5. if (plugin.kit.get(player) != "terrorist" && stopIt == false || player.isDead() && stopIt == false) {
    6. Bukkit.getScheduler().cancelTask(tasktnt);
    7. Bukkit.getScheduler().cancelTask(countDown);
    8. Bukkit.getScheduler().cancelTask(stopExplode);
    9. Bukkit.broadcastMessage("switch/died");
    10. stopIt = true;
    11. } else {
    12. if (player.getMaxHealth() - player.getHealth() > 2 && stopIt == false) {
    13. Bukkit.broadcastMessage("hit");
    14. stopIt = true;
    15. Bukkit.getScheduler().cancelTask(tasktnt);
    16. Bukkit.getScheduler().cancelTask(countDown);
    17. Bukkit.getScheduler().cancelTask(stopExplode);
    18.  
    19. org.bukkit.inventory.ItemStack fuse = new ItemStack(Material.SULPHUR);
    20. ItemMeta meta1 = fuse.getItemMeta();
    21. List<String> lores1 = new ArrayList<String>();
    22. lores1.add("§7§oRight click to suicide");
    23. meta1.setDisplayName("§rFuse");
    24. meta1.setLore(lores1);
    25. fuse.setItemMeta(meta1);
    26. player.getInventory().addItem(fuse);
    27. }
    28. }
    29. }
    30. }, 0L, 1L);
     
  2. Offline

    Skionz

    moo3oo3oo3 Increment an integer, check if the integer equals 10 as an example, cancel the runnable.
     
  3. Offline

    moo3oo3oo3

    But I can't cancel it from within, so I have to make another repeating task to check if it reaches the right number. That means I fixed one problem but created another...
     
  4. Offline

    mythbusterma

    moo3oo3oo3

    What's stopping you from canceling it in the task?
     
  5. Offline

    moo3oo3oo3

    eclipse is giving me an error when I try to cancel a repeating task inside the repeating task I'm trying to cancel saying, "The local variable may not have been initialized"
     
  6. Offline

    mythbusterma

    moo3oo3oo3

    .......well don't not initialize a local variable?
     
  7. Offline

    moo3oo3oo3

    Don't not = do, so you just said "well do initialize a local variable?"
     
  8. Offline

    mythbusterma

    moo3oo3oo3

    Indeed I did, but I don't see how that has any relevance to the task at hand.
     
  9. Offline

    fireblast709

    moo3oo3oo3 use a BukkitRunnable instead of a Runnable, then you can simply invoke cancel(). Do note, don't use the scheduler to schedule BukkitRunnables, use the runTask methods BukkitRunnable supplies to schedule the task.
     
    mythbusterma likes this.
  10. Offline

    moo3oo3oo3

    example?
     
  11. Offline

    mythbusterma

    moo3oo3oo3

    Code:java
    1. class ... extends BukkitRunnable {
    2.  
    3. public void run() {
    4. cancel();
    5. }
    6. }
     
  12. Offline

    fireblast709

    mythbusterma likes this.
  13. Offline

    mythbusterma

    GG
     
Thread Status:
Not open for further replies.

Share This Page