Skip to content

Commit

Permalink
add list support to inventory.contains.scriptname
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Aug 8, 2020
1 parent b05c68c commit e81d7ae
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 20 deletions.
Expand Up @@ -216,7 +216,7 @@ public boolean nonSwitchWithCheck(ScriptPath path, ItemTag held) {
}

public BukkitTagContext getTagContext(ScriptPath path) {
BukkitTagContext context = (BukkitTagContext) getScriptEntryData().getTagContext();
BukkitTagContext context = (BukkitTagContext) getScriptEntryData().getTagContext().clone();
context.script = new ScriptTag(path.container);
context.debug = path.container.shouldDebug();
return context;
Expand Down
Expand Up @@ -1835,23 +1835,27 @@ else if (item != null && item.hasItemMeta() && item.getItemMeta().hasDisplayName
return new ElementTag(found_items >= qty);
}
// <--[tag]
// @attribute <InventoryTag.contains.scriptname[<scriptname>]>
// @attribute <InventoryTag.contains.scriptname[<scriptname>|...]>
// @returns ElementTag(Boolean)
// @description
// Returns whether the inventory contains an item with the specified scriptname.
// Returns whether the inventory contains an item with the specified scriptname(s).
// -->
if (attribute.startsWith("scriptname", 2)) {
if (!attribute.hasContext(2)) {
return null;
}
String scrName = attribute.getContext(2);
ListTag scrNameList = attribute.contextAsType(2, ListTag.class);
HashSet<String> scrNames = new HashSet<>();
for (String name : scrNameList) {
scrNames.add(CoreUtilities.toLowerCase(name));
}
int qty = 1;

// <--[tag]
// @attribute <InventoryTag.contains.scriptname[<scriptname>].quantity[<#>]>
// @attribute <InventoryTag.contains.scriptname[<scriptname>|...].quantity[<#>]>
// @returns ElementTag(Boolean)
// @description
// Returns whether the inventory contains a certain quantity of an item with the specified scriptname.
// Returns whether the inventory contains a certain quantity of an item with the specified scriptname(s).
// -->
if ((attribute.startsWith("quantity", 3) || attribute.startsWith("qty", 3)) && attribute.hasContext(3)) {
if (attribute.startsWith("qty", 3)) {
Expand All @@ -1864,7 +1868,7 @@ else if (item != null && item.hasItemMeta() && item.getItemMeta().hasDisplayName
int found_items = 0;

for (ItemStack item : object.getContents()) {
if (item != null && scrName.equalsIgnoreCase(new ItemTag(item).getScriptName())) {
if (item != null && scrNames.contains(CoreUtilities.toLowerCase(new ItemTag(item).getScriptName()))) {
found_items += item.getAmount();
if (found_items >= qty) {
break;
Expand Down
Expand Up @@ -90,9 +90,7 @@ private enum Type {MONEY, XP, ITEMINHAND, ITEM, INVENTORY, BYDISPLAY, SLOT, BYCO

@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {

for (Argument arg : scriptEntry.getProcessedArgs()) {

if (!scriptEntry.hasObject("type")
&& arg.matches("money", "coins")) {
scriptEntry.addObject("type", Type.MONEY);
Expand Down Expand Up @@ -170,29 +168,22 @@ else if (!scriptEntry.hasObject("inventory")
arg.reportUnhandled();
}
}

scriptEntry.defaultObject("type", Type.ITEM)
.defaultObject("quantity", new ElementTag(1));

Type type = (Type) scriptEntry.getObject("type");

if (type != Type.MONEY && scriptEntry.getObject("inventory") == null) {
scriptEntry.addObject("inventory", Utilities.entryHasPlayer(scriptEntry) ? Utilities.getEntryPlayer(scriptEntry).getInventory() : null);
}

if (!scriptEntry.hasObject("inventory") && type != Type.MONEY) {
throw new InvalidArgumentsException("Must specify an inventory to take from!");
}

if (type == Type.ITEM && scriptEntry.getObject("items") == null) {
throw new InvalidArgumentsException("Must specify item/items!");
}

}

@Override
public void execute(ScriptEntry scriptEntry) {

InventoryTag inventory = scriptEntry.getObjectTag("inventory");
ElementTag quantity = scriptEntry.getElement("quantity");
ElementTag displayname = scriptEntry.getElement("displayname");
Expand All @@ -202,14 +193,11 @@ public void execute(ScriptEntry scriptEntry) {
ElementTag nbtKey = scriptEntry.getElement("nbt_key");
MaterialTag material = scriptEntry.getObjectTag("material");
Type type = (Type) scriptEntry.getObject("type");

Object items_object = scriptEntry.getObject("items");
List<ItemTag> items = null;

if (items_object != null) {
items = (List<ItemTag>) items_object;
}

if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), ArgumentHelper.debugObj("Type", type.name())
+ quantity.debug()
Expand All @@ -222,7 +210,6 @@ public void execute(ScriptEntry scriptEntry) {
+ (material != null ? material.debug() : "")
+ (titleAuthor != null ? titleAuthor.debug() : ""));
}

switch (type) {
case INVENTORY: {
inventory.clear();
Expand Down

0 comments on commit e81d7ae

Please sign in to comment.