3838import java .util .function .Consumer ;
3939
4040/**
41- * Simple {@link ItemStack} builder
41+ * Simple {@link ItemStack} builder.
4242 *
4343 * @author MrMicky
4444 */
4545public class ItemBuilder {
4646
4747 private final ItemStack item ;
48- private final ItemMeta meta ;
48+
49+ public static ItemBuilder copyOf (ItemStack item ) {
50+ return new ItemBuilder (item .clone ());
51+ }
4952
5053 public ItemBuilder (Material material ) {
5154 this (new ItemStack (material ));
5255 }
5356
5457 public ItemBuilder (ItemStack item ) {
5558 this .item = Objects .requireNonNull (item , "item" );
56- this .meta = item .getItemMeta ();
59+ }
60+
61+ public ItemBuilder edit (Consumer <ItemStack > function ) {
62+ function .accept (this .item );
63+ return this ;
64+ }
65+
66+ public ItemBuilder meta (Consumer <ItemMeta > metaConsumer ) {
67+ return edit (item -> {
68+ ItemMeta meta = item .getItemMeta ();
5769
58- if (this .meta == null ) {
59- throw new IllegalArgumentException ("The type " + item .getType () + " doesn't support item meta" );
60- }
70+ if (meta != null ) {
71+ metaConsumer .accept (meta );
72+ item .setItemMeta (meta );
73+ }
74+ });
75+ }
76+
77+ public <T extends ItemMeta > ItemBuilder meta (Class <T > metaClass , Consumer <T > metaConsumer ) {
78+ return meta (meta -> {
79+ if (metaClass .isInstance (meta )) {
80+ metaConsumer .accept (metaClass .cast (meta ));
81+ }
82+ });
6183 }
6284
6385 public ItemBuilder type (Material material ) {
64- this .item .setType (material );
65- return this ;
86+ return edit (item -> item .setType (material ));
6687 }
6788
6889 public ItemBuilder data (int data ) {
@@ -71,49 +92,31 @@ public ItemBuilder data(int data) {
7192
7293 @ SuppressWarnings ("deprecation" )
7394 public ItemBuilder durability (short durability ) {
74- this .item .setDurability (durability );
75- return this ;
95+ return edit (item -> item .setDurability (durability ));
7696 }
7797
7898 public ItemBuilder amount (int amount ) {
79- this .item .setAmount (amount );
80- return this ;
99+ return edit (item -> item .setAmount (amount ));
81100 }
82101
83102 public ItemBuilder enchant (Enchantment enchantment ) {
84103 return enchant (enchantment , 1 );
85104 }
86105
87106 public ItemBuilder enchant (Enchantment enchantment , int level ) {
88- this .meta .addEnchant (enchantment , level , true );
89- return this ;
107+ return meta (meta -> meta .addEnchant (enchantment , level , true ));
90108 }
91109
92110 public ItemBuilder removeEnchant (Enchantment enchantment ) {
93- this .meta .removeEnchant (enchantment );
94- return this ;
111+ return meta (meta -> meta .removeEnchant (enchantment ));
95112 }
96113
97114 public ItemBuilder removeEnchants () {
98- this .meta .getEnchants ().keySet ().forEach (this .meta ::removeEnchant );
99- return this ;
100- }
101-
102- public ItemBuilder meta (Consumer <ItemMeta > metaConsumer ) {
103- metaConsumer .accept (this .meta );
104- return this ;
105- }
106-
107- public <T extends ItemMeta > ItemBuilder meta (Class <T > metaClass , Consumer <T > metaConsumer ) {
108- if (metaClass .isInstance (this .meta )) {
109- metaConsumer .accept (metaClass .cast (this .meta ));
110- }
111- return this ;
115+ return meta (m -> m .getEnchants ().keySet ().forEach (m ::removeEnchant ));
112116 }
113117
114118 public ItemBuilder name (String name ) {
115- this .meta .setDisplayName (name );
116- return this ;
119+ return meta (meta -> meta .setDisplayName (name ));
117120 }
118121
119122 public ItemBuilder lore (String lore ) {
@@ -125,60 +128,62 @@ public ItemBuilder lore(String... lore) {
125128 }
126129
127130 public ItemBuilder lore (List <String > lore ) {
128- this .meta .setLore (lore );
129- return this ;
131+ return meta (meta -> meta .setLore (lore ));
130132 }
131133
132134 public ItemBuilder addLore (String line ) {
133- List <String > lore = this .meta .getLore ();
135+ return meta (meta -> {
136+ List <String > lore = meta .getLore ();
134137
135- if (lore == null ) {
136- return lore (line );
137- }
138+ if (lore == null ) {
139+ meta .setLore (Collections .singletonList (line ));
140+ return ;
141+ }
138142
139- lore .add (line );
140- return lore (lore );
143+ lore .add (line );
144+ meta .setLore (lore );
145+ });
141146 }
142147
143148 public ItemBuilder addLore (String ... lines ) {
144149 return addLore (Arrays .asList (lines ));
145150 }
146151
147152 public ItemBuilder addLore (List <String > lines ) {
148- List <String > lore = this .meta .getLore ();
153+ return meta (meta -> {
154+ List <String > lore = meta .getLore ();
149155
150- if (lore == null ) {
151- return lore (lines );
152- }
156+ if (lore == null ) {
157+ meta .setLore (lines );
158+ return ;
159+ }
153160
154- lore .addAll (lines );
155- return lore (lore );
161+ lore .addAll (lines );
162+ meta .setLore (lore );
163+ });
156164 }
157165
158166 public ItemBuilder flags (ItemFlag ... flags ) {
159- this .meta .addItemFlags (flags );
160- return this ;
167+ return meta (meta -> meta .addItemFlags (flags ));
161168 }
162169
163170 public ItemBuilder flags () {
164171 return flags (ItemFlag .values ());
165172 }
166173
167174 public ItemBuilder removeFlags (ItemFlag ... flags ) {
168- this .meta .removeItemFlags (flags );
169- return this ;
175+ return meta (meta -> meta .removeItemFlags (flags ));
170176 }
171177
172178 public ItemBuilder removeFlags () {
173179 return removeFlags (ItemFlag .values ());
174180 }
175181
176182 public ItemBuilder armorColor (Color color ) {
177- return meta (LeatherArmorMeta .class , m -> m .setColor (color ));
183+ return meta (LeatherArmorMeta .class , meta -> meta .setColor (color ));
178184 }
179185
180186 public ItemStack build () {
181- this .item .setItemMeta (this .meta );
182187 return this .item ;
183188 }
184189}
0 commit comments