Class ItemStack
- All Implemented Interfaces:
PersistentDataViewHolder
,Cloneable
,HoverEventSource<HoverEvent.ShowItem>
,Translatable
,ConfigurationSerializable
,Translatable
IMPORTANT: An ItemStack is only designed to contain items. Do not
use this class to encapsulate Materials for which Material.isItem()
returns false.
-
Constructor Summary
ModifierConstructorDescriptionprotected
Defaults stack size to 1, with no extra data.An item stack with no extra data.Deprecated.Deprecated, for removal: This API element is subject to removal in a future version.this method uses an ambiguous data byte objectCreates a new item stack derived from the specified stack -
Method Summary
Modifier and TypeMethodDescriptionadd()
Adds 1 to this itemstack.add
(int qty) Adds quantity to this itemstack.boolean
addAttributeModifier
(@NotNull Attribute attribute, @NotNull AttributeModifier modifier) Add an Attribute and it's Modifier.boolean
addEnchant
(@NotNull Enchantment ench, int level, boolean ignoreLevelRestriction) Adds the specified enchantment to this item meta.void
addEnchantment
(@NotNull Enchantment ench, int level) Adds the specifiedEnchantment
to this item stack.void
addEnchantments
(@NotNull Map<Enchantment, Integer> enchantments) Adds the specified enchantments to this item stack.void
addItemFlags
(@NotNull ItemFlag... itemFlags) Set itemflags which should be ignored when rendering a ItemStack in the Client.void
addUnsafeEnchantment
(@NotNull Enchantment ench, int level) Adds the specifiedEnchantment
to this item stack.void
addUnsafeEnchantments
(@NotNull Map<Enchantment, Integer> enchantments) Adds the specified enchantments to this item stack in an unsafe manner.Creates a hover event with value derived from this object.asOne()
Clones the itemstack and returns it a single quantity.asQuantity
(int qty) Clones the itemstack and returns it as the specified quantityboolean
Checks if this itemstack can repair another.clone()
computeTooltipLines
(TooltipContext tooltipContext, Player player) Computes the tooltip lines for this stack.boolean
Checks if this ItemStack contains the givenEnchantment
boolean
damage()
Damages this item by 1 durabilityboolean
damage
(int amount) Damages this item's durability by amountboolean
damage
(int amount, boolean ignoreUnbreaking) Damages this item's durability by amountdamage
(int amount, LivingEntity livingEntity) Damages this itemstack by the specified amount.deserialize
(@NotNull Map<String, Object> args) Required method for configuration serializationdeserializeBytes
(@org.jetbrains.annotations.NotNull byte[] bytes) Deserializes this itemstack from raw NBT bytes.deserializeItemsFromBytes
(byte @NotNull [] bytes) Deserializes this itemstack from raw NBT bytes.Get the formatted display name of theItemStack
.<M extends ItemMeta>
booleanEdits theItemMeta
of this stack if the meta is of the specified type.boolean
Edits theItemMeta
of this stack.empty()
Returns an empty item stack, consists of an air material and a stack size of 0.enchantWithLevels
(@org.jetbrains.annotations.Range(from=1L, to=30L) int levels, boolean allowTreasure, Random random) Randomly enchants a copy of thisItemStack
using the given experience levels.enchantWithLevels
(@org.jetbrains.annotations.Range(from=1L, to=30L) int levels, RegistryKeySet<@NotNull Enchantment> keySet, Random random) Randomly enchants a copy of thisItemStack
using the given experience levels.Minecraft updates are converting simple item stacks into more complex NBT oriented Item Stacks.boolean
int
Gets the amount of items in this stackReturn an immutable copy of all Attributes and their modifiers in this ItemMeta.
Returns null if none exist.getAttributeModifiers
(@NotNull Attribute attribute) Return an immutable copy of allAttributeModifier
s for a givenAttribute
Return an immutable copy of allAttribute
s and theirAttributeModifier
s for a givenEquipmentSlot
.
AnyAttributeModifier
that does have have a givenEquipmentSlot
will be returned.getBlockData
(@NotNull Material material) Returns the currently attached block data for this item or creates a new one if one doesn't exist.int
Gets the custom model data that is set.int
Gets the damagegetData()
Deprecated, for removal: This API element is subject to removal in a future version.cast toBlockDataMeta
and useBlockDataMeta.getBlockData(Material)
Gets the display name that is set.short
Deprecated.int
Checks for the level of the specified enchantment.int
Gets the level of the specified enchantment on this item stackGets a map containing all enchantments and their levels on this item.Returns a copy the enchantments in this ItemMeta.Deprecated.ItemStack
implementsTranslatable
; use that andComponent.translatable(net.kyori.adventure.translation.Translatable)
instead.Get current set itemFlags.Get a copy of this ItemStack'sItemMeta
.Gets the localized display name that is set.getLore()
Deprecated.in favor oflore()
int
Deprecated, for removal: This API element is subject to removal in a future version.usegetMaxItemUseDuration(org.bukkit.entity.LivingEntity)
; crossbows, later possibly more items require an entity parameterint
getMaxItemUseDuration
(LivingEntity entity) int
Get the maximum stack size for this item.Returns a custom tag container view capable of viewing tags on the object.Deprecated, for removal: This API element is subject to removal in a future version.int
Gets the repair penaltyDeprecated, for removal: This API element is subject to removal in a future version.getType()
Gets the type of this itemboolean
Checks for the existence of any AttributeModifiers.boolean
Returns whether the item has block data currently attached to it.boolean
Checks if the specified enchantment conflicts with any enchantments in this ItemMeta.boolean
Checks for existence of custom model data.boolean
Checks to see if this item has damageboolean
Checks for existence of a display name.boolean
hasEnchant
(@NotNull Enchantment ench) Checks for existence of the specified enchantment.boolean
Checks for the existence of any enchantments.int
hashCode()
boolean
hasItemFlag
(@NotNull ItemFlag flag) Check if the specified flag is present on this item.boolean
Checks to see if any meta data has been defined.boolean
Checks for existence of a localized name.boolean
hasLore()
Checks for existence of lore.boolean
Checks to see if this has a repair penaltyboolean
isEmpty()
Returns whether this item stack is empty and contains no item.boolean
isRepairableBy
(@NotNull ItemStack repairMaterial) Checks if an itemstack can repair this itemstack.boolean
This method is the same as equals, but does not consider stack size (amount).boolean
Return if the unbreakable tag is true.lore()
If the item has lore, returns it, else it will return nullvoid
Sets the lore for this item.Creates an itemstack with the specified item type and a count of 1.Creates an itemstack with the specified item type and count.boolean
removeAttributeModifier
(@NotNull Attribute attribute) Remove allAttributeModifier
s associated with the givenAttribute
.boolean
removeAttributeModifier
(@NotNull Attribute attribute, @NotNull AttributeModifier modifier) Remove a specificAttribute
andAttributeModifier
.boolean
Remove allAttribute
s andAttributeModifier
s for a givenEquipmentSlot
.
If the givenEquipmentSlot
is null, this will remove allAttributeModifier
s that do not have an EquipmentSlot set.boolean
removeEnchant
(@NotNull Enchantment ench) Removes the specified enchantment from this item meta.int
Removes the specifiedEnchantment
if it exists on this ItemStackvoid
Removes all enchantments on this ItemStack.void
removeItemFlags
(@NotNull ItemFlag... itemFlags) Remove specific set of itemFlags.void
repair()
Repairs this item by 1 durabilityvoid
repair
(int amount) Repairs this item's durability by amountCreates a Map representation of this class.@org.jetbrains.annotations.NotNull byte[]
Serializes this itemstack to raw bytes in NBT.static byte @NotNull []
Serializes a collection of items to raw bytes in NBT.static byte @NotNull []
serializeItemsAsBytes
(@Nullable ItemStack @NotNull [] items) Serializes a collection of items to raw bytes in NBT.void
setAmount
(int amount) Sets the amount of items in this stackvoid
setAttributeModifiers
(@Nullable Multimap<Attribute, AttributeModifier> attributeModifiers) Set allAttribute
s and theirAttributeModifier
s.void
setBlockData
(@NotNull BlockData blockData) Attaches a copy of the passed block data to the item.void
Sets the custom model data.void
setDamage
(int damage) Sets the damagevoid
setData
(@Nullable MaterialData data) Deprecated, for removal: This API element is subject to removal in a future version.void
setDisplayName
(@Nullable String name) Sets the display name.void
setDurability
(short durability) Deprecated.durability is now part of ItemMeta.boolean
setItemMeta
(@Nullable ItemMeta itemMeta) Set the ItemMeta of this ItemStack.void
setLocalizedName
(@Nullable String name) Sets the localized name.void
Deprecated.in favour oflore(java.util.List)
void
setRepairCost
(int cost) Sets the repair penaltyvoid
Deprecated.Setting the material type of ItemStacks is no longer supported.void
setUnbreakable
(boolean unbreakable) Sets the unbreakable tag.subtract()
Subtracts 1 to this itemstack.subtract
(int qty) Subtracts quantity to this itemstack.toString()
Gets the translation key.Creates a new ItemStack with the specified Material type, where the item count and item meta is preserved.Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface net.kyori.adventure.text.event.HoverEventSource
asHoverEvent
-
Constructor Details
-
ItemStack
protected ItemStack() -
ItemStack
Defaults stack size to 1, with no extra data.IMPORTANT: An ItemStack is only designed to contain items. Do not use this class to encapsulate Materials for which
Material.isItem()
returns false.- Parameters:
type
- item material- See Also:
- API Note:
- use
of(Material)
-
ItemStack
An item stack with no extra data.IMPORTANT: An ItemStack is only designed to contain items. Do not use this class to encapsulate Materials for which
Material.isItem()
returns false.- Parameters:
type
- item materialamount
- stack size- See Also:
- API Note:
- Use
of(Material, int)
-
ItemStack
Deprecated.An item stack with the specified damage / durability- Parameters:
type
- item materialamount
- stack sizedamage
- durability / damage
-
ItemStack
@Deprecated(forRemoval=true, since="1.13") public ItemStack(@NotNull @NotNull Material type, int amount, short damage, @Nullable @Nullable Byte data) Deprecated, for removal: This API element is subject to removal in a future version.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
@Obsolete(since="1.21") public ItemStack(@NotNull @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- See Also:
- API Note:
- Use
clone()
-
-
Method Details
-
of
@Contract(value="_ -> new", pure=true) @NotNull public static @NotNull ItemStack of(@NotNull @NotNull Material type) Creates an itemstack with the specified item type and a count of 1.- Parameters:
type
- the item type to use- Returns:
- a new itemstack
- Throws:
IllegalArgumentException
- if the Material provided is not an item (Material.isItem()
)
-
of
@Contract(value="_, _ -> new", pure=true) @NotNull public static @NotNull ItemStack of(@NotNull @NotNull Material type, int amount) Creates an itemstack with the specified item type and count.- Parameters:
type
- the item type to useamount
- the count of items in the stack- Returns:
- a new itemstack
- Throws:
IllegalArgumentException
- if the Material provided is not an item (Material.isItem()
)IllegalArgumentException
- if the amount is less than 1
-
getPersistentDataContainer
Description copied from interface:PersistentDataViewHolder
Returns a custom tag container view capable of viewing tags on the object.Note that the tags stored on this container are all stored under their own custom namespace therefore modifying default tags using this
PersistentDataViewHolder
is impossible.- Specified by:
getPersistentDataContainer
in interfacePersistentDataViewHolder
- Returns:
- the persistent data container view
-
getType
Gets the type of this item- Returns:
- Type of the items in this stack
-
setType
Deprecated.Setting the material type of ItemStacks is no longer supported.This method is deprecated due to potential illegal behavior that may occur during the context of which this ItemStack is being used, allowing for certain item validation to be bypassed. It is recommended to instead create a new ItemStack object with the desired Material type, and if possible, set it in the appropriate context. Using this method in ItemStacks passed in events will result in undefined behavior.
Sets the type of this itemNote that in doing so you will reset the MaterialData for this stack.
IMPORTANT: An ItemStack is only designed to contain items. Do not use this class to encapsulate Materials for which
Material.isItem()
returns false.- Parameters:
type
- New type to set the items in this stack to- See Also:
-
withType
@NotNull @Contract(value="_ -> new", pure=true) public @NotNull ItemStack withType(@NotNull @NotNull Material type) Creates a new ItemStack with the specified Material type, where the item count and item meta is preserved.- Parameters:
type
- The Material type of the new ItemStack.- Returns:
- A new ItemStack instance with the specified Material type.
-
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
Deprecated, for removal: This API element is subject to removal in a future version.cast toBlockDataMeta
and useBlockDataMeta.getBlockData(Material)
Gets the MaterialData for this stack of items- Returns:
- MaterialData for this item
-
setData
@Deprecated(forRemoval=true, since="1.13") public void setData(@Nullable @Nullable MaterialData data) Deprecated, for removal: This API element is subject to removal in a future version.Sets the MaterialData for this stack of items- Parameters:
data
- New MaterialData for this item
-
setDurability
Deprecated.durability is now part of ItemMeta. To avoid confusion and misuse,getItemMeta()
,setItemMeta(ItemMeta)
andDamageable.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.Gets the durability of this item- Returns:
- Durability of this item
-
getMaxStackSize
public int getMaxStackSize()Get the maximum stack size for this item. If this item has a max stack size component (ItemMeta.hasMaxStackSize()
), the value of that component will be returned. Otherwise, this item's Material'sdefault maximum stack size
will be returned instead.- Returns:
- The maximum you can stack this item to.
-
toString
-
equals
-
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
-
hashCode
public int hashCode() -
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:
IllegalArgumentException
- if the specified enchantments is nullIllegalArgumentException
- 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:
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
-
removeEnchantments
public void removeEnchantments()Removes all enchantments on this ItemStack. -
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:
-
editMeta
Edits theItemMeta
of this stack.The
Consumer
must only interact with this stack'sItemMeta
through the providedItemMeta
instance. Calling this method or any other meta-related method of theItemStack
class (such asgetItemMeta()
,addItemFlags(ItemFlag...)
,lore()
, etc.) from inside the consumer is disallowed and will produce undefined results or exceptions.- Parameters:
consumer
- the meta consumer- Returns:
true
if the edit was successful,false
otherwise
-
editMeta
public <M extends ItemMeta> boolean editMeta(@NotNull @NotNull Class<M> metaClass, @NotNull Consumer<@NotNull ? super M> consumer) Edits theItemMeta
of this stack if the meta is of the specified type.The
Consumer
must only interact with this stack'sItemMeta
through the providedItemMeta
instance. Calling this method or any other meta-related method of theItemStack
class (such asgetItemMeta()
,addItemFlags(ItemFlag...)
,lore()
, etc.) from inside the consumer is disallowed and will produce undefined results or exceptions.- Type Parameters:
M
- the meta type- Parameters:
metaClass
- the type of meta to editconsumer
- the meta consumer- Returns:
true
if the edit was successful,false
otherwise
-
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:
IllegalArgumentException
- if the item meta was not created by theItemFactory
-
getTranslationKey
Deprecated, for removal: This API element is subject to removal in a future version.Description copied from interface:Translatable
Get the translation key, suitable for use in a translation component.- Specified by:
getTranslationKey
in interfaceTranslatable
- Returns:
- the translation key
-
enchantWithLevels
@NotNull public @NotNull ItemStack enchantWithLevels(@org.jetbrains.annotations.Range(from=1L, to=30L) int levels, boolean allowTreasure, @NotNull Random random) Randomly enchants a copy of thisItemStack
using the given experience levels.If this ItemStack is already enchanted, the existing enchants will be removed before enchanting.
Levels must be in range
[1, 30]
.- Parameters:
levels
- levels to use for enchantingallowTreasure
- whether to allow enchantments whereEnchantment.isTreasure()
returns truerandom
-Random
instance to use for enchanting- Returns:
- enchanted copy of the provided ItemStack
- Throws:
IllegalArgumentException
- on bad arguments
-
enchantWithLevels
@NotNull public @NotNull ItemStack enchantWithLevels(@org.jetbrains.annotations.Range(from=1L, to=30L) int levels, @NotNull RegistryKeySet<@NotNull Enchantment> keySet, @NotNull Random random) Randomly enchants a copy of thisItemStack
using the given experience levels.If the provided ItemStack is already enchanted, the existing enchants will be removed before enchanting.
Levels must be in range
[1, 30]
.- Parameters:
levels
- levels to use for enchantingkeySet
- registry key set defining the set of possible enchantments, e.g.EnchantmentTagKeys.IN_ENCHANTING_TABLE
.random
-Random
instance to use for enchanting- Returns:
- enchanted copy of the provided ItemStack
- Throws:
IllegalArgumentException
- on bad arguments
-
asHoverEvent
@NotNull public HoverEvent<HoverEvent.ShowItem> asHoverEvent(@NotNull UnaryOperator<HoverEvent.ShowItem> op) Description copied from interface:net.kyori.adventure.text.event.HoverEventSource
Creates a hover event with value derived from this object.The event value will be passed through the provided callback to allow transforming the original value of the event.
- Specified by:
asHoverEvent
in interfaceHoverEventSource<HoverEvent.ShowItem>
- Parameters:
op
- transformation on value- Returns:
- a hover event
-
displayName
Get the formatted display name of theItemStack
.- Returns:
- display name of the
ItemStack
-
ensureServerConversions
Minecraft updates are converting simple item stacks into more complex NBT oriented Item Stacks. Use this method to ensure any desired data conversions are processed. The input itemstack will not be the same as the returned itemstack.- Returns:
- A potentially Data Converted ItemStack
-
deserializeBytes
@NotNull public static @NotNull ItemStack deserializeBytes(@NotNull @org.jetbrains.annotations.NotNull byte[] bytes) Deserializes this itemstack from raw NBT bytes. NBT is safer for data migrations as it will use the built in data converter instead of bukkits dangerous serialization system. This expects that the DataVersion was stored on the root of the Compound, as saved from theserializeAsBytes()
API returned.- Parameters:
bytes
- bytes representing an item in NBT- Returns:
- ItemStack migrated to this version of Minecraft if needed.
-
serializeAsBytes
Serializes this itemstack to raw bytes in NBT. NBT is safer for data migrations as it will use the built in data converter instead of bukkits dangerous serialization system.- Returns:
- bytes representing this item in NBT.
-
serializeItemsAsBytes
Serializes a collection of items to raw bytes in NBT. Serializes null items asempty()
.If you need a string representation to put into a file, you can for example use
Base64
encoding.- Parameters:
items
- items to serialize- Returns:
- bytes representing the items in NBT
- See Also:
-
serializeItemsAsBytes
public static byte @NotNull [] serializeItemsAsBytes(@Nullable @Nullable ItemStack @NotNull [] items) Serializes a collection of items to raw bytes in NBT. Serializes null items asempty()
.If you need a string representation to put into a file, you can for example use
Base64
encoding.- Parameters:
items
- items to serialize- Returns:
- bytes representing the items in NBT
- See Also:
-
deserializeItemsFromBytes
@NotNull public static @NotNull ItemStack @NotNull [] deserializeItemsFromBytes(byte @NotNull [] bytes) Deserializes this itemstack from raw NBT bytes.If you need a string representation to put into a file, you can for example use
Base64
encoding.- Parameters:
bytes
- bytes representing an item in NBT- Returns:
- ItemStack array migrated to this version of Minecraft if needed
- See Also:
-
getI18NDisplayName
Deprecated.ItemStack
implementsTranslatable
; use that andComponent.translatable(net.kyori.adventure.translation.Translatable)
instead.Gets the Display name as seen in the Client. Currently the server only supports the English language. To override this, You must replace the language file embedded in the server jar.- Returns:
- Display name of Item
-
getMaxItemUseDuration
Deprecated, for removal: This API element is subject to removal in a future version.usegetMaxItemUseDuration(org.bukkit.entity.LivingEntity)
; crossbows, later possibly more items require an entity parameter -
getMaxItemUseDuration
-
asOne
Clones the itemstack and returns it a single quantity.- Returns:
- The new itemstack with 1 quantity
-
asQuantity
Clones the itemstack and returns it as the specified quantity- Parameters:
qty
- The quantity of the cloned item- Returns:
- The new itemstack with specified quantity
-
add
Adds 1 to this itemstack. Will not go over the items max stack size.- Returns:
- The same item (not a clone)
-
add
Adds quantity to this itemstack. Will not go over the items max stack size.- Parameters:
qty
- The amount to add- Returns:
- The same item (not a clone)
-
subtract
Subtracts 1 to this itemstack. Going to 0 or less will invalidate the item.- Returns:
- The same item (not a clone)
-
subtract
Subtracts quantity to this itemstack. Going to 0 or less will invalidate the item.- Parameters:
qty
- The amount to add- Returns:
- The same item (not a clone)
-
getLore
Deprecated.in favor oflore()
If the item has lore, returns it, else it will return null- Returns:
- The lore, or null
-
lore
If the item has lore, returns it, else it will return null- Returns:
- The lore, or null
-
setLore
Deprecated.in favour oflore(java.util.List)
Sets the lore for this item. Removes lore when given null.- Parameters:
lore
- the lore that will be set
-
lore
Sets the lore for this item. Removes lore when given null.- Parameters:
lore
- the lore that will be set
-
addItemFlags
Set itemflags which should be ignored when rendering a ItemStack in the Client. This Method does silently ignore double set itemFlags.- Parameters:
itemFlags
- The hideflags which shouldn't be rendered
-
removeItemFlags
Remove specific set of itemFlags. This tells the Client it should render it again. This Method does silently ignore double removed itemFlags.- Parameters:
itemFlags
- Hideflags which should be removed
-
getItemFlags
Get current set itemFlags. The collection returned is unmodifiable.- Returns:
- A set of all itemFlags set
-
hasItemFlag
Check if the specified flag is present on this item.- Parameters:
flag
- the flag to check- Returns:
- if it is present
-
translationKey
Gets the translation key.This is not the same as getting the translation key for the material of this itemstack.
- Specified by:
translationKey
in interfaceTranslatable
- Returns:
- the translation key
-
getRarity
Deprecated, for removal: This API element is subject to removal in a future version.Gets the item rarity of the itemstack. The rarity can change based on enchantments.- Returns:
- the itemstack rarity
-
isRepairableBy
Checks if an itemstack can repair this itemstack. Returns false ifthis
orrepairMaterial
's type is not an item (Material.isItem()
).- Parameters:
repairMaterial
- the repair material- Returns:
- true if it is repairable by, false if not
-
canRepair
Checks if this itemstack can repair another. Returns false ifthis
ortoBeRepaired
's type is not an item (Material.isItem()
).- Parameters:
toBeRepaired
- the itemstack to be repaired- Returns:
- true if it can repair, false if not
-
damage
Damages this itemstack by the specified amount. This runs all logic associated with damaging an itemstack like events and stat changes.- Parameters:
amount
- the amount of damage to dolivingEntity
- the entity related to the damage- Returns:
- the damaged itemstack or an empty one if it broke. May return the same instance of ItemStack
- See Also:
-
empty
Returns an empty item stack, consists of an air material and a stack size of 0. Any item stack with a material of air or a stack size of 0 is seen as being empty byisEmpty()
. -
isEmpty
public boolean isEmpty()Returns whether this item stack is empty and contains no item. This means it is either air or the stack has a size of 0. -
computeTooltipLines
public @NotNull @Unmodifiable List<Component> computeTooltipLines(@NotNull TooltipContext tooltipContext, @Nullable Player player) Computes the tooltip lines for this stack.Disclaimer: Tooltip contents are not guaranteed to be consistent across different Minecraft versions.
- Parameters:
tooltipContext
- the tooltip contextplayer
- a player for player-specific tooltip lines- Returns:
- an immutable list of components (can be empty)
-
getDisplayName
Gets the display name that is set.Plugins should check that hasDisplayName() returns
true
before calling this method.- Returns:
- the display name that is set
-
setDisplayName
Sets the display name.- Parameters:
name
- the name to set
-
hasDisplayName
public boolean hasDisplayName()Checks for existence of a display name.- Returns:
- true if this has a display name
-
getLocalizedName
Gets the localized display name that is set.Plugins should check that hasLocalizedName() returns
true
before calling this method.- Returns:
- the localized name that is set
-
setLocalizedName
Sets the localized name.- Parameters:
name
- the name to set
-
hasLocalizedName
public boolean hasLocalizedName()Checks for existence of a localized name.- Returns:
- true if this has a localized name
-
hasLore
public boolean hasLore()Checks for existence of lore.- Returns:
- true if this has lore
-
hasEnchant
Checks for existence of the specified enchantment.- Parameters:
ench
- enchantment to check- Returns:
- true if this enchantment exists for this meta
-
getEnchantLevel
Checks for the level of the specified enchantment.- Parameters:
ench
- enchantment to check- Returns:
- The level that the specified enchantment has, or 0 if none
-
getEnchants
Returns a copy the enchantments in this ItemMeta.
Returns an empty map if none.- Returns:
- An immutable copy of the enchantments
-
addEnchant
public boolean addEnchant(@NotNull @NotNull Enchantment ench, int level, boolean ignoreLevelRestriction) Adds the specified enchantment to this item meta.- Parameters:
ench
- Enchantment to addlevel
- Level for the enchantmentignoreLevelRestriction
- this indicates the enchantment should be applied, ignoring the level limit- Returns:
- true if the item meta changed as a result of this call, false otherwise
-
removeEnchant
Removes the specified enchantment from this item meta.- Parameters:
ench
- Enchantment to remove- Returns:
- true if the item meta changed as a result of this call, false otherwise
-
hasEnchants
public boolean hasEnchants()Checks for the existence of any enchantments.- Returns:
- true if an enchantment exists on this meta
-
hasConflictingEnchant
Checks if the specified enchantment conflicts with any enchantments in this ItemMeta.- Parameters:
ench
- enchantment to test- Returns:
- true if the enchantment conflicts, false otherwise
-
setCustomModelData
Sets the custom model data.CustomModelData is an integer that may be associated client side with a custom item model.
- Parameters:
data
- the data to set, or null to clear
-
getCustomModelData
public int getCustomModelData()Gets the custom model data that is set.CustomModelData is an integer that may be associated client side with a custom item model.
Plugins should check that hasCustomModelData() returns
true
before calling this method.- Returns:
- the localized name that is set
-
hasCustomModelData
public boolean hasCustomModelData()Checks for existence of custom model data.CustomModelData is an integer that may be associated client side with a custom item model.
- Returns:
- true if this has custom model data
-
hasBlockData
public boolean hasBlockData()Returns whether the item has block data currently attached to it.- Returns:
- whether block data is already attached
-
getBlockData
Returns the currently attached block data for this item or creates a new one if one doesn't exist. The state is a copy, it must be set back (or to another item) withsetBlockData(BlockData)
- Parameters:
material
- the material we wish to get this data in the context of- Returns:
- the attached data or new data
-
setBlockData
Attaches a copy of the passed block data to the item.- Parameters:
blockData
- the block data to attach to the block.- Throws:
IllegalArgumentException
- if the blockData is null or invalid for this item.
-
getRepairCost
public int getRepairCost()Gets the repair penalty- Returns:
- the repair penalty
-
setRepairCost
public void setRepairCost(int cost) Sets the repair penalty- Parameters:
cost
- repair penalty
-
hasRepairCost
public boolean hasRepairCost()Checks to see if this has a repair penalty- Returns:
- true if this has a repair penalty
-
isUnbreakable
public boolean isUnbreakable()Return if the unbreakable tag is true. An unbreakable item will not lose durability.- Returns:
- true if the unbreakable tag is true
-
setUnbreakable
public void setUnbreakable(boolean unbreakable) Sets the unbreakable tag. An unbreakable item will not lose durability.- Parameters:
unbreakable
- true if set unbreakable
-
hasAttributeModifiers
public boolean hasAttributeModifiers()Checks for the existence of any AttributeModifiers.- Returns:
- true if any AttributeModifiers exist
-
getAttributeModifiers
Return an immutable copy of all Attributes and their modifiers in this ItemMeta.
Returns null if none exist.- Returns:
- an immutable
Multimap
of Attributes and their AttributeModifiers, or null if none exist
-
getAttributeModifiers
@NotNull public @NotNull Multimap<Attribute,AttributeModifier> getAttributeModifiers(@Nullable @Nullable EquipmentSlot slot) Return an immutable copy of allAttribute
s and theirAttributeModifier
s for a givenEquipmentSlot
.
AnyAttributeModifier
that does have have a givenEquipmentSlot
will be returned. This is because AttributeModifiers without a slot are active in any slot.
If there are no attributes set for the given slot, an empty map will be returned.- Parameters:
slot
- theEquipmentSlot
to check- Returns:
- the immutable
Multimap
with the respective Attributes and modifiers, or an empty map if no attributes are set.
-
getAttributeModifiers
@Nullable public @Nullable Collection<AttributeModifier> getAttributeModifiers(@NotNull @NotNull Attribute attribute) Return an immutable copy of allAttributeModifier
s for a givenAttribute
- Parameters:
attribute
- theAttribute
- Returns:
- an immutable collection of
AttributeModifier
s or null if no AttributeModifiers exist for the Attribute. - Throws:
NullPointerException
- if Attribute is null
-
addAttributeModifier
public boolean addAttributeModifier(@NotNull @NotNull Attribute attribute, @NotNull @NotNull AttributeModifier modifier) Add an Attribute and it's Modifier. AttributeModifiers can now supportEquipmentSlot
s. If not set, theAttributeModifier
will be active in ALL slots.
TwoAttributeModifier
s that have the sameUUID
cannot exist on the same Attribute.- Parameters:
attribute
- theAttribute
to modifymodifier
- theAttributeModifier
specifying the modification- Returns:
- true if the Attribute and AttributeModifier were successfully added
- Throws:
NullPointerException
- if Attribute is nullNullPointerException
- if AttributeModifier is nullIllegalArgumentException
- if AttributeModifier already exists
-
setAttributeModifiers
public void setAttributeModifiers(@Nullable @Nullable Multimap<Attribute, AttributeModifier> attributeModifiers) Set allAttribute
s and theirAttributeModifier
s. To clear all currently set Attributes and AttributeModifiers use null or an empty Multimap. If not null nor empty, this will filter all entries that are not-null and add them to the ItemStack.- Parameters:
attributeModifiers
- the new Multimap containing the Attributes and their AttributeModifiers
-
removeAttributeModifier
Remove allAttributeModifier
s associated with the givenAttribute
. This will return false if nothing was removed.- Parameters:
attribute
- attribute to remove- Returns:
- true if all modifiers were removed from a given Attribute. Returns false if no attributes were removed.
- Throws:
NullPointerException
- if Attribute is null
-
removeAttributeModifier
Remove allAttribute
s andAttributeModifier
s for a givenEquipmentSlot
.
If the givenEquipmentSlot
is null, this will remove allAttributeModifier
s that do not have an EquipmentSlot set.- Parameters:
slot
- theEquipmentSlot
to clear all Attributes and their modifiers for- Returns:
- true if all modifiers were removed that match the given EquipmentSlot.
-
removeAttributeModifier
public boolean removeAttributeModifier(@NotNull @NotNull Attribute attribute, @NotNull @NotNull AttributeModifier modifier) Remove a specificAttribute
andAttributeModifier
. AttributeModifiers are matched according to theirUUID
.- Parameters:
attribute
- theAttribute
to removemodifier
- theAttributeModifier
to remove- Returns:
- if any attribute modifiers were remove
- Throws:
NullPointerException
- if the Attribute is nullNullPointerException
- if the AttributeModifier is null- See Also:
-
hasDamage
public boolean hasDamage()Checks to see if this item has damage- Returns:
- true if this has damage
-
getDamage
public int getDamage()Gets the damage- Returns:
- the damage
-
setDamage
public void setDamage(int damage) Sets the damage- Parameters:
damage
- item damage
-
repair
public void repair()Repairs this item by 1 durability -
damage
public boolean damage()Damages this item by 1 durability- Returns:
- True if damage broke the item
-
repair
public void repair(int amount) Repairs this item's durability by amount- Parameters:
amount
- Amount of durability to repair
-
damage
public boolean damage(int amount) Damages this item's durability by amount- Parameters:
amount
- Amount of durability to damage- Returns:
- True if damage broke the item
-
damage
public boolean damage(int amount, boolean ignoreUnbreaking) Damages this item's durability by amount- Parameters:
amount
- Amount of durability to damageignoreUnbreaking
- Ignores unbreaking enchantment- Returns:
- True if damage broke the item
-
setDurability(short)