Skip to content

Commit

Permalink
slot:hand
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Apr 14, 2021
1 parent 0cd59d7 commit ff187b2
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 9 deletions.
Expand Up @@ -1996,7 +1996,7 @@ else if (meta.hasDisplayName() && CoreUtilities.toLowerCase(meta.getDisplayName(
return null;
}
else if (slots.size() == 1) {
int slot = SlotHelper.nameToIndex(attribute.getContext(1));
int slot = SlotHelper.nameToIndexFor(attribute.getContext(1), object.getInventory().getHolder());
if (slot < 0) {
slot = 0;
}
Expand All @@ -2008,7 +2008,7 @@ else if (slot > object.getInventory().getSize() - 1) {
else {
ListTag result = new ListTag();
for (String slotText : slots) {
int slot = SlotHelper.nameToIndex(slotText);
int slot = SlotHelper.nameToIndexFor(slotText, object.getInventory().getHolder());
if (slot < 0) {
slot = 0;
}
Expand Down
Expand Up @@ -123,7 +123,10 @@ public void execute(ScriptEntry scriptEntry) {
Debug.report(scriptEntry, getName(), ArgumentHelper.debugList("items", items) + elSlot.debug() + duration.debug()
+ ArgumentHelper.debugList("players", players) + player_only.debug());
}
int slot = SlotHelper.nameToIndex(elSlot.asString());
if (players.size() == 0) {
return;
}
int slot = SlotHelper.nameToIndex(elSlot.asString(), players.get(0).getPlayerEntity());
if (slot == -1) {
Debug.echoError(scriptEntry.getResidingQueue(), "The input '" + elSlot.asString() + "' is not a valid slot!");
return;
Expand Down
Expand Up @@ -184,7 +184,7 @@ public void execute(ScriptEntry scriptEntry) {
if (set_quantity) {
is.setAmount(quantity.asInt());
}
int slotId = SlotHelper.nameToIndex(slot.asString());
int slotId = SlotHelper.nameToIndexFor(slot.asString(), inventory.getInventory().getHolder());
if (slotId == -1) {
Debug.echoError(scriptEntry.getResidingQueue(), "The input '" + slot.asString() + "' is not a valid slot!");
return;
Expand Down
Expand Up @@ -139,7 +139,7 @@ public InventoryCommand() {
//
// @Usage
// Use to set a temporary flag on the player's held item.
// - inventory flag slot:<player.held_item_slot> my_target:<player.cursor_on> duration:1d
// - inventory flag slot:hand my_target:<player.cursor_on> duration:1d
// -->

private enum Action {OPEN, CLOSE, COPY, MOVE, SWAP, ADD, REMOVE, SET, KEEP, EXCLUDE, FILL, CLEAR, UPDATE, ADJUST, FLAG}
Expand Down Expand Up @@ -262,7 +262,7 @@ public void execute(final ScriptEntry scriptEntry) {
+ (duration != null ? duration.debug() : "")
+ slot.debug());
}
int slotId = SlotHelper.nameToIndex(slot.asString());
int slotId = SlotHelper.nameToIndexFor(slot.asString(), destination.getInventory().getHolder());
if (slotId < 0) {
if (slotId == -1) {
Debug.echoError(scriptEntry.getResidingQueue(), "The input '" + slot.asString() + "' is not a valid slot (unrecognized)!");
Expand Down
Expand Up @@ -419,7 +419,7 @@ && equalOrNull(titleAuthor.get(0), ((BookMeta) item.getItemMeta()).getTitle())
}
case SLOT: {
for (String slot : slotList) {
int slotId = SlotHelper.nameToIndex(slot);
int slotId = SlotHelper.nameToIndexFor(slot, inventory.getInventory().getHolder());
if (slotId == -1 || slotId >= inventory.getSize()) {
Debug.echoError(scriptEntry.getResidingQueue(), "The input '" + slot + "' is not a valid slot!");
return;
Expand Down
Expand Up @@ -158,7 +158,7 @@ public void serverTag(ReplaceableTagEvent event) {
// Returns the slot ID number for an input slot (see <@link language Slot Inputs>).
// -->
if (attribute.startsWith("slot_id") && attribute.hasContext(1)) {
int slotId = SlotHelper.nameToIndex(attribute.getContext(1));
int slotId = SlotHelper.nameToIndex(attribute.getContext(1), null);
if (slotId != -1) {
event.setReplacedObject(new ElementTag(slotId).getObjectAttribute(attribute.fulfill(1)));
}
Expand Down
@@ -1,7 +1,10 @@
package com.denizenscript.denizen.utilities.inventory;

import com.denizenscript.denizencore.objects.ArgumentHelper;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;

Expand Down Expand Up @@ -66,6 +69,7 @@ public static int slotForItem(PlayerInventory inventory, ItemStack item) {
// @description
// Whenever a script component requires a slot ID (such as the take command, when using '- take slot:#')
// you can give the slot ID input as either a number of the 1-based index (where the first slot is 1, the second is 2, etc.)
// If the slot given is 'hand', for a player the held item slot will be used, for any other entity the slot will be 1.
// OR you can give the following names (valid for player inventories only):
// BOOTS: equivalent to 37
// LEGGINGS: equivalent to 38
Expand Down Expand Up @@ -125,15 +129,22 @@ public static int equipSlotToIndex(EquipmentSlot slot) {
nameIndexMap.put("offhand", OFFHAND);
}

public static int nameToIndexFor(String name, InventoryHolder holder) {
return nameToIndex(name, holder instanceof Entity ? (Entity) holder : null);
}

/**
* Converts a user given slot name (or number) to a valid internal slot index.
* Will subtract 1 from a user-given number, per Denizen standard for user input (1-based slot index).
*/
public static int nameToIndex(String name) {
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;
}
Integer matched = nameIndexMap.get(name);
if (matched != null) {
return matched;
Expand Down

0 comments on commit ff187b2

Please sign in to comment.