Skip to content

Commit 0141bf7

Browse files
committed
improvement: split ItemBuilder to a version with HeadDatabase
1 parent 56d20db commit 0141bf7

3 files changed

Lines changed: 339 additions & 15 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>de.codeblocksmc.codelib</groupId>
88
<artifactId>CodeLib</artifactId>
9-
<version>3.6.0</version> <!-- When uploading, please also use LATEST -->
9+
<version>3.6.1</version> <!-- When uploading, please also use LATEST -->
1010
<!-- 3.4.0 -->
1111
<packaging>jar</packaging>
1212

Lines changed: 338 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,338 @@
1+
package de.codeblocksmc.codelib.wrapping;
2+
3+
import com.destroystokyo.paper.profile.PlayerProfile;
4+
import me.arcaniax.hdb.api.HeadDatabaseAPI;
5+
import net.kyori.adventure.text.Component;
6+
import org.bukkit.Color;
7+
import org.bukkit.Material;
8+
import org.bukkit.MusicInstrument;
9+
import org.bukkit.enchantments.Enchantment;
10+
import org.bukkit.entity.Axolotl;
11+
import org.bukkit.inventory.ItemFlag;
12+
import org.bukkit.inventory.ItemStack;
13+
import org.bukkit.inventory.meta.*;
14+
import org.bukkit.inventory.meta.trim.ArmorTrim;
15+
import org.bukkit.potion.PotionEffect;
16+
17+
import java.util.Arrays;
18+
import java.util.List;
19+
import java.util.Objects;
20+
21+
/**
22+
* A utility class for creating and modifying {@link ItemStack} instances with a fluent API.
23+
*
24+
* <p>This class allows easy configuration of items with custom properties such as display names,
25+
* lore, enchantments, item flags, and more.</p>
26+
*
27+
* @author JustCody
28+
* @version 1.1
29+
*/
30+
public class HItemBuilder {
31+
32+
private final HeadDatabaseAPI hapi = new HeadDatabaseAPI();
33+
34+
private ItemStack stack;
35+
private ItemMeta meta;
36+
37+
private PotionMeta potionMeta;
38+
39+
/**
40+
* Constructs an {@link HItemBuilder} with the specified {@link Material}.
41+
*
42+
* @param material The {@link Material} for the item.
43+
*
44+
* Note: Material with type AIR can be used, but may cause bugs.
45+
*/
46+
public HItemBuilder(Material material) {
47+
Objects.requireNonNull(material, "Material cannot be null.");
48+
if (material == Material.AIR) {
49+
this.stack = new ItemStack(material);
50+
return;
51+
}
52+
this.stack = new ItemStack(material);
53+
this.meta = stack.getItemMeta();
54+
if (material.equals(Material.POTION) || material.equals(Material.SPLASH_POTION) || material.equals(Material.LINGERING_POTION)) {
55+
potionMeta = (PotionMeta) stack.getItemMeta();
56+
}
57+
}
58+
59+
/**
60+
* Sets the display name of the item.
61+
*
62+
* @param name The new display name.
63+
* @return This builder instance.
64+
*/
65+
public HItemBuilder displayname(String name) {
66+
meta.setDisplayName(name);
67+
return this;
68+
}
69+
70+
public HItemBuilder material(Material material) {
71+
if (material == Material.AIR) throw new IllegalArgumentException("Material cannot be null or AIR.");
72+
stack = new ItemStack(material);
73+
meta = stack.getItemMeta();
74+
return this;
75+
}
76+
77+
/**
78+
* Sets the item to be unbreakable.
79+
*
80+
* @param unbreakable Whether the item is unbreakable.
81+
* @return This builder instance.
82+
*/
83+
public HItemBuilder unbreakable(boolean unbreakable) {
84+
meta.setUnbreakable(unbreakable);
85+
return this;
86+
}
87+
88+
/**
89+
* Sets the lore of the item.
90+
*
91+
* @param lore The lore lines to set.
92+
* @return This builder instance.
93+
*/
94+
public HItemBuilder lore(String... lore) {
95+
meta.setLore(Arrays.asList(lore));
96+
return this;
97+
}
98+
99+
/**
100+
* Sets the lore of the item.
101+
*
102+
* @param lore The lore lines as a list.
103+
* @return This builder instance.
104+
*/
105+
public HItemBuilder lore(List<String> lore) {
106+
meta.setLore(lore);
107+
return this;
108+
}
109+
110+
/**
111+
* Sets custom model data for the item.
112+
*
113+
* @param data The custom model data.
114+
* @return This builder instance.
115+
*/
116+
public HItemBuilder customModelData(int data) {
117+
meta.setCustomModelData(data);
118+
return this;
119+
}
120+
121+
/**
122+
* Adds a single {@link ItemFlag} to the item.
123+
*
124+
* @param flag The flag to add.
125+
* @return This builder instance.
126+
*/
127+
public HItemBuilder flag(ItemFlag flag) {
128+
meta.addItemFlags(flag);
129+
return this;
130+
}
131+
132+
/**
133+
* Adds multiple {@link ItemFlag}s to the item.
134+
*
135+
* @param flags The flags to add.
136+
* @return This builder instance.
137+
*/
138+
public HItemBuilder flags(ItemFlag... flags) {
139+
meta.addItemFlags(flags);
140+
return this;
141+
}
142+
143+
/**
144+
* Adds an {@link Enchantment} to the item.
145+
*
146+
* @param enchantment The enchantment to add.
147+
* @param level The level of the enchantment.
148+
* @return This builder instance.
149+
*/
150+
public HItemBuilder enchant(Enchantment enchantment, int level) {
151+
meta.addEnchant(enchantment, level, true);
152+
return this;
153+
}
154+
155+
/**
156+
* Sets the amount of the item stack.
157+
*
158+
* @param amount The new amount.
159+
* @return This builder instance.
160+
*/
161+
public HItemBuilder amount(int amount) {
162+
stack.setAmount(amount);
163+
return this;
164+
}
165+
166+
/**
167+
* Applies a player profile to the item (for player heads).
168+
*
169+
* @param profile The {@link PlayerProfile} to set.
170+
* @return This builder instance.
171+
*/
172+
public HItemBuilder owner(PlayerProfile profile) {
173+
if (meta instanceof SkullMeta skullMeta) {
174+
skullMeta.setPlayerProfile(profile);
175+
}
176+
return this;
177+
}
178+
179+
/**
180+
* Sets an armor trim for the item.
181+
*
182+
* @param trim The {@link ArmorTrim} to apply.
183+
* @return This builder instance.
184+
*/
185+
public HItemBuilder armorTrim(ArmorTrim trim) {
186+
if (meta instanceof ArmorMeta armorMeta) {
187+
armorMeta.setTrim(trim);
188+
}
189+
return this;
190+
}
191+
192+
/**
193+
* Sets a leather color for leather armor.
194+
*
195+
* @param color The {@link Color} to apply.
196+
* @return This builder instance.
197+
*/
198+
public HItemBuilder leatherColor(Color color) {
199+
if (meta instanceof LeatherArmorMeta leatherArmorMeta) {
200+
leatherArmorMeta.setColor(color);
201+
}
202+
return this;
203+
}
204+
205+
/**
206+
* Sets a color for potions.
207+
*
208+
* @param color The {@link Color} to apply.
209+
* @return This builder instance.
210+
*/
211+
public HItemBuilder potionColor(Color color) {
212+
potionMeta.setColor(color);
213+
stack.setItemMeta(potionMeta);
214+
return this;
215+
}
216+
217+
/**
218+
* Adds a potion effect to the item (if it's a potion).
219+
*
220+
* @param effect The {@link PotionEffect} to add.
221+
* @return This builder instance.
222+
*/
223+
public HItemBuilder potionEffect(PotionEffect effect) {
224+
potionMeta.addCustomEffect(effect, true);
225+
stack.setItemMeta(potionMeta);
226+
return this;
227+
}
228+
229+
/**
230+
* Adds multiple potion effects to the item (if it's a potion).
231+
*
232+
* @param effects The potion effects to add.
233+
* @return This builder instance.
234+
*/
235+
public HItemBuilder potionEffects(PotionEffect... effects) {
236+
for (PotionEffect effect : effects) {
237+
potionMeta.addCustomEffect(effect, true);
238+
}
239+
stack.setItemMeta(potionMeta);
240+
return this;
241+
}
242+
243+
/**
244+
* Sets the instrument for a goat horn.
245+
*
246+
* @param instrument The {@link MusicInstrument} to set.
247+
* @return This builder instance.
248+
*/
249+
public HItemBuilder musicInstrument(MusicInstrument instrument) {
250+
if (meta instanceof MusicInstrumentMeta musicMeta) {
251+
musicMeta.setInstrument(instrument);
252+
}
253+
return this;
254+
}
255+
256+
/**
257+
* Sets the title of a book.
258+
*
259+
* @param title The title to set.
260+
* @return This builder instance.
261+
*/
262+
public HItemBuilder bookTitle(String title) {
263+
if (meta instanceof BookMeta bookMeta) {
264+
bookMeta.setTitle(title);
265+
}
266+
return this;
267+
}
268+
269+
/**
270+
* Sets the author of a book.
271+
*
272+
* @param author The author to set.
273+
* @return This builder instance.
274+
*/
275+
public HItemBuilder bookAuthor(String author) {
276+
if (meta instanceof BookMeta bookMeta) {
277+
bookMeta.setAuthor(author);
278+
}
279+
return this;
280+
}
281+
282+
/**
283+
* Adds a page to a book.
284+
*
285+
* @param page The content of the page.
286+
* @return This builder instance.
287+
*/
288+
public HItemBuilder bookPage(String page) {
289+
if (meta instanceof BookMeta bookMeta) {
290+
bookMeta.addPages(Component.text(page));
291+
}
292+
return this;
293+
}
294+
295+
/**
296+
* Sets the axolotl variant for axolotl buckets.
297+
*
298+
* @param variant The {@link Axolotl.Variant} to set.
299+
* @return This builder instance.
300+
*/
301+
public HItemBuilder axolotl(Axolotl.Variant variant) {
302+
if (meta instanceof AxolotlBucketMeta axolotlMeta) {
303+
axolotlMeta.setVariant(variant);
304+
}
305+
return this;
306+
}
307+
308+
public HItemBuilder hdb(String id) {
309+
ItemStack s = hapi.getItemHead(id);
310+
PlayerProfile pr = ((SkullMeta) s.getItemMeta()).getPlayerProfile();
311+
if (!(meta instanceof SkullMeta)) throw new IllegalStateException("The item must be a PLAYER_HEAD.");
312+
((SkullMeta) meta).setPlayerProfile(pr);
313+
stack.setItemMeta(meta);
314+
return this;
315+
}
316+
317+
/**
318+
* Builds the item and returns the {@link ItemStack}.
319+
*
320+
* @return The built {@link ItemStack}.
321+
*/
322+
public ItemStack build() {
323+
stack.setItemMeta(meta);
324+
Material material = stack.getType();
325+
if (material.equals(Material.POTION) || material.equals(Material.SPLASH_POTION) || material.equals(Material.LINGERING_POTION)) {
326+
potionMeta = (PotionMeta) stack.getItemMeta();
327+
}
328+
return stack;
329+
}
330+
331+
public Material getMaterial() {
332+
return stack.getType();
333+
}
334+
335+
public int getAmount() {
336+
return stack.getAmount();
337+
}
338+
}

src/main/java/de/codeblocksmc/codelib/wrapping/ItemBuilder.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
package de.codeblocksmc.codelib.wrapping;
22

33
import com.destroystokyo.paper.profile.PlayerProfile;
4-
import me.arcaniax.hdb.api.HeadDatabaseAPI;
54
import net.kyori.adventure.text.Component;
6-
import org.bukkit.Bukkit;
75
import org.bukkit.Color;
86
import org.bukkit.Material;
97
import org.bukkit.MusicInstrument;
108
import org.bukkit.enchantments.Enchantment;
119
import org.bukkit.entity.Axolotl;
12-
import org.bukkit.entity.Player;
1310
import org.bukkit.inventory.ItemFlag;
1411
import org.bukkit.inventory.ItemStack;
1512
import org.bukkit.inventory.meta.*;
@@ -31,8 +28,6 @@
3128
*/
3229
public class ItemBuilder {
3330

34-
private HeadDatabaseAPI hapi = new HeadDatabaseAPI();
35-
3631
private ItemStack stack;
3732
private ItemMeta meta;
3833

@@ -307,15 +302,6 @@ public ItemBuilder axolotl(Axolotl.Variant variant) {
307302
return this;
308303
}
309304

310-
public ItemBuilder hdb(String id) {
311-
ItemStack s = hapi.getItemHead(id);
312-
PlayerProfile pr = ((SkullMeta) s.getItemMeta()).getPlayerProfile();
313-
if (!(meta instanceof SkullMeta)) throw new IllegalStateException("The item must be a PLAYER_HEAD.");
314-
((SkullMeta) meta).setPlayerProfile(pr);
315-
stack.setItemMeta(meta);
316-
return this;
317-
}
318-
319305
/**
320306
* Builds the item and returns the {@link ItemStack}.
321307
*

0 commit comments

Comments
 (0)