Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
org.bukkit.inventory.ItemStack Class Reference
Inheritance diagram for org.bukkit.inventory.ItemStack:
org.bukkit.configuration.serialization.ConfigurationSerializable

Public Member Functions

 ItemStack (final int type)
 
 ItemStack (final Material type)
 
 ItemStack (final int type, final int amount)
 
 ItemStack (final Material type, final int amount)
 
 ItemStack (final int type, final int amount, final short damage)
 
 ItemStack (final Material type, final int amount, final short damage)
 
 ItemStack (final int type, final int amount, final short damage, final Byte data)
 
 ItemStack (final Material type, final int amount, final short damage, final Byte data)
 
 ItemStack (final ItemStack stack) throws IllegalArgumentException
 
Material getType ()
 
void setType (Material type)
 
int getTypeId ()
 
void setTypeId (int type)
 
int getAmount ()
 
void setAmount (int amount)
 
MaterialData getData ()
 
void setData (MaterialData data)
 
void setDurability (final short durability)
 
short getDurability ()
 
int getMaxStackSize ()
 
String toString ()
 
boolean equals (Object obj)
 
boolean isSimilar (ItemStack stack)
 
ItemStack clone ()
 
final int hashCode ()
 
boolean containsEnchantment (Enchantment ench)
 
int getEnchantmentLevel (Enchantment ench)
 
Map< Enchantment, Integer > getEnchantments ()
 
void addEnchantments (Map< Enchantment, Integer > enchantments)
 
void addEnchantment (Enchantment ench, int level)
 
void addUnsafeEnchantments (Map< Enchantment, Integer > enchantments)
 
void addUnsafeEnchantment (Enchantment ench, int level)
 
int removeEnchantment (Enchantment ench)
 
Map< String, Object > serialize ()
 
ItemMeta getItemMeta ()
 
boolean hasItemMeta ()
 
boolean setItemMeta (ItemMeta itemMeta)
 

Static Public Member Functions

static ItemStack deserialize (Map< String, Object > args)
 

Detailed Description

Represents a stack of items

Definition at line 19 of file ItemStack.java.

Constructor & Destructor Documentation

org.bukkit.inventory.ItemStack.ItemStack ( final int  type)

Defaults stack size to 1, with no extra data

Parameters
typeitem material id
Deprecated:
Magic value

Definition at line 36 of file ItemStack.java.

36  {
37  this(type, 1);
38  }
org.bukkit.inventory.ItemStack.ItemStack ( final Material  type)

Defaults stack size to 1, with no extra data

Parameters
typeitem material

Definition at line 45 of file ItemStack.java.

45  {
46  this(type, 1);
47  }
org.bukkit.inventory.ItemStack.ItemStack ( final int  type,
final int  amount 
)

An item stack with no extra data

Parameters
typeitem material id
amountstack size
Deprecated:
Magic value

Definition at line 57 of file ItemStack.java.

57  {
58  this(type, amount, (short) 0);
59  }
org.bukkit.inventory.ItemStack.ItemStack ( final Material  type,
final int  amount 
)

An item stack with no extra data

Parameters
typeitem material
amountstack size

Definition at line 67 of file ItemStack.java.

References org.bukkit.Material.getId().

67  {
68  this(type.getId(), amount);
69  }
org.bukkit.inventory.ItemStack.ItemStack ( final int  type,
final int  amount,
final short  damage 
)

An item stack with the specified damage / durability

Parameters
typeitem material id
amountstack size
damagedurability / damage
Deprecated:
Magic value

Definition at line 80 of file ItemStack.java.

80  {
81  this.type = type;
82  this.amount = amount;
83  this.durability = damage;
84  }
org.bukkit.inventory.ItemStack.ItemStack ( final Material  type,
final int  amount,
final short  damage 
)

An item stack with the specified damage / durabiltiy

Parameters
typeitem material
amountstack size
damagedurability / damage

Definition at line 93 of file ItemStack.java.

References org.bukkit.Material.getId().

93  {
94  this(type.getId(), amount, damage);
95  }
org.bukkit.inventory.ItemStack.ItemStack ( final int  type,
final int  amount,
final short  damage,
final Byte  data 
)
Deprecated:
this method uses an ambiguous data byte object

Definition at line 101 of file ItemStack.java.

101  {
102  this.type = type;
103  this.amount = amount;
104  this.durability = damage;
105  if (data != null) {
106  createData(data);
107  this.durability = data;
108  }
109  }
org.bukkit.inventory.ItemStack.ItemStack ( final Material  type,
final int  amount,
final short  damage,
final Byte  data 
)
Deprecated:
this method uses an ambiguous data byte object

Definition at line 115 of file ItemStack.java.

References org.bukkit.Material.getId().

115  {
116  this(type.getId(), amount, damage, data);
117  }
org.bukkit.inventory.ItemStack.ItemStack ( final ItemStack  stack) throws IllegalArgumentException

Creates a new item stack derived from the specified stack

Parameters
stackthe stack to copy
Exceptions
IllegalArgumentExceptionif the specified stack is null or returns an item meta not created by the item factory

Definition at line 126 of file ItemStack.java.

References org.bukkit.material.MaterialData.getData().

126  {
127  Validate.notNull(stack, "Cannot copy null stack");
128  this.type = stack.getTypeId();
129  this.amount = stack.getAmount();
130  this.durability = stack.getDurability();
131  this.data = stack.getData();
132  if (stack.hasItemMeta()) {
133  setItemMeta0(stack.getItemMeta(), getType0());
134  }
135  }

Member Function Documentation

void org.bukkit.inventory.ItemStack.addEnchantment ( Enchantment  ench,
int  level 
)

Adds the specified Enchantment to this item stack.

If this item stack already contained the given enchantment (at any level), it will be replaced.

Parameters
enchEnchantment to add
levelLevel of the enchantment
Exceptions
IllegalArgumentExceptionif enchantment null, or enchantment is not applicable

Definition at line 427 of file ItemStack.java.

References org.bukkit.inventory.ItemStack.addUnsafeEnchantment(), org.bukkit.enchantments.Enchantment.canEnchantItem(), org.bukkit.enchantments.Enchantment.getMaxLevel(), and org.bukkit.enchantments.Enchantment.getStartLevel().

Referenced by org.bukkit.inventory.ItemStack.addEnchantments().

427  {
428  Validate.notNull(ench, "Enchantment cannot be null");
429  if ((level < ench.getStartLevel()) || (level > ench.getMaxLevel())) {
430  throw new IllegalArgumentException("Enchantment level is either too low or too high (given " + level + ", bounds are " + ench.getStartLevel() + " to " + ench.getMaxLevel() + ")");
431  } else if (!ench.canEnchantItem(this)) {
432  throw new IllegalArgumentException("Specified enchantment cannot be applied to this itemstack");
433  }
434 
435  addUnsafeEnchantment(ench, level);
436  }
void addUnsafeEnchantment(Enchantment ench, int level)
Definition: ItemStack.java:466
void org.bukkit.inventory.ItemStack.addEnchantments ( Map< Enchantment, Integer >  enchantments)

Adds the specified enchantments to this item stack.

This method is the same as calling addEnchantment(org.bukkit.enchantments.Enchantment, int) for each element of the map.

Parameters
enchantmentsEnchantments to add
Exceptions
IllegalArgumentExceptionif the specified enchantments is null
IllegalArgumentExceptionif any specific enchantment or level is null. Warning: Some enchantments may be added before this exception is thrown.

Definition at line 408 of file ItemStack.java.

References org.bukkit.inventory.ItemStack.addEnchantment().

408  {
409  Validate.notNull(enchantments, "Enchantments cannot be null");
410  for (Map.Entry<Enchantment, Integer> entry : enchantments.entrySet()) {
411  addEnchantment(entry.getKey(), entry.getValue());
412  }
413  }
void addEnchantment(Enchantment ench, int level)
Definition: ItemStack.java:427
void org.bukkit.inventory.ItemStack.addUnsafeEnchantment ( Enchantment  ench,
int  level 
)

Adds the specified Enchantment to this item stack.

If this item stack already contained the given enchantment (at any level), it will be replaced.

This method is unsafe and will ignore level restrictions or item type. Use at your own discretion.

Parameters
enchEnchantment to add
levelLevel of the enchantment

Definition at line 466 of file ItemStack.java.

References org.bukkit.Bukkit.getItemFactory(), and org.bukkit.inventory.ItemFactory.getItemMeta().

Referenced by org.bukkit.inventory.ItemStack.addEnchantment(), org.bukkit.inventory.ItemStack.addUnsafeEnchantments(), and org.bukkit.inventory.ItemStack.deserialize().

466  {
467  (meta == null ? meta = Bukkit.getItemFactory().getItemMeta(getType0()) : meta).addEnchant(ench, level, true);
468  }
void org.bukkit.inventory.ItemStack.addUnsafeEnchantments ( Map< Enchantment, Integer >  enchantments)

Adds the specified enchantments to this item stack in an unsafe manner.

This method is the same as calling addUnsafeEnchantment(org.bukkit.enchantments.Enchantment, int) for each element of the map.

Parameters
enchantmentsEnchantments to add

Definition at line 448 of file ItemStack.java.

References org.bukkit.inventory.ItemStack.addUnsafeEnchantment().

448  {
449  for (Map.Entry<Enchantment, Integer> entry : enchantments.entrySet()) {
450  addUnsafeEnchantment(entry.getKey(), entry.getValue());
451  }
452  }
void addUnsafeEnchantment(Enchantment ench, int level)
Definition: ItemStack.java:466
boolean org.bukkit.inventory.ItemStack.containsEnchantment ( Enchantment  ench)

Checks if this ItemStack contains the given Enchantment

Parameters
enchEnchantment to test
Returns
True if this has the given enchantment

Definition at line 371 of file ItemStack.java.

371  {
372  return meta == null ? false : meta.hasEnchant(ench);
373  }
boolean hasEnchant(Enchantment ench)
static ItemStack org.bukkit.inventory.ItemStack.deserialize ( Map< String, Object >  args)
static

Required method for configuration serialization

Parameters
argsmap to deserialize
Returns
deserialized item stack
See also
ConfigurationSerializable

Definition at line 515 of file ItemStack.java.

References org.bukkit.inventory.ItemStack.addUnsafeEnchantment(), org.bukkit.enchantments.Enchantment.getByName(), org.bukkit.Material.getMaterial(), and org.bukkit.inventory.ItemStack.setItemMeta().

515  {
516  Material type = Material.getMaterial((String) args.get("type"));
517  short damage = 0;
518  int amount = 1;
519 
520  if (args.containsKey("damage")) {
521  damage = ((Number) args.get("damage")).shortValue();
522  }
523 
524  if (args.containsKey("amount")) {
525  amount = (Integer) args.get("amount");
526  }
527 
528  ItemStack result = new ItemStack(type, amount, damage);
529 
530  if (args.containsKey("enchantments")) { // Backward compatiblity, @deprecated
531  Object raw = args.get("enchantments");
532 
533  if (raw instanceof Map) {
534  Map<?, ?> map = (Map<?, ?>) raw;
535 
536  for (Map.Entry<?, ?> entry : map.entrySet()) {
537  Enchantment enchantment = Enchantment.getByName(entry.getKey().toString());
538 
539  if ((enchantment != null) && (entry.getValue() instanceof Integer)) {
540  result.addUnsafeEnchantment(enchantment, (Integer) entry.getValue());
541  }
542  }
543  }
544  } else if (args.containsKey("meta")) { // We cannot and will not have meta when enchantments (pre-ItemMeta) exist
545  Object raw = args.get("meta");
546  if (raw instanceof ItemMeta) {
547  result.setItemMeta((ItemMeta) raw);
548  }
549  }
550 
551  return result;
552  }
int org.bukkit.inventory.ItemStack.getAmount ( )

Gets the amount of items in this stack

Returns
Amount of items in this stick

Definition at line 202 of file ItemStack.java.

Referenced by org.bukkit.inventory.ItemStack.serialize().

202  {
203  return amount;
204  }
MaterialData org.bukkit.inventory.ItemStack.getData ( )

Gets the MaterialData for this stack of items

Returns
MaterialData for this item

Definition at line 220 of file ItemStack.java.

References org.bukkit.Material.getData(), org.bukkit.inventory.ItemStack.getDurability(), org.bukkit.Material.getNewData(), and org.bukkit.inventory.ItemStack.getType().

220  {
221  Material mat = getType();
222  if (data == null && mat != null && mat.getData() != null) {
223  data = mat.getNewData((byte) this.getDurability());
224  }
225 
226  return data;
227  }
short org.bukkit.inventory.ItemStack.getDurability ( )

Gets the durability of this item

Returns
Durability of this item

Definition at line 262 of file ItemStack.java.

Referenced by org.bukkit.inventory.ItemStack.getData(), org.bukkit.inventory.ItemStack.isSimilar(), org.bukkit.inventory.ShapelessRecipe.removeIngredient(), and org.bukkit.inventory.ItemStack.serialize().

262  {
263  return durability;
264  }
int org.bukkit.inventory.ItemStack.getEnchantmentLevel ( Enchantment  ench)

Gets the level of the specified enchantment on this item stack

Parameters
enchEnchantment to check
Returns
Level of the enchantment, or 0

Definition at line 381 of file ItemStack.java.

Referenced by org.bukkit.inventory.ItemStack.removeEnchantment().

381  {
382  return meta == null ? 0 : meta.getEnchantLevel(ench);
383  }
int getEnchantLevel(Enchantment ench)
Map<Enchantment, Integer> org.bukkit.inventory.ItemStack.getEnchantments ( )

Gets a map containing all enchantments and their levels on this item.

Returns
Map of enchantments.

Definition at line 390 of file ItemStack.java.

390  {
391  return meta == null ? ImmutableMap.<Enchantment, Integer>of() : meta.getEnchants();
392  }
ItemMeta org.bukkit.inventory.ItemStack.getItemMeta ( )

Get a copy of this ItemStack's ItemMeta.

Returns
a copy of the current ItemStack's ItemData

Definition at line 559 of file ItemStack.java.

References org.bukkit.Bukkit.getItemFactory(), and org.bukkit.inventory.ItemFactory.getItemMeta().

Referenced by org.bukkit.inventory.ItemStack.isSimilar(), and org.bukkit.inventory.ItemStack.serialize().

559  {
560  return this.meta == null ? Bukkit.getItemFactory().getItemMeta(getType0()) : this.meta.clone();
561  }
int org.bukkit.inventory.ItemStack.getMaxStackSize ( )

Get the maximum stacksize for the material hold in this ItemStack. (Returns -1 if it has no idea)

Returns
The maximum you can stack this material to.

Definition at line 273 of file ItemStack.java.

References org.bukkit.Material.getMaxStackSize(), and org.bukkit.inventory.ItemStack.getType().

273  {
274  Material material = getType();
275  if (material != null) {
276  return material.getMaxStackSize();
277  }
278  return -1;
279  }
int org.bukkit.inventory.ItemStack.getTypeId ( )

Gets the type id of this item

Returns
Type Id of the items in this stack
Deprecated:
Magic value

Definition at line 176 of file ItemStack.java.

Referenced by org.bukkit.inventory.ItemStack.getType(), and org.bukkit.inventory.ItemStack.isSimilar().

176  {
177  return type;
178  }
boolean org.bukkit.inventory.ItemStack.hasItemMeta ( )

Checks to see if any meta data has been defined.

Returns
Returns true if some meta data has been set for this item

Definition at line 568 of file ItemStack.java.

References org.bukkit.inventory.ItemFactory.equals(), and org.bukkit.Bukkit.getItemFactory().

Referenced by org.bukkit.inventory.ItemStack.isSimilar().

568  {
569  return !Bukkit.getItemFactory().equals(meta, null);
570  }
boolean org.bukkit.inventory.ItemStack.isSimilar ( ItemStack  stack)

This method is the same as equals, but does not consider stack size (amount).

Parameters
stackthe item stack to compare to
Returns
true if the two stacks are equal, ignoring the amount

Definition at line 323 of file ItemStack.java.

References org.bukkit.inventory.ItemFactory.equals(), org.bukkit.inventory.ItemStack.getDurability(), org.bukkit.Bukkit.getItemFactory(), org.bukkit.inventory.ItemStack.getItemMeta(), org.bukkit.inventory.ItemStack.getTypeId(), and org.bukkit.inventory.ItemStack.hasItemMeta().

323  {
324  if (stack == null) {
325  return false;
326  }
327  if (stack == this) {
328  return true;
329  }
330  return getTypeId() == stack.getTypeId() && getDurability() == stack.getDurability() && hasItemMeta() == stack.hasItemMeta() && (hasItemMeta() ? Bukkit.getItemFactory().equals(getItemMeta(), stack.getItemMeta()) : true);
331  }
int org.bukkit.inventory.ItemStack.removeEnchantment ( Enchantment  ench)

Removes the specified Enchantment if it exists on this ItemStack

Parameters
enchEnchantment to remove
Returns
Previous level, or 0

Definition at line 477 of file ItemStack.java.

References org.bukkit.inventory.ItemStack.getEnchantmentLevel().

477  {
478  int level = getEnchantmentLevel(ench);
479  if (level == 0 || meta == null) {
480  return level;
481  }
482  meta.removeEnchant(ench);
483  return level;
484  }
boolean removeEnchant(Enchantment ench)
int getEnchantmentLevel(Enchantment ench)
Definition: ItemStack.java:381
Map<String, Object> org.bukkit.inventory.ItemStack.serialize ( )

Creates a Map representation of this class.

This class must provide a method to restore this class, as defined in the ConfigurationSerializable interface javadocs.

Returns
Map containing the current state of this class

Implements org.bukkit.configuration.serialization.ConfigurationSerializable.

Definition at line 487 of file ItemStack.java.

References org.bukkit.inventory.ItemFactory.equals(), org.bukkit.inventory.ItemStack.getAmount(), org.bukkit.inventory.ItemStack.getDurability(), org.bukkit.Bukkit.getItemFactory(), org.bukkit.inventory.ItemStack.getItemMeta(), and org.bukkit.inventory.ItemStack.getType().

487  {
488  Map<String, Object> result = new LinkedHashMap<String, Object>();
489 
490  result.put("type", getType().name());
491 
492  if (getDurability() != 0) {
493  result.put("damage", getDurability());
494  }
495 
496  if (getAmount() != 1) {
497  result.put("amount", getAmount());
498  }
499 
500  ItemMeta meta = getItemMeta();
501  if (!Bukkit.getItemFactory().equals(meta, null)) {
502  result.put("meta", meta);
503  }
504 
505  return result;
506  }
void org.bukkit.inventory.ItemStack.setAmount ( int  amount)

Sets the amount of items in this stack

Parameters
amountNew amount of items in this stack

Definition at line 211 of file ItemStack.java.

211  {
212  this.amount = amount;
213  }
void org.bukkit.inventory.ItemStack.setData ( MaterialData  data)

Sets the MaterialData for this stack of items

Parameters
dataNew MaterialData for this item

Definition at line 234 of file ItemStack.java.

References org.bukkit.Material.getData(), and org.bukkit.inventory.ItemStack.getType().

234  {
235  Material mat = getType();
236 
237  if (data == null || mat == null || mat.getData() == null) {
238  this.data = data;
239  } else {
240  if ((data.getClass() == mat.getData()) || (data.getClass() == MaterialData.class)) {
241  this.data = data;
242  } else {
243  throw new IllegalArgumentException("Provided data is not of type " + mat.getData().getName() + ", found " + data.getClass().getName());
244  }
245  }
246  }
void org.bukkit.inventory.ItemStack.setDurability ( final short  durability)

Sets the durability of this item

Parameters
durabilityDurability of this item

Definition at line 253 of file ItemStack.java.

Referenced by org.bukkit.potion.Potion.apply().

253  {
254  this.durability = durability;
255  }
boolean org.bukkit.inventory.ItemStack.setItemMeta ( ItemMeta  itemMeta)

Set the ItemMeta of this ItemStack.

Parameters
itemMetanew ItemMeta, or null to indicate meta data be cleared.
Returns
True if successfully applied ItemMeta, see ItemFactory#isApplicable(ItemMeta, ItemStack)
Exceptions
IllegalArgumentExceptionif the item meta was not created by the ItemFactory

Definition at line 581 of file ItemStack.java.

Referenced by org.bukkit.inventory.ItemStack.deserialize().

581  {
582  return setItemMeta0(itemMeta, getType0());
583  }
void org.bukkit.inventory.ItemStack.setType ( Material  type)

Sets the type of this item

Note that in doing so you will reset the MaterialData for this stack

Parameters
typeNew type to set the items in this stack to

Definition at line 164 of file ItemStack.java.

References org.bukkit.Material.getId(), and org.bukkit.inventory.ItemStack.setTypeId().

164  {
165  Validate.notNull(type, "Material cannot be null");
166  setTypeId(type.getId());
167  }
void org.bukkit.inventory.ItemStack.setTypeId ( int  type)

Sets the type id of this item

Note that in doing so you will reset the MaterialData for this stack

Parameters
typeNew type id to set the items in this stack to
Deprecated:
Magic value

Definition at line 189 of file ItemStack.java.

References org.bukkit.inventory.ItemFactory.asMetaFor(), and org.bukkit.Bukkit.getItemFactory().

Referenced by org.bukkit.inventory.ItemStack.setType().

189  {
190  this.type = type;
191  if (this.meta != null) {
192  this.meta = Bukkit.getItemFactory().asMetaFor(meta, getType0());
193  }
194  createData((byte) 0);
195  }

The documentation for this class was generated from the following file: