1 package org.bukkit.inventory;
3 import com.google.common.collect.ImmutableMap;
4 import java.util.LinkedHashMap;
7 import org.apache.commons.lang.Validate;
8 import org.bukkit.Bukkit;
9 import org.bukkit.Material;
10 import org.bukkit.Utility;
11 import org.bukkit.configuration.serialization.ConfigurationSerializable;
12 import org.bukkit.enchantments.Enchantment;
13 import org.bukkit.inventory.meta.ItemMeta;
14 import org.bukkit.material.MaterialData;
21 private int amount = 0;
23 private short durability = 0;
53 public ItemStack(
final int type,
final int amount) {
54 this(type, amount, (short) 0);
64 this(type.
getId(), amount);
74 public ItemStack(
final int type,
final int amount,
final short damage) {
77 this.durability = damage;
88 this(type.
getId(), amount, damage);
95 public ItemStack(
final int type,
final int amount,
final short damage,
final Byte data) {
98 this.durability = damage;
101 this.durability = data;
110 this(type.
getId(), amount, damage, data);
120 Validate.notNull(stack,
"Cannot copy null stack");
121 this.type = stack.getTypeId();
122 this.amount = stack.getAmount();
123 this.durability = stack.getDurability();
125 if (stack.hasItemMeta()) {
126 setItemMeta0(stack.getItemMeta(), getType0());
141 return getType0(this.type);
144 private static Material getType0(
int id) {
158 Validate.notNull(type,
"Material cannot be null");
180 if (this.meta != null) {
183 createData((byte) 0);
201 this.amount = amount;
211 if (data == null && mat != null && mat.
getData() != null) {
226 if (data == null || mat == null || mat.
getData() == null) {
232 throw new IllegalArgumentException(
"Provided data is not of type " + mat.
getData().getName() +
", found " + data.getClass().getName());
243 this.durability = durability;
264 if (material != null) {
270 private void createData(
final byte data) {
283 StringBuilder
toString =
new StringBuilder(
"ItemStack{").append(
getType().name()).append(
" x ").append(
getAmount());
287 return toString.append(
'}').toString();
326 if (this.meta != null) {
327 itemStack.meta = this.meta.
clone();
330 if (this.data != null) {
331 itemStack.data = this.data.
clone();
335 }
catch (CloneNotSupportedException e) {
360 return meta == null ?
false : meta.
hasEnchant(ench);
395 Validate.notNull(enchantments,
"Enchantments cannot be null");
396 for (Map.Entry<
Enchantment, Integer> entry : enchantments.entrySet()) {
412 Validate.notNull(ench,
"Enchantment cannot be null");
414 throw new IllegalArgumentException(
"Enchantment level is either too low or too high (given " + level +
", bounds are " + ench.
getStartLevel() +
" to " + ench.
getMaxLevel());
416 throw new IllegalArgumentException(
"Specified enchantment cannot be applied to this itemstack");
432 for (Map.Entry<
Enchantment, Integer> entry : enchantments.entrySet()) {
460 if (level == 0 || meta == null) {
469 Map<String, Object> result =
new LinkedHashMap<String, Object>();
471 result.put(
"type",
getType().name());
483 result.put(
"meta", meta);
501 if (args.containsKey(
"damage")) {
502 damage = ((Number) args.get(
"damage")).shortValue();
505 if (args.containsKey(
"amount")) {
506 amount = (Integer) args.get(
"amount");
511 if (args.containsKey(
"enchantments")) {
512 Object raw = args.get(
"enchantments");
514 if (raw instanceof Map) {
515 Map<?, ?> map = (Map<?, ?>) raw;
517 for (Map.Entry<?, ?> entry : map.entrySet()) {
520 if ((enchantment != null) && (entry.getValue() instanceof Integer)) {
525 }
else if (args.containsKey(
"meta")) {
526 Object raw = args.get(
"meta");
561 return setItemMeta0(itemMeta, getType0());
568 if (itemMeta == null) {
575 this.meta = Bukkit.getItemFactory().asMetaFor(itemMeta, material);
576 if (this.meta == itemMeta) {
577 this.meta = itemMeta.
clone();