diff --git a/src/main/java/net/aufdemrand/denizen/objects/dItem.java b/src/main/java/net/aufdemrand/denizen/objects/dItem.java index 31a2194875..62d06641e5 100644 --- a/src/main/java/net/aufdemrand/denizen/objects/dItem.java +++ b/src/main/java/net/aufdemrand/denizen/objects/dItem.java @@ -526,27 +526,6 @@ public String getAttribute(Attribute attribute) { if (attribute == null) return null; - // <--[tag] - // @attribute - // @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 - // @returns Element - // @description - // Returns a valid identification for the item. - // --> - if (attribute.startsWith("identify")) { - return new Element(identify()) - .getAttribute(attribute.fulfill(1)); - } - // <--[tag] // @attribute // @returns Element(Number) @@ -557,16 +536,6 @@ public String getAttribute(Attribute attribute) { return new Element(getItemStack().getTypeId()) .getAttribute(attribute.fulfill(1)); - // <--[tag] - // @attribute - // @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 // @returns Element(Number) @@ -832,7 +801,6 @@ public void adjust(Mechanism mechanism) { // // --> 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|..."); @@ -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 + // + // + // --> + if (mechanism.matches("quantity") && mechanism.requireInteger()) { + item.setAmount(value.asInt()); + } + + if (!mechanism.fulfilled()) mechanism.reportInvalid(); diff --git a/src/main/java/net/aufdemrand/denizen/objects/properties/Item/ItemQuantity.java b/src/main/java/net/aufdemrand/denizen/objects/properties/Item/ItemQuantity.java new file mode 100644 index 0000000000..1c61af20a0 --- /dev/null +++ b/src/main/java/net/aufdemrand/denizen/objects/properties/Item/ItemQuantity.java @@ -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 + // @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 + // @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"; + } +} diff --git a/src/main/java/net/aufdemrand/denizen/objects/properties/PropertyParser.java b/src/main/java/net/aufdemrand/denizen/objects/properties/PropertyParser.java index 795294d5e6..f7d5d1849e 100644 --- a/src/main/java/net/aufdemrand/denizen/objects/properties/PropertyParser.java +++ b/src/main/java/net/aufdemrand/denizen/objects/properties/PropertyParser.java @@ -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); } diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/item/GiveCommand.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/item/GiveCommand.java index f1925d7eca..68d3a22bed 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/commands/item/GiveCommand.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/commands/item/GiveCommand.java @@ -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")) @@ -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 leftovers = inventory.addWithLeftovers(is);