From ee8d8c8b636494fcd205ac2d8657c36d49400d48 Mon Sep 17 00:00:00 2001 From: Patrick Miller Date: Fri, 12 Jul 2024 13:54:52 -0400 Subject: [PATCH] Fix index of player's tool (#6846) --- .../njol/skript/util/slot/EquipmentSlot.java | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/main/java/ch/njol/skript/util/slot/EquipmentSlot.java b/src/main/java/ch/njol/skript/util/slot/EquipmentSlot.java index 5ab854e68c8..47f229bda23 100644 --- a/src/main/java/ch/njol/skript/util/slot/EquipmentSlot.java +++ b/src/main/java/ch/njol/skript/util/slot/EquipmentSlot.java @@ -20,6 +20,7 @@ import java.util.Locale; +import org.bukkit.entity.Entity; import org.bukkit.entity.HumanEntity; import org.bukkit.entity.Player; import org.bukkit.event.Event; @@ -132,10 +133,18 @@ public void set(final EntityEquipment e, final @Nullable ItemStack item) { private final EntityEquipment e; private final EquipSlot slot; + private final int slotIndex; private final boolean slotToString; public EquipmentSlot(final EntityEquipment e, final EquipSlot slot, final boolean slotToString) { this.e = e; + int slotIndex = -1; + if (slot == EquipSlot.TOOL) { + Entity holder = e.getHolder(); + if (holder instanceof Player) + slotIndex = ((Player) holder).getInventory().getHeldItemSlot(); + } + this.slotIndex = slotIndex; this.slot = slot; this.slotToString = slotToString; } @@ -146,10 +155,12 @@ public EquipmentSlot(final EntityEquipment e, final EquipSlot slot) { @SuppressWarnings("null") public EquipmentSlot(HumanEntity holder, int index) { - this.e = holder.getEquipment(); - this.slot = values[41 - index]; // 6 entries in EquipSlot, indices descending - // So this math trick gets us the EquipSlot from inventory slot index - this.slotToString = true; // Referring to numeric slot id, right? + /* + * slot: 6 entries in EquipSlot, indices descending + * So this math trick gets us the EquipSlot from inventory slot index + * slotToString: Referring to numeric slot id, right? + */ + this(holder.getEquipment(), values[41 - index], true); } @Override @@ -189,7 +200,8 @@ public EquipSlot getEquipSlot() { @Override public int getIndex() { - return slot.slotNumber; + // use specific slotIndex if available + return slotIndex != -1 ? slotIndex : slot.slotNumber; } @Override