Class ItemStack

    • Constructor Detail

      • ItemStack

        protected ItemStack()
      • ItemStack

        public ItemStack​(@NotNull
                         Material type)
        Defaults stack size to 1, with no extra data
        Parameters:
        type - item material
      • ItemStack

        public ItemStack​(@NotNull
                         Material type,
                         int amount)
        An item stack with no extra data
        Parameters:
        type - item material
        amount - stack size
      • ItemStack

        public ItemStack​(@NotNull
                         Material type,
                         int amount,
                         short damage)
        Deprecated.
        An item stack with the specified damage / durability
        Parameters:
        type - item material
        amount - stack size
        damage - durability / damage
      • ItemStack

        @Deprecated
        public ItemStack​(@NotNull
                         Material type,
                         int amount,
                         short damage,
                         @Nullable
                         Byte data)
        Deprecated.
        this method uses an ambiguous data byte object
        Parameters:
        type - the type
        amount - the amount in the stack
        damage - the damage value of the item
        data - the data value or null
      • ItemStack

        public ItemStack​(@NotNull
                         ItemStack stack)
                  throws IllegalArgumentException
        Creates a new item stack derived from the specified stack
        Parameters:
        stack - the stack to copy
        Throws:
        IllegalArgumentException - if the specified stack is null or returns an item meta not created by the item factory
    • Method Detail

      • getType

        @NotNull
        public Material getType()
        Gets the type of this item
        Returns:
        Type of the items in this stack
      • setType

        public void setType​(@NotNull
                            Material type)
        Sets the type of this item

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

        Parameters:
        type - New type 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 stack
      • setAmount

        public void setAmount​(int amount)
        Sets the amount of items in this stack
        Parameters:
        amount - New amount of items in this stack
      • getData

        @Nullable
        public MaterialData getData()
        Gets the MaterialData for this stack of items
        Returns:
        MaterialData for this item
      • setData

        public void setData​(@Nullable
                            MaterialData data)
        Sets the MaterialData for this stack of items
        Parameters:
        data - New MaterialData for this item
      • setDurability

        @Deprecated
        public void setDurability​(short durability)
        Deprecated.
        durability is now part of ItemMeta. To avoid confusion and misuse, getItemMeta(), setItemMeta(ItemMeta) and Damageable.setDamage(int) should be used instead. This is because any call to this method will be overwritten by subsequent setting of ItemMeta which was created before this call.
        Sets the durability of this item
        Parameters:
        durability - Durability of this item
      • getDurability

        @Deprecated
        public short getDurability()
        Deprecated.
        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.
      • isSimilar

        public boolean isSimilar​(@Nullable
                                 ItemStack stack)
        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
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object
      • containsEnchantment

        public boolean containsEnchantment​(@NotNull
                                           Enchantment ench)
        Checks if this ItemStack contains the given Enchantment
        Parameters:
        ench - Enchantment to test
        Returns:
        True if this has the given enchantment
      • getEnchantmentLevel

        public int getEnchantmentLevel​(@NotNull
                                       Enchantment ench)
        Gets the level of the specified enchantment on this item stack
        Parameters:
        ench - Enchantment to check
        Returns:
        Level of the enchantment, or 0
      • getEnchantments

        @NotNull
        public Map<Enchantment,​Integer> getEnchantments()
        Gets a map containing all enchantments and their levels on this item.
        Returns:
        Map of enchantments.
      • addEnchantment

        public void addEnchantment​(@NotNull
                                   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:
        ench - Enchantment to add
        level - Level of the enchantment
        Throws:
        IllegalArgumentException - if enchantment null, or enchantment is not applicable
      • addUnsafeEnchantment

        public void addUnsafeEnchantment​(@NotNull
                                         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:
        ench - Enchantment to add
        level - Level of the enchantment
      • removeEnchantment

        public int removeEnchantment​(@NotNull
                                     Enchantment ench)
        Removes the specified Enchantment if it exists on this ItemStack
        Parameters:
        ench - Enchantment to remove
        Returns:
        Previous level, or 0
      • deserialize

        @NotNull
        public static ItemStack deserialize​(@NotNull
                                            Map<String,​Object> args)
        Required method for configuration serialization
        Parameters:
        args - map to deserialize
        Returns:
        deserialized item stack
        See Also:
        ConfigurationSerializable
      • getItemMeta

        @Nullable
        public ItemMeta getItemMeta()
        Get a copy of this ItemStack's ItemMeta.
        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