Skip to content

Commit

Permalink
generic trySlot
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Oct 18, 2022
1 parent db12e71 commit ee9d583
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 23 deletions.
Expand Up @@ -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;
Expand All @@ -26,7 +25,7 @@ public class PlayerInventorySlotChangeScriptEvent extends BukkitScriptEvent impl
//
// @Switch from:<item> to only process the event if the previous item in the slot matches the specified item.
// @Switch to:<item> to only process the event if the new item in the slot matches the specified item.
// @Switch slot:<slot> to only process the event if a specific slot was clicked.
// @Switch slot:<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.
Expand All @@ -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;
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}
Expand Down
Expand Up @@ -76,7 +76,7 @@ public class PlayerClicksInInventoryScriptEvent extends BukkitScriptEvent implem
// @Switch with:<item> to only process the event if a specified cursor item was used.
// @Switch in_area:<area> replaces the default 'in:<area>' for this event.
// @Switch action:<action> to only process the event if a specified action occurred.
// @Switch slot:<slot> to only process the event if a specified slot or slot_type was clicked.
// @Switch slot:<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
//
Expand Down Expand Up @@ -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);
Expand Down
@@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -114,19 +117,32 @@ public static int equipSlotToIndex(EquipmentSlot slot) {
}

public static final HashMap<String, Integer> nameIndexMap = new HashMap<>();
public static final List<String>[] indexNameMap = new List[50];

public static void registerSlotName(String name, int index) {
nameIndexMap.put(name, index);
nameIndexMap.put(name + "s", index);
List<String> 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) {
Expand All @@ -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;
}
Expand All @@ -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<String> 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;
}
}

0 comments on commit ee9d583

Please sign in to comment.