Class ItemBuilder

java.lang.Object
wtf.choco.arrows.utils.ItemBuilder

public final class ItemBuilder
extends Object
A utility class to assist in the creation of ItemStacks in the confines of a single line.
Author:
Parker Hawke - Choco
  • Method Summary

    Modifier and Type Method Description
    @NotNull ItemBuilder amount​(int amount)
    Set the item amount.
    @NotNull ItemBuilder attribute​(@NotNull org.bukkit.attribute.Attribute attribute, @NotNull org.bukkit.attribute.AttributeModifier modifier)
    Add an attribute modifier to this item.
    @NotNull org.bukkit.inventory.ItemStack build()
    Complete the building of this ItemBuilder and return the resulting ItemStack.
    @NotNull ItemBuilder damage​(int damage)
    Set the item damage.
    @NotNull ItemBuilder enchantment​(@NotNull org.bukkit.enchantments.Enchantment enchantment, int level)
    Apply an enchantment with the specified level to the item.
    @NotNull ItemBuilder flags​(@NotNull org.bukkit.inventory.ItemFlag... flags)
    Apply flags to the item.
    boolean isSupportedMeta​(@NotNull Class<? extends org.bukkit.inventory.meta.ItemMeta> type)
    Check whether the specified type of ItemMeta is supported by this ItemBuilder.
    @NotNull ItemBuilder localizedName​(@Nullable String name)
    Set the item's localized name.
    @NotNull ItemBuilder lore​(@NotNull String... lore)
    Set the item lore in the form of varargs.
    @NotNull ItemBuilder lore​(@Nullable List<String> lore)
    Set the item lore in the form of a List<String>.
    @NotNull ItemBuilder modelData​(int data)
    Set the item's custom model data flag.
    static @NotNull ItemBuilder modify​(@NotNull org.bukkit.inventory.ItemStack item)
    Get a new instance of ItemBuilder to modify an existing ItemStack.
    @NotNull ItemBuilder name​(@Nullable String name)
    Set the item name.
    static @NotNull ItemBuilder of​(@NotNull org.bukkit.Material type)
    Get a new instance of an ItemBuilder given a (non-null and non-air) Material.
    static @NotNull ItemBuilder of​(@NotNull org.bukkit.Material type, int amount)
    Get a new instance of an ItemBuilder given a (non-null and non-air) Material and a quantity greater than 0 and less than or equal to Material.getMaxStackSize().
    <T extends org.bukkit.inventory.meta.ItemMeta>
    @NotNull ItemBuilder
    specific​(@NotNull Class<T> type, @NotNull Consumer<T> applier)
    Apply a method from a more specific type of ItemMeta to this ItemBuilder instance.
    @NotNull ItemBuilder unbreakable()
    Set the unbreakable state of this item to true.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • of

      @NotNull public static @NotNull ItemBuilder of​(@NotNull @NotNull org.bukkit.Material type, int amount)
      Get a new instance of an ItemBuilder given a (non-null and non-air) Material and a quantity greater than 0 and less than or equal to Material.getMaxStackSize().
      Parameters:
      type - the type of item to build
      amount - the item amount
      Returns:
      the ItemBuilder instance for the provided values
    • of

      @NotNull public static @NotNull ItemBuilder of​(@NotNull @NotNull org.bukkit.Material type)
      Get a new instance of an ItemBuilder given a (non-null and non-air) Material.
      Parameters:
      type - the type of item to build
      Returns:
      the ItemBuilder instance for the provided material
    • modify

      @NotNull public static @NotNull ItemBuilder modify​(@NotNull @NotNull org.bukkit.inventory.ItemStack item)
      Get a new instance of ItemBuilder to modify an existing ItemStack. The ItemStack passed will be cloned, therefore the passed reference will not be modified, but rather a copy of it. The result of build() will be a separate item with the changes applied from this builder instance. The provided item acts as a base for the values in this builder.
      Parameters:
      item - the item to build
      Returns:
      the ItemBuilder instance for the provided item
    • isSupportedMeta

      public boolean isSupportedMeta​(@NotNull @NotNull Class<? extends org.bukkit.inventory.meta.ItemMeta> type)
      Check whether the specified type of ItemMeta is supported by this ItemBuilder.
      Parameters:
      type - the type of meta to check
      Returns:
      true if supported, false otherwise or if null
    • specific

      @NotNull public <T extends org.bukkit.inventory.meta.ItemMeta> @NotNull ItemBuilder specific​(@NotNull @NotNull Class<T> type, @NotNull @NotNull Consumer<T> applier)
      Apply a method from a more specific type of ItemMeta to this ItemBuilder instance. If the type provided is unsupported by this ItemBuilder (according to isSupportedMeta(Class)), this method will throw an exception, therefore it is recommended that it be checked before invoking this method if you are unsure as to what is and is not supported.
      Type Parameters:
      T - The ItemMeta type to be applied in the consumer function
      Parameters:
      type - the type of ItemMeta to apply
      applier - the function to apply to the ItemMeta instance
      Returns:
      this instance. Allows for chained method calls
    • name

      @NotNull public @NotNull ItemBuilder name​(@Nullable @Nullable String name)
      Set the item name.
      Parameters:
      name - the name to set
      Returns:
      this instance. Allows for chained method calls
      See Also:
      ItemMeta.setDisplayName(String)
    • lore

      @NotNull public @NotNull ItemBuilder lore​(@NotNull @NotNull String... lore)
      Set the item lore in the form of varargs.
      Parameters:
      lore - the lore to set
      Returns:
      this instance. Allows for chained method calls
      See Also:
      lore(List), ItemMeta.setLore(List)
    • lore

      @NotNull public @NotNull ItemBuilder lore​(@Nullable @Nullable List<String> lore)
      Set the item lore in the form of a List<String>.
      Parameters:
      lore - the lore to set
      Returns:
      this instance. Allows for chained method calls
      See Also:
      lore(String...), ItemMeta.setLore(List)
    • damage

      @NotNull public @NotNull ItemBuilder damage​(int damage)
      Set the item damage. Some items may not display damage or accept the damage attribute at all, in which case this method will simply fail silently.
      Parameters:
      damage - the damage to set
      Returns:
      this instance. Allows for chained method calls
      See Also:
      Damageable.setDamage(int)
    • amount

      @NotNull public @NotNull ItemBuilder amount​(int amount)
      Set the item amount. This damage must range between 1 and Material.getMaxStackSize() according to the type being built in this ItemBuilder instance.
      Parameters:
      amount - the amount to set
      Returns:
      this instance. Allows for chained method calls
      See Also:
      ItemStack.setAmount(int)
    • enchantment

      @NotNull public @NotNull ItemBuilder enchantment​(@NotNull @NotNull org.bukkit.enchantments.Enchantment enchantment, int level)
      Apply an enchantment with the specified level to the item. This method does not respect the level limitations of an enchantment (i.e. Sharpness VI may be applied if desired).
      Parameters:
      enchantment - the enchantment to add
      level - the enchantment level to set
      Returns:
      this instance. Allows for chained method calls
      See Also:
      ItemMeta.addEnchant(Enchantment, int, boolean)
    • attribute

      @NotNull public @NotNull ItemBuilder attribute​(@NotNull @NotNull org.bukkit.attribute.Attribute attribute, @NotNull @NotNull org.bukkit.attribute.AttributeModifier modifier)
      Add an attribute modifier to this item.
      Parameters:
      attribute - the attribute for which to add a modifier
      modifier - the modifier to apply
      Returns:
      this instance. Allows for chained method calls
    • flags

      @NotNull public @NotNull ItemBuilder flags​(@NotNull @NotNull org.bukkit.inventory.ItemFlag... flags)
      Apply flags to the item.
      Parameters:
      flags - the flags to set
      Returns:
      this instance. Allows for chained method calls
      See Also:
      ItemMeta.addItemFlags(ItemFlag...)
    • unbreakable

      @NotNull public @NotNull ItemBuilder unbreakable()
      Set the unbreakable state of this item to true.
      Returns:
      this instance. Allows for chained method calls
      See Also:
      ItemMeta.setUnbreakable(boolean)
    • localizedName

      @NotNull public @NotNull ItemBuilder localizedName​(@Nullable @Nullable String name)
      Set the item's localized name.
      Parameters:
      name - the localized name to set
      Returns:
      this instance. Allows for chained method calls
      See Also:
      ItemMeta.setLocalizedName(String)
    • modelData

      @NotNull public @NotNull ItemBuilder modelData​(int data)
      Set the item's custom model data flag.
      Parameters:
      data - the data to set
      Returns:
      this instance. Allows for chained method calls
      See Also:
      ItemMeta.setCustomModelData(Integer)
    • build

      @NotNull public @NotNull org.bukkit.inventory.ItemStack build()
      Complete the building of this ItemBuilder and return the resulting ItemStack.
      Returns:
      the completed ItemStack instance built by this builder