Resource ItemBuilder

Discussion in 'Spigot Plugin Development' started by NexoNixxx, Sep 16, 2018 at 4:32 PM.

  1. Hey Guys,

    This is a example ItemBuilder class that you can use for your own projects. I think the class includes the most important things, which you need to build cool items. Have fun!
    Code (Java):
    public class ItemBuilder {

        private ItemStack item;
        private ItemMeta itemMeta;
     
     
        public ItemBuilder(Material material) {
            item = new ItemStack(material);
            itemMeta = item.getItemMeta();
        }
     
        public ItemBuilder(Material material, short subID) {
            item = new ItemStack(material, 1, subID);
            itemMeta = item.getItemMeta();
         
        }
        public ItemBuilder setDisplayName(String name) {
            itemMeta.setDisplayName(name);
            return this;
        }
        public ItemBuilder setEnchantment(Enchantment enchantment, int level, Boolean bool) {
            itemMeta.addEnchant(enchantment, level, bool);
            return this;
        }
        public ItemBuilder addItemFlag(ItemFlag itemFlag) {
            itemMeta.addItemFlags(itemFlag);
            return this;
        }
        public ItemStack build() {
            item.setItemMeta(itemMeta);
            return item;
        }

     
     
    }
    I hope it's useful for you. If you think a method is missing, please comment.
     
    #1 NexoNixxx, Sep 16, 2018 at 4:32 PM
    Last edited: Sep 16, 2018 at 6:31 PM
    • Like Like x 1
  2. There is a lot of things missing, but most importantly setDisPlayName?!?
     
    • Funny Funny x 2
    • Agree Agree x 1
  3. I'm avidly happy that Kotlin has <T>T?#apply for stuff like this..
     
    • Agree Agree x 1
  4. Kotlin is so amazing, kotlin so nice. If it were a woman i would marry it.

    TL;DR Kotlin is amazing.
     
    • Funny Funny x 1
  5. There could be a possibility to add lore text, set the text of a book, or just dye armor.
     
  6. Ok, I will do it. Thanks for the suggestion.
     
    #6 NexoNixxx, Sep 16, 2018 at 8:39 PM
    Last edited: Sep 16, 2018 at 8:55 PM

Share This Page