From faa2afbe79fc38478bc8a492f22a623999ac8625 Mon Sep 17 00:00:00 2001 From: mcmonkey Date: Fri, 18 Jan 2019 07:54:30 -0800 Subject: [PATCH] Add 'inventory adjust ...' Also improve the adjust command --- .../commands/BukkitCommandRegistry.java | 15 ++-- .../scripts/commands/core/AdjustCommand.java | 73 ++++++++----------- .../commands/item/InventoryCommand.java | 39 +++++++++- 3 files changed, 75 insertions(+), 52 deletions(-) diff --git a/plugin/src/main/java/net/aufdemrand/denizen/scripts/commands/BukkitCommandRegistry.java b/plugin/src/main/java/net/aufdemrand/denizen/scripts/commands/BukkitCommandRegistry.java index 4dfc792db3..303c48b8b1 100644 --- a/plugin/src/main/java/net/aufdemrand/denizen/scripts/commands/BukkitCommandRegistry.java +++ b/plugin/src/main/java/net/aufdemrand/denizen/scripts/commands/BukkitCommandRegistry.java @@ -103,6 +103,8 @@ public void registerCoreMembers() { // interface to deal with those adjustments. To easily accomplish this, use this command with a valid object // mechanism, and sometimes accompanying value. // + // To adjust an item, use <@link command inventory>, as '- inventory adjust slot:<#> :'. + // // @Tags // returns the adjusted object. // returns a dList of adjusted objects. @@ -110,11 +112,6 @@ public void registerCoreMembers() { // @Usage // Use to set a custom display name on an entity. // - adjust e@1000 'custom_name:ANGRY!' - // - // @Usage - // Use as part of the steps to modify the item a player is holding (adjust the item, then swap the new item into the original inventory) - // - adjust "lore:Advanced Item" save:myitem - // - inventory set slot: d: o: // --> registerCoreMember(AdjustCommand.class, "ADJUST", "adjust [|...] [](:)", 2); @@ -2017,7 +2014,7 @@ public void registerCoreMembers() { // <--[command] // @Name Inventory - // @Syntax inventory [open/close/copy/move/swap/add/remove/set/keep/exclude/fill/clear/update] (destination:) (origin:/|...) (slot:) + // @Syntax inventory [open/close/copy/move/swap/add/remove/set/keep/exclude/fill/clear/update/adjust :] (destination:) (origin:/|...) (slot:) // @Required 1 // @Stable stable // @Short Edits the inventory of a player, NPC, or chest. @@ -2066,9 +2063,13 @@ public void registerCoreMembers() { // @Usage // Use to swap two players' inventories. // - inventory swap d:in@player[holder=p@mcmonkey4eva] o: + // + // @Usage + // Use to adjust a specific item in the player's inventory. + // - inventory adjust slot:5 "lore:Item modified!" // --> registerCoreMember(InventoryCommand.class, - "INVENTORY", "inventory [open/close/copy/move/swap/add/remove/set/keep/exclude/fill/clear/update] (destination:) (origin:/|...) (slot:)", 1); + "INVENTORY", "inventory [open/close/copy/move/swap/add/remove/set/keep/exclude/fill/clear/update/adjust :] (destination:) (origin:/|...) (slot:)", 1); // <--[command] diff --git a/plugin/src/main/java/net/aufdemrand/denizen/scripts/commands/core/AdjustCommand.java b/plugin/src/main/java/net/aufdemrand/denizen/scripts/commands/core/AdjustCommand.java index 7e0842d670..06d7a43f5a 100644 --- a/plugin/src/main/java/net/aufdemrand/denizen/scripts/commands/core/AdjustCommand.java +++ b/plugin/src/main/java/net/aufdemrand/denizen/scripts/commands/core/AdjustCommand.java @@ -44,6 +44,32 @@ else if (!scriptEntry.hasObject("mechanism")) { } + public void adjust(dObject object, Element mechanism, Element value, ScriptEntry entry) { + String objectString = object.toString(); + if (objectString.equalsIgnoreCase("server")) { + ServerTags.adjustServer(new Mechanism(mechanism, value)); + return; + } + else if (objectString.equalsIgnoreCase("system")) { + UtilTags.adjustSystem(new Mechanism(mechanism, value)); + return; + } + if (object instanceof Element) { + object = ObjectFetcher.pickObjectFor(objectString, entry.entryData.getTagContext()); + if (object instanceof Element) { + dB.echoError("Unable to determine what object to adjust (missing object notation?), for: " + objectString); + return; + } + } + // Make sure this object is Adjustable + if (!(object instanceof Adjustable)) { + dB.echoError("'" + objectString + "' is not an adjustable object type."); + return; + } + ((Adjustable) object).adjust(new Mechanism(mechanism, value)); + } + + @Override public void execute(ScriptEntry scriptEntry) throws CommandExecutionException { @@ -53,59 +79,20 @@ public void execute(ScriptEntry scriptEntry) throws CommandExecutionException { dList objects = scriptEntry.getdObject("object"); if (scriptEntry.dbCallShouldDebug()) { - dB.report(scriptEntry, getName(), objects.debug() + mechanism.debug() + (value == null ? "" : value.debug())); - } dList result = new dList(); - for (String object : objects) { - if (object.equalsIgnoreCase("server")) { - ServerTags.adjustServer(new Mechanism(mechanism, value)); - continue; - } - else if (object.equalsIgnoreCase("system")) { - UtilTags.adjustSystem(new Mechanism(mechanism, value)); - continue; - } - - Class object_class = ObjectFetcher.getObjectClass(object.split("@")[0]); - - if (object_class == null) { - dB.echoError("Unfetchable object found '" + object + "'!"); - return; - } - - dObject fetched; - - // Check to make sure this is a valid constructor by checking the 'matches' static method - if (!ObjectFetcher.checkMatch(object_class, object)) { - throw new CommandExecutionException('\'' + object + "' is returning null."); - } - - // Get the object with the 'valueOf' static method - fetched = ObjectFetcher.getObjectFrom(object_class, object); - - // Make sure this object is Adjustable - if (fetched == null || !(fetched instanceof Adjustable)) { - dB.echoError("'" + object + "' is not adjustable."); - return; - } - - // Do the adjustment! - ((Adjustable) fetched).adjust(new Mechanism(mechanism, value)); - - // Add it to the entry for later access + for (dObject object : objects.objectForms) { + adjust(object, mechanism, value, scriptEntry); if (objects.size() == 1) { - scriptEntry.addObject("result", fetched); + scriptEntry.addObject("result", object); } - result.add(fetched.identify()); - - // :) + result.addObject(object); } scriptEntry.addObject("result_list", result); diff --git a/plugin/src/main/java/net/aufdemrand/denizen/scripts/commands/item/InventoryCommand.java b/plugin/src/main/java/net/aufdemrand/denizen/scripts/commands/item/InventoryCommand.java index b4a7b10823..f2701967dc 100644 --- a/plugin/src/main/java/net/aufdemrand/denizen/scripts/commands/item/InventoryCommand.java +++ b/plugin/src/main/java/net/aufdemrand/denizen/scripts/commands/item/InventoryCommand.java @@ -11,10 +11,12 @@ import net.aufdemrand.denizencore.exceptions.CommandExecutionException; import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException; import net.aufdemrand.denizencore.objects.Element; +import net.aufdemrand.denizencore.objects.Mechanism; import net.aufdemrand.denizencore.objects.aH; import net.aufdemrand.denizencore.objects.dList; import net.aufdemrand.denizencore.scripts.ScriptEntry; import net.aufdemrand.denizencore.scripts.commands.AbstractCommand; +import org.bukkit.inventory.ItemStack; import java.util.AbstractMap; import java.util.List; @@ -53,17 +55,21 @@ public class InventoryCommand extends AbstractCommand { // // --> - private enum Action {OPEN, CLOSE, COPY, MOVE, SWAP, ADD, REMOVE, SET, KEEP, EXCLUDE, FILL, CLEAR, UPDATE} + private enum Action {OPEN, CLOSE, COPY, MOVE, SWAP, ADD, REMOVE, SET, KEEP, EXCLUDE, FILL, CLEAR, UPDATE, ADJUST} @SuppressWarnings("unchecked") @Override public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException { + boolean isAdjust = false; + for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) { // Check for a dList of actions - if (arg.matchesEnumList(Action.values())) { + if (!scriptEntry.hasObject("actions") + && arg.matchesEnumList(Action.values())) { scriptEntry.addObject("actions", arg.asType(dList.class).filter(Action.values())); + isAdjust = arg.toString().equalsIgnoreCase("adjust"); } // Check for an origin, which can be a dInventory, dEntity, dLocation @@ -88,6 +94,18 @@ else if (!scriptEntry.hasObject("slot") && arg.matchesPrefix("slot, s")) { scriptEntry.addObject("slot", arg.asElement()); } + + else if (!scriptEntry.hasObject("mechanism") + && isAdjust) { + if (arg.hasPrefix()) { + scriptEntry.addObject("mechanism", new Element(arg.getPrefix().getValue())); + scriptEntry.addObject("mechanism_value", arg.asElement()); + } + else { + scriptEntry.addObject("mechanism", arg.asElement()); + } + } + else { arg.reportUnhandled(); } @@ -98,6 +116,14 @@ else if (!scriptEntry.hasObject("slot") throw new InvalidArgumentsException("Must specify an Inventory action!"); } + if (isAdjust && !scriptEntry.hasObject("mechanism")) { + throw new InvalidArgumentsException("Inventory adjust must have a mechanism!"); + } + + if (isAdjust && !scriptEntry.hasObject("slot")) { + throw new InvalidArgumentsException("Inventory adjust must have an explicit slot!"); + } + scriptEntry.defaultObject("slot", new Element(1)); scriptEntry.defaultObject("destination", @@ -121,12 +147,16 @@ public void execute(final ScriptEntry scriptEntry) throws CommandExecutionExcept AbstractMap.SimpleEntry destinationentry = (AbstractMap.SimpleEntry) scriptEntry.getObject("destination"); dInventory destination = destinationentry.getValue(); Element slot = scriptEntry.getElement("slot"); + Element mechanism = scriptEntry.getElement("mechanism"); + Element mechanismValue = scriptEntry.getElement("mechanism_value"); if (scriptEntry.dbCallShouldDebug()) { dB.report(scriptEntry, getName(), aH.debugObj("actions", actions.toString()) + (destination.debug()) + (origin != null ? origin.debug() : "") + + (mechanism != null ? mechanism.debug() : "") + + (mechanismValue != null ? mechanismValue.debug() : "") + slot.debug()); } @@ -257,6 +287,11 @@ public void execute(final ScriptEntry scriptEntry) throws CommandExecutionExcept } break; + case ADJUST: + dItem toAdjust = new dItem(destination.getInventory().getItem(slotId)); + toAdjust.adjust(new Mechanism(mechanism, mechanismValue)); + destination.getInventory().setItem(slotId, toAdjust.getItemStack()); + break; } } }