Key combos now always cancel corresponding events

This commit is contained in:
Jules 2022-03-22 19:58:29 +01:00
parent 8a5a1b1070
commit a49a375eed
3 changed files with 21 additions and 13 deletions

View File

@ -5,6 +5,12 @@ import net.Indyuce.mmocore.skill.cast.PlayerKey;
import org.bukkit.event.Cancellable; import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList; 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 { public class PlayerKeyPressEvent extends PlayerDataEvent implements Cancellable {
private static final HandlerList handlers = new HandlerList(); 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 * only fired if the user has chosen the 'key combos' casting
* method * method
* *
* @param playerData Player pressing the key * @param playerData Player pressing the key
* @param pressed Key being pressed * @param pressed Key being pressed
* @param cancellable Cancellable event being wrapped
*/ */
public PlayerKeyPressEvent(PlayerData playerData, PlayerKey pressed, Cancellable cancellable) { public PlayerKeyPressEvent(PlayerData playerData, PlayerKey pressed, Cancellable cancellable) {
super(playerData); super(playerData);

View File

@ -15,11 +15,14 @@ import org.bukkit.event.player.PlayerSwapHandItemsEvent;
import org.bukkit.event.player.PlayerToggleSneakEvent; 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 { public class PlayerPressKeyListener implements Listener {
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST) @EventHandler(priority = EventPriority.LOWEST)
public void registerCrouchKey(PlayerToggleSneakEvent event) { public void registerCrouchKey(PlayerToggleSneakEvent event) {
if (event.isSneaking()) { if (event.isSneaking()) {
PlayerKeyPressEvent called = new PlayerKeyPressEvent(PlayerData.get(event.getPlayer()), PlayerKey.CROUCH, event); 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) { public void registerClickKey(PlayerInteractEvent event) {
if (event.useItemInHand() != Event.Result.DENY && event.getAction().name().contains("CLICK")) { 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; 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) { public void registerDropKey(PlayerDropItemEvent event) {
PlayerKeyPressEvent called = new PlayerKeyPressEvent(PlayerData.get(event.getPlayer()), PlayerKey.DROP, event); PlayerKeyPressEvent called = new PlayerKeyPressEvent(PlayerData.get(event.getPlayer()), PlayerKey.DROP, event);
Bukkit.getPluginManager().callEvent(called); Bukkit.getPluginManager().callEvent(called);
} }
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST) @EventHandler(priority = EventPriority.LOWEST)
public void registerSwapHandsKey(PlayerSwapHandItemsEvent event) { public void registerSwapHandsKey(PlayerSwapHandItemsEvent event) {
PlayerKeyPressEvent called = new PlayerKeyPressEvent(PlayerData.get(event.getPlayer()), PlayerKey.SWAP_HANDS, event); PlayerKeyPressEvent called = new PlayerKeyPressEvent(PlayerData.get(event.getPlayer()), PlayerKey.SWAP_HANDS, event);
Bukkit.getPluginManager().callEvent(called); Bukkit.getPluginManager().callEvent(called);

View File

@ -100,9 +100,8 @@ public class KeyCombos implements Listener {
if (!event.getData().isCasting()) { if (!event.getData().isCasting()) {
if (event.getPressed() == initializerKey) { if (event.getPressed() == initializerKey) {
// Cancel event if necessary // Always cancel event
if (event.getPressed().shouldCancelEvent()) event.setCancelled(true);
event.setCancelled(true);
// Start combo // Start combo
playerData.setSkillCasting(new CustomSkillCastingHandler(playerData)); playerData.setSkillCasting(new CustomSkillCastingHandler(playerData));
@ -119,9 +118,8 @@ public class KeyCombos implements Listener {
if (comboClickSound != null) if (comboClickSound != null)
comboClickSound.playTo(player); comboClickSound.playTo(player);
// Cancel event if necessary // Always cancel event
if (event.getPressed().shouldCancelEvent()) event.setCancelled(true);
event.setCancelled(true);
// Hash current combo and check // Hash current combo and check
if (combos.containsKey(casting.current)) { if (combos.containsKey(casting.current)) {