From d30ab0a9285f96be9238c68932de3132af96084c Mon Sep 17 00:00:00 2001 From: Dinnerbone Date: Fri, 14 Jan 2011 00:00:54 +0000 Subject: Added MaterialData --- src/main/java/org/bukkit/ItemStack.java | 75 +++++++++++++++++++--- .../java/org/bukkit/material/MaterialData.java | 75 ++++++++++++++++++++++ 2 files changed, 142 insertions(+), 8 deletions(-) create mode 100644 src/main/java/org/bukkit/material/MaterialData.java (limited to 'src/main') diff --git a/src/main/java/org/bukkit/ItemStack.java b/src/main/java/org/bukkit/ItemStack.java index b2a4c6f4..108c530d 100644 --- a/src/main/java/org/bukkit/ItemStack.java +++ b/src/main/java/org/bukkit/ItemStack.java @@ -1,25 +1,27 @@ package org.bukkit; +import org.bukkit.material.MaterialData; + /** * Represents a stack of items */ public class ItemStack { private int type; private int amount = 0; + private MaterialData data = null; private byte damage = 0; public ItemStack(final int type) { - this.type = type; + this(type, 0); } public ItemStack(final Material type) { - this(type.getID()); + this(type, 0); } public ItemStack(final int type, final int amount) { - this.type = type; - this.amount = amount; + this(type, amount, (byte) 0); } public ItemStack(final Material type, final int amount) { @@ -27,13 +29,29 @@ public class ItemStack { } public ItemStack(final int type, final int amount, final byte damage) { + this(type, amount, damage, (byte) 0); + } + + public ItemStack(final Material type, final int amount, final byte damage) { + this(type.getID(), amount, damage); + } + + public ItemStack(final int type, final int amount, final byte damage, final byte data) { this.type = type; this.amount = amount; this.damage = damage; + + Material mat = Material.getMaterial(type); + + if (mat == null) { + this.data = new MaterialData(type, data); + } else { + this.data = mat.getNewData(data); + } } - public ItemStack(final Material type, final int amount, final byte damage) { - this(type.getID(), amount, damage); + public ItemStack(final Material type, final int amount, final byte damage, final byte data) { + this(type.getID(), amount, damage, data); } /** @@ -46,7 +64,9 @@ public class ItemStack { } /** - * Sets the type of this item + * Sets the type of this item
+ *
+ * Note that in doing so you will reset the MaterialData for this stack * * @param type New type to set the items in this stack to */ @@ -64,12 +84,22 @@ public class ItemStack { } /** - * Sets the type ID of this item + * Sets the type ID of this item
+ *
+ * Note that in doing so you will reset the MaterialData for this stack * * @param type New type ID to set the items in this stack to */ public void setTypeID(int type) { this.type = type; + + Material mat = getType(); + + if (mat == null) { + data = new MaterialData(type, (byte)0); + } else { + data = mat.getNewData((byte)0); + } } /** @@ -90,6 +120,35 @@ public class ItemStack { this.amount = amount; } + /** + * Gets the MaterialData for this stack of items + * + * @return MaterialData for this item + */ + public MaterialData getData() { + return data; + } + + /** + * Sets the MaterialData for this stack of items + * + * @param amount New MaterialData for this item + */ + public void setData(MaterialData data) { + Material mat = getType(); + + if ((mat == null) || (mat.getData() == null)) { + this.data = data; + } else { + if ((data.getClass() == mat.getData()) || (data.getClass() == MaterialData.class)) { + this.data = data; + } else { + throw new IllegalArgumentException("Provided data is not of type " + + mat.getData().getName() + ", found " + data.getClass().getName()); + } + } + } + /** * Sets the damage of this item

* diff --git a/src/main/java/org/bukkit/material/MaterialData.java b/src/main/java/org/bukkit/material/MaterialData.java new file mode 100644 index 00000000..186dc95d --- /dev/null +++ b/src/main/java/org/bukkit/material/MaterialData.java @@ -0,0 +1,75 @@ + +package org.bukkit.material; + +import org.bukkit.ItemStack; +import org.bukkit.Material; + +/** + * Handles specific metadata for certain items or blocks + */ +public class MaterialData { + private final int type; + private byte data = 0; + + public MaterialData(final int type) { + this(type, (byte)0); + } + + public MaterialData(final Material type) { + this(type, (byte)0); + } + + public MaterialData(final int type, final byte data) { + this.type = type; + this.data = data; + } + + public MaterialData(final Material type, final byte data) { + this(type.getID(), data); + } + + /** + * Gets the raw data in this material + * + * @return Raw data + */ + public byte getData() { + return data; + } + + /** + * Sets the raw data of this material + * + * @param data New raw data + */ + public void setData(byte data) { + this.data = data; + } + + /** + * Gets the Material that this MaterialData represents + * + * @return Material represented by this MaterialData + */ + public Material getItemType() { + return Material.getMaterial(type); + } + + /** + * Gets the Material ID that this MaterialData represents + * + * @return Material ID represented by this MaterialData + */ + public int getItemTypeId() { + return type; + } + + /** + * Creates a new ItemStack based on this MaterialData + * + * @return New ItemStack containing a copy of this MaterialData + */ + public ItemStack toItemStack() { + return new ItemStack(type); + } +} -- cgit v1.2.3