Fork of Poseidon providing Bukkit #1060 to older Beta versions (b1.0-b1.7.3)
1package org.bukkit.inventory;
2
3import org.bukkit.Material;
4import org.bukkit.material.MaterialData;
5
6/**
7 * Represents a stack of items
8 */
9public class ItemStack {
10 private int type;
11 private int amount = 0;
12 private MaterialData data = null;
13 private short durability = 0;
14
15 public ItemStack(final int type) {
16 this(type, 0);
17 }
18
19 public ItemStack(final Material type) {
20 this(type, 0);
21 }
22
23 public ItemStack(final int type, final int amount) {
24 this(type, amount, (short) 0);
25 }
26
27 public ItemStack(final Material type, final int amount) {
28 this(type.getId(), amount);
29 }
30
31 public ItemStack(final int type, final int amount, final short damage) {
32 this(type, amount, damage, null);
33 }
34
35 public ItemStack(final Material type, final int amount, final short damage) {
36 this(type.getId(), amount, damage);
37 }
38
39 public ItemStack(final int type, final int amount, final short damage, final Byte data) {
40 this.type = type;
41 this.amount = amount;
42 this.durability = damage;
43 if (data != null) {
44 createData(data);
45 this.durability = data;
46 }
47 }
48
49 public ItemStack(final Material type, final int amount, final short damage, final Byte data) {
50 this(type.getId(), amount, damage, data);
51 }
52
53 /**
54 * Gets the type of this item
55 *
56 * @return Type of the items in this stack
57 */
58 public Material getType() {
59 return Material.getMaterial(type);
60 }
61
62 /**
63 * Sets the type of this item<br />
64 * <br />
65 * Note that in doing so you will reset the MaterialData for this stack
66 *
67 * @param type New type to set the items in this stack to
68 */
69 public void setType(Material type) {
70 setTypeId(type.getId());
71 }
72
73 /**
74 * Gets the type id of this item
75 *
76 * @return Type Id of the items in this stack
77 */
78 public int getTypeId() {
79 return type;
80 }
81
82 /**
83 * Sets the type id of this item<br />
84 * <br />
85 * Note that in doing so you will reset the MaterialData for this stack
86 *
87 * @param type New type id to set the items in this stack to
88 */
89 public void setTypeId(int type) {
90 this.type = type;
91 createData((byte) 0);
92 }
93
94 /**
95 * Gets the amount of items in this stack
96 *
97 * @return Amount of items in this stick
98 */
99 public int getAmount() {
100 return amount;
101 }
102
103 /**
104 * Sets the amount of items in this stack
105 *
106 * @param amount New amount of items in this stack
107 */
108 public void setAmount(int amount) {
109 this.amount = amount;
110 }
111
112 /**
113 * Gets the MaterialData for this stack of items
114 *
115 * @return MaterialData for this item
116 */
117 public MaterialData getData() {
118 if (Material.getMaterial(getTypeId()).getData() != null) {
119 data = Material.getMaterial(getTypeId()).getNewData((byte) this.durability);
120 }
121
122 return data;
123 }
124
125 /**
126 * Sets the MaterialData for this stack of items
127 *
128 * @param amount New MaterialData for this item
129 */
130 public void setData(MaterialData data) {
131 Material mat = getType();
132
133 if ((mat == null) || (mat.getData() == null)) {
134 this.data = data;
135 } else {
136 if ((data.getClass() == mat.getData()) || (data.getClass() == MaterialData.class)) {
137 this.data = data;
138 } else {
139 throw new IllegalArgumentException("Provided data is not of type " + mat.getData().getName() + ", found " + data.getClass().getName());
140 }
141 }
142 }
143
144 /**
145 * Sets the durability of this item
146 *
147 * @param durability Durability of this item
148 */
149 public void setDurability(final short durability) {
150 this.durability = durability;
151 }
152
153 /**
154 * Gets the durability of this item
155 *
156 * @return Durability of this item
157 */
158 public short getDurability() {
159 return durability;
160 }
161
162 /**
163 * Get the maximum stacksize for the material hold in this ItemStack
164 * Returns -1 if it has no idea.
165 *
166 * @return The maximum you can stack this material to.
167 */
168 public int getMaxStackSize() {
169 return -1;
170 }
171
172 private void createData(final byte data) {
173 Material mat = Material.getMaterial(type);
174
175 if (mat == null) {
176 this.data = new MaterialData(type, data);
177 } else {
178 this.data = mat.getNewData(data);
179 }
180 }
181
182 @Override
183 public String toString() {
184 return "ItemStack{" + getType().name() + " x " + getAmount() + "}";
185 }
186
187 @Override
188 public boolean equals(Object obj) {
189 if (!(obj instanceof ItemStack)) {
190 return false;
191 }
192
193 ItemStack item = (ItemStack) obj;
194
195 return item.getAmount() == getAmount() && item.getTypeId() == getTypeId();
196 }
197
198 @Override
199 public ItemStack clone() {
200 return new ItemStack(type, amount, durability);
201 }
202
203 @Override
204 public int hashCode() {
205 int hash = 11;
206
207 hash = hash * 19 + 7 * getTypeId(); // Overriding hashCode since equals is overridden, it's just
208 hash = hash * 7 + 23 * getAmount(); // too bad these are mutable values... Q_Q
209 return hash;
210 }
211}