How to let EventHandler enter cooldown?

Discussion in 'Spigot Plugin Development' started by QAQT0T, Jul 24, 2019.

  1. Code (Text):
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.scheduler.BukkitRunnable;

    public class MoveListener implements Listener{
       
        @EventHandler
        public void onPlayerMove(PlayerMoveEvent e) {
            Player p = e.getPlayer();
            p.sendMessage(ChatColor.GRAY + "You have left editing.");
            p.sendMessage(ChatColor.RED + "Returning in 1 second...");
            new BukkitRunnable() {
                @Override
                public void run() {
                    p.performCommand("server pvp");
                }
            }.runTaskLater(Main.getInstance(), 20);
        }
    Code like that.When player moves,it'll trigger.I only want to trigger it 1 time before teleport.
    Thanks!
     
  2. What are you trying to do? Your code right now will run with every new move event.
     
  3. The player will trigger it by moving a millimeter.If it doesn't have a cooldown,it will spam.[​IMG]
    like this: https://imgur.com/a/iidslay
     
    #3 QAQT0T, Jul 24, 2019
    Last edited: Jul 24, 2019
  4. You could add him to a set and check if he is in it before executing the command.
     
    • Like Like x 1
  5. Code (Text):
        @EventHandler
        public void onPlayerMove(PlayerMoveEvent e) {
            Player p = e.getPlayer();
            new BukkitRunnable() {
                @Override
                public void run() {
                    if (Bukkit.getPlayer(p.getName()) != null) {
                        p.performCommand("stp pvp");
                        p.sendMessage(ChatColor.GRAY + "You have left editing.");
                        p.sendMessage(ChatColor.RED + "Returning in 2 second...");
                    }
                }
            }.runTaskLater(Main.getInstance(), 30);
        }

    }
    Completed like this.I forgot to check that.
    Thank you!