Class ItemBuilder

java.lang.Object
com.samjakob.spigui.item.ItemBuilder

public class ItemBuilder extends Object
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;
 new ItemBuilder(Material.SPONGE).name("&cAlmighty sponge").amount(21).build();
 
See Also:
  • ItemStack
  • Field Details

    • stack

      private final org.bukkit.inventory.ItemStack stack
      The item stack being built.
  • Constructor Details

    • ItemBuilder

      public ItemBuilder(org.bukkit.Material material)
      Creates an ItemStack and ItemBuilder wrapper for a new stack with the given type.
      Parameters:
      material - The Material to 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

      public ItemBuilder type(org.bukkit.Material material)
      Sets the type (Material) of the ItemStack.
      Parameters:
      material - The Material of the stack.
      Returns:
      The ItemBuilder instance.
    • getType

      public org.bukkit.Material getType()
      Returns the type (Material) of the ItemStack.
      Returns:
      The Material of the stack.
    • name

      public ItemBuilder name(String 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 a ChatColor.stripColor(String) call.
      Parameters:
      name - The desired display name of the item stack.
      Returns:
      The ItemBuilder instance.
    • getName

      public String 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's ItemMeta, so you should take extra care when comparing names with color codes - particularly if you used the name(String) method as they will be in their translated sectional symbol (§) form, rather than their 'coded' form (&).
      For example, if you used name(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

      public ItemBuilder amount(int amount)
      Sets the amount of items in the ItemStack.
      Parameters:
      amount - The new amount.
      Returns:
      The ItemBuilder instance.
    • getAmount

      public int getAmount()
      Returns the amount of items in the ItemStack.
      Returns:
      The amount of items in the stack.
    • lore

      public ItemBuilder lore(String... lore)
      Sets the lore of the item. This method is a var-args alias for the lore(List) method.
      Parameters:
      lore - The desired lore of the item, with each line as a separate string.
      Returns:
      The ItemBuilder instance.
    • lore

      public ItemBuilder lore(List<String> lore)
      Sets the lore of the item. As with name(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 ItemBuilder instance.
    • getLore

      public List<String> 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 with name(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

      public ItemBuilder color(ItemDataColor color)
      An alias for durability(short) that takes an ItemDataColor as 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 ItemBuilder instance.
    • data

      public ItemBuilder data(short data)
      An alias for durability(short).
      Parameters:
      data - The desired data-value (durability) of the item.
      Returns:
      The ItemBuilder instance.
    • durability

      public ItemBuilder durability(short durability)
      Sets the durability (data value) of the item.
      Parameters:
      durability - The desired durability of the item.
      Returns:
      The updated ItemBuilder object.
    • getDurability

      public short getDurability()
      Returns the durability or data value of the item.
      Returns:
      The durability of the item.
    • getColor

      public ItemDataColor getColor()
      Essentially a proxy for ItemDataColor.getByValue(short).
      Similar to getDurability() however it returns the value as an ItemDataColor where it is applicable, or null where it isn't.
      Returns:
      The appropriate ItemDataColor of the item or null.
    • enchant

      public ItemBuilder enchant(org.bukkit.enchantments.Enchantment enchantment, int level)
      Adds the specified enchantment to the stack.
      This method uses ItemStack.addUnsafeEnchantment(Enchantment, int) rather than ItemStack.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 ItemBuilder instance.
    • unenchant

      public ItemBuilder unenchant(org.bukkit.enchantments.Enchantment enchantment)
      Removes the specified enchantment from the stack.
      Parameters:
      enchantment - The enchantment to remove from the item.
      Returns:
      The ItemBuilder instance.
    • flag

      public ItemBuilder flag(org.bukkit.inventory.ItemFlag... flag)
      Accepts a variable number of ItemFlags to apply to the stack.
      Parameters:
      flag - A variable-length argument containing the flags to be applied.
      Returns:
      The ItemBuilder instance.
    • deflag

      public ItemBuilder deflag(org.bukkit.inventory.ItemFlag... flag)
      Accepts a variable number of ItemFlags to remove from the stack.
      Parameters:
      flag - A variable-length argument containing the flags to be removed.
      Returns:
      The ItemBuilder instance.
    • skullOwner

      public ItemBuilder skullOwner(String name)
      If the item has SkullMeta (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 ItemBuilder instance.
    • ifThen

      public ItemBuilder ifThen(Predicate<ItemBuilder> ifTrue, Function<ItemBuilder,Object> then)
      This is used to, inline, perform an operation if a given condition is true.
      The ItemBuilder instance is supplied to both the predicate (condition) and result function. The result of then is 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, then should be performed.
      then - The action to perform if the predicate, ifTrue, is true.
      Returns:
      The ItemBuilder instance.
    • build

      public org.bukkit.inventory.ItemStack build()
      An alias for get().
      Returns:
      See get().
    • get

      public org.bukkit.inventory.ItemStack get()
      Returns the ItemStack that the ItemBuilder instance represents.
      The modifications are performed as they are called, so this method simply returns the class's private stack field.
      Returns:
      The manipulated ItemStack.