-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathMenuItem.java
More file actions
637 lines (564 loc) · 28.2 KB
/
MenuItem.java
File metadata and controls
637 lines (564 loc) · 28.2 KB
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
package com.extendedclip.deluxemenus.menu;
import com.extendedclip.deluxemenus.DeluxeMenus;
import com.extendedclip.deluxemenus.hooks.ItemHook;
import com.extendedclip.deluxemenus.menu.options.HeadType;
import com.extendedclip.deluxemenus.menu.options.LoreAppendMode;
import com.extendedclip.deluxemenus.menu.options.MenuItemOptions;
import com.extendedclip.deluxemenus.menu.options.CustomModelDataComponent;
import com.extendedclip.deluxemenus.nbt.NbtProvider;
import com.extendedclip.deluxemenus.utils.DebugLevel;
import com.extendedclip.deluxemenus.utils.ItemUtils;
import com.extendedclip.deluxemenus.utils.StringUtils;
import com.extendedclip.deluxemenus.utils.VersionHelper;
import com.google.common.collect.ImmutableMultimap;
import org.bukkit.Color;
import org.bukkit.FireworkEffect;
import org.bukkit.Material;
import org.bukkit.Registry;
import org.bukkit.NamespacedKey;
import org.bukkit.block.Banner;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.type.Light;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemRarity;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ArmorMeta;
import org.bukkit.inventory.meta.BannerMeta;
import org.bukkit.inventory.meta.BlockDataMeta;
import org.bukkit.inventory.meta.BlockStateMeta;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import org.bukkit.inventory.meta.FireworkEffectMeta;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.LeatherArmorMeta;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.inventory.meta.trim.ArmorTrim;
import org.bukkit.inventory.meta.trim.TrimMaterial;
import org.bukkit.inventory.meta.trim.TrimPattern;
import org.bukkit.potion.PotionEffect;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Objects;
import java.util.logging.Level;
import java.util.stream.Collectors;
import static com.extendedclip.deluxemenus.utils.Constants.INVENTORY_ITEM_ACCESSORS;
import static com.extendedclip.deluxemenus.utils.Constants.PLACEHOLDER_PREFIX;
import static com.extendedclip.deluxemenus.utils.Constants.STACK_PREFIX;
public class MenuItem {
private final DeluxeMenus plugin;
private final MenuItemOptions options;
public MenuItem(@NotNull final DeluxeMenus plugin, @NotNull final MenuItemOptions options) {
this.plugin = plugin;
this.options = options;
}
public static ItemStack base64ToItemStack(String data) {
try {
byte[] bytes = Base64.getDecoder().decode(data);
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream);
dataInput.close();
Object object = dataInput.readObject();
if (object instanceof ItemStack) {
return (ItemStack) object;
}
return null;
} catch (IllegalArgumentException e) {
return null;
} catch (IOException e) {
return null;
} catch (ClassNotFoundException e) {
return null;
}
}
public ItemStack getItemStack(@NotNull final MenuHolder holder) {
final Player viewer = holder.getViewer();
ItemStack itemStack = null;
int amount = 1;
String stringMaterial = this.options.material();
String lowercaseStringMaterial = stringMaterial.toLowerCase(Locale.ROOT);
if (ItemUtils.isPlaceholderOption(lowercaseStringMaterial)) {
stringMaterial = holder.setPlaceholdersAndArguments(stringMaterial.substring(PLACEHOLDER_PREFIX.length()));
lowercaseStringMaterial = stringMaterial.toLowerCase(Locale.ENGLISH);
}
if (ItemUtils.isItemStackOption(lowercaseStringMaterial)) {
stringMaterial = holder.setPlaceholdersAndArguments(stringMaterial.substring(STACK_PREFIX.length()));
ItemStack base64Item = base64ToItemStack(stringMaterial);
if (base64Item != null) {
itemStack = base64Item;
amount = itemStack.getAmount();
lowercaseStringMaterial = itemStack.getType().toString().toLowerCase(Locale.ENGLISH);
}
}
if (ItemUtils.isPlayerItem(lowercaseStringMaterial)) {
final ItemStack playerItem = INVENTORY_ITEM_ACCESSORS.get(lowercaseStringMaterial).apply(viewer.getInventory());
if (playerItem == null || playerItem.getType() == Material.AIR) {
return new ItemStack(Material.AIR);
}
itemStack = playerItem.clone();
amount = playerItem.getAmount();
}
final int temporaryAmount = amount;
final String finalMaterial = lowercaseStringMaterial;
final ItemHook pluginHook = plugin.getItemHooks().values()
.stream()
.filter(x -> finalMaterial.startsWith(x.getPrefix()))
.findFirst()
.orElse(null);
if (pluginHook != null) {
itemStack = pluginHook.getItem(
viewer,
holder.setPlaceholdersAndArguments(stringMaterial.substring(pluginHook.getPrefix().length()))
);
}
if (ItemUtils.isWaterBottle(stringMaterial)) {
itemStack = ItemUtils.createWaterBottles(amount);
}
// The item is neither a water bottle nor plugin hook item
if (itemStack == null) {
final Material material = Material.getMaterial(stringMaterial.toUpperCase(Locale.ROOT));
if (material == null) {
plugin.debug(
DebugLevel.HIGHEST,
Level.WARNING,
"Material: " + stringMaterial + " is not valid! Setting to Stone."
);
itemStack = new ItemStack(Material.STONE, temporaryAmount);
} else {
itemStack = new ItemStack(material, temporaryAmount);
}
}
if (ItemUtils.isBanner(itemStack.getType())) {
final BannerMeta meta = (BannerMeta) itemStack.getItemMeta();
if (meta != null) {
if (!this.options.bannerMeta().isEmpty()) {
meta.setPatterns(this.options.bannerMeta());
}
itemStack.setItemMeta(meta);
}
}
if (ItemUtils.isShield(itemStack.getType())) {
final BlockStateMeta blockStateMeta = (BlockStateMeta) itemStack.getItemMeta();
if (blockStateMeta != null) {
final Banner banner = (Banner) blockStateMeta.getBlockState();
if (this.options.baseColor().isPresent()) {
banner.setBaseColor(this.options.baseColor().get());
banner.update();
blockStateMeta.setBlockState(banner);
}
if (!this.options.bannerMeta().isEmpty()) {
banner.setPatterns(this.options.bannerMeta());
banner.update();
blockStateMeta.setBlockState(banner);
}
itemStack.setItemMeta(blockStateMeta);
}
}
if (ItemUtils.hasPotionMeta(itemStack)) {
final PotionMeta meta = (PotionMeta) itemStack.getItemMeta();
if (meta != null) {
if (this.options.rgb().isPresent()) {
final Color color = parseRGBColor(holder.setPlaceholdersAndArguments(this.options.rgb().get()));
if (color != null) {
meta.setColor(color);
}
}
if (!this.options.potionEffects().isEmpty()) {
for (PotionEffect effect : this.options.potionEffects()) {
meta.addCustomEffect(effect, true);
}
}
itemStack.setItemMeta(meta);
}
}
if (itemStack.getType() == Material.AIR) {
return itemStack;
}
if (this.options.damage().isPresent()) {
final String parsedDamage = holder.setPlaceholdersAndArguments(this.options.damage().get());
try {
int damage = Integer.parseInt(parsedDamage);
if (damage > 0) {
final ItemMeta meta = itemStack.getItemMeta();
if (meta instanceof Damageable) {
((Damageable) meta).setDamage(damage);
itemStack.setItemMeta(meta);
}
}
} catch (final NumberFormatException exception) {
plugin.printStacktrace(
"Invalid damage found: " + parsedDamage + ".",
exception
);
}
}
if (this.options.amount() != -1) {
amount = this.options.amount();
}
if (this.options.dynamicAmount().isPresent()) {
try {
final int dynamicAmount = (int) Double.parseDouble(holder.setPlaceholdersAndArguments(this.options.dynamicAmount().get()));
amount = Math.max(dynamicAmount, 1);
} catch (final NumberFormatException ignored) {
}
}
if (amount > 64) {
amount = 64;
}
itemStack.setAmount(amount);
final ItemMeta itemMeta = itemStack.getItemMeta();
if (itemMeta == null) {
return itemStack;
}
if (VersionHelper.IS_CUSTOM_MODEL_DATA && this.options.customModelData().isPresent()) {
try {
final int modelData = Integer.parseInt(holder.setPlaceholdersAndArguments(this.options.customModelData().get()));
itemMeta.setCustomModelData(modelData);
} catch (final Exception ignored) {
}
}
if (VersionHelper.IS_CUSTOM_MODEL_DATA_COMPONENT && this.options.customModelDataComponent().isPresent()) {
itemMeta.setCustomModelDataComponent(parseCustomModelDataComponent(this.options.customModelDataComponent().get(), itemMeta.getCustomModelDataComponent(), holder));
}
if (this.options.displayName().isPresent()) {
final String displayName = holder.setPlaceholdersAndArguments(this.options.displayName().get());
itemMeta.setDisplayName(StringUtils.color(displayName));
}
List<String> lore = new ArrayList<>();
// This checks if a lore should be kept from the hooked item, and then if a lore exists on the item
// ItemMeta.getLore is nullable. In that case, we just create a new ArrayList so we don't add stuff to a null list.
List<String> itemLore = Objects.requireNonNullElse(itemMeta.getLore(), new ArrayList<>());
// Ensures backwards compatibility with how hooked items are currently handled
LoreAppendMode mode = this.options.loreAppendMode().orElse(LoreAppendMode.OVERRIDE);
if (!this.options.hasLore() && this.options.loreAppendMode().isEmpty()) mode = LoreAppendMode.IGNORE;
switch (mode) {
case IGNORE: // DM lore is not added at all
lore.addAll(itemLore);
break;
case TOP: // DM lore is added at the top
lore.addAll(getMenuItemLore(holder, this.options.lore()));
lore.addAll(itemLore);
break;
case BOTTOM: // DM lore is bottom at the bottom
lore.addAll(itemLore);
lore.addAll(getMenuItemLore(holder, this.options.lore()));
break;
case OVERRIDE: // Lore from DM overrides the lore from the item
lore.addAll(getMenuItemLore(holder, this.options.lore()));
break;
}
itemMeta.setLore(lore);
if (this.options.unbreakable()) {
itemMeta.setUnbreakable(true);
}
if (VersionHelper.HAS_DATA_COMPONENTS) {
if (this.options.hideTooltip().isPresent()) {
String hideTooltip = holder.setPlaceholdersAndArguments(this.options.hideTooltip().get());
itemMeta.setHideTooltip(Boolean.parseBoolean(hideTooltip));
}
if (this.options.enchantmentGlintOverride().isPresent()) {
String enchantmentGlintOverride = holder.setPlaceholdersAndArguments(this.options.enchantmentGlintOverride().get());
itemMeta.setEnchantmentGlintOverride(Boolean.parseBoolean(enchantmentGlintOverride));
}
if (this.options.rarity().isPresent()) {
String rarity = holder.setPlaceholdersAndArguments(this.options.rarity().get());
try {
itemMeta.setRarity(ItemRarity.valueOf(rarity.toUpperCase()));
} catch (IllegalArgumentException e) {
plugin.debug(
DebugLevel.HIGHEST,
Level.WARNING,
"Rarity " + rarity + " is not a valid!"
);
}
}
}
if (VersionHelper.HAS_TOOLTIP_STYLE) {
if (this.options.tooltipStyle().isPresent()) {
NamespacedKey tooltipStyle = NamespacedKey.fromString(holder.setPlaceholdersAndArguments(this.options.tooltipStyle().get()));
if (tooltipStyle != null) itemMeta.setTooltipStyle(tooltipStyle);
}
if (this.options.itemModel().isPresent()) {
NamespacedKey itemModel = NamespacedKey.fromString(holder.setPlaceholdersAndArguments(this.options.itemModel().get()));
if (itemModel != null) itemMeta.setItemModel(itemModel);
}
}
if (VersionHelper.HAS_ARMOR_TRIMS && ItemUtils.hasArmorMeta(itemStack)) {
final Optional<String> trimMaterialName = this.options.trimMaterial();
final Optional<String> trimPatternName = this.options.trimPattern();
if (trimMaterialName.isPresent() && trimPatternName.isPresent()) {
final TrimMaterial trimMaterial = Registry.TRIM_MATERIAL.match(holder.setPlaceholdersAndArguments(trimMaterialName.get()));
final TrimPattern trimPattern = Registry.TRIM_PATTERN.match(holder.setPlaceholdersAndArguments(trimPatternName.get()));
if (trimMaterial != null && trimPattern != null) {
final ArmorTrim armorTrim = new ArmorTrim(trimMaterial, trimPattern);
final ArmorMeta armorMeta = (ArmorMeta) itemMeta;
armorMeta.setTrim(armorTrim);
itemStack.setItemMeta(armorMeta);
} else {
if (trimMaterial == null) {
plugin.debug(
DebugLevel.HIGHEST,
Level.WARNING,
"Trim material " + trimMaterialName.get() + " is not a valid!"
);
}
if (trimPattern == null) {
plugin.debug(
DebugLevel.HIGHEST,
Level.WARNING,
"Trim pattern " + trimPatternName.get() + " is not a valid!"
);
}
}
} else if (trimMaterialName.isPresent()) {
plugin.debug(
DebugLevel.HIGHEST,
Level.WARNING,
"Trim pattern is not set for item with trim material " + trimMaterialName.get()
);
} else if (trimPatternName.isPresent()) {
plugin.debug(
DebugLevel.HIGHEST,
Level.WARNING,
"Trim material is not set for item with trim pattern " + trimPatternName.get()
);
}
}
if (itemMeta instanceof LeatherArmorMeta && this.options.rgb().isPresent()) {
final LeatherArmorMeta leatherArmorMeta = (LeatherArmorMeta) itemMeta;
final Color color = parseRGBColor(holder.setPlaceholdersAndArguments(this.options.rgb().get()));
if (color != null) {
leatherArmorMeta.setColor(color);
} else {
plugin.debug(
DebugLevel.HIGHEST,
Level.WARNING,
"Invalid rgb colors found for leather armor: " + this.options.rgb().get()
);
}
itemStack.setItemMeta(leatherArmorMeta);
} else if (itemMeta instanceof FireworkEffectMeta && this.options.rgb().isPresent()) {
final FireworkEffectMeta fireworkEffectMeta = (FireworkEffectMeta) itemMeta;
final Color color = parseRGBColor(holder.setPlaceholdersAndArguments(this.options.rgb().get()));
if (color != null) {
fireworkEffectMeta.setEffect(FireworkEffect.builder().withColor(color).build());
} else {
plugin.debug(
DebugLevel.HIGHEST,
Level.WARNING,
"Invalid RGB color found for firework or firework star: " + this.options.rgb().get()
);
}
itemStack.setItemMeta(fireworkEffectMeta);
} else if (itemMeta instanceof EnchantmentStorageMeta && !this.options.enchantments().isEmpty()) {
final EnchantmentStorageMeta enchantmentStorageMeta = (EnchantmentStorageMeta) itemMeta;
for (final Map.Entry<Enchantment, Integer> entry : this.options.enchantments().entrySet()) {
final boolean result = enchantmentStorageMeta.addStoredEnchant(entry.getKey(), entry.getValue(), true);
if (!result) {
plugin.debug(
DebugLevel.HIGHEST,
Level.INFO,
"Failed to add enchantment " + entry.getKey().getName() + " to item " + itemStack.getType()
);
}
}
itemStack.setItemMeta(enchantmentStorageMeta);
} else {
itemStack.setItemMeta(itemMeta);
}
if (!(itemMeta instanceof EnchantmentStorageMeta) && !this.options.enchantments().isEmpty()) {
this.options.enchantments().forEach((enchantment, level) -> itemMeta.addEnchant(enchantment, level, true));
}
if (this.options.lightLevel().isPresent() && itemMeta instanceof BlockDataMeta) {
final BlockDataMeta blockDataMeta = (BlockDataMeta) itemMeta;
final BlockData blockData = blockDataMeta.getBlockData(itemStack.getType());
if (blockData instanceof Light) {
final Light light = (Light) blockData;
final String parsedLightLevel = holder.setPlaceholdersAndArguments(this.options.lightLevel().get());
try {
final int lightLevel = Math.min(Integer.parseInt(parsedLightLevel), light.getMaximumLevel());
light.setLevel(Math.max(lightLevel, 0));
if (lightLevel < 0) {
plugin.debug(
DebugLevel.MEDIUM,
Level.WARNING,
"Invalid light level found for light block: " + parsedLightLevel + ". Setting to 0."
);
}
if (lightLevel > light.getMaximumLevel()) {
plugin.debug(
DebugLevel.MEDIUM,
Level.WARNING,
"Invalid light level found for light block: " + parsedLightLevel + ". Setting to " + light.getMaximumLevel() + "."
);
}
blockDataMeta.setBlockData(light);
} catch (final Exception exception) {
plugin.printStacktrace(
"Invalid light level found for light block: " + parsedLightLevel,
exception
);
}
}
}
if (!this.options.itemFlags().isEmpty()) {
for (final ItemFlag flag : this.options.itemFlags()) {
itemMeta.addItemFlags(flag);
if (flag == ItemFlag.HIDE_ATTRIBUTES && VersionHelper.HAS_DATA_COMPONENTS) {
itemMeta.setAttributeModifiers(ImmutableMultimap.of());
}
}
}
itemStack.setItemMeta(itemMeta);
if (NbtProvider.isAvailable()) {
if (this.options.nbtString().isPresent()) {
final String tag = holder.setPlaceholdersAndArguments(this.options.nbtString().get());
if (tag.contains(":")) {
final String[] parts = tag.split(":", 2);
itemStack = NbtProvider.setString(itemStack, parts[0], parts[1]);
}
}
if (this.options.nbtByte().isPresent()) {
final String tag = holder.setPlaceholdersAndArguments(this.options.nbtByte().get());
if (tag.contains(":")) {
final String[] parts = tag.split(":");
itemStack = NbtProvider.setByte(itemStack, parts[0], Byte.parseByte(parts[1]));
}
}
if (this.options.nbtShort().isPresent()) {
final String tag = holder.setPlaceholdersAndArguments(this.options.nbtShort().get());
if (tag.contains(":")) {
final String[] parts = tag.split(":");
itemStack = NbtProvider.setShort(itemStack, parts[0], Short.parseShort(parts[1]));
}
}
if (this.options.nbtInt().isPresent()) {
final String tag = holder.setPlaceholdersAndArguments(this.options.nbtInt().get());
if (tag.contains(":")) {
final String[] parts = tag.split(":");
itemStack = NbtProvider.setInt(itemStack, parts[0], Integer.parseInt(parts[1]));
}
}
for (String nbtTag : this.options.nbtStrings()) {
final String tag = holder.setPlaceholdersAndArguments(nbtTag);
if (tag.contains(":")) {
final String[] parts = tag.split(":", 2);
itemStack = NbtProvider.setString(itemStack, parts[0], parts[1]);
}
}
for (String nbtTag : this.options.nbtBytes()) {
final String tag = holder.setPlaceholdersAndArguments(nbtTag);
if (tag.contains(":")) {
final String[] parts = tag.split(":");
itemStack = NbtProvider.setByte(itemStack, parts[0], Byte.parseByte(parts[1]));
}
}
for (String nbtTag : this.options.nbtShorts()) {
final String tag = holder.setPlaceholdersAndArguments(nbtTag);
if (tag.contains(":")) {
final String[] parts = tag.split(":");
itemStack = NbtProvider.setShort(itemStack, parts[0], Short.parseShort(parts[1]));
}
}
for (String nbtTag : this.options.nbtInts()) {
final String tag = holder.setPlaceholdersAndArguments(nbtTag);
if (tag.contains(":")) {
final String[] parts = tag.split(":");
itemStack = NbtProvider.setInt(itemStack, parts[0], Integer.parseInt(parts[1]));
}
}
}
return itemStack;
}
/**
* Checks if the string is a head item. The check is case-insensitive.
* Head items are:
* <ul>
* <li>"head-{player-name}" (a simple named player head, supports placeholders. eg. "head-%player_name% or head-extendedclip")</li>
* <li>"texture-{texture-url}" (a head with a custom texture specified by a texture url. eg. "texture-93a728ad8d31486a7f9aad200edb373ea803d1fc5fd4321b2e2a971348234443")</li>
* <li>"basehead-{base64-encoded-texture-url}" (a head with a custom texture specified by a base64 encoded texture url)</li>
* <li>"hdb-{hdb-head-id}" (a head with a custom texture specified by a <a href="https://www.spigotmc.org/resources/14280/">HeadDatabase</a> id)</li>
* </ul>
*
* @param material The string to check
* @return true if the string is a head item, false otherwise
*/
private boolean isHeadItem(@NotNull final String material) {
final Optional<HeadType> headType = HeadType.parseHeadType(material);
headType.ifPresent(this.options::headType);
return headType.isPresent();
}
private @NotNull Optional<ItemStack> getItemFromHook(String hookName, String... args) {
return plugin.getItemHook(hookName).map(itemHook -> itemHook.getItem(args));
}
protected List<String> getMenuItemLore(@NotNull final MenuHolder holder, @NotNull final List<String> lore) {
return lore.stream()
.map(holder::setPlaceholdersAndArguments)
.map(StringUtils::color)
.map(line -> line.split("\n"))
.flatMap(Arrays::stream)
.map(line -> line.split("\\\\n"))
.flatMap(Arrays::stream)
.collect(Collectors.toList());
}
private @NotNull org.bukkit.inventory.meta.components.CustomModelDataComponent parseCustomModelDataComponent(
@NotNull final CustomModelDataComponent unparsedComponent,
@NotNull final org.bukkit.inventory.meta.components.CustomModelDataComponent component,
@NotNull final MenuHolder holder
) {
if (!unparsedComponent.colors().isEmpty()) {
final List<Color> colors = unparsedComponent.colors()
.stream()
.map(holder::setPlaceholdersAndArguments)
.map(this::parseRGBColor)
.filter(Objects::nonNull)
.collect(Collectors.toList());
component.setColors(colors);
}
if (!unparsedComponent.flags().isEmpty()) {
final List<Boolean> flags = unparsedComponent.flags()
.stream()
.map(holder::setPlaceholdersAndArguments)
.map(Boolean::parseBoolean)
.collect(Collectors.toList());
component.setFlags(flags);
}
if (!unparsedComponent.floats().isEmpty()) {
final List<Float> floats = unparsedComponent.floats()
.stream()
.map(holder::setPlaceholdersAndArguments)
.map(Float::parseFloat)
.collect(Collectors.toList());
component.setFloats(floats);
}
if (!unparsedComponent.strings().isEmpty()) {
final List<String> strings = unparsedComponent.strings()
.stream()
.map(holder::setPlaceholdersAndArguments)
.collect(Collectors.toList());
component.setStrings(strings);
}
return component;
}
private @Nullable Color parseRGBColor(@NotNull final String input) {
final Color color = StringUtils.parseRGBColor(input);
if (color == null) {
plugin.debug(
DebugLevel.HIGHEST,
Level.WARNING,
"Invalid RGB color found: " + input
);
}
return color;
}
public @NotNull MenuItemOptions options() {
return options;
}
}