Solved How do i create game tick loops?

Discussion in 'Spigot Plugin Development' started by FlameFOxYT, Feb 15, 2020.

  1. 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()?
     
  2. Explain what you want to do. Why do you need ticks? How are you going to take the ticks ?
     
  3. Bukkit.getScheduler().runTaskTimer(plugin, task, delay, period)

    This return a BukkitTask that can be cancelled anytime with cancel()
     
  4. cancel() does not work, Do you have an example?
     
  5. Bukkit.getScheduler().cancelTask(taskId)
     
  6. 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
     
  7. 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();
     
  8. 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.
     
    #8 Escad, Feb 15, 2020
    Last edited: Feb 15, 2020
  9. How do i create the id for the task?
    Or it is just 0 because its only 1 task.
     
  10. 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
     
  11. you can call #cancel from within the task for reference.
     
  12. 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
     
    • Like Like x 1