-
-
Notifications
You must be signed in to change notification settings - Fork 131
/
ItemBuilder.java
297 lines (239 loc) · 9.59 KB
/
ItemBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package com.bgsoftware.superiorskyblock.utils.items;
import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin;
import com.bgsoftware.superiorskyblock.api.service.placeholders.PlaceholdersService;
import com.bgsoftware.superiorskyblock.api.wrappers.SuperiorPlayer;
import com.bgsoftware.superiorskyblock.formatting.Formatters;
import com.bgsoftware.superiorskyblock.utils.ServerVersion;
import com.bgsoftware.superiorskyblock.utils.legacy.Materials;
import org.bukkit.ChatColor;
import org.bukkit.Color;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.LeatherArmorMeta;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.inventory.meta.SpawnEggMeta;
import org.bukkit.potion.PotionEffect;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
@SuppressWarnings({"WeakerAccess", "UnusedReturnValue"})
public final class ItemBuilder {
private static final SuperiorSkyblockPlugin plugin = SuperiorSkyblockPlugin.getPlugin();
private ItemStack itemStack;
@Nullable
private ItemMeta itemMeta;
private String textureValue = "";
public ItemBuilder(ItemStack itemStack) {
this(itemStack.getType(), itemStack.getDurability());
this.itemMeta = itemStack.getItemMeta().clone();
}
public ItemBuilder(Material type) {
this(type, 0);
}
public ItemBuilder(Material type, int damage) {
itemStack = new ItemStack(type, 1, (short) damage);
itemMeta = itemStack.getItemMeta();
}
public ItemBuilder withType(Material type) {
this.itemStack.setType(type);
return this;
}
public ItemBuilder withDurablity(short durability) {
if (durability >= 0)
this.itemStack.setDurability(durability);
return this;
}
public ItemBuilder withAmount(int amount) {
if (amount >= 1 && amount <= itemStack.getMaxStackSize())
itemStack.setAmount(amount);
return this;
}
public ItemBuilder asSkullOf(SuperiorPlayer superiorPlayer) {
if (itemStack.getType() == Materials.PLAYER_HEAD.toBukkitType())
textureValue = superiorPlayer == null ? HeadUtils.getNullPlayerTexture() : superiorPlayer.getTextureValue();
return this;
}
public ItemBuilder asSkullOf(String textureValue) {
if (itemStack.getType() == Materials.PLAYER_HEAD.toBukkitType())
this.textureValue = textureValue;
return this;
}
public ItemBuilder withName(String name) {
if (itemMeta != null && name != null)
itemMeta.setDisplayName(Formatters.COLOR_FORMATTER.format(name));
return this;
}
public ItemBuilder replaceName(String regex, String replace) {
if (itemMeta != null && itemMeta.hasDisplayName())
withName(itemMeta.getDisplayName().replace(regex, replace));
return this;
}
public ItemBuilder withLore(List<String> lore) {
if (itemMeta != null && lore != null)
itemMeta.setLore(Formatters.formatList(lore, Formatters.COLOR_FORMATTER));
return this;
}
public ItemBuilder appendLore(List<String> lore) {
List<String> currentLore = itemMeta == null || itemMeta.getLore() == null ? new ArrayList<>() : itemMeta.getLore();
currentLore.addAll(lore);
return withLore(currentLore);
}
public ItemBuilder withLore(String... lore) {
return withLore(Arrays.asList(lore));
}
public ItemBuilder withLore(String firstLine, List<String> listLine) {
if (itemMeta == null)
return this;
List<String> loreList = new ArrayList<>();
firstLine = Formatters.COLOR_FORMATTER.format(firstLine);
loreList.add(firstLine);
for (String line : listLine)
loreList.add(ChatColor.getLastColors(firstLine) + Formatters.COLOR_FORMATTER.format(line));
if (loreList.size() > 10) {
for (int i = 10; i < loreList.size(); i++) {
loreList.remove(loreList.get(i));
}
loreList.add(ChatColor.getLastColors(firstLine) + "...");
}
itemMeta.setLore(loreList);
return this;
}
public ItemBuilder withLore(String firstLine, ConfigurationSection configurationSection) {
if (itemMeta == null)
return this;
List<String> loreList = new ArrayList<>();
firstLine = Formatters.COLOR_FORMATTER.format(firstLine);
loreList.add(firstLine);
for (String section : configurationSection.getKeys(false)) {
section = section + ": " + configurationSection.get(section).toString();
loreList.add(ChatColor.getLastColors(firstLine) + Formatters.COLOR_FORMATTER.format(section));
}
if (loreList.size() > 16) {
loreList = loreList.subList(0, 16);
loreList.add(ChatColor.getLastColors(firstLine) + "...");
}
itemMeta.setLore(loreList);
return this;
}
public ItemBuilder replaceLore(String regex, String replace) {
if (itemMeta == null || !itemMeta.hasLore())
return this;
List<String> loreList = new ArrayList<>();
for (String line : itemMeta.getLore()) {
loreList.add(line.replace(regex, replace));
}
withLore(loreList);
return this;
}
public ItemBuilder replaceLoreWithLines(String regex, String... lines) {
if (itemMeta == null || !itemMeta.hasLore())
return this;
List<String> loreList = new ArrayList<>();
List<String> linesToAdd = Arrays.asList(lines);
boolean isEmpty = linesToAdd.isEmpty() || linesToAdd.stream().allMatch(String::isEmpty);
for (String line : itemMeta.getLore()) {
if (line.contains(regex)) {
if (!isEmpty)
loreList.addAll(linesToAdd);
} else {
loreList.add(line);
}
}
withLore(loreList);
return this;
}
public ItemBuilder replaceAll(String regex, String replace) {
replaceName(regex, replace);
replaceLore(regex, replace);
return this;
}
public ItemBuilder withEnchant(Enchantment enchant, int level) {
if (itemMeta != null)
itemMeta.addEnchant(enchant, level, true);
return this;
}
public ItemBuilder withFlags(ItemFlag... itemFlags) {
if (itemMeta != null)
itemMeta.addItemFlags(itemFlags);
return this;
}
public ItemBuilder setUnbreakable() {
if (itemMeta != null)
itemMeta.spigot().setUnbreakable(true);
return this;
}
public ItemBuilder withPotionEffect(PotionEffect potionEffect) {
if (itemMeta instanceof PotionMeta)
plugin.getNMSAlgorithms().addPotion((PotionMeta) itemMeta, potionEffect);
return this;
}
@SuppressWarnings("deprecation")
public ItemBuilder withEntityType(EntityType entityType) {
if (itemMeta == null)
return this;
if (ItemUtils.isValidAndSpawnEgg(itemStack)) {
if (ServerVersion.isLegacy()) {
try {
((SpawnEggMeta) itemMeta).setSpawnedType(entityType);
} catch (NoClassDefFoundError error) {
itemStack.setDurability(entityType.getTypeId());
}
} else {
itemStack.setType(Material.valueOf(entityType.name() + "_SPAWN_EGG"));
}
}
return this;
}
public ItemBuilder withCustomModel(int customModel) {
plugin.getNMSAlgorithms().setCustomModel(itemMeta, customModel);
return this;
}
public ItemBuilder withLeatherColor(int leatherColor) {
if (itemMeta instanceof LeatherArmorMeta) {
LeatherArmorMeta leatherArmorMeta = (LeatherArmorMeta) itemMeta;
leatherArmorMeta.setColor(Color.fromRGB(leatherColor));
}
return this;
}
@Nullable
public ItemMeta getItemMeta() {
return itemMeta;
}
public ItemStack build(SuperiorPlayer superiorPlayer) {
OfflinePlayer offlinePlayer = superiorPlayer.asOfflinePlayer();
PlaceholdersService placeholdersService = plugin.getServices().getPlaceholdersService();
if (itemMeta != null) {
if (itemMeta.hasDisplayName()) {
withName(placeholdersService.parsePlaceholders(offlinePlayer, itemMeta.getDisplayName()));
}
if (itemMeta.hasLore()) {
withLore(itemMeta.getLore().stream()
.map(line -> placeholdersService.parsePlaceholders(offlinePlayer, line))
.collect(Collectors.toList()));
}
}
if (textureValue.equals("%superior_player_texture%"))
textureValue = superiorPlayer.getTextureValue();
return build();
}
public ItemStack build() {
itemStack.setItemMeta(itemMeta);
return textureValue.isEmpty() ? itemStack : HeadUtils.getPlayerHead(itemStack, textureValue);
}
public ItemBuilder copy() {
ItemBuilder itemBuilder = new ItemBuilder(Material.AIR);
itemBuilder.itemStack = itemStack.clone();
if (itemMeta != null)
itemBuilder.itemMeta = itemMeta.clone();
itemBuilder.textureValue = textureValue;
return itemBuilder;
}
}