Skip to content

Commit

Permalink
add item.with_flag and inventory flag
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Dec 9, 2020
1 parent ce6bc3d commit 8c6c3d4
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 13 deletions.
Expand Up @@ -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;
Expand All @@ -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"
Expand All @@ -42,7 +45,43 @@ private ItemFlags(ItemTag item) {

@Override
public ObjectTag getObjectAttribute(Attribute attribute) {
// Handled elsewhere

if (attribute == null) {
return null;
}

// <--[tag]
// @attribute <ItemTag.with_flag[<flag_set_action>]>
// @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 <ItemTag.with_flag[<flag_set_action>].duration[<expire_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;
}

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

Expand All @@ -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 <mechanism>:<value>] (destination:<inventory>) (origin:<inventory>/<item>|...) (slot:<slot>)");
setSyntax("inventory [open/close/copy/move/swap/set/keep/exclude/fill/clear/update/adjust <mechanism>:<value>/flag <name>(:<action>)[:<value>] (duration:<duration>)] (destination:<inventory>) (origin:<inventory>/<item>|...) (slot:<slot>)");
setRequiredArguments(1, 6);
isProcedural = false;
}
Expand Down Expand Up @@ -61,7 +62,7 @@ public InventoryCommand() {

// <--[command]
// @Name Inventory
// @Syntax inventory [open/close/copy/move/swap/set/keep/exclude/fill/clear/update/adjust <mechanism>:<value>] (destination:<inventory>) (origin:<inventory>/<item>|...) (slot:<slot>)
// @Syntax inventory [open/close/copy/move/swap/set/keep/exclude/fill/clear/update/adjust <mechanism>:<value>/flag <name>(:<action>)[:<value>] (duration:<duration>)] (destination:<inventory>) (origin:<inventory>/<item>|...) (slot:<slot>)
// @Required 1
// @Maximum 6
// @Short Edits the inventory of a player, NPC, or chest.
Expand All @@ -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>.
//
Expand Down Expand Up @@ -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:<player.held_item_slot> flag:myflag:<player.cursor_on>
//
// @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:<player.held_item_slot> flag my_target:<player.cursor_on> 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")
Expand All @@ -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) {
Expand All @@ -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();
}
Expand All @@ -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) ?
Expand All @@ -204,13 +221,17 @@ 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())
+ (destination.debug())
+ (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());
Expand Down Expand Up @@ -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;
}
}
}
Expand Down

0 comments on commit 8c6c3d4

Please sign in to comment.