Skip to content

Commit

Permalink
add ItemQuantity property
Browse files Browse the repository at this point in the history
A property to keep track of the item's quantity
  • Loading branch information
mcmonkey4eva committed Dec 16, 2013
1 parent dcb6436 commit e570375
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 34 deletions.
47 changes: 15 additions & 32 deletions src/main/java/net/aufdemrand/denizen/objects/dItem.java
Expand Up @@ -526,27 +526,6 @@ public String getAttribute(Attribute attribute) {

if (attribute == null) return null;

// <--[tag]
// @attribute <i@item.qty>
// @returns Element(Number)
// @description
// Returns the number of items in the dItem's itemstack.
// -->
if (attribute.startsWith("qty"))
return new Element(getItemStack().getAmount())
.getAttribute(attribute.fulfill(1));

// <--[tag]
// @attribute <i@item.identify>
// @returns Element
// @description
// Returns a valid identification for the item.
// -->
if (attribute.startsWith("identify")) {
return new Element(identify())
.getAttribute(attribute.fulfill(1));
}

// <--[tag]
// @attribute <i@item.id>
// @returns Element(Number)
Expand All @@ -557,16 +536,6 @@ public String getAttribute(Attribute attribute) {
return new Element(getItemStack().getTypeId())
.getAttribute(attribute.fulfill(1));

// <--[tag]
// @attribute <i@item.max_stack>
// @returns Element(Number)
// @description
// Returns the max number of this item possible in a single stack of this type.
// -->
if (attribute.startsWith("max_stack"))
return new Element(getItemStack().getMaxStackSize())
.getAttribute(attribute.fulfill(1));

// <--[tag]
// @attribute <i@item.data>
// @returns Element(Number)
Expand Down Expand Up @@ -832,7 +801,6 @@ public void adjust(Mechanism mechanism) {
// <i@item.enchantments.with_levels>
// -->
if (mechanism.matches("enchantments")) {
dList enchants = value.asType(dList.class);
for (String enchant: value.asType(dList.class)) {
if (!enchant.contains(","))
dB.echoError("Invalid enchantment format, use name,level|...");
Expand All @@ -852,6 +820,21 @@ public void adjust(Mechanism mechanism) {
}
}

// <--[mechanism]
// @object dItem
// @name quantity
// @input Element(Number)
// @description
// Changes the number of items in this stack.
// @tags
// <i@item.qty>
// <i@item.max_stack>
// -->
if (mechanism.matches("quantity") && mechanism.requireInteger()) {
item.setAmount(value.asInt());
}


if (!mechanism.fulfilled())
mechanism.reportInvalid();

Expand Down
@@ -0,0 +1,70 @@
package net.aufdemrand.denizen.objects.properties.Item;


import net.aufdemrand.denizen.objects.Element;
import net.aufdemrand.denizen.objects.dItem;
import net.aufdemrand.denizen.objects.dObject;
import net.aufdemrand.denizen.objects.properties.Property;
import net.aufdemrand.denizen.tags.Attribute;

public class ItemQuantity implements Property {

public static boolean describes(dObject item) {
// all items can have a quantity
return item instanceof dItem;
}

public static ItemQuantity getFrom(dObject _item) {
if (!describes(_item)) return null;
else return new ItemQuantity((dItem)_item);
}


private ItemQuantity(dItem _item) {
item = _item;
}

dItem item;

@Override
public String getAttribute(Attribute attribute) {

if (attribute == null) return "null";

// <--[tag]
// @attribute <i@item.qty>
// @returns Element(Number)
// @description
// Returns the number of items in the dItem's itemstack.
// -->
if (attribute.startsWith("qty"))
return new Element(item.getItemStack().getAmount())
.getAttribute(attribute.fulfill(1));

// <--[tag]
// @attribute <i@item.max_stack>
// @returns Element(Number)
// @description
// Returns the max number of this item possible in a single stack of this type.
// -->
if (attribute.startsWith("max_stack"))
return new Element(item.getItemStack().getMaxStackSize())
.getAttribute(attribute.fulfill(1));

return null;
}


@Override
public String getPropertyString() {
if (item.getItemStack().getAmount() != 1)
return String.valueOf(item.getItemStack().getAmount());
else
return null;
}

@Override
public String getPropertyId() {
return "quantity";
}
}
Expand Up @@ -37,6 +37,7 @@ public PropertyParser() {
registerProperty(ItemEnchantments.class, dItem.class);
registerProperty(ItemDisplayname.class, dItem.class);
registerProperty(ItemLore.class, dItem.class);
registerProperty(ItemQuantity.class, dItem.class);

}

Expand Down
Expand Up @@ -45,8 +45,10 @@ public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException

if (!scriptEntry.hasObject("qty")
&& arg.matchesPrefix("q, qty, quantity")
&& arg.matchesPrimitive(aH.PrimitiveType.Double))
&& arg.matchesPrimitive(aH.PrimitiveType.Double)) {
scriptEntry.addObject("qty", arg.asElement());
scriptEntry.addObject("set_quantity", Element.TRUE);
}

else if (!scriptEntry.hasObject("type")
&& arg.matches("money, coins"))
Expand Down Expand Up @@ -118,7 +120,8 @@ public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
case ITEM:
for (dItem item : items) {
ItemStack is = item.getItemStack();
is.setAmount(qty.asInt());
if (scriptEntry.hasObject("set_quantity"))
is.setAmount(qty.asInt());
if (engrave.asBoolean()) is = CustomNBT.addCustomNBT(item.getItemStack(), "owner", scriptEntry.getPlayer().getName());

HashMap<Integer, ItemStack> leftovers = inventory.addWithLeftovers(is);
Expand Down

0 comments on commit e570375

Please sign in to comment.