diff --git a/paper/src/main/java/com/denizenscript/denizen/paper/events/PlayerInventorySlotChangeScriptEvent.java b/paper/src/main/java/com/denizenscript/denizen/paper/events/PlayerInventorySlotChangeScriptEvent.java index 604619920c..9d066037e5 100644 --- a/paper/src/main/java/com/denizenscript/denizen/paper/events/PlayerInventorySlotChangeScriptEvent.java +++ b/paper/src/main/java/com/denizenscript/denizen/paper/events/PlayerInventorySlotChangeScriptEvent.java @@ -4,7 +4,6 @@ import com.denizenscript.denizen.objects.EntityTag; import com.denizenscript.denizen.objects.ItemTag; import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData; -import com.denizenscript.denizen.utilities.inventory.SlotHelper; import com.denizenscript.denizencore.objects.ObjectTag; import com.denizenscript.denizencore.objects.core.ElementTag; import com.denizenscript.denizencore.scripts.ScriptEntryData; @@ -26,7 +25,7 @@ public class PlayerInventorySlotChangeScriptEvent extends BukkitScriptEvent impl // // @Switch from: to only process the event if the previous item in the slot matches the specified item. // @Switch to: to only process the event if the new item in the slot matches the specified item. - // @Switch slot: to only process the event if a specific slot was clicked. + // @Switch slot: to only process the event if a specific slot was clicked. For slot input options, see <@link language Slot Inputs>. // // @Triggers when the item in a slot of a player's inventory changes. // Note that this fires for every item in the player's inventory when they join. @@ -52,11 +51,8 @@ public PlayerInventorySlotChangeScriptEvent() { @Override public boolean matches(ScriptPath path) { - if (!runGenericSwitchCheck(path, "slot", String.valueOf(event.getSlot() + 1))) { - int altIndex = SlotHelper.nameToIndex(path.switches.get("slot"), event.getPlayer()); - if (event.getSlot() != altIndex) { - return false; - } + if (!trySlot(path, "slot", event.getPlayer(), event.getSlot())) { + return false; } if (!runWithCheck(path, oldItem, "from")) { return false; diff --git a/plugin/src/main/java/com/denizenscript/denizen/events/BukkitScriptEvent.java b/plugin/src/main/java/com/denizenscript/denizen/events/BukkitScriptEvent.java index b68c223185..10338efd6f 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/events/BukkitScriptEvent.java +++ b/plugin/src/main/java/com/denizenscript/denizen/events/BukkitScriptEvent.java @@ -9,6 +9,7 @@ import com.denizenscript.denizen.tags.BukkitTagContext; import com.denizenscript.denizen.utilities.NotedAreaTracker; import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData; +import com.denizenscript.denizen.utilities.inventory.SlotHelper; import com.denizenscript.denizencore.flags.AbstractFlagTracker; import com.denizenscript.denizencore.flags.FlaggableObject; import com.denizenscript.denizencore.objects.ObjectTag; @@ -795,6 +796,14 @@ else if (isAdvancedMatchable(lower)) { } } + public static boolean trySlot(ScriptPath path, String switchName, Entity entity, int slot) { + String slotMatch = path.switches.get(switchName); + if (slotMatch != null) { + return SlotHelper.doesMatch(slotMatch, entity, slot); + } + return true; + } + public static boolean runWithCheck(ScriptPath path, ItemTag held) { return runWithCheck(path, held, "with"); } diff --git a/plugin/src/main/java/com/denizenscript/denizen/events/player/PlayerClicksInInventoryScriptEvent.java b/plugin/src/main/java/com/denizenscript/denizen/events/player/PlayerClicksInInventoryScriptEvent.java index 405a0774ad..5dfca3aa54 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/events/player/PlayerClicksInInventoryScriptEvent.java +++ b/plugin/src/main/java/com/denizenscript/denizen/events/player/PlayerClicksInInventoryScriptEvent.java @@ -76,7 +76,7 @@ public class PlayerClicksInInventoryScriptEvent extends BukkitScriptEvent implem // @Switch with: to only process the event if a specified cursor item was used. // @Switch in_area: replaces the default 'in:' for this event. // @Switch action: to only process the event if a specified action occurred. - // @Switch slot: to only process the event if a specified slot or slot_type was clicked. + // @Switch slot: to only process the event if a specified slot or slot_type was clicked. For slot input options, see <@link language Slot Inputs>. // // @Location true // @@ -173,7 +173,7 @@ public boolean matches(ScriptPath path) { if (!runGenericSwitchCheck(path, "action", event.getAction().name())) { return false; } - if (!runGenericSwitchCheck(path, "slot", String.valueOf(event.getSlot() + 1)) && !runGenericSwitchCheck(path, "slot", event.getSlotType().name())) { + if (!trySlot(path, "slot", event.getWhoClicked(), event.getSlot()) && !runGenericSwitchCheck(path, "slot", event.getSlotType().name())) { return false; } return super.matches(path); diff --git a/plugin/src/main/java/com/denizenscript/denizen/utilities/inventory/SlotHelper.java b/plugin/src/main/java/com/denizenscript/denizen/utilities/inventory/SlotHelper.java index 8be6680460..7f8d7ec7d0 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/utilities/inventory/SlotHelper.java +++ b/plugin/src/main/java/com/denizenscript/denizen/utilities/inventory/SlotHelper.java @@ -1,5 +1,6 @@ package com.denizenscript.denizen.utilities.inventory; +import com.denizenscript.denizencore.events.ScriptEvent; import com.denizenscript.denizencore.objects.ArgumentHelper; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; @@ -8,7 +9,9 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.PlayerInventory; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; /** * Helper for player inventory slots. @@ -114,19 +117,32 @@ public static int equipSlotToIndex(EquipmentSlot slot) { } public static final HashMap nameIndexMap = new HashMap<>(); + public static final List[] indexNameMap = new List[50]; + + public static void registerSlotName(String name, int index) { + nameIndexMap.put(name, index); + nameIndexMap.put(name + "s", index); + List list = indexNameMap[index]; + if (list == null) { + list = new ArrayList<>(); + indexNameMap[index] = list; + } + list.add(name); + list.add(name + "s"); + } static { - nameIndexMap.put("boot", BOOTS); - nameIndexMap.put("feet", BOOTS); - nameIndexMap.put("foot", BOOTS); - nameIndexMap.put("shoe", BOOTS); - nameIndexMap.put("leg", LEGGINGS); - nameIndexMap.put("legging", LEGGINGS); - nameIndexMap.put("chest", CHESTPLATE); - nameIndexMap.put("chestplate", CHESTPLATE); - nameIndexMap.put("helmet", HELMET); - nameIndexMap.put("head", HELMET); - nameIndexMap.put("offhand", OFFHAND); + registerSlotName("boot", BOOTS); + registerSlotName("feet", BOOTS); + registerSlotName("foot", BOOTS); + registerSlotName("shoe", BOOTS); + registerSlotName("leg", LEGGINGS); + registerSlotName("legging", LEGGINGS); + registerSlotName("chest", CHESTPLATE); + registerSlotName("chestplate", CHESTPLATE); + registerSlotName("helmet", HELMET); + registerSlotName("head", HELMET); + registerSlotName("offhand", OFFHAND); } public static int nameToIndexFor(String name, InventoryHolder holder) { @@ -139,9 +155,6 @@ public static int nameToIndexFor(String name, InventoryHolder holder) { */ public static int nameToIndex(String name, Entity entity) { name = name.toLowerCase().replace("_", ""); - if (name.endsWith("s")) { - name = name.substring(0, name.length() - 1); - } if (name.equals("hand")) { return entity instanceof Player ? ((Player) entity).getInventory().getHeldItemSlot() : 0; } @@ -154,4 +167,26 @@ public static int nameToIndex(String name, Entity entity) { } return -1; } + + public static boolean doesMatch(String text, Entity entity, int slot) { + if (slot >= 0 && slot < indexNameMap.length) { + List names = indexNameMap[slot]; + if (names != null) { + for (String name : names) { + if (ScriptEvent.runGenericCheck(text, name)) { + return true; + } + } + } + } + if (entity instanceof Player && slot == ((Player) entity).getInventory().getHeldItemSlot()) { + if (ScriptEvent.runGenericCheck(text, "hand")) { + return true; + } + } + if (ScriptEvent.runGenericCheck(text, String.valueOf(slot + 1))) { + return true; + } + return false; + } }