Class ItemStack

java.lang.Object
net.minestom.server.item.ItemStack
All Implemented Interfaces:
DataContainer

public class ItemStack
extends java.lang.Object
implements DataContainer
Represents an item in an inventory (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 Details

  • Method Details

    • getAirItem

      @NotNull public static ItemStack getAirItem()
      Gets a new ItemStack with the material sets to Material.AIR.

      Used when you require a "null item".

      Returns:
      an air item
    • getDefaultStackingRule

      @NotNull public static StackingRule getDefaultStackingRule()
      Gets the default StackingRule for newly created ItemStack.
      Returns:
      the default stacking rule
    • setDefaultStackingRule

      public static void setDefaultStackingRule​(@NotNull StackingRule defaultStackingRule)
      Changes the default stacking rule for created item stack.
      Parameters:
      defaultStackingRule - the default item stack
      Throws:
      java.lang.NullPointerException - if defaultStackingRule is null
    • fromNBT

      @NotNull public static ItemStack fromNBT​(@NotNull org.jglrxavpok.hephaistos.nbt.NBTCompound nbt)
      Loads an ItemStack from nbt.
      Parameters:
      nbt - the nbt compound containing the item
      Returns:
      the parsed item stack
    • isAir

      public boolean isAir()
      Gets if the item material is Material.AIR.
      Returns:
      true if the material is air, false otherwise
    • isSimilar

      public boolean isSimilar​(@NotNull ItemStack itemStack)
      Gets if two items are similar. It does not take getAmount() and getStackingRule() 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

      @Nullable public ItemMeta getItemMeta()
      Gets the special meta object for this item.

      Can be null if not any.

      Returns:
      the item meta
    • setItemMeta

      public void setItemMeta​(@Nullable ItemMeta itemMeta)
      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

      @Nullable public ColoredText getDisplayName()
      Gets the item display name.
      Returns:
      the item display name, can be null if not present
    • setDisplayName

      public void setDisplayName​(@Nullable ColoredText displayName)
      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

      @Nullable public java.util.ArrayList<ColoredText> getLore()
      Gets the item lore.
      Returns:
      a modifiable list containing the item lore, can be null if not present
    • setLore

      public void setLore​(@Nullable java.util.ArrayList<ColoredText> lore)
      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

      @NotNull public java.util.Map<Enchantment,​java.lang.Short> getEnchantmentMap()
      Gets the item enchantment map.
      Returns:
      an unmodifiable map containing the item enchantments
    • setEnchantment

      public void setEnchantment​(@NotNull Enchantment enchantment, short level)
      Sets an enchantment level.
      Parameters:
      enchantment - the enchantment type
      level - the enchantment level
    • removeEnchantment

      public void removeEnchantment​(@NotNull Enchantment enchantment)
      Removes an enchantment.
      Parameters:
      enchantment - the enchantment type
    • getEnchantmentLevel

      public int getEnchantmentLevel​(@NotNull Enchantment enchantment)
      Gets an enchantment level.
      Parameters:
      enchantment - the enchantment type
      Returns:
      the stored enchantment level, 0 if not present
    • getAttributes

      @NotNull public java.util.List<ItemAttribute> getAttributes()
      Gets the item attributes.
      Returns:
      an unmodifiable List containing the item attributes
    • getAttribute

      public ItemAttribute getAttribute​(@NotNull java.lang.String internalName)
      Gets the ItemAttribute 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

      public void addAttribute​(@NotNull ItemAttribute itemAttribute)
      Adds an attribute to the item.
      Parameters:
      itemAttribute - the attribute to add
    • removeAttribute

      public void removeAttribute​(@NotNull ItemAttribute itemAttribute)
      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

      public void addItemFlags​(@NotNull ItemFlag... flags)
      Adds flags to the item.
      Parameters:
      flags - the flags to add
    • removeItemFlags

      public void removeItemFlags​(@NotNull ItemFlag... flags)
      Removes flags from the item.
      Parameters:
      flags - the flags to remove
    • getItemFlags

      @NotNull public java.util.Set<ItemFlag> getItemFlags()
      Gets the item flags.
      Returns:
      an unmodifiable Set containing the item flags
    • hasItemFlag

      public boolean hasItemFlag​(@NotNull ItemFlag flag)
      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

      @NotNull public Material getMaterial()
      Gets the item Material.
      Returns:
      the item material
    • setMaterial

      public void setMaterial​(@NotNull Material material)
      Changes the item Material.
      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

      @NotNull public ItemStack copy()
      Copies this item stack.
      Returns:
      a cloned item stack
    • getData

      @Nullable public Data getData()
      Description copied from interface: DataContainer
      Gets the Data 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 interface DataContainer
      Returns:
      the Data of this container, can be null
    • setData

      public void setData​(@Nullable Data data)
      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 interface DataContainer
      Parameters:
      data - the new Data of this container, null to remove it
    • getNBTConsumer

      @Nullable public NBTConsumer getNBTConsumer()
      Gets the NBTConsumer called when the item is serialized into a packet.
      Returns:
      the item nbt consumer, null if not any
    • setNBTConsumer

      public void setNBTConsumer​(@Nullable NBTConsumer nbtConsumer)
      Changes the item NBTConsumer.
      Parameters:
      nbtConsumer - the new item nbt consumer, can be null
    • getStackingRule

      @NotNull public StackingRule getStackingRule()
      Gets the item StackingRule.
      Returns:
      the item stacking rule
    • setStackingRule

      public void setStackingRule​(@NotNull StackingRule stackingRule)
      Changes the StackingRule of the item.
      Parameters:
      stackingRule - the new item stacking rule
      Throws:
      java.lang.NullPointerException - if stackingRule is null
    • consume

      @Nullable public ItemStack consume​(int amount)
      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 a NBTCompound containing the data of this item.

      WARNING: modifying the returned nbt will not affect the item.

      Returns:
      this item nbt
    • getCustomDisplay

      public ItemDisplay getCustomDisplay​(Player player)
      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 for player, null to use the normal item display name & lore
    • onRightClick

      public void onRightClick​(@NotNull Player player, @NotNull Player.Hand hand)
      Called when the player right clicks with this item.
      Parameters:
      player - the player who used the item
      hand - the hand used
    • onLeftClick

      public void onLeftClick​(@NotNull Player player, @NotNull Player.Hand hand)
      Called when the player left clicks with this item.
      Parameters:
      player - the player who used the item
      hand - 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 item
      hand - the hand used
      position - the position of the interacted block
      blockFace - 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 item
      clickType - the click type
      slot - the slot clicked
      playerInventory - true if the click is in the player inventory