From 952df8f8b921471083f0f73a4b1ea807edcab00d Mon Sep 17 00:00:00 2001 From: Indyuce Date: Sat, 22 Jan 2022 19:19:16 +0100 Subject: [PATCH] hotbar swap --- .../java/net/Indyuce/mmocore/MMOCore.java | 9 +++- .../api/util/math/format/RomanFormat.java | 9 ---- .../Indyuce/mmocore/gui/eco/GoldPouch.java | 10 +++-- .../compat/UltimateClansGuildModule.java | 3 -- .../mmocore/listener/GoldPouchesListener.java | 17 ++++---- .../mmocore/listener/option/HotbarSwap.java | 41 ++++++++++++++++++ .../option/VanillaExperienceOverride.java | 42 ++++++++++--------- .../mmocore/manager/ConfigManager.java | 10 ----- .../mmocore/skill/cast/SkillCastingMode.java | 2 +- .../mmocore/skill/cast/listener/SkillBar.java | 15 +++---- src/main/resources/config.yml | 12 +----- 11 files changed, 94 insertions(+), 76 deletions(-) delete mode 100644 src/main/java/net/Indyuce/mmocore/api/util/math/format/RomanFormat.java create mode 100644 src/main/java/net/Indyuce/mmocore/listener/option/HotbarSwap.java diff --git a/src/main/java/net/Indyuce/mmocore/MMOCore.java b/src/main/java/net/Indyuce/mmocore/MMOCore.java index bd186966..2a86dc55 100644 --- a/src/main/java/net/Indyuce/mmocore/MMOCore.java +++ b/src/main/java/net/Indyuce/mmocore/MMOCore.java @@ -9,7 +9,6 @@ import net.Indyuce.mmocore.api.ConfigFile; import net.Indyuce.mmocore.api.PlayerActionBar; import net.Indyuce.mmocore.api.player.PlayerData; import net.Indyuce.mmocore.api.player.profess.resource.PlayerResource; -import net.Indyuce.mmocore.guild.provided.Guild; import net.Indyuce.mmocore.api.player.stats.StatType; import net.Indyuce.mmocore.api.util.debug.DebugMode; import net.Indyuce.mmocore.command.*; @@ -27,6 +26,7 @@ import net.Indyuce.mmocore.comp.region.WorldGuardMMOLoader; 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.guild.provided.Guild; import net.Indyuce.mmocore.listener.*; import net.Indyuce.mmocore.listener.event.PlayerPressKeyListener; import net.Indyuce.mmocore.listener.option.*; @@ -251,6 +251,13 @@ public class MMOCore extends LuminePlugin { if (configManager.overrideVanillaExp = getConfig().getBoolean("override-vanilla-exp")) Bukkit.getPluginManager().registerEvents(new VanillaExperienceOverride(), this); + if (getConfig().getBoolean("hotbar-swapping.enabled")) + try { + Bukkit.getPluginManager().registerEvents(new HotbarSwap(getConfig().getConfigurationSection("hotbar-swapping")), this); + } catch (RuntimeException exception) { + getLogger().log(Level.WARNING, "Could not load hotbar swapping: " + exception.getMessage()); + } + if (getConfig().getBoolean("prevent-spawner-xp")) Bukkit.getPluginManager().registerEvents(new NoSpawnerEXP(), this); diff --git a/src/main/java/net/Indyuce/mmocore/api/util/math/format/RomanFormat.java b/src/main/java/net/Indyuce/mmocore/api/util/math/format/RomanFormat.java deleted file mode 100644 index 9726c71f..00000000 --- a/src/main/java/net/Indyuce/mmocore/api/util/math/format/RomanFormat.java +++ /dev/null @@ -1,9 +0,0 @@ -package net.Indyuce.mmocore.api.util.math.format; - -public class RomanFormat { - - public RomanFormat() { - // TODO Auto-generated constructor stub - } - -} diff --git a/src/main/java/net/Indyuce/mmocore/gui/eco/GoldPouch.java b/src/main/java/net/Indyuce/mmocore/gui/eco/GoldPouch.java index e0f018ff..0d553637 100644 --- a/src/main/java/net/Indyuce/mmocore/gui/eco/GoldPouch.java +++ b/src/main/java/net/Indyuce/mmocore/gui/eco/GoldPouch.java @@ -32,10 +32,6 @@ public class GoldPouch extends PluginInventory { return inv; } - /* - * if the player has opened a backpack, he cannot click a backpack. bug fix - * - the player can move the backpack and lose the inventory he had opened - */ @Override public void whenClicked(InventoryClickEvent event) { @@ -63,6 +59,12 @@ public class GoldPouch extends PluginInventory { return; } + /* + * Player cannot interact with a backpack item while + * interacting with a backpack inventory. This fixes a + * huge glitch where the player would lose the backpack + * contents + */ if (nbt.hasTag("RpgPouchInventory")) event.setCancelled(true); } diff --git a/src/main/java/net/Indyuce/mmocore/guild/compat/UltimateClansGuildModule.java b/src/main/java/net/Indyuce/mmocore/guild/compat/UltimateClansGuildModule.java index 467f6f11..505066e6 100644 --- a/src/main/java/net/Indyuce/mmocore/guild/compat/UltimateClansGuildModule.java +++ b/src/main/java/net/Indyuce/mmocore/guild/compat/UltimateClansGuildModule.java @@ -6,7 +6,6 @@ import net.Indyuce.mmocore.api.player.PlayerData; import net.Indyuce.mmocore.guild.AbstractGuild; import net.Indyuce.mmocore.guild.GuildModule; import org.bukkit.Bukkit; -import org.jetbrains.annotations.Nullable; public class UltimateClansGuildModule implements GuildModule { private static final UClans API = (UClans) Bukkit.getPluginManager().getPlugin("UltimateCLans"); @@ -17,8 +16,6 @@ public class UltimateClansGuildModule implements GuildModule { } class CustomGuild implements AbstractGuild { - - @Nullable private final ClanData clan; CustomGuild(ClanData clan) { diff --git a/src/main/java/net/Indyuce/mmocore/listener/GoldPouchesListener.java b/src/main/java/net/Indyuce/mmocore/listener/GoldPouchesListener.java index 3de515f0..86cf1b8f 100644 --- a/src/main/java/net/Indyuce/mmocore/listener/GoldPouchesListener.java +++ b/src/main/java/net/Indyuce/mmocore/listener/GoldPouchesListener.java @@ -1,5 +1,7 @@ package net.Indyuce.mmocore.listener; +import io.lumine.mythic.lib.api.item.NBTItem; +import net.Indyuce.mmocore.gui.eco.GoldPouch; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; @@ -8,9 +10,6 @@ import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.ItemStack; -import net.Indyuce.mmocore.gui.eco.GoldPouch; -import io.lumine.mythic.lib.api.item.NBTItem; - public class GoldPouchesListener implements Listener { @EventHandler public void a(PlayerInteractEvent event) { @@ -21,11 +20,13 @@ public class GoldPouchesListener implements Listener { if (!nbt.hasTag("RpgPouchInventory")) return; - // that way ppl can't open a chest when right clicking a backpack - // when they wanted to open the backpack + /* + * That way players cannot open a chest when right clicking + * a backpack when they wanted to open the backpack + */ event.setCancelled(true); - // dupe bug : open 2 stacked backpacks and split them to dupe. + // Dupe bug: open 2 stacked backpacks and split them to dupe. if (event.getItem().getAmount() > 1) return; @@ -33,9 +34,9 @@ public class GoldPouchesListener implements Listener { } /* - * if a player has a backpack open, he cannot pick up a backpack. bug fix - + * If a player has a backpack open, he cannot pick up a backpack. bug fix - * he can pick up a backpack, and dupe items when the items are saved in a - * amount=2 backpack itemstack TODO register and unregister listener. + * amount=2 backpack itemstack */ @EventHandler public void b(EntityPickupItemEvent event) { diff --git a/src/main/java/net/Indyuce/mmocore/listener/option/HotbarSwap.java b/src/main/java/net/Indyuce/mmocore/listener/option/HotbarSwap.java new file mode 100644 index 00000000..157c9211 --- /dev/null +++ b/src/main/java/net/Indyuce/mmocore/listener/option/HotbarSwap.java @@ -0,0 +1,41 @@ +package net.Indyuce.mmocore.listener.option; + +import io.lumine.mythic.lib.UtilityMethods; +import net.Indyuce.mmocore.MMOCore; +import net.Indyuce.mmocore.api.SoundEvent; +import net.Indyuce.mmocore.api.event.PlayerKeyPressEvent; +import net.Indyuce.mmocore.skill.cast.PlayerKey; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.inventory.ItemStack; + +import java.util.Objects; + +public class HotbarSwap implements Listener { + private final PlayerKey keybind; + private final boolean crouching; + + public HotbarSwap(ConfigurationSection config) { + this.keybind = PlayerKey.valueOf(UtilityMethods.enumName(Objects.requireNonNull(config.getString("keybind"), "Could not find keybind"))); + this.crouching = config.getBoolean("crouching"); + } + + @EventHandler + public void keyPress(PlayerKeyPressEvent event) { + Player player = event.getPlayer(); + if (event.getPressed() == keybind && event.getPlayer().isSneaking() == crouching) { + + if (event.getPressed().shouldCancelEvent()) + event.setCancelled(true); + + MMOCore.plugin.soundManager.getSound(SoundEvent.HOTBAR_SWAP).playTo(player); + 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); + } + } + } +} diff --git a/src/main/java/net/Indyuce/mmocore/listener/option/VanillaExperienceOverride.java b/src/main/java/net/Indyuce/mmocore/listener/option/VanillaExperienceOverride.java index 8332cadd..14b22c46 100644 --- a/src/main/java/net/Indyuce/mmocore/listener/option/VanillaExperienceOverride.java +++ b/src/main/java/net/Indyuce/mmocore/listener/option/VanillaExperienceOverride.java @@ -1,33 +1,35 @@ package net.Indyuce.mmocore.listener.option; +import net.Indyuce.mmocore.MMOCore; +import net.Indyuce.mmocore.api.player.PlayerData; import org.bukkit.Bukkit; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.enchantment.EnchantItemEvent; import org.bukkit.event.player.PlayerExpChangeEvent; -import net.Indyuce.mmocore.MMOCore; -import net.Indyuce.mmocore.api.player.PlayerData; - public class VanillaExperienceOverride implements Listener { - /* - * when picking up exp orbs or any action like that - */ - @EventHandler(priority = EventPriority.HIGHEST) - public void a(PlayerExpChangeEvent event) { - event.setAmount(0); - } + /** + * When picking up exp orbs or any action like that + */ + @EventHandler(priority = EventPriority.HIGHEST) + public void a(PlayerExpChangeEvent event) { + event.setAmount(0); + } - /* - * this event is not supported by the expChangeEvent. since the event is - * actually called before applying the enchant and consuming levels, we must - * update the player level using a delayed task. setExpLevelCost(level) DOES - * NOT WORK - */ - @EventHandler - public void b(EnchantItemEvent event) { - Bukkit.getScheduler().runTask(MMOCore.plugin, () -> event.getEnchanter().setLevel(PlayerData.get(event.getEnchanter()).getLevel())); - } + /** + * This event is not supported by the expChangeEvent. Since the event is + * actually called before applying the enchant and consuming levels, we must + * update the player level using a delayed task. + *

+ * {@link EnchantItemEvent#setExpLevelCost(int)} does NOT work + */ + @EventHandler + public void b(EnchantItemEvent event) { + Player player = event.getEnchanter(); + Bukkit.getScheduler().runTask(MMOCore.plugin, () -> player.setLevel(PlayerData.get(player).getLevel())); + } } diff --git a/src/main/java/net/Indyuce/mmocore/manager/ConfigManager.java b/src/main/java/net/Indyuce/mmocore/manager/ConfigManager.java index 0bcd9843..96a41f87 100644 --- a/src/main/java/net/Indyuce/mmocore/manager/ConfigManager.java +++ b/src/main/java/net/Indyuce/mmocore/manager/ConfigManager.java @@ -1,7 +1,6 @@ package net.Indyuce.mmocore.manager; import io.lumine.mythic.lib.MythicLib; -import io.lumine.mythic.lib.api.util.EnumUtils; import net.Indyuce.mmocore.MMOCore; import net.Indyuce.mmocore.api.ConfigFile; import net.Indyuce.mmocore.api.player.PlayerData; @@ -28,7 +27,6 @@ public class ConfigManager { public String partyChatPrefix, noSkillBoundPlaceholder; public ChatColor staminaFull, staminaHalf, staminaEmpty; public long combatLogTimer, lootChestExpireTime, lootChestPlayerCooldown, globalSkillCooldown; - public SwapAction normalSwapAction, sneakingSwapAction; private final FileConfiguration messages; private final boolean chatInput; @@ -104,8 +102,6 @@ public class ConfigManager { staminaHalf = getColorOrDefault("stamina-half", ChatColor.DARK_GREEN); staminaEmpty = getColorOrDefault("stamina-empty", ChatColor.WHITE); - normalSwapAction = EnumUtils.getIfPresent(SwapAction.class, MMOCore.plugin.getConfig().getString("swap-keybind.normal").toUpperCase()).orElse(SwapAction.VANILLA); - sneakingSwapAction = EnumUtils.getIfPresent(SwapAction.class, MMOCore.plugin.getConfig().getString("swap-keybind.sneaking").toUpperCase()).orElse(SwapAction.VANILLA); canCreativeCast = MMOCore.plugin.getConfig().getBoolean("can-creative-cast"); cobbleGeneratorXP = MMOCore.plugin.getConfig().getBoolean("should-cobblestone-generators-give-exp"); saveDefaultClassInfo = MMOCore.plugin.getConfig().getBoolean("save-default-class-info"); @@ -177,10 +173,4 @@ public class ConfigManager { return !msg.isEmpty(); } } - - public enum SwapAction { - VANILLA, - SPELL_CAST, - HOTBAR_SWAP - } } diff --git a/src/main/java/net/Indyuce/mmocore/skill/cast/SkillCastingMode.java b/src/main/java/net/Indyuce/mmocore/skill/cast/SkillCastingMode.java index 88a0cc39..49c85ade 100644 --- a/src/main/java/net/Indyuce/mmocore/skill/cast/SkillCastingMode.java +++ b/src/main/java/net/Indyuce/mmocore/skill/cast/SkillCastingMode.java @@ -19,7 +19,7 @@ public enum SkillCastingMode { SKILL_BAR(config -> new SkillBar(config)), /** - * TODO + * */ SKILL_SCROLL(config -> new SkillScroller(config)), diff --git a/src/main/java/net/Indyuce/mmocore/skill/cast/listener/SkillBar.java b/src/main/java/net/Indyuce/mmocore/skill/cast/listener/SkillBar.java index 643db2e7..4947f519 100644 --- a/src/main/java/net/Indyuce/mmocore/skill/cast/listener/SkillBar.java +++ b/src/main/java/net/Indyuce/mmocore/skill/cast/listener/SkillBar.java @@ -1,5 +1,6 @@ package net.Indyuce.mmocore.skill.cast.listener; +import io.lumine.mythic.lib.UtilityMethods; import io.lumine.mythic.lib.api.player.EquipmentSlot; import io.lumine.mythic.lib.player.PlayerMetadata; import io.lumine.mythic.lib.skill.trigger.TriggerMetadata; @@ -7,7 +8,6 @@ import net.Indyuce.mmocore.MMOCore; import net.Indyuce.mmocore.api.SoundEvent; import net.Indyuce.mmocore.api.event.PlayerKeyPressEvent; import net.Indyuce.mmocore.api.player.PlayerData; -import net.Indyuce.mmocore.manager.ConfigManager; import net.Indyuce.mmocore.skill.ClassSkill; import net.Indyuce.mmocore.skill.cast.PlayerKey; import net.Indyuce.mmocore.skill.cast.SkillCastingHandler; @@ -17,7 +17,6 @@ import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerItemHeldEvent; -import org.bukkit.event.player.PlayerSwapHandItemsEvent; import java.util.Objects; @@ -25,7 +24,7 @@ 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")); + mainKey = PlayerKey.valueOf(UtilityMethods.enumName(Objects.requireNonNull(config.getString("open"), "Could not find open key"))); } @EventHandler @@ -61,7 +60,7 @@ public class SkillBar implements Listener { super(playerData, 1); } - @EventHandler() + @EventHandler public void onSkillCast(PlayerItemHeldEvent event) { Player player = event.getPlayer(); if (!getCaster().isOnline()) return; @@ -91,13 +90,9 @@ public class SkillBar implements Listener { } @EventHandler - public void stopCasting(PlayerSwapHandItemsEvent event) { + public void stopCasting(PlayerKeyPressEvent event) { Player player = event.getPlayer(); - ConfigManager.SwapAction action = player.isSneaking() - ? MMOCore.plugin.configManager.sneakingSwapAction - : MMOCore.plugin.configManager.normalSwapAction; - if (action != ConfigManager.SwapAction.SPELL_CAST || !getCaster().isOnline()) return; - if (event.getPlayer().equals(getCaster().getPlayer())) { + if (event.getPressed() == mainKey && event.getPlayer().equals(getCaster().getPlayer())) { MMOCore.plugin.soundManager.getSound(SoundEvent.SPELL_CAST_END).playTo(player); MMOCore.plugin.configManager.getSimpleMessage("casting.no-longer").send(getCaster().getPlayer()); PlayerData.get(player).leaveCastingMode(); diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 673be8e6..adce82fd 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,14 +1,6 @@ -# -# ███ ███ ███ ███ ██████ ██████ ██████ ██████ ███████ -# ████ ████ ████ ████ ██ ██ ██ ██ ██ ██ ██ ██ -# ██ ████ ██ ██ ████ ██ ██ ██ ██ ██ ██ ██████ █████ -# ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ -# ██ ██ ██ ██ ██████ ██████ ██████ ██ ██ ███████ -# -# a Spigot Plugin by Team Requiem # DO NOT TOUCH -config-version: 8 +config-version: 9 # Auto-Save feature automatically saves playerdata # (class, level, etc.) and guild data @@ -157,7 +149,7 @@ death-exp-loss: # Fun extra RPG feature that switches the player's hotbar with # the 9 lower row items of his inventory. This allows the player # to have two different item sets or quickly have access to pots -# TODO +# Requires a SERVER reload when changed. hotbar-swapping: enabled: true