Skip to content

Commit

Permalink
Fix index of player's tool (#6846)
Browse files Browse the repository at this point in the history
  • Loading branch information
APickledWalrus authored Jul 12, 2024
1 parent 5325b17 commit ee8d8c8
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/main/java/ch/njol/skript/util/slot/EquipmentSlot.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ee8d8c8

Please sign in to comment.