Skip to content

Commit

Permalink
Add <player.inventory.qty> and <player.inventory.stacks> tags to dInv…
Browse files Browse the repository at this point in the history
…entory.
  • Loading branch information
davidcernat committed Jul 1, 2013
1 parent 48dc42f commit 0ad5ed1
Showing 1 changed file with 49 additions and 3 deletions.
52 changes: 49 additions & 3 deletions src/main/java/net/aufdemrand/denizen/objects/dInventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}



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

Expand Down

0 comments on commit 0ad5ed1

Please sign in to comment.