From 0ad5ed1a30ade75dc1c089cbf1b328716eedcc15 Mon Sep 17 00:00:00 2001 From: David Cernat Date: Mon, 1 Jul 2013 19:09:06 +0300 Subject: [PATCH] Add and tags to dInventory. --- .../denizen/objects/dInventory.java | 52 +++++++++++++++++-- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/aufdemrand/denizen/objects/dInventory.java b/src/main/java/net/aufdemrand/denizen/objects/dInventory.java index 89ded9ed67..a4a045a019 100644 --- a/src/main/java/net/aufdemrand/denizen/objects/dInventory.java +++ b/src/main/java/net/aufdemrand/denizen/objects/dInventory.java @@ -90,6 +90,28 @@ public int countItems(ItemStack item) return qty; } + public int countStacks(ItemStack item) + { + int qty = 0; + + for (ItemStack invStack : inventory) + { + // If ItemStacks are empty here, they are null + if (invStack != null) + { + // If item is null, add up every stack in the + // inventory + // + // If not, add up the stacks that match the item + + if (item == null || invStack.isSimilar(item)) + qty++; + } + } + + return qty; + } + ////////////////////////////// @@ -149,24 +171,48 @@ public String getAttribute(Attribute attribute) { } return new Element(getInventory().containsAtLeast - (dItem.valueOf(attribute.getContext(1)).getItemStack(), qty)) - .getAttribute(attribute.fulfill(1)); + (dItem.valueOf(attribute.getContext(1)).getItemStack(), qty)) + .getAttribute(attribute.fulfill(1)); } } + // Get the combined quantity of itemstacks that match an item if + // one if specified, or the combined quantity of all itemstacks + // if one is not + + if (attribute.startsWith("qty")) + if (attribute.hasContext(1) && dItem.matches(attribute.getContext(1))) + return new Element(String.valueOf(countItems + (dItem.valueOf(attribute.getContext(1)).getItemStack()))) + .getAttribute(attribute.fulfill(1)); + else + return new Element(String.valueOf(countItems(null))) + .getAttribute(attribute.fulfill(1)); + // Return the number of slots in the inventory if (attribute.startsWith("size")) return new Element(String.valueOf(getInventory().getSize())) .getAttribute(attribute.fulfill(1)); + // Get the number of itemstacks that match an item if one is + // specified, or the number of all itemstacks if one is not + + if (attribute.startsWith("stacks")) + if (attribute.hasContext(1) && dItem.matches(attribute.getContext(1))) + return new Element(String.valueOf(countStacks + (dItem.valueOf(attribute.getContext(1)).getItemStack()))) + .getAttribute(attribute.fulfill(1)); + else + return new Element(String.valueOf(countStacks(null))) + .getAttribute(attribute.fulfill(1)); + // Return the type of the inventory (e.g. "PLAYER", "CRAFTING") if (attribute.startsWith("type")) return new Element(getInventory().getType().name()) .getAttribute(attribute.fulfill(1)); - return new Element(identify()).getAttribute(attribute.fulfill(0)); }