1.16.1 How to stop this message from spamming

Discussion in 'Spigot Plugin Development' started by Ian1215, Aug 2, 2020 at 12:15 AM.

  1. I'm making a plugin where I want a certain thing to happen when a player right clicks with a certain item. For simplicity's sake, let's say that the player gets sent the message "test" any time they right click with any item:
    Code (Text):
    @EventHandler
    public void onClick(PlayerInteractEvent event) {
        Player player = (Player) event.getPlayer();
        if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK)
            player.sendMessage("test");
    }
    When I do this and hold down right click, it just spams my chat. How do I make it so that it only sends the message once every time I right click?
     
  2. You can’t. It will always fire.
     
    • Agree Agree x 1
  3. Store the last time they clicked and if they click it again within 1 second, return and update the time. This requires the player to wait more than 1 second between click attempts to get the message. Since holding down right click triggers this event more than once a second, your intended behavior will be achieved.