Class ItemBuilder
java.lang.Object
tech.guilhermekaua.spigotboot.core.spigot.utils.ItemBuilder
A utility class that helps to create and modify ItemStacks.
-
Constructor Summary
ConstructorsConstructorDescriptionItemBuilder(org.bukkit.inventory.ItemStack item) Creates a new ItemBuilder with the given ItemStack.ItemBuilder(org.bukkit.Material type) Creates a new ItemBuilder with the given Material type.ItemBuilder(org.bukkit.Material type, int data) Creates a new ItemBuilder with the given Material type and data value.ItemBuilder(org.bukkit.Material type, org.bukkit.Color color) Creates a new ItemBuilder with a leather armor item of the given color. -
Method Summary
Modifier and TypeMethodDescriptionAdds a string to the lore of the ItemStack at the given index.Adds some strings to the lore of the ItemStack.changeItemMeta(Consumer<org.bukkit.inventory.meta.ItemMeta> consumer) Changes the ItemMeta of the ItemStack using the given Consumer function.deleteLoreLine(int index) Deletes a string from the lore of the ItemStack at the given index.intFinds the index of a string in the lore of the ItemStack that contains the given substring.getLore()Returns the lore of the ItemStack as a list of strings.Replaces the current item with a player head owned by the named player.Replaces the current item with a player head skinned from a Mojang texture URL.Replaces the current item with a player head owned by the player with the given UUID string.Replaces the current item with a player head owned by the player with the given UUID.static ItemBuilderCreates anItemBuilderwrapping a player head resolved from either a Mojang texture URL or a player name.static ItemBuilderofMaterial(String... candidates) Resolves the firstMaterialthat matches one of the given config names (modern name first, legacy fallback viaTypeUtil.getMaterialFromLegacy(String)), falling back toMaterial.STONEwhen none resolve — so a GUI never fails to render an item.placeholder(String placeholder, String value) Replaces a placeholder in the lore of the ItemStack with a given value.setAmount(int amount) Sets the stack size, clamped to at least 1.setCustomModelData(Integer data) Sets the custom model data.setCustomModelDataColors(org.bukkit.Color... colors) Sets custom model data component tint colors.setCustomModelDataComponent(List<Float> floats, List<Boolean> flags, List<String> strings, List<org.bukkit.Color> colors) Sets custom model data component values on servers that expose that API.setCustomModelDataFlags(Boolean... flags) Sets custom model data component boolean flag values.setCustomModelDataFloats(Float... floats) Sets custom model data component float values.setCustomModelDataStrings(String... strings) Sets custom model data component string values.setGlow(boolean glow) setItem(org.bukkit.inventory.ItemStack item) setItemModel(String itemModel) Sets the direct item model key on servers that expose that API.setLegacyModelData(Integer data) Sets the legacy durability/data value used by old pre-flattening item variants.Replaces a string in the lore of the ItemStack at the given index.Sets the lore of the ItemStack.Sets the lore of the ItemStack.setMaterial(String... candidates) Sets the display name of the ItemStack.setRawLore(String... lore) Sets the lore verbatim, without colour translation — for lines that are already formatted.setRawLore(List<String> lore) Sets the lore verbatim, without colour translation — for lines that are already formatted.setRawName(String name) Sets the display name verbatim, without colour translation — for names that are already formatted (e.g.org.bukkit.inventory.ItemStackwrap()Returns the ItemStack that this builder is working on.
-
Constructor Details
-
ItemBuilder
public ItemBuilder(org.bukkit.inventory.ItemStack item) Creates a new ItemBuilder with the given ItemStack.- Parameters:
item- the ItemStack to use
-
ItemBuilder
public ItemBuilder(org.bukkit.Material type) Creates a new ItemBuilder with the given Material type.- Parameters:
type- the Material type to use
-
ItemBuilder
public ItemBuilder(org.bukkit.Material type, int data) Creates a new ItemBuilder with the given Material type and data value.- Parameters:
type- the Material type to usedata- the data value to use
-
ItemBuilder
public ItemBuilder(org.bukkit.Material type, org.bukkit.Color color) Creates a new ItemBuilder with a leather armor item of the given color.- Parameters:
type- the Material type to usecolor- the Color to use
-
-
Method Details
-
setItem
-
headUrl
Replaces the current item with a player head skinned from a Mojang texture URL. Works version-proof from 1.8.8 to the latest release (seeHeadUtils.getHeadByUrl(String)); a null, blank, or malformed URL yields a bare head rather than throwing.- Parameters:
url- the Mojang texture URL (e.g.http://textures.minecraft.net/texture/<hash>)- Returns:
- this ItemBuilder instance for chaining
-
headName
Replaces the current item with a player head owned by the named player.- Parameters:
name- the player name whose skin to use- Returns:
- this ItemBuilder instance for chaining
-
headUUID
Replaces the current item with a player head owned by the player with the given UUID.- Parameters:
uuid- the player UUID whose skin to use- Returns:
- this ItemBuilder instance for chaining
-
headUUID
Replaces the current item with a player head owned by the player with the given UUID string. A null or unparseable UUID yields a bare head rather than throwing.- Parameters:
uuid- the player UUID in canonical string form- Returns:
- this ItemBuilder instance for chaining
-
setGlow
-
changeItemMeta
Changes the ItemMeta of the ItemStack using the given Consumer function.- Parameters:
consumer- the Consumer function to apply to the ItemMeta- Returns:
- this ItemBuilder instance for chaining
-
setName
Sets the display name of the ItemStack.- Parameters:
name- the display name to use- Returns:
- this ItemBuilder instance for chaining
-
addLore
Adds some strings to the lore of the ItemStack.- Parameters:
lore- an array of strings to add to the lore- Returns:
- this ItemBuilder instance for chaining
-
addLore
Adds a string to the lore of the ItemStack at the given index.- Parameters:
index- the index to insert the string atlore- the string to add to the lore- Returns:
- this ItemBuilder instance for chaining
-
setLore
Replaces a string in the lore of the ItemStack at the given index.- Parameters:
index- the index to replace the string atlore- the string to replace in the lore- Returns:
- this ItemBuilder instance for chaining
-
deleteLoreLine
Deletes a string from the lore of the ItemStack at the given index.- Parameters:
index- the index to delete the string from- Returns:
- this ItemBuilder instance for chaining
-
findLore
Finds the index of a string in the lore of the ItemStack that contains the given substring.- Parameters:
lore- the substring to look for in the lore- Returns:
- the index of the string that contains the substring, or -1 if not found
-
getLore
Returns the lore of the ItemStack as a list of strings.- Returns:
- the lore of the ItemStack, or null if none
-
setLore
Sets the lore of the ItemStack.- Parameters:
lore- an array of strings to use as the lore- Returns:
- this ItemBuilder instance for chaining
-
setLore
Sets the lore of the ItemStack.- Parameters:
lore- a list of strings to use as the lore- Returns:
- this ItemBuilder instance for chaining
-
placeholder
Replaces a placeholder in the lore of the ItemStack with a given value.- Parameters:
placeholder- the placeholder to replacevalue- the value to replace with- Returns:
- this ItemBuilder instance for chaining
-
setAmount
Sets the stack size, clamped to at least 1.- Parameters:
amount- the desired amount- Returns:
- this ItemBuilder instance for chaining
-
setLegacyModelData
Sets the legacy durability/data value used by old pre-flattening item variants.- Parameters:
data- the legacy data value, or null to leave it unchanged- Returns:
- this ItemBuilder instance for chaining
-
setCustomModelData
Sets the custom model data. This uses the component-backed custom model data float list when available on newer servers, falls back to the 1.14+ integer API, and is a no-op on older servers or whendatais null.- Parameters:
data- the custom model data, or null to leave it unset- Returns:
- this ItemBuilder instance for chaining
-
setCustomModelDataComponent
public ItemBuilder setCustomModelDataComponent(List<Float> floats, List<Boolean> flags, List<String> strings, List<org.bukkit.Color> colors) Sets custom model data component values on servers that expose that API. Null lists are left unchanged on the component snapshot.- Parameters:
floats- range dispatch float values, or null to leave unchangedflags- condition flag values, or null to leave unchangedstrings- select string values, or null to leave unchangedcolors- tint colors, or null to leave unchanged- Returns:
- this ItemBuilder instance for chaining
-
setCustomModelDataFloats
Sets custom model data component float values.- Parameters:
floats- range dispatch float values- Returns:
- this ItemBuilder instance for chaining
-
setCustomModelDataFlags
Sets custom model data component boolean flag values.- Parameters:
flags- condition flag values- Returns:
- this ItemBuilder instance for chaining
-
setCustomModelDataStrings
Sets custom model data component string values.- Parameters:
strings- select string values- Returns:
- this ItemBuilder instance for chaining
-
setCustomModelDataColors
Sets custom model data component tint colors.- Parameters:
colors- tint colors- Returns:
- this ItemBuilder instance for chaining
-
setItemModel
Sets the direct item model key on servers that expose that API.- Parameters:
itemModel- a namespaced key such asmy_pack:bronze_sword- Returns:
- this ItemBuilder instance for chaining
-
ofMaterial
Resolves the firstMaterialthat matches one of the given config names (modern name first, legacy fallback viaTypeUtil.getMaterialFromLegacy(String)), falling back toMaterial.STONEwhen none resolve — so a GUI never fails to render an item.- Parameters:
candidates- material names to try in order- Returns:
- a builder for the resolved material, or STONE
-
ofHead
Creates anItemBuilderwrapping a player head resolved from either a Mojang texture URL or a player name. WhentextureUrlOrNamematchesHeadUtils.isTextureUrl(String), the head is skinned viaHeadUtils.getHeadByUrl(String); otherwiseHeadUtils.getHeadByName(String)is used. A null or blank value yields a bare head rather than throwing.- Parameters:
textureUrlOrName- a Mojang texture URL or player name- Returns:
- a builder wrapping the resolved player head
-
setMaterial
-
setRawName
Sets the display name verbatim, without colour translation — for names that are already formatted (e.g. byChatMarkup.legacy(...)).- Parameters:
name- the pre-formatted display name- Returns:
- this ItemBuilder instance for chaining
-
setRawLore
Sets the lore verbatim, without colour translation — for lines that are already formatted.- Parameters:
lore- the pre-formatted lore lines- Returns:
- this ItemBuilder instance for chaining
-
setRawLore
Sets the lore verbatim, without colour translation — for lines that are already formatted.- Parameters:
lore- the pre-formatted lore lines- Returns:
- this ItemBuilder instance for chaining
-
wrap
public org.bukkit.inventory.ItemStack wrap()Returns the ItemStack that this builder is working on.- Returns:
- the ItemStack instance
-