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); } } }
Get the scheduler using Code (Java): Bukkit.getScheduler() and cancel the task via Code (Java): Bukkit.getScheduler().cancelTask(int taskId)
store a bukkit task instead. Code (Java): private BukkiTask task; public void foo() { this.task = new BukkitRunnable() { ...; } } then you can call BukkitTask#cancel
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.
A BukkitRunnable is the way to go here, you dont even have to store it in a variable. Just call cancel() internally.
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);