So I've been building some EventHandlers in Bukkit and now I am stuck on this. It's probably a really dumb mistake, so forgive me for that.
What I want is the following: when I rightclick on a block, it should just put each second: test
in chat. But it actually puts it twice each second. I've been debugging pretty much the whole code, but I'm not getting it working. In my idea it is registering twice.
Main() class:
public class Main extends JavaPlugin {
@Override
public void onEnable() {
getServer().getPluginManager().registerEvents(new Events(), this);
}
@Override
public void onDisable() {
}
}
Events() class:
public class Events implements Listener {
public Plugin plugin = Main.getPlugin(Main.class);
@EventHandler
public void onInteract(PlayerInteractEvent e) {
Player player = e.getPlayer();
Action action = e.getAction();
if(action.equals(Action.RIGHT_CLICK_BLOCK)) {
new BukkitRunnable() {
@Override
public void run() {
player.sendMessage("Test");
}
}.runTaskTimer(plugin, 1, 20);
}
}
}
Thanks in advance!
The reason PlayerInteractEvent
calls twice is that it calls for two hands - once for OFF_HAND
and once for HAND
. So, to handle this event only 1 time you must check exactly hand you want:
if (e.getHand() == EquipmentSlot.HAND) {
// ... Your code with test
}