Fixed equip priority

This commit is contained in:
Jules 2022-02-23 07:29:04 +01:00
parent 07f6247235
commit 7446e1137c
5 changed files with 57 additions and 98 deletions

View File

@ -90,7 +90,6 @@ public class MMOItems extends LuminePlugin {
private TierManager tierManager; private TierManager tierManager;
private StatManager statManager; private StatManager statManager;
private SetManager setManager; private SetManager setManager;
private EquipListener equipListener;
private PlaceholderParser placeholderParser = new DefaultPlaceholderParser(); private PlaceholderParser placeholderParser = new DefaultPlaceholderParser();
private VaultSupport vaultSupport; private VaultSupport vaultSupport;
@ -190,7 +189,6 @@ public class MMOItems extends LuminePlugin {
dropTableManager = new DropTableManager(); dropTableManager = new DropTableManager();
worldGenManager = new WorldGenManager(); worldGenManager = new WorldGenManager();
blockManager = new BlockManager(); blockManager = new BlockManager();
equipListener = new EquipListener();
if (Bukkit.getPluginManager().getPlugin("Vault") != null) vaultSupport = new VaultSupport(); if (Bukkit.getPluginManager().getPlugin("Vault") != null) vaultSupport = new VaultSupport();
@ -512,10 +510,6 @@ public class MMOItems extends LuminePlugin {
return placeholderParser; return placeholderParser;
} }
public EquipListener getEquipListener() {
return equipListener;
}
public TemplateManager getTemplates() { public TemplateManager getTemplates() {
return templateManager; return templateManager;
} }

View File

@ -53,7 +53,6 @@ public class ReloadCommandTreeNode extends CommandTreeNode {
} }
public void reloadMain(CommandSender sender) { public void reloadMain(CommandSender sender) {
MMOItems.plugin.getEquipListener().reload();
MMOItems.plugin.getLanguage().reload(); MMOItems.plugin.getLanguage().reload();
MMOItems.plugin.getDropTables().reload(); MMOItems.plugin.getDropTables().reload();
MMOItems.plugin.getTypes().reload(); MMOItems.plugin.getTypes().reload();

View File

@ -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);
}
}
}
});
}
}
}

View File

@ -10,12 +10,15 @@ import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityPickupItemEvent; import org.bukkit.event.entity.EntityPickupItemEvent;
import org.bukkit.event.inventory.CraftItemEvent; import org.bukkit.event.inventory.CraftItemEvent;
import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryType; import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.player.PlayerDropItemEvent; import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -116,7 +119,7 @@ public class ItemListener implements Listener {
// Drop all those items // Drop all those items
for (ItemStack drop : player.getInventory().addItem( for (ItemStack drop : player.getInventory().addItem(
mod.getReforgingOutput().toArray(A)).values()) { mod.getReforgingOutput().toArray(new ItemStack[0])).values()) {
// Not air right // Not air right
if (SilentNumbers.isAir(drop)) { continue; } if (SilentNumbers.isAir(drop)) { continue; }
@ -128,5 +131,57 @@ public class ItemListener implements Listener {
return mod.getResult(); 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;
}
}
} }

View File

@ -22,16 +22,6 @@ locked-skins: true
# off-hand like tomes! # off-hand like tomes!
disable-abilities-in-offhand: false 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 # By default, all player inventories will be updated every
# 10 ticks which corresponds to 2 inventory updates a second. # 10 ticks which corresponds to 2 inventory updates a second.
inventory-update-delay: 10 inventory-update-delay: 10