'Timer on a command - Minecraft Plugins

I am trying to add a timer/cooldown to my plugin. I want to be like when they run a command is says please do not move for 3s and then if they don't move it will teleport them but if they do move it will cancel and reset the timer. Thank you for any help!



Solution 1:[1]

There are multiple ways to check this. You could, for example, start a delayed task from Bukkit's built-in scheduler and validate the position there. Or you could set a flag for the user somewhere and check for that in the playerMove event.

You'll want to use Bukkit's internal scheduler in case you decide for the first mentioned option: https://bukkit.gamepedia.com/Scheduler_Programming

// proudly stolen from here:
// https://bukkit.org/threads/making-a-countdown-timer.97071/#post-1312737

this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
  public void run() {
      // Your check if the player actually has moved
      // Your teleport logic
  }
}, 60L); // 60 L == 3 sec, 20 ticks == 1 sec

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 festie