Class ItemStackBuilder

java.lang.Object
de.eldoria.eldoutilities.builder.ItemStackBuilder

public final class ItemStackBuilder extends Object
A class which allows the creation of ItemStacks with a builder like pattern.
Since:
1.1.0
  • Constructor Details

    • ItemStackBuilder

      public ItemStackBuilder(ItemStack itemStack)
  • Method Details

    • of

      public static ItemStackBuilder of(Material material)
      Creates a new item stack builder
      Parameters:
      material - material of item stack
      Returns:
      builder instance
    • of

      public static ItemStackBuilder of(Material material, int amount)
      Creates a new item stack builder
      Parameters:
      material - material of item stack
      amount - size of item stack.
      Returns:
      builder instance
    • of

      public static ItemStackBuilder of(ItemStack stack)
      Load a item stack into a item stack builder.

      The item stack will be cloned to provide immutability.

      Parameters:
      stack - item stack to load
      Returns:
      builder instance
    • of

      public static ItemStackBuilder of(ItemStack stack, boolean clone)
      Load a item stack into a item stack builder.
      Parameters:
      stack - item stack to load
      clone - true if the item should be cloned
      Returns:
      builder instance
    • withMetaValue

      public ItemStackBuilder withMetaValue(Consumer<@NotNull ItemMeta> itemMetaConsumer)
      Applies an item meta consumer on the item meta, if a item meta is present.
      Parameters:
      itemMetaConsumer - consumer for non null item meta.
      Returns:
      builder instance
    • build

      public ItemStack build()
      Clone the underlying itemstack and return a cloned instance.
      Returns:
      new item stack instance
    • withEnchantments

      public ItemStackBuilder withEnchantments(@NotNull @NotNull Map<Enchantment,Integer> enchantments)
      Adds the specified enchantments to this item stack.

      This method is the same as calling withEnchantment(Enchantment, int) for each element of the map.

      Parameters:
      enchantments - Enchantments to add
      Returns:
      builder instance
      Throws:
      IllegalArgumentException - if the specified enchantments is null
      IllegalArgumentException - if any specific enchantment or level is null. Warning: Some enchantments may be added before this exception is thrown.
    • withEnchantment

      public ItemStackBuilder withEnchantment(@NotNull @NotNull Enchantment ench, int level)
      Adds the specified Enchantment to this item stack.

      If this item stack already contained the given enchantment (at any level), it will be replaced.

      Parameters:
      ench - Enchantment to add
      level - Level of the enchantment
      Returns:
      builder instance
      Throws:
      IllegalArgumentException - if enchantment null, or enchantment is not applicable
    • withUnsafeEnchantments

      public ItemStackBuilder withUnsafeEnchantments(@NotNull @NotNull Map<Enchantment,Integer> enchantments)
      Adds the specified enchantments to this item stack in an unsafe manner.

      This method is the same as calling withUnsafeEnchantment(Enchantment, int) for each element of the map.

      Parameters:
      enchantments - Enchantments to add
      Returns:
      builder instance
    • withUnsafeEnchantment

      public ItemStackBuilder withUnsafeEnchantment(@NotNull @NotNull Enchantment ench, int level)
      Adds the specified Enchantment to this item stack.

      If this item stack already contained the given enchantment (at any level), it will be replaced.

      This method is unsafe and will ignore level restrictions or item type. Use at your own discretion.

      Parameters:
      ench - Enchantment to add
      level - Level of the enchantment
      Returns:
      builder instance
    • ofAmount

      public ItemStackBuilder ofAmount(int amount)
      Sets the amount of items in this stack
      Parameters:
      amount - New amount of items in this stack
      Returns:
      builder instance
    • withDisplayName

      public ItemStackBuilder withDisplayName(@Nullable @Nullable String name)
      Sets the display name.
      Parameters:
      name - the name to set
      Returns:
      builder instance
    • withLocalizedName

      public ItemStackBuilder withLocalizedName(@Nullable @Nullable String name)
      Sets the localized name.
      Parameters:
      name - the name to set
      Returns:
      builder instance
    • withLore

      public ItemStackBuilder withLore(@Nullable @Nullable List<String> lore)
      Sets the lore for this item.
      Parameters:
      lore - the lore that will be set
      Returns:
      builder instance
    • withLore

      public ItemStackBuilder withLore(String... lore)
      Sets the lore for this item.
      Parameters:
      lore - the lore that will be set
      Returns:
      builder instance
    • withCustomModelData

      public ItemStackBuilder withCustomModelData(@Nullable @Nullable Integer data)
      Sets the custom model data.

      CustomModelData is an integer that may be associated client side with a custom item model.

      Parameters:
      data - the data to set, or null to clear
      Returns:
      builder instance
    • withEnchant

      public ItemStackBuilder withEnchant(@NotNull @NotNull Enchantment ench, int level, boolean ignoreLevelRestriction)
      Adds the specified enchantment to this item meta.
      Parameters:
      ench - Enchantment to add
      level - Level for the enchantment
      ignoreLevelRestriction - this indicates the enchantment should be applied, ignoring the level limit
      Returns:
      builder instance
    • withItemFlags

      public ItemStackBuilder withItemFlags(@NotNull @NotNull ItemFlag... itemFlags)
      Set itemflags which should be ignored when rendering a ItemStack in the Client. This Method does silently ignore double set itemFlags.
      Parameters:
      itemFlags - The hideflags which shouldn't be rendered
      Returns:
      builder instance
    • asUnbreakable

      public ItemStackBuilder asUnbreakable()
      Sets the unbreakable tag. An unbreakable item will not lose durability.
      Returns:
      builder instance
    • asBreakable

      public ItemStackBuilder asBreakable()
      Sets the breakable tag. An breakable item will lose durability. This is the default value.
      Returns:
      builder instance
    • withAttributeModifier

      public ItemStackBuilder withAttributeModifier(@NotNull @NotNull Attribute attribute, @NotNull @NotNull AttributeModifier modifier)
      Add an Attribute and it's Modifier. AttributeModifiers can now support EquipmentSlots. If not set, the AttributeModifier will be active in ALL slots.
      Two AttributeModifiers that have the same UUID cannot exist on the same Attribute.
      Parameters:
      attribute - the Attribute to modify
      modifier - the AttributeModifier specifying the modification
      Returns:
      builder instance
      Throws:
      NullPointerException - if Attribute is null
      NullPointerException - if AttributeModifier is null
      IllegalArgumentException - if AttributeModifier already exists
    • withDurability

      public ItemStackBuilder withDurability(int damage)
      Sets the damage
      Parameters:
      damage - item damage
      Returns:
      builder instance
    • withNBTData

      public ItemStackBuilder withNBTData(Consumer<@NotNull PersistentDataContainer> dataConsumer)
      Applies a consumer on the PersistentDataHolder.getPersistentDataContainer(). The consumer will only be executed if a item meta is present
      Parameters:
      dataConsumer - consumer for data container
      Returns:
      builder instance
    • withMetaValue

      public <T extends ItemMeta> ItemStackBuilder withMetaValue(Class<T> clazz, Consumer<@NotNull T> consumer)
      Casts the item meta to the defined class if possible.

      Will apply the consumer on the meta if the cast was successful.

      Type Parameters:
      T - type of meta
      Parameters:
      clazz - class of item meta
      consumer - consumer for item meta
      Returns:
      builder instance