Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A property to keep track of the item's quantity
- Loading branch information
1 parent
dcb6436
commit e570375
Showing
4 changed files
with
91 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/main/java/net/aufdemrand/denizen/objects/properties/Item/ItemQuantity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters