From 7446e1137c223821d11396ca5cdcbcc3dd631640 Mon Sep 17 00:00:00 2001 From: Jules Date: Wed, 23 Feb 2022 07:29:04 +0100 Subject: [PATCH] Fixed equip priority --- .../java/net/Indyuce/mmoitems/MMOItems.java | 6 -- .../mmoitems/ReloadCommandTreeNode.java | 1 - .../mmoitems/listener/EquipListener.java | 79 ------------------- .../mmoitems/listener/ItemListener.java | 59 +++++++++++++- src/main/resources/config.yml | 10 --- 5 files changed, 57 insertions(+), 98 deletions(-) delete mode 100644 src/main/java/net/Indyuce/mmoitems/listener/EquipListener.java diff --git a/src/main/java/net/Indyuce/mmoitems/MMOItems.java b/src/main/java/net/Indyuce/mmoitems/MMOItems.java index 1290af10..ee41ac27 100644 --- a/src/main/java/net/Indyuce/mmoitems/MMOItems.java +++ b/src/main/java/net/Indyuce/mmoitems/MMOItems.java @@ -90,7 +90,6 @@ public class MMOItems extends LuminePlugin { private TierManager tierManager; private StatManager statManager; private SetManager setManager; - private EquipListener equipListener; private PlaceholderParser placeholderParser = new DefaultPlaceholderParser(); private VaultSupport vaultSupport; @@ -190,7 +189,6 @@ public class MMOItems extends LuminePlugin { dropTableManager = new DropTableManager(); worldGenManager = new WorldGenManager(); blockManager = new BlockManager(); - equipListener = new EquipListener(); if (Bukkit.getPluginManager().getPlugin("Vault") != null) vaultSupport = new VaultSupport(); @@ -512,10 +510,6 @@ public class MMOItems extends LuminePlugin { return placeholderParser; } - public EquipListener getEquipListener() { - return equipListener; - } - public TemplateManager getTemplates() { return templateManager; } diff --git a/src/main/java/net/Indyuce/mmoitems/command/mmoitems/ReloadCommandTreeNode.java b/src/main/java/net/Indyuce/mmoitems/command/mmoitems/ReloadCommandTreeNode.java index 75851454..70bb369a 100644 --- a/src/main/java/net/Indyuce/mmoitems/command/mmoitems/ReloadCommandTreeNode.java +++ b/src/main/java/net/Indyuce/mmoitems/command/mmoitems/ReloadCommandTreeNode.java @@ -53,7 +53,6 @@ public class ReloadCommandTreeNode extends CommandTreeNode { } public void reloadMain(CommandSender sender) { - MMOItems.plugin.getEquipListener().reload(); MMOItems.plugin.getLanguage().reload(); MMOItems.plugin.getDropTables().reload(); MMOItems.plugin.getTypes().reload(); diff --git a/src/main/java/net/Indyuce/mmoitems/listener/EquipListener.java b/src/main/java/net/Indyuce/mmoitems/listener/EquipListener.java deleted file mode 100644 index 19d632af..00000000 --- a/src/main/java/net/Indyuce/mmoitems/listener/EquipListener.java +++ /dev/null @@ -1,79 +0,0 @@ -package net.Indyuce.mmoitems.listener; - -import io.lumine.mythic.lib.api.item.NBTItem; -import io.lumine.mythic.utils.Events; -import net.Indyuce.mmoitems.MMOItems; -import net.Indyuce.mmoitems.manager.Reloadable; -import org.bukkit.event.Listener; -import org.bukkit.event.block.Action; -import org.bukkit.event.player.PlayerInteractEvent; -import org.bukkit.inventory.ItemStack; - -public class EquipListener implements Reloadable, Listener { - public EquipListener(){ - this.reload(); - } - @Override - public void reload() { - if(MMOItems.plugin.getConfig().getBoolean("auto-equip-feature", true)) { - Events.subscribe(PlayerInteractEvent.class).handler(event -> { - /* - * We only care about listening for right click and shift right clicks. - */ - if (!(event.getAction().equals(Action.RIGHT_CLICK_AIR))) { - return; - } - - if (event.getItem() == null || event.getPlayer() == null) { - return; - } - - if (event.getItem().getType().toString().toLowerCase().contains("boots") - || event.getItem().getType().toString().toLowerCase().contains("leggings") - || event.getItem().getType().toString().toLowerCase().contains("chestplate") - || event.getItem().getType().toString().toLowerCase().contains("helmet")) { - - NBTItem nbtItem = NBTItem.get(event.getItem()); - - Integer priority = nbtItem.getInteger("MMOITEMS_EQUIP_PRIORITY"); - - if (event.getItem().getType().toString().toLowerCase().contains("helmet")) { - NBTItem helmet = NBTItem.get(event.getPlayer().getInventory().getHelmet()); - Integer helmPriority = helmet.getInteger("MMOITEMS_EQUIP_PRIORITY"); - if (priority >= helmPriority) { - ItemStack h = event.getPlayer().getInventory().getHelmet(); - //event.getPlayer().getInventory().setHelmet(event.getItem()); - //event.getPlayer().getInventory().setItemInMainHand(h); - } - } else if (event.getItem().getType().toString().toLowerCase().contains("chestplate")) { - NBTItem chestplate = NBTItem.get(event.getPlayer().getInventory().getChestplate()); - Integer chestPriority = chestplate.getInteger("MMOITEMS_EQUIP_PRIORITY"); - if (priority >= chestPriority) { - ItemStack c = event.getPlayer().getInventory().getChestplate(); - //event.getPlayer().getInventory().setChestplate(event.getItem()); - //event.getPlayer().getInventory().setItemInMainHand(c); - } - } else if (event.getItem().getType().toString().toLowerCase().contains("leggings")) { - NBTItem leggings = NBTItem.get(event.getPlayer().getInventory().getLeggings()); - Integer legPriority = leggings.getInteger("MMOITEMS_EQUIP_PRIORITY"); - if (priority >= legPriority) { - ItemStack l = event.getPlayer().getInventory().getLeggings(); - //event.getPlayer().getInventory().setLeggings(event.getItem()); - //event.getPlayer().getInventory().setItemInMainHand(l); - } - } else { - NBTItem boots = NBTItem.get(event.getPlayer().getInventory().getBoots()); - Integer bootsPriority = boots.getInteger("MMOITEMS_EQUIP_PRIORITY"); - if (priority >= bootsPriority) { - ItemStack b = event.getPlayer().getInventory().getBoots(); - //event.getPlayer().getInventory().setBoots(event.getItem()); - //event.getPlayer().getInventory().setItemInMainHand(b); - } - } - - } - - }); - } - } -} diff --git a/src/main/java/net/Indyuce/mmoitems/listener/ItemListener.java b/src/main/java/net/Indyuce/mmoitems/listener/ItemListener.java index 9971e61a..a85af39e 100644 --- a/src/main/java/net/Indyuce/mmoitems/listener/ItemListener.java +++ b/src/main/java/net/Indyuce/mmoitems/listener/ItemListener.java @@ -10,12 +10,15 @@ import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; +import org.bukkit.event.block.Action; import org.bukkit.event.entity.EntityPickupItemEvent; import org.bukkit.event.inventory.CraftItemEvent; import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryType; import org.bukkit.event.player.PlayerDropItemEvent; +import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -116,7 +119,7 @@ public class ItemListener implements Listener { // Drop all those items for (ItemStack drop : player.getInventory().addItem( - mod.getReforgingOutput().toArray(A)).values()) { + mod.getReforgingOutput().toArray(new ItemStack[0])).values()) { // Not air right if (SilentNumbers.isAir(drop)) { continue; } @@ -128,5 +131,57 @@ public class ItemListener implements Listener { return mod.getResult(); } - public static final ItemStack[] A = new ItemStack[0]; + @EventHandler + public void autoEquip(PlayerInteractEvent event) { + if (event.getAction() != Action.RIGHT_CLICK_AIR && event.getAction() != Action.RIGHT_CLICK_BLOCK) + return; + + if (event.getItem() == null) + return; + + ArmorSlot slot = ArmorSlot.getArmorSlot(event.getItem()); + if (slot == null) + return; + + Player player = event.getPlayer(); + NBTItem equipping = NBTItem.get(event.getItem()), + equipped = NBTItem.get(slot.getEquipped(player)); + int equippingPriority = equipping.getInteger("MMOITEMS_EQUIP_PRIORITY"), + equippedPriority = equipped.getInteger("MMOITEMS_EQUIP_PRIORITY"); + + if (equippingPriority >= equippedPriority) { + player.getInventory().setItem(slot.equipmentSlot, event.getItem()); + player.getInventory().setItem(event.getHand(), equipped.getItem()); + } + } + + private enum ArmorSlot { + HELMET(EquipmentSlot.HEAD), + CHESTPLATE(EquipmentSlot.CHEST), + LEGGINGS(EquipmentSlot.LEGS), + BOOTS(EquipmentSlot.FEET); + + final EquipmentSlot equipmentSlot; + + ArmorSlot(EquipmentSlot equipmentSlot) { + this.equipmentSlot = equipmentSlot; + } + + ItemStack getEquipped(Player player) { + return player.getInventory().getItem(equipmentSlot); + } + + boolean isItem(@Nullable ItemStack item) { + return item != null && item.getType().name().contains(name()); + } + + @Nullable + public static ArmorSlot getArmorSlot(ItemStack item) { + for (ArmorSlot slot : ArmorSlot.values()) + if (slot.isItem(item)) + return slot; + + return null; + } + } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 7534d39e..dae4142a 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -22,16 +22,6 @@ locked-skins: true # off-hand like tomes! disable-abilities-in-offhand: false -# Enable/disable the automatic equip feature. -# By right clicking with a piece of armor, the plugin will -# check if your currently equipped armor's "Equip Priority" stat -# is lower/higher than the one you're currently right clicking with, if -# the one in your hand is higher it will auto equip that and put the -# old piece back into your hand. Used to hot swap armor. -# -# Warning: Could be resource intensive if abused? -auto-equip-feature: true - # By default, all player inventories will be updated every # 10 ticks which corresponds to 2 inventory updates a second. inventory-update-delay: 10