I had a problem, i wanted to create a loop that uses game ticks that i can stop at any moment, i don't want it to run forever. Also is it possible to do it with for()?
Bukkit.getScheduler().runTaskTimer(plugin, task, delay, period) This return a BukkitTask that can be cancelled anytime with cancel()
I explained very well what i wanted to do, I want to create a loop that has a delay with minecraft game ticks that i can stop in any moment. Please read before answering
Code (Java): BukkitTask task = new BukkitRunnable() { @Override public void run() { // do your task } }.runTaskTimer(plugin, 0, 1L); Runs a specified code every tick. You can simply cancel via Code (Java): task.cancel();
Does nobody link javadocs anymore? Let OP figure at least a little for themselves. #cancel only can be called from the BukkitTask returned when you schedule a task (or you can cancel a task by ID, which increments by 1 for each task previously scheduled), or when you use a BukkitRunnable instead of a Runnable, as Runnables do not have a #cancel method. Take a look at the BukkitScheduler javadocs and the scheduler wiki page.
See the answer above, you get the taskId from the runTaskTimer function, so : int taskId = Bukkit.getScheduler().runTaskTimer(plugin, task, delay, period) Bukkit.getScheduler().cancelTask(taskId) Or just look at the BukkitRunable answer from Schottky
I found that inside a BukkitTask task = new BukkitRunnable() { @Override public void run() { // do your task } }.runTaskTimer(plugin, 0, 1L); you can put cancel(); and it works