Skip to content

Commit

Permalink
InventoryTag.exclude_item
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jul 11, 2021
1 parent 7b9bc4e commit 56a6951
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
Expand Up @@ -1271,16 +1271,16 @@ public static void registerTags() {
});

// <--[tag]
// @attribute <InventoryTag.exclude[<item>|...]>
// @attribute <InventoryTag.exclude_item[<item_matcher>]>
// @returns InventoryTag
// @description
// Returns a copy of the InventoryTag with items excluded.
// Returns a copy of the InventoryTag with all matching items excluded.
// -->
registerTag("exclude", (attribute, object) -> {
registerTag("exclude_item", (attribute, object) -> {
if (!attribute.hasContext(1)) {
return null;
}
List<ItemTag> items = ListTag.getListFor(attribute.getContextObject(1), attribute.context).filter(ItemTag.class, attribute.context);
String matcher = attribute.getContext(1);
InventoryTag dummyInv = new InventoryTag(object.inventory.getType(), AdvancedTextImpl.instance.getTitle(object.inventory));
if (object.inventory.getType() == InventoryType.CHEST) {
dummyInv.setSize(object.inventory.getSize());
Expand All @@ -1291,13 +1291,54 @@ public static void registerTags() {
dummyInv.idHolder = object.idHolder;
}
trackTemporaryInventory(dummyInv);
int quantity = Integer.MAX_VALUE;

// <--[tag]
// @attribute <InventoryTag.exclude[<item>].quantity[<#>]>
// @attribute <InventoryTag.exclude_item[<item_matcher>].quantity[<#>]>
// @returns InventoryTag
// @description
// Returns the InventoryTag with a certain quantity of an item excluded.
// Returns the InventoryTag with a certain quantity of matching items excluded.
// -->
if (attribute.startsWith("quantity", 2) && attribute.hasContext(2)) {
quantity = attribute.getIntContext(2);
attribute.fulfill(1);
}
for (int slot = 0; slot < dummyInv.inventory.getSize(); slot++) {
ItemStack item = dummyInv.inventory.getItem(slot);
if (item != null && BukkitScriptEvent.tryItem(new ItemTag(item), matcher)) {
quantity -= item.getAmount();
if (quantity >= 0) {
dummyInv.inventory.setItem(slot, null);
}
else {
item = item.clone();
item.setAmount(-quantity);
dummyInv.inventory.setItem(slot, item);
}
if (quantity <= 0) {
break;
}
}
}
return dummyInv;
});

registerTag("exclude", (attribute, object) -> {
Deprecations.inventoryNonMatcherTags.warn(attribute.context);
if (!attribute.hasContext(1)) {
return null;
}
List<ItemTag> items = ListTag.getListFor(attribute.getContextObject(1), attribute.context).filter(ItemTag.class, attribute.context);
InventoryTag dummyInv = new InventoryTag(object.inventory.getType(), AdvancedTextImpl.instance.getTitle(object.inventory));
if (object.inventory.getType() == InventoryType.CHEST) {
dummyInv.setSize(object.inventory.getSize());
}
dummyInv.setContents(object.getContents());
if (object.idHolder instanceof ScriptTag) {
dummyInv.idType = "script";
dummyInv.idHolder = object.idHolder;
}
trackTemporaryInventory(dummyInv);
if ((attribute.startsWith("quantity", 2) || attribute.startsWith("qty", 2)) && attribute.hasContext(2)) {
if (attribute.startsWith("qty", 2)) {
Deprecations.qtyTags.warn(attribute.context);
Expand Down
Expand Up @@ -308,6 +308,10 @@ public void adjust(Mechanism mechanism) {
BookMeta meta = (BookMeta) item.getItemMeta();
if (mechanism.getValue().asString().startsWith("map@")) {
MapTag mapData = mechanism.valueAsType(MapTag.class);
if (mapData == null) {
mechanism.echoError("Book input is an invalid map?");
return;
}
ObjectTag author = mapData.getObject("author");
ObjectTag title = mapData.getObject("title");
if (author != null && title != null) {
Expand Down

0 comments on commit 56a6951

Please sign in to comment.