Class ItemBuilder
java.lang.Object
top.magstar.framework.ItemBuilder
A fluent builder utility for creating and modifying
ItemStack instances in Bukkit/Spigot/Paper.
The ItemBuilder wraps an ItemStack and its ItemMeta, exposing a chainable API
to conveniently customize item properties such as display name, lore, enchantments, attributes,
unbreakability, and more. It supports both legacy BaseComponent
and Adventure Component systems, as well as custom
SmartString formatting.
Example
ItemStack sword = new ItemBuilder(new ItemStack(Material.DIAMOND_SWORD))
.setDisplayName(Component.text("§bEpic Sword"))
.appendLore("§7This blade was forged in fire.", "§eSharp and deadly!")
.addEnchants(5, Enchantment.DAMAGE_ALL)
.setUnbreakable(true)
.toItemStack();
Features
- Set display names via Strings, Adventure Components, BaseComponents, or SmartStrings.
- Set or modify lore lines with various overloads.
- Insert, append, or prepend lore at specific positions.
- Support for enchantments and attribute modifiers.
- Convenient cloning, equality comparison, and hash code computation.
- Builder-style pattern for method chaining.
Note: toItemStack() must be called to apply changes and obtain the final item.
- Since:
- 1.0.0
- Version:
- 1.0.0
- Author:
- Magstar
-
Constructor Summary
ConstructorsConstructorDescriptionItemBuilder(org.bukkit.inventory.ItemStack originStack) Constructs a newItemBuilderwith a given originItemStack.ItemBuilder(org.bukkit.inventory.ItemStack originStack, org.bukkit.inventory.meta.ItemMeta originMeta) Constructs a newItemBuilderwith a given originItemStackand explicitItemMeta. -
Method Summary
Modifier and TypeMethodDescriptionaddEnchants(int level, org.bukkit.enchantments.Enchantment... enchantment) Adds one or more enchantments to the item with given level.appendLore(String... lore) Appends lore to the end of the current lore list.appendLore(net.md_5.bungee.api.chat.BaseComponent[]... lore) Appends lore asBaseComponentlines.appendLore(SmartString... lore) Appends lore usingSmartString.clone()Creates a deep clone of this builder, including meta.static org.bukkit.inventory.ItemStackdeserialize(String serialized) Discards unsaved changes and restores the originalItemMeta.booleanTo check if the two ItemBuilders will produce same results.static ItemBuilderfromCustomStack(dev.lone.itemsadder.api.CustomStack customStack) static ItemBuilderfromCustomStack(String key) org.bukkit.inventory.meta.ItemMetagetMeta()Returns the currentItemMetain use.<T> TbooleangetNBTBoolean(String key) byte[]getNBTByte(String key) byte[]getNBTByteArray(String key) <E extends Enum<E>>
EgetNBTEnum(String key, Class<E> clazz) floatgetNBTFloat(String key) int获取 NBT 整数值int获取 NBT 整数值,带默认值int[]getNBTIntArray(String key) org.bukkit.inventory.ItemStackgetNBTItemStack(String key) org.bukkit.inventory.ItemStack[]longgetNBTLong(String key) long[]getNBTLongArray(String key) shortgetNBTShort(String key) getNBTString(String key) 获取 NBT 字符串值getNBTString(String key, String defaultValue) 获取 NBT 字符串值,带默认值inthashCode()Computes the hash code based on the builtItemStack.boolean检查是否存在指定的 NBT 标签insertLoreAt(int position, String... lore) Insert a series of things into the lore at a specified position.insertLoreAt(int position, net.md_5.bungee.api.chat.BaseComponent[]... lore) Inserts lore asBaseComponentlines at a specified position.insertLoreAt(int position, SmartString... lore) InsertsSmartStringlore at a specified position.修改物品的 NBT 数据<T> T修改 NBT 并返回结果prependLore(String... lore) Prepends lore to the beginning of the current lore list.prependLore(net.md_5.bungee.api.chat.BaseComponent[]... lore) PrependsBaseComponentlore at the beginning.prependLore(SmartString... lore) PrependsSmartStringlore at the beginning.<T> T只读取 NBT 数据,不修改removeEnchants(org.bukkit.enchantments.Enchantment... enchantment) Removes one or more enchantments from the item.removeNBTTag(String key) 移除 NBT 标签static Stringserialize(org.bukkit.inventory.ItemStack itemStack) setAmount(int amount) Sets the stack size.setAttributeModifier(org.bukkit.attribute.Attribute attribute, org.bukkit.attribute.AttributeModifier am) Assigns or removes anAttributeModifierfor a specificAttribute.setDisplayName(String name) Sets the display name using a legacy String.setDisplayName(net.kyori.adventure.text.Component component) Sets the display name using AdventureComponent.setDisplayName(net.md_5.bungee.api.chat.BaseComponent... name) Sets the display name using BungeeBaseComponentsetDisplayName(SmartString name) Sets the display name using aSmartString.Sets the lore using raw strings (one line per entry).setLore(net.md_5.bungee.api.chat.BaseComponent[]... lore) Sets the lore using BungeeBaseComponentlines.setLore(SmartString... lore) Sets the lore usingSmartStringlines.setLoreByLines(String lore) Splits a long string into multiple lines by newline markers and sets them as lore.<T> ItemBuildersetNBTBoolean(String key, boolean value) 设置自定义 NBT 布尔值setNBTByte(String key, byte value) setNBTByteArray(String key, byte[] value) setNBTDouble(String key, double value) 设置自定义 NBT 双精度值<E extends Enum<?>>
ItemBuildersetNBTEnum(String key, E value) setNBTFloat(String key, float value) 设置自定义 NBT 整数值setNBTIntArray(String key, int[] value) setNBTItemStack(String key, org.bukkit.inventory.ItemStack value) setNBTItemStackArray(String key, org.bukkit.inventory.ItemStack[] value) setNBTLong(String key, long value) setNBTLongArray(String key, long[] value) setNBTShort(String key, short value) setNBTString(String key, String value) 设置自定义 NBT 字符串值setUnbreakable(boolean unbreakable) Sets whether the item is unbreakable.org.bukkit.inventory.ItemStackBuilds and returns the finalItemStackwith applied changes.<T extends org.bukkit.inventory.meta.ItemMeta>
ItemBuilderwithMeta(Class<T> metaClass, Function<T, ItemBuilder> modifier) 修改特定类型的 ItemMeta
-
Constructor Details
-
ItemBuilder
@ParametersAreNonnullByDefault public ItemBuilder(org.bukkit.inventory.ItemStack originStack) Constructs a newItemBuilderwith a given originItemStack. If the stack has noItemMeta, a default one is created.- Parameters:
originStack- the baseItemStack
-
ItemBuilder
@ParametersAreNonnullByDefault public ItemBuilder(org.bukkit.inventory.ItemStack originStack, org.bukkit.inventory.meta.ItemMeta originMeta) Constructs a newItemBuilderwith a given originItemStackand explicitItemMeta.- Parameters:
originStack- the baseItemStackoriginMeta- theItemMetato use
-
-
Method Details
-
toItemStack
@Nonnull public org.bukkit.inventory.ItemStack toItemStack()Builds and returns the finalItemStackwith applied changes.- Returns:
- the updated
ItemStack
-
setDisplayName
@Nonnull @ParametersAreNullableByDefault public ItemBuilder setDisplayName(net.kyori.adventure.text.Component component) Sets the display name using AdventureComponent. -
setDisplayName
Sets the display name using a legacy String. -
setDisplayName
Sets the display name using aSmartString. -
setDisplayName
@Nonnull @ParametersAreNullableByDefault public ItemBuilder setDisplayName(net.md_5.bungee.api.chat.BaseComponent... name) Sets the display name using BungeeBaseComponent -
setLore
Sets the lore using raw strings (one line per entry). -
setLore
@Nonnull @ParametersAreNullableByDefault public ItemBuilder setLore(net.md_5.bungee.api.chat.BaseComponent[]... lore) Sets the lore using BungeeBaseComponentlines. -
setLore
Sets the lore usingSmartStringlines. -
insertLoreAt
@Nonnull @ParametersAreNullableByDefault public ItemBuilder insertLoreAt(int position, String... lore) Insert a series of things into the lore at a specified position.- Parameters:
position- Where to insert, negative to count from back.lore- What to insert.- Returns:
- The builder.
-
insertLoreAt
@Nonnull @ParametersAreNullableByDefault public ItemBuilder insertLoreAt(int position, net.md_5.bungee.api.chat.BaseComponent[]... lore) Inserts lore asBaseComponentlines at a specified position. -
insertLoreAt
@Nonnull @ParametersAreNullableByDefault public ItemBuilder insertLoreAt(int position, SmartString... lore) InsertsSmartStringlore at a specified position. -
setLoreByLines
Splits a long string into multiple lines by newline markers and sets them as lore.- Parameters:
lore- multiline string separated by "\\n"- Returns:
- this builder
-
appendLore
Appends lore to the end of the current lore list.- Parameters:
lore- What to append.- Returns:
- The builder.
-
appendLore
@Nonnull @ParametersAreNullableByDefault public ItemBuilder appendLore(net.md_5.bungee.api.chat.BaseComponent[]... lore) Appends lore asBaseComponentlines. -
appendLore
Appends lore usingSmartString. -
prependLore
Prepends lore to the beginning of the current lore list.- Parameters:
lore- What to prepend.- Returns:
- The builder.
-
prependLore
@Nonnull @ParametersAreNullableByDefault public ItemBuilder prependLore(net.md_5.bungee.api.chat.BaseComponent[]... lore) PrependsBaseComponentlore at the beginning. -
prependLore
PrependsSmartStringlore at the beginning. -
dropChanges
Discards unsaved changes and restores the originalItemMeta.- Returns:
- this builder
-
setAmount
Sets the stack size. -
clone
Creates a deep clone of this builder, including meta. -
equals
-
hashCode
-
addEnchants
@Nonnull @ParametersAreNullableByDefault public ItemBuilder addEnchants(int level, org.bukkit.enchantments.Enchantment... enchantment) Adds one or more enchantments to the item with given level.- Parameters:
level- enchantment levelenchantment- enchantments to apply- Returns:
- this builder
-
removeEnchants
@Nonnull @ParametersAreNullableByDefault public ItemBuilder removeEnchants(org.bukkit.enchantments.Enchantment... enchantment) Removes one or more enchantments from the item.- Parameters:
enchantment- enchantments to remove- Returns:
- this builder
-
setUnbreakable
Sets whether the item is unbreakable.- Parameters:
unbreakable- true to make the item unbreakable- Returns:
- this builder
-
setAttributeModifier
@Nonnull @ParametersAreNullableByDefault public ItemBuilder setAttributeModifier(org.bukkit.attribute.Attribute attribute, org.bukkit.attribute.AttributeModifier am) Assigns or removes anAttributeModifierfor a specificAttribute.If the modifier is null, it removes the modifier for the attribute.
- Parameters:
attribute- the attributeam- the modifier (or null to remove)- Returns:
- this builder
-
getMeta
@Nonnull public org.bukkit.inventory.meta.ItemMeta getMeta()Returns the currentItemMetain use.- Returns:
- the
ItemMeta
-
withMeta
@Nonnull public <T extends org.bukkit.inventory.meta.ItemMeta> ItemBuilder withMeta(Class<T> metaClass, Function<T, ItemBuilder> modifier) 修改特定类型的 ItemMeta- Type Parameters:
T- ItemMeta 的子类型- Parameters:
metaClass- 期望的 ItemMeta 类型modifier- 修改操作的函数- Returns:
- this builder
-
modifyNBT
修改物品的 NBT 数据- Parameters:
modifier- NBT 修改操作- Returns:
- this builder
-
modifyNBT
修改 NBT 并返回结果- Type Parameters:
T- 返回值类型- Parameters:
modifier- NBT 修改操作,可以返回值- Returns:
- 修改操作的返回值
-
readNBT
只读取 NBT 数据,不修改- Type Parameters:
T- 返回值类型- Parameters:
reader- NBT 读取操作- Returns:
- 读取操作的返回值
-
setNBTString
设置自定义 NBT 字符串值 -
setNBTInt
设置自定义 NBT 整数值 -
setNBTBoolean
设置自定义 NBT 布尔值 -
setNBTDouble
设置自定义 NBT 双精度值 -
setNBTFloat
-
setNBTLong
-
setNBTIntArray
-
setNBTLongArray
-
setNBTByteArray
-
setNBTByte
-
setNBTShort
-
setNBTItemStack
-
setNBTItemStackArray
@Nonnull public ItemBuilder setNBTItemStackArray(String key, org.bukkit.inventory.ItemStack[] value) -
setNBTEnum
-
setNBT
-
getNBTString
-
getNBTFloat
-
getNBTLong
-
getNBTIntArray
-
getNBTLongArray
-
getNBTByteArray
-
getNBTByte
-
getNBTShort
-
getNBTItemStack
-
getNBTItemStackArray
-
getNBTBoolean
-
getNBTEnum
-
getNBT
-
getNBTString
-
getNBTInt
获取 NBT 整数值 -
getNBTInt
获取 NBT 整数值,带默认值 -
hasNBTTag
检查是否存在指定的 NBT 标签 -
removeNBTTag
移除 NBT 标签 -
serialize
-
serialize
-
deserialize
-
fromCustomStack
-
fromCustomStack
-