From 56a695176e9498a32993385553bd76a20e3fe5c6 Mon Sep 17 00:00:00 2001 From: Alex 'mcmonkey' Goodwin Date: Sun, 11 Jul 2021 14:59:47 -0700 Subject: [PATCH] InventoryTag.exclude_item --- .../denizen/objects/InventoryTag.java | 53 ++++++++++++++++--- .../objects/properties/item/ItemBook.java | 4 ++ 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/plugin/src/main/java/com/denizenscript/denizen/objects/InventoryTag.java b/plugin/src/main/java/com/denizenscript/denizen/objects/InventoryTag.java index 2285c8c680..9f90683f9d 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/objects/InventoryTag.java +++ b/plugin/src/main/java/com/denizenscript/denizen/objects/InventoryTag.java @@ -1271,16 +1271,16 @@ public static void registerTags() { }); // <--[tag] - // @attribute |...]> + // @attribute ]> // @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 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()); @@ -1291,13 +1291,54 @@ public static void registerTags() { dummyInv.idHolder = object.idHolder; } trackTemporaryInventory(dummyInv); + int quantity = Integer.MAX_VALUE; // <--[tag] - // @attribute ].quantity[<#>]> + // @attribute ].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 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); diff --git a/plugin/src/main/java/com/denizenscript/denizen/objects/properties/item/ItemBook.java b/plugin/src/main/java/com/denizenscript/denizen/objects/properties/item/ItemBook.java index 9a46ab5693..95d2325898 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/objects/properties/item/ItemBook.java +++ b/plugin/src/main/java/com/denizenscript/denizen/objects/properties/item/ItemBook.java @@ -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) {