Solved Canceling a Repeating Task

Discussion in 'Spigot Plugin Development' started by AppleMonJeans, Feb 2, 2018.

  1. I am having troubles canceling a repeating task here is what I have that is not working.
    Code (Text):
    public class genbucketCobblestone implements Listener {

        int taskID = 0;

        @EventHandler
        public void onGenbucketPlace(PlayerBucketEmptyEvent e) {
            Player p = e.getPlayer();
            Block b = e.getBlockClicked().getRelative(e.getBlockFace());
            Location loc = b.getLocation();

            if (p.getItemInHand().getType().equals(Material.LAVA_BUCKET)) {
                e.setCancelled(true);
                b.setType(Material.COBBLESTONE);
                loc.subtract(0, 1, 0);
                taskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(main.getMain(), new Runnable() {
                    public void run() {
                        if (loc.getBlock().getType().equals(Material.AIR) && loc.getY() > 0) {
                            loc.getBlock().setType(Material.COBBLESTONE);
                            loc.subtract(0, 1, 0);
                        } else {
                            Bukkit.getScheduler().cancelTask(taskID);
                        }
                    }
                }, 20, 20);
            }
        }
    }
     
  2. you need to use a bukkitrunnable and use cancel();
     
    • Agree Agree x 1
  3. Senmori

    Senmori Retired Resource Staff
    Retired Patron

    Get the scheduler using
    Code (Java):
    Bukkit.getScheduler()
    and cancel the task via
    Code (Java):
    Bukkit.getScheduler().cancelTask(int taskId)
     
  4. That is what im doing, but it does not work for me.
     
  5. Senmori

    Senmori Retired Resource Staff
    Retired Patron

    We need to see your code then. We can't help you unless you otherwise.
     
  6. If you want to see my code look at the main post :D
     
  7. FrostedSnowman

    Resource Staff

    store a bukkit task instead.

    Code (Java):

    private BukkiTask task;


    public void foo() {
         this.task = new BukkitRunnable() {
               ...;
        }
    }
    then you can call BukkitTask#cancel
     
  8. How would I make the BukkitTask repeat?
     
  9. Senmori

    Senmori Retired Resource Staff
    Retired Patron

    In your code just use
    Code (Java):
    this.cancel()
    where you are currently cancelling the task and it should cancel itself. If that still doesn't work then you've probably done something really wrong.
     
  10. A BukkitRunnable is the way to go here, you dont even have to store it in a variable. Just call cancel() internally.
     
    • Agree Agree x 1
  11. Thanks to everyone that helped I got it fixed
    Code (Text):
                new BukkitRunnable() {
                    @Override
                    public void run() {
                        if (loc.getBlock().getType().equals(Material.AIR) && loc.getY() > 0 && top.getBlock().getType().equals(Material.COBBLESTONE)) {
                            loc.getBlock().setType(Material.COBBLESTONE);
                            loc.subtract(0, 1, 0);
                        } else {
                            cancel();
                            p.sendMessage(ChatColor.RED + "Your genbucket has ended!");
                        }
                    }
                }.runTaskTimer(main.getMain(), 20, 20);