Package de.eldoria.eldoutilities.builder
Class ItemStackBuilder
java.lang.Object
de.eldoria.eldoutilities.builder.ItemStackBuilder
A class which allows the creation of ItemStacks with a builder like pattern.
- Since:
- 1.1.0
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionSets the breakable tag.Sets the unbreakable tag.build()
Clone the underlying itemstack and return a cloned instance.static ItemStackBuilder
Load a item stack into a item stack builder.static ItemStackBuilder
Load a item stack into a item stack builder.static ItemStackBuilder
Creates a new item stack builderstatic ItemStackBuilder
Creates a new item stack builderofAmount
(int amount) Sets the amount of items in this stackwithAttributeModifier
(@NotNull Attribute attribute, @NotNull AttributeModifier modifier) Add an Attribute and it's Modifier.Sets the custom model data.withDisplayName
(@Nullable String name) Sets the display name.withDurability
(int damage) Sets the damagewithEnchant
(@NotNull Enchantment ench, int level, boolean ignoreLevelRestriction) Adds the specified enchantment to this item meta.withEnchantment
(@NotNull Enchantment ench, int level) Adds the specifiedEnchantment
to this item stack.withEnchantments
(@NotNull Map<Enchantment, Integer> enchantments) Adds the specified enchantments to this item stack.withItemFlags
(@NotNull ItemFlag... itemFlags) Set itemflags which should be ignored when rendering a ItemStack in the Client.withLocalizedName
(@Nullable String name) Sets the localized name.Sets the lore for this item.Sets the lore for this item.<T extends ItemMeta>
ItemStackBuilderwithMetaValue
(Class<T> clazz, Consumer<@NotNull T> consumer) Casts the item meta to the defined class if possible.withMetaValue
(Consumer<@NotNull ItemMeta> itemMetaConsumer) Applies an item meta consumer on the item meta, if a item meta is present.withNBTData
(Consumer<@NotNull PersistentDataContainer> dataConsumer) Applies a consumer on thePersistentDataHolder.getPersistentDataContainer()
.withUnsafeEnchantment
(@NotNull Enchantment ench, int level) Adds the specifiedEnchantment
to this item stack.withUnsafeEnchantments
(@NotNull Map<Enchantment, Integer> enchantments) Adds the specified enchantments to this item stack in an unsafe manner.
-
Constructor Details
-
ItemStackBuilder
-
-
Method Details
-
of
Creates a new item stack builder- Parameters:
material
- material of item stack- Returns:
- builder instance
-
of
Creates a new item stack builder- Parameters:
material
- material of item stackamount
- size of item stack.- Returns:
- builder instance
-
of
Load a item stack into a item stack builder.The item stack will be cloned to provide immutability.
- Parameters:
stack
- item stack to load- Returns:
- builder instance
-
of
Load a item stack into a item stack builder.- Parameters:
stack
- item stack to loadclone
- true if the item should be cloned- Returns:
- builder instance
-
withMetaValue
Applies an item meta consumer on the item meta, if a item meta is present.- Parameters:
itemMetaConsumer
- consumer for non null item meta.- Returns:
- builder instance
-
build
Clone the underlying itemstack and return a cloned instance.- Returns:
- new item stack instance
-
withEnchantments
Adds the specified enchantments to this item stack.This method is the same as calling
withEnchantment(Enchantment, int)
for each element of the map.- Parameters:
enchantments
- Enchantments to add- Returns:
- builder instance
- 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.
-
withEnchantment
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- Returns:
- builder instance
- Throws:
IllegalArgumentException
- if enchantment null, or enchantment is not applicable
-
withUnsafeEnchantments
public ItemStackBuilder withUnsafeEnchantments(@NotNull @NotNull Map<Enchantment, Integer> enchantments) Adds the specified enchantments to this item stack in an unsafe manner.This method is the same as calling
withUnsafeEnchantment(Enchantment, int)
for each element of the map.- Parameters:
enchantments
- Enchantments to add- Returns:
- builder instance
-
withUnsafeEnchantment
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- Returns:
- builder instance
-
ofAmount
Sets the amount of items in this stack- Parameters:
amount
- New amount of items in this stack- Returns:
- builder instance
-
withDisplayName
Sets the display name.- Parameters:
name
- the name to set- Returns:
- builder instance
-
withLocalizedName
Sets the localized name.- Parameters:
name
- the name to set- Returns:
- builder instance
-
withLore
Sets the lore for this item.- Parameters:
lore
- the lore that will be set- Returns:
- builder instance
-
withLore
Sets the lore for this item.- Parameters:
lore
- the lore that will be set- Returns:
- builder instance
-
withCustomModelData
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- Returns:
- builder instance
-
withEnchant
public ItemStackBuilder withEnchant(@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:
- builder instance
-
withItemFlags
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- Returns:
- builder instance
-
asUnbreakable
Sets the unbreakable tag. An unbreakable item will not lose durability.- Returns:
- builder instance
-
asBreakable
Sets the breakable tag. An breakable item will lose durability. This is the default value.- Returns:
- builder instance
-
withAttributeModifier
public ItemStackBuilder withAttributeModifier(@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:
- builder instance
- Throws:
NullPointerException
- if Attribute is nullNullPointerException
- if AttributeModifier is nullIllegalArgumentException
- if AttributeModifier already exists
-
withDurability
Sets the damage- Parameters:
damage
- item damage- Returns:
- builder instance
-
withNBTData
Applies a consumer on thePersistentDataHolder.getPersistentDataContainer()
. The consumer will only be executed if a item meta is present- Parameters:
dataConsumer
- consumer for data container- Returns:
- builder instance
-
withMetaValue
public <T extends ItemMeta> ItemStackBuilder withMetaValue(Class<T> clazz, Consumer<@NotNull T> consumer) Casts the item meta to the defined class if possible.Will apply the consumer on the meta if the cast was successful.
- Type Parameters:
T
- type of meta- Parameters:
clazz
- class of item metaconsumer
- consumer for item meta- Returns:
- builder instance
-