Class ItemStack
- All Implemented Interfaces:
java.lang.Cloneable
,ConfigurationSerializable
public class ItemStack extends java.lang.Object implements java.lang.Cloneable, ConfigurationSerializable
-
Constructor Summary
Constructors Modifier Constructor Description protected
ItemStack()
ItemStack(int type)
Deprecated.Magic valueItemStack(int type, int amount)
Deprecated.Magic valueItemStack(int type, int amount, short damage)
Deprecated.Magic valueItemStack(int type, int amount, short damage, java.lang.Byte data)
Deprecated.this method uses an ambiguous data byte objectItemStack(ItemStack stack)
Creates a new item stack derived from the specified stackItemStack(Material type)
Defaults stack size to 1, with no extra dataItemStack(Material type, int amount)
An item stack with no extra dataItemStack(Material type, int amount, short damage)
An item stack with the specified damage / durabiltiyItemStack(Material type, int amount, short damage, java.lang.Byte data)
Deprecated.this method uses an ambiguous data byte object -
Method Summary
Modifier and Type Method Description void
addEnchantment(Enchantment ench, int level)
Adds the specifiedEnchantment
to this item stack.void
addEnchantments(java.util.Map<Enchantment,java.lang.Integer> enchantments)
Adds the specified enchantments to this item stack.void
addUnsafeEnchantment(Enchantment ench, int level)
Adds the specifiedEnchantment
to this item stack.void
addUnsafeEnchantments(java.util.Map<Enchantment,java.lang.Integer> enchantments)
Adds the specified enchantments to this item stack in an unsafe manner.ItemStack
clone()
boolean
containsEnchantment(Enchantment ench)
Checks if this ItemStack contains the givenEnchantment
static ItemStack
deserialize(java.util.Map<java.lang.String,java.lang.Object> args)
Required method for configuration serializationboolean
equals(java.lang.Object obj)
int
getAmount()
Gets the amount of items in this stackMaterialData
getData()
Gets the MaterialData for this stack of itemsshort
getDurability()
Gets the durability of this itemint
getEnchantmentLevel(Enchantment ench)
Gets the level of the specified enchantment on this item stackjava.util.Map<Enchantment,java.lang.Integer>
getEnchantments()
Gets a map containing all enchantments and their levels on this item.ItemMeta
getItemMeta()
Get a copy of this ItemStack'sItemMeta
.int
getMaxStackSize()
Get the maximum stacksize for the material hold in this ItemStack.Material
getType()
Gets the type of this itemint
getTypeId()
Deprecated.Magic valueint
hashCode()
boolean
hasItemMeta()
Checks to see if any meta data has been defined.boolean
isSimilar(ItemStack stack)
This method is the same as equals, but does not consider stack size (amount).int
removeEnchantment(Enchantment ench)
Removes the specifiedEnchantment
if it exists on this ItemStackjava.util.Map<java.lang.String,java.lang.Object>
serialize()
Creates a Map representation of this class.void
setAmount(int amount)
Sets the amount of items in this stackvoid
setData(MaterialData data)
Sets the MaterialData for this stack of itemsvoid
setDurability(short durability)
Sets the durability of this itemboolean
setItemMeta(ItemMeta itemMeta)
Set the ItemMeta of this ItemStack.void
setType(Material type)
Sets the type of this itemvoid
setTypeId(int type)
Deprecated.Magic valuejava.lang.String
toString()
-
Constructor Details
-
ItemStack
protected ItemStack() -
ItemStack
@Deprecated public ItemStack(int type)Deprecated.Magic valueDefaults stack size to 1, with no extra data- Parameters:
type
- item material id
-
ItemStack
Defaults stack size to 1, with no extra data- Parameters:
type
- item material
-
ItemStack
@Deprecated public ItemStack(int type, int amount)Deprecated.Magic valueAn item stack with no extra data- Parameters:
type
- item material idamount
- stack size
-
ItemStack
An item stack with no extra data- Parameters:
type
- item materialamount
- stack size
-
ItemStack
@Deprecated public ItemStack(int type, int amount, short damage)Deprecated.Magic valueAn item stack with the specified damage / durability- Parameters:
type
- item material idamount
- stack sizedamage
- durability / damage
-
ItemStack
An item stack with the specified damage / durabiltiy- Parameters:
type
- item materialamount
- stack sizedamage
- durability / damage
-
ItemStack
@Deprecated public ItemStack(int type, int amount, short damage, java.lang.Byte data)Deprecated.this method uses an ambiguous data byte object- Parameters:
type
- the raw type idamount
- the amount in the stackdamage
- the damage value of the itemdata
- the data value or null
-
ItemStack
Deprecated.this method uses an ambiguous data byte object- Parameters:
type
- the typeamount
- the amount in the stackdamage
- the damage value of the itemdata
- the data value or null
-
ItemStack
Creates a new item stack derived from the specified stack- Parameters:
stack
- the stack to copy- Throws:
java.lang.IllegalArgumentException
- if the specified stack is null or returns an item meta not created by the item factory
-
-
Method Details
-
getType
Gets the type of this item- Returns:
- Type of the items in this stack
-
setType
Sets the type of this itemNote that in doing so you will reset the MaterialData for this stack
- Parameters:
type
- New type to set the items in this stack to
-
getTypeId
@Deprecated public int getTypeId()Deprecated.Magic valueGets the type id of this item- Returns:
- Type Id of the items in this stack
-
setTypeId
@Deprecated public void setTypeId(int type)Deprecated.Magic valueSets the type id of this itemNote that in doing so you will reset the MaterialData for this stack
- Parameters:
type
- New type id to set the items in this stack to
-
getAmount
public int getAmount()Gets the amount of items in this stack- Returns:
- Amount of items in this stick
-
setAmount
public void setAmount(int amount)Sets the amount of items in this stack- Parameters:
amount
- New amount of items in this stack
-
getData
Gets the MaterialData for this stack of items- Returns:
- MaterialData for this item
-
setData
Sets the MaterialData for this stack of items- Parameters:
data
- New MaterialData for this item
-
setDurability
public void setDurability(short durability)Sets the durability of this item- Parameters:
durability
- Durability of this item
-
getDurability
public short getDurability()Gets the durability of this item- Returns:
- Durability of this item
-
getMaxStackSize
public int 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.
-
toString
public java.lang.String toString()- Overrides:
toString
in classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)- Overrides:
equals
in classjava.lang.Object
-
isSimilar
This method is the same as equals, but does not consider stack size (amount).- Parameters:
stack
- the item stack to compare to- Returns:
- true if the two stacks are equal, ignoring the amount
-
clone
- Overrides:
clone
in classjava.lang.Object
-
hashCode
public final int hashCode()- Overrides:
hashCode
in classjava.lang.Object
-
containsEnchantment
Checks if this ItemStack contains the givenEnchantment
- Parameters:
ench
- Enchantment to test- Returns:
- True if this has the given enchantment
-
getEnchantmentLevel
Gets the level of the specified enchantment on this item stack- Parameters:
ench
- Enchantment to check- Returns:
- Level of the enchantment, or 0
-
getEnchantments
Gets a map containing all enchantments and their levels on this item.- Returns:
- Map of enchantments.
-
addEnchantments
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:
enchantments
- Enchantments to add- Throws:
java.lang.IllegalArgumentException
- if the specified enchantments is nulljava.lang.IllegalArgumentException
- if any specific enchantment or level is null. Warning: Some enchantments may be added before this exception is thrown.
-
addEnchantment
Adds the specifiedEnchantment
to this item stack.If this item stack already contained the given enchantment (at any level), it will be replaced.
- Parameters:
ench
- Enchantment to addlevel
- Level of the enchantment- Throws:
java.lang.IllegalArgumentException
- if enchantment null, or enchantment is not applicable
-
addUnsafeEnchantments
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:
enchantments
- Enchantments to add
-
addUnsafeEnchantment
Adds the specifiedEnchantment
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:
ench
- Enchantment to addlevel
- Level of the enchantment
-
removeEnchantment
Removes the specifiedEnchantment
if it exists on this ItemStack- Parameters:
ench
- Enchantment to remove- Returns:
- Previous level, or 0
-
serialize
public java.util.Map<java.lang.String,java.lang.Object> serialize()Description copied from interface:ConfigurationSerializable
Creates a Map representation of this class.This class must provide a method to restore this class, as defined in the
ConfigurationSerializable
interface javadocs.- Specified by:
serialize
in interfaceConfigurationSerializable
- Returns:
- Map containing the current state of this class
-
deserialize
Required method for configuration serialization- Parameters:
args
- map to deserialize- Returns:
- deserialized item stack
- See Also:
ConfigurationSerializable
-
getItemMeta
Get a copy of this ItemStack'sItemMeta
.- Returns:
- a copy of the current ItemStack's ItemData
-
hasItemMeta
public boolean hasItemMeta()Checks to see if any meta data has been defined.- Returns:
- Returns true if some meta data has been set for this item
-
setItemMeta
Set the ItemMeta of this ItemStack.- Parameters:
itemMeta
- new ItemMeta, or null to indicate meta data be cleared.- Returns:
- True if successfully applied ItemMeta, see
ItemFactory.isApplicable(ItemMeta, ItemStack)
- Throws:
java.lang.IllegalArgumentException
- if the item meta was not created by theItemFactory
-