From feeb3aea7809908c38ffc75f96a9bd19c435072c Mon Sep 17 00:00:00 2001 From: Jules Date: Sat, 15 Jan 2022 18:36:54 +0100 Subject: [PATCH] !api for better skill casting --- .../java/net/Indyuce/mmocore/MMOCore.java | 2 + .../api/event/PlayerKeyPressEvent.java | 51 +++++++++++++++++++ .../event/PlayerPressKeyListener.java | 50 ++++++++++++++++++ .../mmocore/skill/cast/CastingMethod.java | 48 +++++++++++++++++ .../Indyuce/mmocore/skill/cast/PlayerKey.java | 29 +++++++++++ .../cast/listener/SkillBar.java} | 42 +++++++-------- 6 files changed, 198 insertions(+), 24 deletions(-) create mode 100644 src/main/java/net/Indyuce/mmocore/api/event/PlayerKeyPressEvent.java create mode 100644 src/main/java/net/Indyuce/mmocore/listener/event/PlayerPressKeyListener.java create mode 100644 src/main/java/net/Indyuce/mmocore/skill/cast/CastingMethod.java create mode 100644 src/main/java/net/Indyuce/mmocore/skill/cast/PlayerKey.java rename src/main/java/net/Indyuce/mmocore/{listener/SpellCast.java => skill/cast/listener/SkillBar.java} (84%) diff --git a/src/main/java/net/Indyuce/mmocore/MMOCore.java b/src/main/java/net/Indyuce/mmocore/MMOCore.java index c465d3eb..8d4b3b8c 100644 --- a/src/main/java/net/Indyuce/mmocore/MMOCore.java +++ b/src/main/java/net/Indyuce/mmocore/MMOCore.java @@ -27,6 +27,7 @@ import net.Indyuce.mmocore.comp.region.WorldGuardRegionHandler; import net.Indyuce.mmocore.comp.vault.VaultEconomy; import net.Indyuce.mmocore.comp.vault.VaultMMOLoader; import net.Indyuce.mmocore.listener.*; +import net.Indyuce.mmocore.listener.event.PlayerPressKeyListener; import net.Indyuce.mmocore.listener.option.*; import net.Indyuce.mmocore.listener.profession.FishingListener; import net.Indyuce.mmocore.listener.profession.PlayerCollectStats; @@ -241,6 +242,7 @@ public class MMOCore extends LuminePlugin { Bukkit.getPluginManager().registerEvents(new GuildListener(), this); Bukkit.getPluginManager().registerEvents(new FishingListener(), this); Bukkit.getPluginManager().registerEvents(new PlayerCollectStats(), this); + Bukkit.getPluginManager().registerEvents(new PlayerPressKeyListener(), this); /* * Initialize player data from all online players. This is very important to do diff --git a/src/main/java/net/Indyuce/mmocore/api/event/PlayerKeyPressEvent.java b/src/main/java/net/Indyuce/mmocore/api/event/PlayerKeyPressEvent.java new file mode 100644 index 00000000..4e8055f8 --- /dev/null +++ b/src/main/java/net/Indyuce/mmocore/api/event/PlayerKeyPressEvent.java @@ -0,0 +1,51 @@ +package net.Indyuce.mmocore.api.event; + +import net.Indyuce.mmocore.api.player.PlayerData; +import net.Indyuce.mmocore.skill.cast.PlayerKey; +import org.bukkit.event.Cancellable; +import org.bukkit.event.HandlerList; + +public class PlayerKeyPressEvent extends PlayerDataEvent implements Cancellable { + private static final HandlerList handlers = new HandlerList(); + + private final Cancellable cancellable; + private final PlayerKey pressed; + + /** + * Called when a player presses some key. This event is + * only fired if the user has chosen the 'key combos' casting + * method + * + * @param playerData Player pressing the key + * @param pressed Key being pressed + */ + public PlayerKeyPressEvent(PlayerData playerData, PlayerKey pressed, Cancellable cancellable) { + super(playerData); + + this.pressed = pressed; + this.cancellable = cancellable; + } + + public PlayerKey getPressed() { + return pressed; + } + + @Override + public boolean isCancelled() { + return cancellable.isCancelled(); + } + + @Override + public void setCancelled(boolean b) { + cancellable.setCancelled(b); + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } +} diff --git a/src/main/java/net/Indyuce/mmocore/listener/event/PlayerPressKeyListener.java b/src/main/java/net/Indyuce/mmocore/listener/event/PlayerPressKeyListener.java new file mode 100644 index 00000000..2598c460 --- /dev/null +++ b/src/main/java/net/Indyuce/mmocore/listener/event/PlayerPressKeyListener.java @@ -0,0 +1,50 @@ +package net.Indyuce.mmocore.listener.event; + +import net.Indyuce.mmocore.api.event.PlayerKeyPressEvent; +import net.Indyuce.mmocore.api.player.PlayerData; +import net.Indyuce.mmocore.skill.cast.PlayerKey; +import org.bukkit.Bukkit; +import org.bukkit.event.Event; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.block.Action; +import org.bukkit.event.player.PlayerDropItemEvent; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.event.player.PlayerSwapHandItemsEvent; +import org.bukkit.event.player.PlayerToggleSneakEvent; + +/** + * This registers all the KeyPress events + */ +public class PlayerPressKeyListener implements Listener { + + @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST) + public void registerCrouchKey(PlayerToggleSneakEvent event) { + if (event.isSneaking()) { + PlayerKeyPressEvent called = new PlayerKeyPressEvent(PlayerData.get(event.getPlayer()), PlayerKey.CROUCH, event); + Bukkit.getPluginManager().callEvent(called); + } + } + + @EventHandler(priority = EventPriority.HIGHEST) + 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; + PlayerKeyPressEvent called = new PlayerKeyPressEvent(PlayerData.get(event.getPlayer()), rightClick ? PlayerKey.RIGHT_CLICK : PlayerKey.LEFT_CLICK, event); + Bukkit.getPluginManager().callEvent(called); + } + } + + @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST) + 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) + 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/CastingMethod.java b/src/main/java/net/Indyuce/mmocore/skill/cast/CastingMethod.java new file mode 100644 index 00000000..a43f15d3 --- /dev/null +++ b/src/main/java/net/Indyuce/mmocore/skill/cast/CastingMethod.java @@ -0,0 +1,48 @@ +package net.Indyuce.mmocore.skill.cast; + +import net.Indyuce.mmocore.skill.cast.listener.SkillBar; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.event.Listener; + +import java.util.function.Function; + +public enum CastingMethod { + + /** + * The first ever casting method to be implemented in MMOCore. + *

+ * When pressing a key, the list of bound skills display on the + * action bar + */ + SKILL_BAR(config-> new SkillBar(config)), + + SKILL_SCROLL; + + /** + * Initialize your skill combo by pressing some key + */ + KEY_COMBOS(), + + /** + * Not implemented yet. + *

+ * This would allow players to cast skills by opening + * a book with all the skills displayed into it and click + * some clickable text to cast the skill. + */ + SPELL_BOOK(), + + /** + * Not implemented yet. + *

+ * Much like the spell book but using a custom GUI instead + * of a spell book to display the available skills. + */ + SPELL_GUI(); + + private final Function listenerLoader; + + CastingMethod(Function listenerLoader) { + this.listenerLoader = listenerLoader; + } +} diff --git a/src/main/java/net/Indyuce/mmocore/skill/cast/PlayerKey.java b/src/main/java/net/Indyuce/mmocore/skill/cast/PlayerKey.java new file mode 100644 index 00000000..c470b910 --- /dev/null +++ b/src/main/java/net/Indyuce/mmocore/skill/cast/PlayerKey.java @@ -0,0 +1,29 @@ +package net.Indyuce.mmocore.skill.cast; + +public enum PlayerKey { + + /** + * When a player left clicks + */ + LEFT_CLICK, + + /** + * When a player right clicks + */ + RIGHT_CLICK, + + /** + * When a player drops the item they are holding + */ + DROP, + + /** + * When a player swaps their hand items + */ + SWAP_HANDS, + + /** + * When a player sneaks (doesn't trigger when unsneaking) + */ + CROUCH; +} diff --git a/src/main/java/net/Indyuce/mmocore/listener/SpellCast.java b/src/main/java/net/Indyuce/mmocore/skill/cast/listener/SkillBar.java similarity index 84% rename from src/main/java/net/Indyuce/mmocore/listener/SpellCast.java rename to src/main/java/net/Indyuce/mmocore/skill/cast/listener/SkillBar.java index 31017c64..8190076e 100644 --- a/src/main/java/net/Indyuce/mmocore/listener/SpellCast.java +++ b/src/main/java/net/Indyuce/mmocore/skill/cast/listener/SkillBar.java @@ -1,15 +1,19 @@ -package net.Indyuce.mmocore.listener; +package net.Indyuce.mmocore.skill.cast.listener; import io.lumine.mythic.lib.api.player.EquipmentSlot; import io.lumine.mythic.lib.player.PlayerMetadata; import io.lumine.mythic.lib.skill.trigger.TriggerMetadata; import net.Indyuce.mmocore.MMOCore; +import net.Indyuce.mmocore.api.event.PlayerKeyPressEvent; import net.Indyuce.mmocore.api.player.PlayerData; +import net.Indyuce.mmocore.listener.event.PlayerPressKeyListener; import net.Indyuce.mmocore.manager.ConfigManager; import net.Indyuce.mmocore.manager.SoundManager; import net.Indyuce.mmocore.skill.ClassSkill; +import net.Indyuce.mmocore.skill.cast.PlayerKey; import org.bukkit.Bukkit; import org.bukkit.GameMode; +import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.HandlerList; @@ -19,36 +23,26 @@ import org.bukkit.event.player.PlayerSwapHandItemsEvent; import org.bukkit.inventory.ItemStack; import org.bukkit.scheduler.BukkitRunnable; -public class SpellCast implements Listener { +import java.util.Objects; + +public class SkillBar implements Listener { + private final PlayerKey mainKey; + + public SkillBar(ConfigurationSection config) { + mainKey = PlayerKey.valueOf(Objects.requireNonNull(config.getString("open"), "Could not find open key")); + } @EventHandler - public void a(PlayerSwapHandItemsEvent event) { - Player player = event.getPlayer(); - ConfigManager.SwapAction action = player.isSneaking() ? MMOCore.plugin.configManager.sneakingSwapAction : MMOCore.plugin.configManager.normalSwapAction; - - // Vanilla action does nothing - if (action == ConfigManager.SwapAction.VANILLA) + public void a(PlayerKeyPressEvent event) { + if (event.getPressed() != mainKey) return; - // Always cancel event if it's not the vanilla action + // Always cancel event event.setCancelled(true); - /* - * Hotbar swap feature, this entirely switches the - * player's hotbar with their 9 lowest inventory slots - */ - if (action == ConfigManager.SwapAction.HOTBAR_SWAP) { - MMOCore.plugin.soundManager.play(player, SoundManager.SoundEvent.HOTBAR_SWAP); - for (int j = 0; j < 9; j++) { - ItemStack replaced = player.getInventory().getItem(j + 9 * 3); - player.getInventory().setItem(j + 9 * 3, player.getInventory().getItem(j)); - player.getInventory().setItem(j, replaced); - } - return; - } - // Enter spell casting - PlayerData playerData = PlayerData.get(player); + Player player = event.getData().getPlayer(); + PlayerData playerData = event.getData(); if (player.getGameMode() != GameMode.SPECTATOR && (MMOCore.plugin.configManager.canCreativeCast || player.getGameMode() != GameMode.CREATIVE) && !playerData.isCasting()