From 8c6c3d4090da0994c8fa960af74cf4b502027764 Mon Sep 17 00:00:00 2001 From: Alex 'mcmonkey' Goodwin Date: Wed, 9 Dec 2020 02:28:22 -0800 Subject: [PATCH] add item.with_flag and inventory flag --- .../objects/properties/item/ItemFlags.java | 43 ++++++++++++++- .../commands/item/InventoryCommand.java | 52 +++++++++++++++---- 2 files changed, 82 insertions(+), 13 deletions(-) diff --git a/plugin/src/main/java/com/denizenscript/denizen/objects/properties/item/ItemFlags.java b/plugin/src/main/java/com/denizenscript/denizen/objects/properties/item/ItemFlags.java index 0d360c461d..1e9b8bcf56 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/objects/properties/item/ItemFlags.java +++ b/plugin/src/main/java/com/denizenscript/denizen/objects/properties/item/ItemFlags.java @@ -5,7 +5,9 @@ import com.denizenscript.denizencore.flags.MapTagFlagTracker; import com.denizenscript.denizencore.objects.Mechanism; import com.denizenscript.denizencore.objects.ObjectTag; +import com.denizenscript.denizencore.objects.core.DurationTag; import com.denizenscript.denizencore.objects.core.MapTag; +import com.denizenscript.denizencore.objects.core.TimeTag; import com.denizenscript.denizencore.objects.properties.Property; import com.denizenscript.denizencore.scripts.commands.core.FlagCommand; import com.denizenscript.denizencore.tags.Attribute; @@ -28,7 +30,8 @@ public static ItemFlags getFrom(ObjectTag item) { } public static final String[] handledTags = new String[] { - }; // None: use the standard FlaggableObject flag tags + "with_flag" + }; public static final String[] handledMechs = new String[] { "flag", "flag_map" @@ -42,7 +45,43 @@ private ItemFlags(ItemTag item) { @Override public ObjectTag getObjectAttribute(Attribute attribute) { - // Handled elsewhere + + if (attribute == null) { + return null; + } + + // <--[tag] + // @attribute ]> + // @returns ElementTag(Boolean) + // @mechanism ItemTag.flag + // @group properties + // @description + // Returns a copy of the item with the specified flag data action applied to it. + // --> + if (attribute.startsWith("with_flag")) { + ItemTag item = new ItemTag(this.item.getItemStack().clone()); + FlagCommand.FlagActionProvider provider = new FlagCommand.FlagActionProvider(); + provider.tracker = item.getFlagTracker(); + DataAction action = DataActionHelper.parse(provider, attribute.getContext(1)); + + // <--[tag] + // @attribute ].duration[]> + // @returns ElementTag(Boolean) + // @mechanism ItemTag.flag + // @group properties + // @description + // Returns a copy of the item with the specified flag data action (and the specified expiration duration) applied to it. + // --> + if (attribute.startsWith("duration", 2)) { + provider.expiration = new TimeTag(TimeTag.now().millis() + attribute.getContextObject(2).asType(DurationTag.class, attribute.context).getMillis()); + attribute.fulfill(1); + } + action.execute(attribute.context); + item.reapplyTracker(provider.tracker); + return item + .getObjectAttribute(attribute.fulfill(1)); + } + return null; } diff --git a/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/item/InventoryCommand.java b/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/item/InventoryCommand.java index 5f50a7be83..4abfa2f958 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/item/InventoryCommand.java +++ b/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/item/InventoryCommand.java @@ -8,12 +8,13 @@ import com.denizenscript.denizen.utilities.inventory.SlotHelper; import com.denizenscript.denizencore.exceptions.InvalidArgumentsException; import com.denizenscript.denizencore.objects.*; -import com.denizenscript.denizencore.objects.core.ElementTag; -import com.denizenscript.denizencore.objects.core.ListTag; -import com.denizenscript.denizencore.objects.core.MapTag; +import com.denizenscript.denizencore.objects.core.*; import com.denizenscript.denizencore.scripts.ScriptEntry; import com.denizenscript.denizencore.scripts.commands.AbstractCommand; +import com.denizenscript.denizencore.scripts.commands.core.FlagCommand; import com.denizenscript.denizencore.utilities.Deprecations; +import com.denizenscript.denizencore.utilities.data.DataAction; +import com.denizenscript.denizencore.utilities.data.DataActionHelper; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; @@ -24,7 +25,7 @@ public class InventoryCommand extends AbstractCommand { public InventoryCommand() { setName("inventory"); - setSyntax("inventory [open/close/copy/move/swap/set/keep/exclude/fill/clear/update/adjust :] (destination:) (origin:/|...) (slot:)"); + setSyntax("inventory [open/close/copy/move/swap/set/keep/exclude/fill/clear/update/adjust :/flag (:)[:] (duration:)] (destination:) (origin:/|...) (slot:)"); setRequiredArguments(1, 6); isProcedural = false; } @@ -61,7 +62,7 @@ public InventoryCommand() { // <--[command] // @Name Inventory - // @Syntax inventory [open/close/copy/move/swap/set/keep/exclude/fill/clear/update/adjust :] (destination:) (origin:/|...) (slot:) + // @Syntax inventory [open/close/copy/move/swap/set/keep/exclude/fill/clear/update/adjust :/flag (:)[:] (duration:)] (destination:) (origin:/|...) (slot:) // @Required 1 // @Maximum 6 // @Short Edits the inventory of a player, NPC, or chest. @@ -84,6 +85,9 @@ public InventoryCommand() { // The "adjust" option adjusts mechanisms on an item within a specific slot of an inventory (the "slot" parameter is required). // Note that this is only for items, it does NOT adjust the inventory itself. Use <@link command adjust> to adjust an inventory mechanism. // + // The "flag" option sets a flag on items, similar to <@link command flag>. + // See also <@link language flag system>. + // // Note that to add items to an inventory, you should usually use <@link command give>, // and to remove items from an inventory, you should usually use <@link command take>. // @@ -125,20 +129,20 @@ public InventoryCommand() { // - inventory adjust slot:5 "lore:Item modified!" // // @Usage - // Use to set a flag on the player's held item. - // - inventory adjust slot: flag:myflag: - // - // @Usage // Use to set a single stick into slot 10 of the player's inventory. // - inventory set o:stick slot:10 + // + // @Usage + // Use to set a temporary flag on the player's held item. + // - inventory flag slot: flag my_target: duration:1d // --> - private enum Action {OPEN, CLOSE, COPY, MOVE, SWAP, ADD, REMOVE, SET, KEEP, EXCLUDE, FILL, CLEAR, UPDATE, ADJUST} + private enum Action {OPEN, CLOSE, COPY, MOVE, SWAP, ADD, REMOVE, SET, KEEP, EXCLUDE, FILL, CLEAR, UPDATE, ADJUST, FLAG} @SuppressWarnings("unchecked") @Override public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException { - boolean isAdjust = false; + boolean isAdjust = false, isFlag = false; for (Argument arg : scriptEntry.getProcessedArgs()) { if (!scriptEntry.hasObject("origin") && arg.matchesPrefix("origin", "o", "source", "items", "item", "i", "from", "f") @@ -159,6 +163,7 @@ else if (!scriptEntry.hasObject("actions") && arg.matchesEnumList(Action.values())) { scriptEntry.addObject("actions", arg.asType(ListTag.class).filter(Action.values())); isAdjust = arg.toString().equalsIgnoreCase("adjust"); + isFlag = arg.toString().equalsIgnoreCase("flag"); } else if (!scriptEntry.hasObject("mechanism") && isAdjust) { @@ -170,6 +175,15 @@ else if (!scriptEntry.hasObject("mechanism") scriptEntry.addObject("mechanism", arg.asElement()); } } + else if (!scriptEntry.hasObject("duration") + && arg.matchesArgumentType(DurationTag.class) + && isFlag) { + scriptEntry.addObject("duration", arg.asType(DurationTag.class)); + } + else if (!scriptEntry.hasObject("flag_action") + && isFlag) { + scriptEntry.addObject("flag_action", DataActionHelper.parse(new FlagCommand.FlagActionProvider(), arg.getRawValue())); + } else { arg.reportUnhandled(); } @@ -184,6 +198,9 @@ else if (!scriptEntry.hasObject("mechanism") if (isAdjust && !scriptEntry.hasObject("slot")) { throw new InvalidArgumentsException("Inventory adjust must have an explicit slot!"); } + if (isFlag && !scriptEntry.hasObject("flag_action")) { + throw new InvalidArgumentsException("Inventory flag must have a flag action!"); + } scriptEntry.defaultObject("slot", new ElementTag(1)); scriptEntry.defaultObject("destination", Utilities.entryHasPlayer(scriptEntry) ? @@ -204,6 +221,8 @@ public void execute(final ScriptEntry scriptEntry) { ElementTag slot = scriptEntry.getElement("slot"); ElementTag mechanism = scriptEntry.getElement("mechanism"); ElementTag mechanismValue = scriptEntry.getElement("mechanism_value"); + DataAction flagAction = (DataAction) scriptEntry.getObject("flag_action"); + DurationTag duration = scriptEntry.getObjectTag("duration"); if (scriptEntry.dbCallShouldDebug()) { Debug.report(scriptEntry, getName(), ArgumentHelper.debugObj("actions", actions.toString()) @@ -211,6 +230,8 @@ public void execute(final ScriptEntry scriptEntry) { + (origin != null ? origin.debug() : "") + (mechanism != null ? mechanism.debug() : "") + (mechanismValue != null ? mechanismValue.debug() : "") + + (flagAction != null ? flagAction.debug() : "") + + (duration != null ? duration.debug() : "") + slot.debug()); } int slotId = SlotHelper.nameToIndex(slot.asString()); @@ -370,6 +391,15 @@ public void execute(final ScriptEntry scriptEntry) { toAdjust.safeAdjust(new Mechanism(mechanism, mechanismValue, scriptEntry.entryData.getTagContext())); NMSHandler.getItemHelper().setInventoryItem(destination.getInventory(), toAdjust.getItemStack(), slotId); break; + case FLAG: + ItemTag toFlag = new ItemTag(destination.getInventory().getItem(slotId)); + FlagCommand.FlagActionProvider provider = (FlagCommand.FlagActionProvider) flagAction.provider; + provider.expiration = duration == null ? null : new TimeTag(TimeTag.now().millis() + duration.getMillis()); + provider.tracker = toFlag.getFlagTracker(); + flagAction.execute(scriptEntry.context); + toFlag.reapplyTracker(provider.tracker); + NMSHandler.getItemHelper().setInventoryItem(destination.getInventory(), toFlag.getItemStack(), slotId); + break; } } }