Class ItemStack
- All Implemented Interfaces:
DataContainer
public class ItemStack extends java.lang.Object implements DataContainer
PlayerInventory
, Inventory
) or on the ground (ItemEntity
).
An item stack cannot be null, you can however use getAirItem()
instead.
WARNING: all setters will not update the item automatically, it will need to be refreshed manually.
Here a non-exhaustive list of what you can do to update the item:
PlayerInventory.refreshSlot(short)
, Inventory.refreshSlot(short)
or a raw SetSlotPacket
.
-
Constructor Summary
-
Method Summary
Modifier and Type Method Description void
addAttribute(ItemAttribute itemAttribute)
Adds an attribute to the item.void
addItemFlags(ItemFlag... flags)
Adds flags to the item.ItemStack
consume(int amount)
Consumes this item by a specific amount.ItemStack
copy()
Copies this item stack.static ItemStack
fromNBT(org.jglrxavpok.hephaistos.nbt.NBTCompound nbt)
Loads anItemStack
from nbt.static ItemStack
getAirItem()
Gets a newItemStack
with the material sets toMaterial.AIR
.byte
getAmount()
Gets the item amount.ItemAttribute
getAttribute(java.lang.String internalName)
Gets theItemAttribute
with the specified internal name.java.util.List<ItemAttribute>
getAttributes()
Gets the item attributes.ItemDisplay
getCustomDisplay(Player player)
WARNING: not implemented yet.int
getCustomModelData()
Gets the item custom model data.int
getDamage()
Gets the item damage (durability).Data
getData()
Gets theData
of this container.static StackingRule
getDefaultStackingRule()
Gets the defaultStackingRule
for newly createdItemStack
.ColoredText
getDisplayName()
Gets the item display name.int
getEnchantmentLevel(Enchantment enchantment)
Gets an enchantment level.java.util.Map<Enchantment,java.lang.Short>
getEnchantmentMap()
Gets the item enchantment map.int
getHideFlag()
Gets the item hide flag.java.util.Set<ItemFlag>
getItemFlags()
Gets the item flags.ItemMeta
getItemMeta()
Gets the special meta object for this item.java.util.ArrayList<ColoredText>
getLore()
Gets the item lore.Material
getMaterial()
Gets the itemMaterial
.NBTConsumer
getNBTConsumer()
Gets theNBTConsumer
called when the item is serialized into a packet.StackingRule
getStackingRule()
Gets the itemStackingRule
.boolean
hasDisplayName()
Gets if the item has a display name.boolean
hasItemFlag(ItemFlag flag)
Gets if the item has an item flag.boolean
hasLore()
Gets if the item has a lore.boolean
hasNbtTag()
Gets if the item has any nbt tag.boolean
isAir()
Gets if the item material isMaterial.AIR
.boolean
isSimilar(ItemStack itemStack)
Gets if two items are similar.boolean
isUnbreakable()
Gets if the item is unbreakable.void
onInventoryClick(Player player, ClickType clickType, int slot, boolean playerInventory)
Called when the player click on this item on an inventory.void
onLeftClick(Player player, Player.Hand hand)
Called when the player left clicks with this item.void
onRightClick(Player player, Player.Hand hand)
Called when the player right clicks with this item.boolean
onUseOnBlock(Player player, Player.Hand hand, BlockPosition position, Direction blockFace)
Called when the player right clicks with this item on a block.void
removeAttribute(ItemAttribute itemAttribute)
Removes an attribute to the item.void
removeEnchantment(Enchantment enchantment)
Removes an enchantment.void
removeItemFlags(ItemFlag... flags)
Removes flags from the item.void
setAmount(byte amount)
Changes the item amount.void
setCustomModelData(int customModelData)
Changes the item custom model data.void
setDamage(int damage)
Sets the item damage (durability).void
setData(Data data)
Sets the data of this item.static void
setDefaultStackingRule(StackingRule defaultStackingRule)
Changes the default stacking rule for created item stack.void
setDisplayName(ColoredText displayName)
Sets the item display name.void
setEnchantment(Enchantment enchantment, short level)
Sets an enchantment level.void
setHideFlag(int hideFlag)
Changes the item hide flag.void
setItemMeta(ItemMeta itemMeta)
Changes the item meta linked to this item.void
setLore(java.util.ArrayList<ColoredText> lore)
Sets the item lore.void
setMaterial(Material material)
Changes the itemMaterial
.void
setNBTConsumer(NBTConsumer nbtConsumer)
Changes the itemNBTConsumer
.void
setStackingRule(StackingRule stackingRule)
Changes theStackingRule
of the item.void
setUnbreakable(boolean unbreakable)
Makes the item unbreakable.org.jglrxavpok.hephaistos.nbt.NBTCompound
toNBT()
Creates aNBTCompound
containing the data of this item.
-
Constructor Details
-
Method Details
-
getAirItem
- Returns:
- an air item
-
getDefaultStackingRule
Gets the defaultStackingRule
for newly createdItemStack
.- Returns:
- the default stacking rule
-
setDefaultStackingRule
Changes the default stacking rule for created item stack.- Parameters:
defaultStackingRule
- the default item stack- Throws:
java.lang.NullPointerException
- ifdefaultStackingRule
is null
-
fromNBT
Loads anItemStack
from nbt.- Parameters:
nbt
- the nbt compound containing the item- Returns:
- the parsed item stack
-
isAir
public boolean isAir()Gets if the item material isMaterial.AIR
.- Returns:
- true if the material is air, false otherwise
-
isSimilar
Gets if two items are similar. It does not takegetAmount()
andgetStackingRule()
in consideration.- Parameters:
itemStack
- The ItemStack to compare to- Returns:
- true if both items are similar
-
getDamage
public int getDamage()Gets the item damage (durability).- Returns:
- the item damage
-
setDamage
public void setDamage(int damage)Sets the item damage (durability).- Parameters:
damage
- the item damage
-
getAmount
public byte getAmount()Gets the item amount.WARNING: for amount computation it would be better to use
StackingRule.getAmount(ItemStack)
to support all stacking implementation.- Returns:
- the item amount
-
setAmount
public void setAmount(byte amount)Changes the item amount.WARNING: for amount computation it would be better to use
StackingRule.getAmount(ItemStack)
to support all stacking implementation.- Parameters:
amount
- the new item amount
-
getItemMeta
Gets the special meta object for this item.Can be null if not any.
- Returns:
- the item meta
-
setItemMeta
Changes the item meta linked to this item.WARNING: be sure to have nbt data useful for this item, items should automatically get the appropriate item meta.
- Parameters:
itemMeta
- the new item meta
-
getDisplayName
Gets the item display name.- Returns:
- the item display name, can be null if not present
-
setDisplayName
Sets the item display name.- Parameters:
displayName
- the item display name
-
hasDisplayName
public boolean hasDisplayName()Gets if the item has a display name.- Returns:
- the item display name
-
getLore
Gets the item lore.- Returns:
- a modifiable list containing the item lore, can be null if not present
-
setLore
Sets the item lore.- Parameters:
lore
- the item lore, can be null to remove
-
hasLore
public boolean hasLore()Gets if the item has a lore.- Returns:
- true if the item has lore, false otherwise
-
getEnchantmentMap
Gets the item enchantment map.- Returns:
- an unmodifiable map containing the item enchantments
-
setEnchantment
Sets an enchantment level.- Parameters:
enchantment
- the enchantment typelevel
- the enchantment level
-
removeEnchantment
Removes an enchantment.- Parameters:
enchantment
- the enchantment type
-
getEnchantmentLevel
Gets an enchantment level.- Parameters:
enchantment
- the enchantment type- Returns:
- the stored enchantment level, 0 if not present
-
getAttributes
Gets the item attributes.- Returns:
- an unmodifiable
List
containing the item attributes
-
getAttribute
Gets theItemAttribute
with the specified internal name.- Parameters:
internalName
- the internal name of the attribute- Returns:
- the
ItemAttribute
with the internal name, null if not found
-
addAttribute
Adds an attribute to the item.- Parameters:
itemAttribute
- the attribute to add
-
removeAttribute
Removes an attribute to the item.- Parameters:
itemAttribute
- the attribute to remove
-
getHideFlag
public int getHideFlag()Gets the item hide flag.- Returns:
- the item hide flag
-
setHideFlag
public void setHideFlag(int hideFlag)Changes the item hide flag. This is the integer sent when updating the item hide flag.- Parameters:
hideFlag
- the new item hide flag
-
getCustomModelData
public int getCustomModelData()Gets the item custom model data.- Returns:
- the item custom model data
-
setCustomModelData
public void setCustomModelData(int customModelData)Changes the item custom model data.- Parameters:
customModelData
- the new item custom data model
-
addItemFlags
Adds flags to the item.- Parameters:
flags
- the flags to add
-
removeItemFlags
Removes flags from the item.- Parameters:
flags
- the flags to remove
-
getItemFlags
Gets the item flags.- Returns:
- an unmodifiable
Set
containing the item flags
-
hasItemFlag
Gets if the item has an item flag.- Parameters:
flag
- the item flag- Returns:
- true if the item has the flag
flag
, false otherwise
-
isUnbreakable
public boolean isUnbreakable()Gets if the item is unbreakable.- Returns:
- true if the item is unbreakable, false otherwise
-
setUnbreakable
public void setUnbreakable(boolean unbreakable)Makes the item unbreakable.- Parameters:
unbreakable
- true to make the item unbreakable, false otherwise
-
getMaterial
Gets the itemMaterial
.- Returns:
- the item material
-
setMaterial
Changes the itemMaterial
.- Parameters:
material
- the new material
-
hasNbtTag
public boolean hasNbtTag()Gets if the item has any nbt tag.- Returns:
- true if the item has nbt tag, false otherwise
-
copy
Copies this item stack.- Returns:
- a cloned item stack
-
getData
Description copied from interface:DataContainer
Gets theData
of this container.A
DataContainer
data is always optional, meaning that this will be null if no data has been defined.- Specified by:
getData
in interfaceDataContainer
- Returns:
- the
Data
of this container, can be null
-
setData
Sets the data of this item.It is recommended to use
NbtDataImpl
if you want the data to be passed to the client.- Specified by:
setData
in interfaceDataContainer
- Parameters:
data
- the newData
of this container, null to remove it
-
getNBTConsumer
Gets theNBTConsumer
called when the item is serialized into a packet.- Returns:
- the item nbt consumer, null if not any
-
setNBTConsumer
Changes the itemNBTConsumer
.- Parameters:
nbtConsumer
- the new item nbt consumer, can be null
-
getStackingRule
Gets the itemStackingRule
.- Returns:
- the item stacking rule
-
setStackingRule
Changes theStackingRule
of the item.- Parameters:
stackingRule
- the new item stacking rule- Throws:
java.lang.NullPointerException
- ifstackingRule
is null
-
consume
Consumes this item by a specific amount.Will return null if the amount's amount isn't enough.
- Parameters:
amount
- the quantity to consume- Returns:
- the new item with the updated amount, null if the item cannot be consumed by this much
-
toNBT
@NotNull public org.jglrxavpok.hephaistos.nbt.NBTCompound toNBT()Creates aNBTCompound
containing the data of this item.WARNING: modifying the returned nbt will not affect the item.
- Returns:
- this item nbt
-
getCustomDisplay
WARNING: not implemented yet.This is be called each time an item is serialized to be send to a player, can be used to customize the display of the item based on player data.
- Parameters:
player
- the player- Returns:
- the custom
ItemDisplay
forplayer
, null to use the normal item display name & lore
-
onRightClick
Called when the player right clicks with this item.- Parameters:
player
- the player who used the itemhand
- the hand used
-
onLeftClick
Called when the player left clicks with this item.- Parameters:
player
- the player who used the itemhand
- the hand used
-
onUseOnBlock
public boolean onUseOnBlock(@NotNull Player player, @NotNull Player.Hand hand, @NotNull BlockPosition position, @NotNull Direction blockFace)Called when the player right clicks with this item on a block.- Parameters:
player
- the player who used the itemhand
- the hand usedposition
- the position of the interacted blockblockFace
- the block face- Returns:
- true if it prevents normal item use (placing blocks for instance)
-
onInventoryClick
public void onInventoryClick(@NotNull Player player, @NotNull ClickType clickType, int slot, boolean playerInventory)Called when the player click on this item on an inventory.Executed before any events.
- Parameters:
player
- the player who clicked on the itemclickType
- the click typeslot
- the slot clickedplayerInventory
- true if the click is in the player inventory
-