Skip to content

Commit

Permalink
Add 'inventory adjust ...'
Browse files Browse the repository at this point in the history
Also improve the adjust command
  • Loading branch information
mcmonkey4eva committed Jan 18, 2019
1 parent 7e12ba6 commit faa2afb
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 52 deletions.
Expand Up @@ -103,18 +103,15 @@ 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:<#> <mechanism>:<value>'.
//
// @Tags
// <entry[saveName].result> returns the adjusted object.
// <entry[saveName].result_list> returns a dList of adjusted objects.
//
// @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 <player.item_in_hand> "lore:Advanced Item" save:myitem
// - inventory set slot:<player.item_in_hand.slot> d:<player.inventory> o:<entry[myitem].result>
// -->
registerCoreMember(AdjustCommand.class,
"ADJUST", "adjust [<dObject>|...] [<mechanism>](:<value>)", 2);
Expand Down Expand Up @@ -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:<inventory>) (origin:<inventory>/<item>|...) (slot:<slot>)
// @Syntax inventory [open/close/copy/move/swap/add/remove/set/keep/exclude/fill/clear/update/adjust <mechanism>:<value>] (destination:<inventory>) (origin:<inventory>/<item>|...) (slot:<slot>)
// @Required 1
// @Stable stable
// @Short Edits the inventory of a player, NPC, or chest.
Expand Down Expand Up @@ -2066,9 +2063,13 @@ public void registerCoreMembers() {
// @Usage
// Use to swap two players' inventories.
// - inventory swap d:in@player[holder=p@mcmonkey4eva] o:<p@fullwall.inventory>
//
// @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:<inventory>) (origin:<inventory>/<item>|...) (slot:<slot>)", 1);
"INVENTORY", "inventory [open/close/copy/move/swap/add/remove/set/keep/exclude/fill/clear/update/adjust <mechanism>:<value>] (destination:<inventory>) (origin:<inventory>/<item>|...) (slot:<slot>)", 1);


// <--[command]
Expand Down
Expand Up @@ -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 {

Expand All @@ -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);
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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();
}
Expand All @@ -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",
Expand All @@ -121,12 +147,16 @@ public void execute(final ScriptEntry scriptEntry) throws CommandExecutionExcept
AbstractMap.SimpleEntry<Integer, dInventory> destinationentry = (AbstractMap.SimpleEntry<Integer, dInventory>) 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());
}

Expand Down Expand Up @@ -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;
}
}
}
Expand Down

0 comments on commit faa2afb

Please sign in to comment.