Class ItemBuilder
java.lang.Object
com.samjakob.spigui.item.ItemBuilder
A helper class for creating or modifying ItemStacks.
The class wraps an ItemStack object and provides convenient chainable, 'builder-pattern' methods for manipulating the stack's metadata.
The intention is that this class will be used in builder form - for example;
The class wraps an ItemStack object and provides convenient chainable, 'builder-pattern' methods for manipulating the stack's metadata.
The intention is that this class will be used in builder form - for example;
new ItemBuilder(Material.SPONGE).name("&cAlmighty sponge").amount(21).build();
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final org.bukkit.inventory.ItemStackThe item stack being built. -
Constructor Summary
ConstructorsConstructorDescriptionItemBuilder(org.bukkit.inventory.ItemStack stack) Creates an ItemBuilder wrapper for a given stack.ItemBuilder(org.bukkit.Material material) Creates anItemStackandItemBuilderwrapper for a new stack with the given type. -
Method Summary
Modifier and TypeMethodDescriptionamount(int amount) Sets the amount of items in theItemStack.org.bukkit.inventory.ItemStackbuild()An alias forget().color(ItemDataColor color) An alias fordurability(short)that takes anItemDataColoras an argument instead.data(short data) An alias fordurability(short).deflag(org.bukkit.inventory.ItemFlag... flag) Accepts a variable number ofItemFlags to remove from the stack.durability(short durability) Sets the durability (data value) of the item.enchant(org.bukkit.enchantments.Enchantment enchantment, int level) Adds the specified enchantment to the stack.flag(org.bukkit.inventory.ItemFlag... flag) Accepts a variable number ofItemFlags to apply to the stack.org.bukkit.inventory.ItemStackget()Returns theItemStackthat theItemBuilderinstance represents.intReturns the amount of items in theItemStack.getColor()Essentially a proxy forItemDataColor.getByValue(short).shortReturns the durability or data value of the item.getLore()Gets the lore of the item as a list of strings.getName()Returns either the display name of the item, if it exists, or null if it doesn't.org.bukkit.MaterialgetType()Returns the type (Material) of the ItemStack.ifThen(Predicate<ItemBuilder> ifTrue, Function<ItemBuilder, Object> then) This is used to, inline, perform an operation if a given condition is true.Sets the lore of the item.Sets the lore of the item.Sets the display name of the item.skullOwner(String name) If the item hasSkullMeta(i.e.type(org.bukkit.Material material) Sets the type (Material) of the ItemStack.unenchant(org.bukkit.enchantments.Enchantment enchantment) Removes the specified enchantment from the stack.
-
Field Details
-
stack
private final org.bukkit.inventory.ItemStack stackThe item stack being built.
-
-
Constructor Details
-
ItemBuilder
public ItemBuilder(org.bukkit.Material material) Creates anItemStackandItemBuilderwrapper for a new stack with the given type.- Parameters:
material- TheMaterialto use when creating the stack.
-
ItemBuilder
public ItemBuilder(org.bukkit.inventory.ItemStack stack) Creates an ItemBuilder wrapper for a given stack.- Parameters:
stack- The ItemStack to wrap.
-
-
Method Details
-
type
Sets the type (Material) of the ItemStack.- Parameters:
material- TheMaterialof the stack.- Returns:
- The
ItemBuilderinstance.
-
getType
public org.bukkit.Material getType()Returns the type (Material) of the ItemStack.- Returns:
- The
Materialof the stack.
-
name
Sets the display name of the item. Color codes using the ampersand (&) are translated, if you want to avoid this, you should wrap your name argument with aChatColor.stripColor(String)call.- Parameters:
name- The desired display name of the item stack.- Returns:
- The
ItemBuilderinstance.
-
getName
Returns either the display name of the item, if it exists, or null if it doesn't.
You should note that this method fetches the name directly from the stack'sItemMeta, so you should take extra care when comparing names with color codes - particularly if you used thename(String)method as they will be in their translated sectional symbol (§) form, rather than their 'coded' form (&).
For example, if you usedname(String)to set the name to '&cMy Item', the output of this method would be '§cMy Item'- Returns:
- The item's display name as returned from its
ItemMeta.
-
amount
Sets the amount of items in theItemStack.- Parameters:
amount- The new amount.- Returns:
- The
ItemBuilderinstance.
-
getAmount
public int getAmount()Returns the amount of items in theItemStack.- Returns:
- The amount of items in the stack.
-
lore
Sets the lore of the item. This method is a var-args alias for thelore(List)method.- Parameters:
lore- The desired lore of the item, with each line as a separate string.- Returns:
- The
ItemBuilderinstance.
-
lore
Sets the lore of the item. As withname(String), color codes will be replaced. Each string represents a line of the lore.
Lines will not be automatically wrapped or truncated, so it is recommended you take some consideration into how the item will be rendered with the lore.- Parameters:
lore- The desired lore of the item, with each line as a separate string.- Returns:
- The
ItemBuilderinstance.
-
getLore
Gets the lore of the item as a list of strings. Each string represents a line of the item's lore in-game.
As withname(String), it should be noted that color-coded lore lines will be returned with the colors codes already translated.- Returns:
- The lore of the item.
-
color
An alias fordurability(short)that takes anItemDataColoras an argument instead. This is to improve code readability when working with items such as glass panes, where the data value represents a glass pane's color.
This method will still be functional for items where the data value does not represent the item's color, however it will obviously be nonsensical.- Parameters:
color- The desired color of the item.- Returns:
- The
ItemBuilderinstance.
-
data
An alias fordurability(short).- Parameters:
data- The desired data-value (durability) of the item.- Returns:
- The
ItemBuilderinstance.
-
durability
Sets the durability (data value) of the item.- Parameters:
durability- The desired durability of the item.- Returns:
- The updated
ItemBuilderobject.
-
getDurability
public short getDurability()Returns the durability or data value of the item.- Returns:
- The durability of the item.
-
getColor
Essentially a proxy forItemDataColor.getByValue(short).
Similar togetDurability()however it returns the value as anItemDataColorwhere it is applicable, or null where it isn't.- Returns:
- The appropriate
ItemDataColorof the item or null.
-
enchant
Adds the specified enchantment to the stack.
This method usesItemStack.addUnsafeEnchantment(Enchantment, int)rather thanItemStack.addEnchantment(Enchantment, int)to avoid the associated checks of whether level is within the range for the enchantment.- Parameters:
enchantment- The enchantment to apply to the item.level- The level of the enchantment to apply to the item.- Returns:
- The
ItemBuilderinstance.
-
unenchant
Removes the specified enchantment from the stack.- Parameters:
enchantment- The enchantment to remove from the item.- Returns:
- The
ItemBuilderinstance.
-
flag
Accepts a variable number ofItemFlags to apply to the stack.- Parameters:
flag- A variable-length argument containing the flags to be applied.- Returns:
- The
ItemBuilderinstance.
-
deflag
Accepts a variable number ofItemFlags to remove from the stack.- Parameters:
flag- A variable-length argument containing the flags to be removed.- Returns:
- The
ItemBuilderinstance.
-
skullOwner
If the item hasSkullMeta(i.e. if the item is a skull), this can be used to set the skull's owner (i.e. the player the skull represents.)
This also sets the skull's data value to 3 for 'player head', as setting the skull's owner doesn't make much sense for the mob skulls.- Parameters:
name- The name of the player the skull item should resemble.- Returns:
- The
ItemBuilderinstance.
-
ifThen
This is used to, inline, perform an operation if a given condition is true.
TheItemBuilderinstance is supplied to both the predicate (condition) and result function. The result ofthenis ignored as the ItemBuilder reference is passed to it.
Example:// Renames the ItemStack, if and only if, the stack's type is Acacia Doors. ifThen(stack -> stack.getType() == Material.ACACIA_DOOR, stack -> stack.name("&aMagic Door"));- Parameters:
ifTrue- The condition upon which,thenshould be performed.then- The action to perform if the predicate,ifTrue, is true.- Returns:
- The
ItemBuilderinstance.
-
build
-
get
public org.bukkit.inventory.ItemStack get()Returns theItemStackthat theItemBuilderinstance represents.
The modifications are performed as they are called, so this method simply returns the class's private stack field.- Returns:
- The manipulated ItemStack.
-