From a49a375eed15444d79ab0ab8399565ab782405cb Mon Sep 17 00:00:00 2001 From: Jules Date: Tue, 22 Mar 2022 19:58:29 +0100 Subject: [PATCH] Key combos now always cancel corresponding events --- .../mmocore/api/event/PlayerKeyPressEvent.java | 11 +++++++++-- .../listener/event/PlayerPressKeyListener.java | 13 ++++++++----- .../mmocore/skill/cast/listener/KeyCombos.java | 10 ++++------ 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/main/java/net/Indyuce/mmocore/api/event/PlayerKeyPressEvent.java b/src/main/java/net/Indyuce/mmocore/api/event/PlayerKeyPressEvent.java index 4e8055f8..ae10904a 100644 --- a/src/main/java/net/Indyuce/mmocore/api/event/PlayerKeyPressEvent.java +++ b/src/main/java/net/Indyuce/mmocore/api/event/PlayerKeyPressEvent.java @@ -5,6 +5,12 @@ import net.Indyuce.mmocore.skill.cast.PlayerKey; import org.bukkit.event.Cancellable; import org.bukkit.event.HandlerList; +/** + * Basically an event wrapper for multiple bukkit events that + * correspond to keys the player can use to cast skill combos. + * + * @author jules + */ public class PlayerKeyPressEvent extends PlayerDataEvent implements Cancellable { private static final HandlerList handlers = new HandlerList(); @@ -16,8 +22,9 @@ public class PlayerKeyPressEvent extends PlayerDataEvent implements Cancellable * only fired if the user has chosen the 'key combos' casting * method * - * @param playerData Player pressing the key - * @param pressed Key being pressed + * @param playerData Player pressing the key + * @param pressed Key being pressed + * @param cancellable Cancellable event being wrapped */ public PlayerKeyPressEvent(PlayerData playerData, PlayerKey pressed, Cancellable cancellable) { super(playerData); diff --git a/src/main/java/net/Indyuce/mmocore/listener/event/PlayerPressKeyListener.java b/src/main/java/net/Indyuce/mmocore/listener/event/PlayerPressKeyListener.java index 2598c460..92afba3b 100644 --- a/src/main/java/net/Indyuce/mmocore/listener/event/PlayerPressKeyListener.java +++ b/src/main/java/net/Indyuce/mmocore/listener/event/PlayerPressKeyListener.java @@ -15,11 +15,14 @@ import org.bukkit.event.player.PlayerSwapHandItemsEvent; import org.bukkit.event.player.PlayerToggleSneakEvent; /** - * This registers all the KeyPress events + * This registers all the KeyPress events. All events are registered + * with LOWEST priority so that if the wrapped event happens to be + * cancelled because of a key press, it is canceled before any plugin + * can deal with it. */ public class PlayerPressKeyListener implements Listener { - @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST) + @EventHandler(priority = EventPriority.LOWEST) public void registerCrouchKey(PlayerToggleSneakEvent event) { if (event.isSneaking()) { PlayerKeyPressEvent called = new PlayerKeyPressEvent(PlayerData.get(event.getPlayer()), PlayerKey.CROUCH, event); @@ -27,7 +30,7 @@ public class PlayerPressKeyListener implements Listener { } } - @EventHandler(priority = EventPriority.HIGHEST) + @EventHandler(priority = EventPriority.LOWEST) public void registerClickKey(PlayerInteractEvent event) { if (event.useItemInHand() != Event.Result.DENY && event.getAction().name().contains("CLICK")) { boolean rightClick = event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK; @@ -36,13 +39,13 @@ public class PlayerPressKeyListener implements Listener { } } - @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST) + @EventHandler(priority = EventPriority.LOWEST) public void registerDropKey(PlayerDropItemEvent event) { PlayerKeyPressEvent called = new PlayerKeyPressEvent(PlayerData.get(event.getPlayer()), PlayerKey.DROP, event); Bukkit.getPluginManager().callEvent(called); } - @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST) + @EventHandler(priority = EventPriority.LOWEST) public void registerSwapHandsKey(PlayerSwapHandItemsEvent event) { PlayerKeyPressEvent called = new PlayerKeyPressEvent(PlayerData.get(event.getPlayer()), PlayerKey.SWAP_HANDS, event); Bukkit.getPluginManager().callEvent(called); diff --git a/src/main/java/net/Indyuce/mmocore/skill/cast/listener/KeyCombos.java b/src/main/java/net/Indyuce/mmocore/skill/cast/listener/KeyCombos.java index ce7709ef..686b78a3 100644 --- a/src/main/java/net/Indyuce/mmocore/skill/cast/listener/KeyCombos.java +++ b/src/main/java/net/Indyuce/mmocore/skill/cast/listener/KeyCombos.java @@ -100,9 +100,8 @@ public class KeyCombos implements Listener { if (!event.getData().isCasting()) { if (event.getPressed() == initializerKey) { - // Cancel event if necessary - if (event.getPressed().shouldCancelEvent()) - event.setCancelled(true); + // Always cancel event + event.setCancelled(true); // Start combo playerData.setSkillCasting(new CustomSkillCastingHandler(playerData)); @@ -119,9 +118,8 @@ public class KeyCombos implements Listener { if (comboClickSound != null) comboClickSound.playTo(player); - // Cancel event if necessary - if (event.getPressed().shouldCancelEvent()) - event.setCancelled(true); + // Always cancel event + event.setCancelled(true); // Hash current combo and check if (combos.containsKey(casting.current)) {