forked from Maxlego08/zMenu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenuItemStackLoader.java
More file actions
749 lines (671 loc) · 37.8 KB
/
MenuItemStackLoader.java
File metadata and controls
749 lines (671 loc) · 37.8 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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
package fr.maxlego08.menu.loader;
import fr.maxlego08.menu.ZMenuItemStack;
import fr.maxlego08.menu.api.ComponentsManager;
import fr.maxlego08.menu.api.InventoryManager;
import fr.maxlego08.menu.api.MenuItemStack;
import fr.maxlego08.menu.api.attribute.AttributeMergeStrategy;
import fr.maxlego08.menu.api.attribute.AttributeWrapper;
import fr.maxlego08.menu.api.configuration.Configuration;
import fr.maxlego08.menu.api.enchantment.Enchantments;
import fr.maxlego08.menu.api.enchantment.MenuEnchantment;
import fr.maxlego08.menu.api.enums.MenuItemRarity;
import fr.maxlego08.menu.api.exceptions.ItemEnchantException;
import fr.maxlego08.menu.api.itemstack.*;
import fr.maxlego08.menu.api.loader.ItemComponentLoader;
import fr.maxlego08.menu.api.utils.Loader;
import fr.maxlego08.menu.api.utils.LoreType;
import fr.maxlego08.menu.common.context.ZBuildContext;
import fr.maxlego08.menu.common.utils.ZUtils;
import fr.maxlego08.menu.common.utils.nms.NmsVersion;
import fr.maxlego08.menu.zcore.logger.Logger;
import org.bukkit.*;
import org.bukkit.block.banner.Pattern;
import org.bukkit.block.banner.PatternType;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.meta.trim.TrimMaterial;
import org.bukkit.inventory.meta.trim.TrimPattern;
import org.bukkit.potion.PotionType;
import org.jspecify.annotations.NonNull;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
@SuppressWarnings("deprecation")
public class MenuItemStackLoader extends ZUtils implements Loader<MenuItemStack> {
private final InventoryManager manager;
/**
* @param manager the inventory manager
*/
public MenuItemStackLoader(InventoryManager manager) {
super();
this.manager = manager;
}
/**
* Load ItemStack
*/
public MenuItemStack load(@NonNull YamlConfiguration configuration, @NonNull String path, Object... objects) {
if (!path.isEmpty() && !path.endsWith("."))
path = path + ".";
File file = (File) objects[0];
ZMenuItemStack menuItemStack = new ZMenuItemStack(this.manager, file.getPath(), path);
var material = configuration.getString(path + "material", null);
if (material != null && material.startsWith("basehead-")) {
menuItemStack.setUrl(material.replace("basehead-", ""));
} else {
menuItemStack.setMaterial(material);
}
menuItemStack.setData(configuration.getString(path + "data", "0"));
menuItemStack.setDurability(configuration.getInt(path + "durability", 0));
menuItemStack.setAmount(configuration.getString(path + "amount", "1"));
menuItemStack.setTargetPlayer(configuration.getString(path + "target", null));
var url = configuration.getString(path + "url", null);
if (url != null) menuItemStack.setUrl(url);
this.loadLeather(menuItemStack, configuration, path);
this.loadPotions(menuItemStack, configuration, path);
this.loadBanner(menuItemStack, configuration, path);
this.loadFireworks(menuItemStack, configuration, path);
this.loadLore(menuItemStack, configuration, path);
menuItemStack.setDisplayName(configuration.getString(path + "name", configuration.getString(path + "display_name", configuration.getString(path + "display-name", null))));
menuItemStack.setCenterName(configuration.getBoolean(path + "center-name", false));
menuItemStack.setCenterLore(configuration.getBoolean(path + "center-lore", false));
try {
menuItemStack.setLoreType(LoreType.valueOf(configuration.getString(path + "lore-type", LoreType.REPLACE.name()).toUpperCase()));
} catch (Exception ignored) {
}
menuItemStack.setGlowing(configuration.getBoolean(path + "glow"));
menuItemStack.setModelID(configuration.getString(path + "modelID", configuration.getString(path + "model-id", configuration.getString(path + "modelId", configuration.getString(path + "customModelId", configuration.getString(path + "customModelData", configuration.getString("model_data", configuration.getString("custom-model-id", configuration.getString("custom-model-data", configuration.getString("model-data", "0"))))))))));
this.loadTranslation(menuItemStack, configuration, path);
this.loadEnchantements(menuItemStack, configuration, path, file);
List<String> flagStrings = configuration.getStringList(path + "flags");
List<ItemFlag> flags = new ArrayList<>(flagStrings.size());
for (String flagName : flagStrings) {
ItemFlag flag = this.getFlag(flagName);
if (flag != null)
flags.add(flag);
}
menuItemStack.setFlags(flags);
this.loadAttributes(menuItemStack, configuration, path);
if (NmsVersion.getCurrentVersion().isNewItemStackAPI()) {
loadNewItemStacks(menuItemStack, configuration, path, file);
}
if (NmsVersion.getCurrentVersion().isNewHeadApi()) {
loadTrims(menuItemStack, configuration, path, file);
}
this.loadItemModel(configuration, menuItemStack, path, file);
if (NmsVersion.getCurrentVersion().isAttributItemStack()) { // 1.20.5+
ConfigurationSection componentsSection = configuration.getConfigurationSection(path + "components.");
if (componentsSection != null) {
ComponentsManager componentsManager = this.manager.getPlugin().getComponentsManager();
for (String componentKey : componentsSection.getKeys(false)) {
ConfigurationSection componentSection = componentsSection.getConfigurationSection(componentKey);
Optional<ItemComponentLoader> optionalItemComponentLoader = componentsManager.getLoader(componentKey);
if (optionalItemComponentLoader.isPresent()) {
try {
ItemComponentLoader itemComponentLoader = optionalItemComponentLoader.get();
ItemComponent itemComponent = itemComponentLoader.load(menuItemStack, file, configuration, path + "components." + componentKey + ".", componentSection);
if (itemComponent != null) {
itemComponent.setParentLoader(itemComponentLoader);
menuItemStack.addItemComponent(itemComponent);
}
} catch (Exception e) {
if (Configuration.enableDebug) {
Logger.info("An error occurred while loading the item component " + componentKey + " for file " + file.getAbsolutePath() + " with path " + path, Logger.LogType.WARNING);
e.printStackTrace();
}
}
}
}
}
}
if (!menuItemStack.isNeedPlaceholderAPI() && Configuration.enableCacheItemStack && !menuItemStack.isDynamicMaterial()) {
try {
menuItemStack.build(new ZBuildContext.Builder().build());
} catch (Exception ignored) { // Fail when a item requires a player to be built.
}
}
return menuItemStack;
}
/**
* Loads the lore from the configuration and applies it to the MenuItemStack.
* The lore can be specified in the configuration as a list of strings
* under the key "lore", or as a single string with newline characters that
* are split into a list of strings.
*
* @param menuItemStack the MenuItemStack to set the lore for
* @param configuration the configuration to read from
* @param path the path to the configuration key for the lore
*/
private void loadLore(ZMenuItemStack menuItemStack, YamlConfiguration configuration, String path) {
List<String> lore = configuration.getStringList(path + "lore");
if (lore.isEmpty()) {
Object object = configuration.get(path + "lore", null);
if (object instanceof String loreString) {
lore = Arrays.asList(loreString.split("\n"));
}
}
menuItemStack.setLore(lore);
}
private void loadItemModel(@NonNull YamlConfiguration configuration, ZMenuItemStack menuItemStack, @NonNull String path, File file) {
if (NmsVersion.getCurrentVersion().isNewItemModelAPI()) {
String itemModel = configuration.getString(path + "item-model");
if (itemModel != null) {
try {
NamespacedKey namespacedKey = NamespacedKey.fromString(itemModel.toLowerCase());
if (namespacedKey != null) {
menuItemStack.setItemModel(namespacedKey);
}
} catch (Exception e) {
if (Configuration.enableDebug) {
Logger.info("An error occurred while loading the item model " + itemModel + " for file " + file.getAbsolutePath() + " with path " + path, Logger.LogType.WARNING);
e.printStackTrace();
}
}
}
}
}
/**
* Loads leather armor settings from the configuration and applies them to the MenuItemStack.
* If the material specified in the configuration is a type of leather armor, it retrieves
* the color configuration and sets the leather armor with the appropriate type and color.
*
* @param menuItemStack The MenuItemStack to apply the leather armor settings to.
* @param configuration The YamlConfiguration to read the material and color from.
* @param path The path in the configuration where the material and color are specified.
*/
private void loadLeather(ZMenuItemStack menuItemStack, YamlConfiguration configuration, String path) {
try {
Material material = Material.valueOf(configuration.getString(path + "material", "").toUpperCase());
String materialName = material.toString();
if (materialName.startsWith("LEATHER_")) {
Color armorColor = getColor(configuration, path + "color", Color.fromRGB(160, 101, 64));
String type = materialName.replace("LEATHER_", "");
menuItemStack.setLeatherArmor(new LeatherArmor(LeatherArmor.ArmorType.valueOf(type), armorColor));
}
} catch (Exception ignored) {
}
}
/**
* Loads the attributes from the given configuration into the given
* {@link ZMenuItemStack}. The attributes are loaded from the "attributes"
* section of the configuration.
*
* @param menuItemStack The {@link ZMenuItemStack} to load the attributes
* into.
* @param configuration The configuration to load the attributes from.
* @param path The path of the configuration section to load the
* attributes from.
*/
private void loadAttributes(ZMenuItemStack menuItemStack, YamlConfiguration configuration, String path) {
List<AttributeWrapper> attributeModifiers = new ArrayList<>();
if (configuration.contains(path + "attributes")) {
List<Map<String, Object>> attributesFromConfig = (List<Map<String, Object>>) configuration.getList(path + "attributes");
if (attributesFromConfig != null) {
for (Map<String, Object> attributeMap : attributesFromConfig) {
attributeModifiers.add(AttributeWrapper.deserialize(attributeMap));
}
}
}
menuItemStack.setAttributeMergeStrategy(AttributeMergeStrategy.valueOf(configuration.getString(path + "attribute-merge-strategy", AttributeMergeStrategy.ADD.name()).toUpperCase()));
menuItemStack.setAttributes(attributeModifiers);
menuItemStack.setClearDefaultAttributes(configuration.getBoolean(path + "clear-default-attributes", false));
}
/**
* Loads the enchantments from the given configuration into the given
* MenuItemStack.
* <p>
* The enchantments are stored in a list under the given path with the
* following format: "enchant,level". If the enchantment is not found or if
* the format is incorrect, an ItemEnchantException is thrown.
* <p>
* The enchantments are loaded using the Enchantments helper class.
* <p>
* The enchantments are stored in a Map of Enchantment to Integer, where the
* Enchantment is the enchantment and the Integer is the level.
* <p>
* {@inheritDoc}
*/
private void loadEnchantements(ZMenuItemStack menuItemStack, YamlConfiguration configuration, String path, File file) {
Enchantments helperEnchantments = this.manager.getEnchantments();
List<String> enchants = configuration.getStringList(path + "enchants");
if (enchants.isEmpty()) enchants = configuration.getStringList(path + "enchantments");
Map<Enchantment, Integer> enchantments = new HashMap<>();
for (String enchantString : enchants) {
try {
String[] splitEnchant = enchantString.contains(":") ? enchantString.split(":") : enchantString.split(",");
if (splitEnchant.length == 1)
throw new ItemEnchantException("an error occurred while loading the enchantment " + enchantString + " for file " + file.getAbsolutePath() + " with path " + path);
int level;
String enchant = splitEnchant[0];
try {
level = Integer.parseInt(splitEnchant[1]);
} catch (NumberFormatException e) {
throw new ItemEnchantException("an error occurred while loading the enchantment " + enchantString + " for file " + file.getAbsolutePath() + " with path " + path);
}
Optional<MenuEnchantment> optional = helperEnchantments.getEnchantments(enchant);
if (optional.isEmpty()) {
throw new ItemEnchantException("an error occurred while loading the enchantment " + enchantString + " for file " + file.getAbsolutePath() + " with path " + path);
}
enchantments.put(optional.get().enchantment(), level);
} catch (ItemEnchantException e) {
e.printStackTrace();
}
}
menuItemStack.setEnchantments(enchantments);
}
/**
* Loads potion effects from the given configuration and applies them to the specified MenuItemStack.
* <p>
* This method checks if the configuration contains potion data at the specified path. If so, it retrieves
* the potion type, level, splash status, and extended duration status from the configuration. A Potion object
* is then created with these attributes and its color is set. Finally, the potion is applied to the MenuItemStack.
* <p>
* Supported configuration keys at the given path include:
* <ul>
* <li>{@code potion}: The type of potion, defaulting to "REGEN" if not specified.</li>
* <li>{@code level}: The level of the potion effect, defaulting to 1.</li>
* <li>{@code splash}: Boolean indicating if the potion is a splash potion, defaulting to false.</li>
* <li>{@code extended}: Boolean indicating if the potion has an extended duration, defaulting to false.</li>
* </ul>
*
* @param menuItemStack the MenuItemStack to apply the potion effects to
* @param configuration the configuration containing the potion data
* @param path the path within the configuration where the potion data is located
*/
private void loadPotions(ZMenuItemStack menuItemStack, YamlConfiguration configuration, String path) {
if (configuration.contains(path + "potion")) {
Color potionColor = getColor(configuration, path + "color", null);
PotionType type = PotionType.valueOf(configuration.getString(path + "potion", "REGEN").toUpperCase());
int level = configuration.getInt(path + "level", 1);
boolean splash = configuration.getBoolean(path + "splash", false);
boolean extended = configuration.getBoolean(path + "extended", false);
boolean arrow = configuration.getBoolean(path + "arrow", false);
Potion potion = new Potion(type, level, splash, extended, arrow);
potion.setColor(potionColor);
menuItemStack.setPotion(potion);
}
}
/**
* Loads a banner from the given configuration at the given path.
* <p>
* The configuration should contain the following elements:
* <ul>
* <li>A string at {@code path + "banner"} which is the base color of the banner.</li>
* <li>A list of strings at {@code path + "patterns"} which are the patterns on the banner. Each string should be in the format {@code "color:pattern"} where {@code color} is a string representing a {@link DyeColor} and {@code pattern} is a string representing a {@link PatternType}.</li>
* </ul>
*
* @param menuItemStack the {@link ZMenuItemStack} to load the banner on
* @param configuration the configuration to load the banner from
* @param path the path to the banner in the configuration
*/
private void loadBanner(ZMenuItemStack menuItemStack, YamlConfiguration configuration, String path) {
if (configuration.contains(path + "banner")) {
DyeColor dyeColor = DyeColor.valueOf(configuration.getString(path + "banner", "WHITE").toUpperCase());
List<String> stringPattern = configuration.getStringList(path + "patterns");
List<Pattern> patterns = new ArrayList<>();
for (String string : stringPattern) {
String[] split = string.split(":");
if (split.length != 2) continue;
patterns.add(new Pattern(DyeColor.valueOf(split[0]), PatternType.valueOf(split[1])));
}
menuItemStack.setBanner(new Banner(dyeColor, patterns));
}
}
/**
* Loads a firework effect from the given configuration section
* and applies it to the given {@link ZMenuItemStack}.
* <p>
* The configuration section should contain the following keys:
*
* <ul>
* <li> {@code star}: whether the firework effect is a star or not. Optional, defaults to {@code false}. </li>
* <li> {@code flicker}: whether the firework effect should flicker. Optional, defaults to {@code false}. </li>
* <li> {@code trail}: whether the firework effect should have a trail. Optional, defaults to {@code false}. </li>
* <li> {@code type}: the type of the firework effect. Optional, defaults to {@code BALL}. </li>
* <li> {@code colors}: the colors of the firework effect. Optional. </li>
* <li> {@code fadeColors}: the fade colors of the firework effect. Optional. </li>
* </ul>
*
* @param menuItemStack the item stack to set the firework effect on
* @param configuration the configuration section to load the firework effect from
* @param path the path to the configuration section
*/
private void loadFireworks(ZMenuItemStack menuItemStack, YamlConfiguration configuration, String path) {
if (configuration.contains(path + "firework")) {
ConfigurationSection section = configuration.getConfigurationSection(path + "firework");
if (section != null) {
boolean isStar = section.getBoolean("star", false);
FireworkEffect.Builder builder = FireworkEffect.builder();
builder.flicker(section.getBoolean("flicker"));
builder.trail(section.getBoolean("trail"));
builder.with(FireworkEffect.Type.valueOf(section.getString("type", "BALL")));
builder.withColor(getColors(section, "colors"));
builder.withFade(getColors(section, "fadeColors"));
menuItemStack.setFirework(new Firework(isStar, builder.build()));
}
}
}
/**
* Load the translated name and lore from the configuration for the given menu item stack.
* <p>
* This method calls {@link #loadTranslatedName(ZMenuItemStack, YamlConfiguration, String)} and
* {@link #loadTranslatedLore(ZMenuItemStack, YamlConfiguration, String)} to load the translated
* name and lore respectively.
*
* @param menuItemStack the menu item stack to load the translated name and lore for
* @param configuration the configuration to load the translated name and lore from
* @param path the path to the configuration key for the translated name and lore
*/
private void loadTranslation(ZMenuItemStack menuItemStack, YamlConfiguration configuration, String path) {
loadTranslatedName(menuItemStack, configuration, path);
loadTranslatedLore(menuItemStack, configuration, path);
}
/**
* Load the translated lore from the configuration for the given menu item stack.
* <p>
* The translated lore is a map from locale to lore, where the locale is the
* language code and the lore is the translated lore of the item.
* <p>
* The translated lore is loaded from the configuration under the key
* "translatedLore" or "translated-lore".
*
* @param menuItemStack the menu item stack to load the translated lore for
* @param configuration the configuration to load the translated lore from
* @param path the path to the configuration key for the translated lore
*/
private void loadTranslatedLore(ZMenuItemStack menuItemStack, YamlConfiguration configuration, String path) {
Map<String, List<String>> translatedLore = new HashMap<>();
String loadString = configuration.contains(path + "translatedLore") ? "translatedLore" : configuration.contains(path + "translated-lore") ? "translated-lore" : null;
if (loadString != null) {
configuration.getMapList(path + loadString).forEach(map -> {
if (map.containsKey("locale") && map.containsKey("lore")) {
String locale = (String) map.get("locale");
List<String> name = (List<String>) map.get("lore");
translatedLore.put(locale.toLowerCase(), name);
}
});
}
menuItemStack.setTranslatedLore(translatedLore);
}
/**
* Load the translated name from the configuration for the given menu item stack.
* <p>
* The translated name is a map from locale to name, where the locale is the
* language code and the name is the translated name of the item.
* <p>
* The translated name is loaded from the configuration under the key
* "translatedName" or "translated-name".
*
* @param menuItemStack the menu item stack to load the translated name for
* @param configuration the configuration to load the translated name from
* @param path the path to the configuration key for the translated name
*/
private void loadTranslatedName(ZMenuItemStack menuItemStack, YamlConfiguration configuration, String path) {
Map<String, String> translatedDisplayName = new HashMap<>();
String loadString = configuration.contains(path + "translatedName") ? "translatedName" : configuration.contains(path + "translated-name") ? "translated-name" : null;
if (loadString != null) {
configuration.getMapList(path + loadString).forEach(map -> {
if (map.containsKey("locale") && map.containsKey("name")) {
String locale = (String) map.get("locale");
String name = (String) map.get("name");
translatedDisplayName.put(locale.toLowerCase(), name);
}
});
}
menuItemStack.setTranslatedDisplayName(translatedDisplayName);
}
/**
* Returns the given object if it is a Boolean, otherwise returns null.
* <p>
* This is a utility method for deserializing booleans from configurations.
*
* @param o the object to check
* @return the object if it is a Boolean, otherwise null
*/
private Boolean getOrNull(Object o) {
return o instanceof Boolean ? (Boolean) o : null;
}
/**
* Loads additional item stack properties from the configuration into the provided MenuItemStack.
* <p>
* This method reads various properties such as max stack size, max damage, repair cost,
* unbreakable status, fire resistance, tooltip visibility, enchantment and attribute glints,
* and item rarity from the provided YamlConfiguration, and applies them to the given MenuItemStack.
* <p>
* Additionally, it configures trim patterns and materials if specified, logging errors for
* any missing configurations.
*
* @param menuItemStack the MenuItemStack to configure
* @param configuration the YamlConfiguration containing item stack properties
* @param path the path within the configuration to read from
* @param file the file from which the configuration was loaded, used for logging errors
*/
private void loadNewItemStacks(ZMenuItemStack menuItemStack, YamlConfiguration configuration, String path, File file) {
menuItemStack.setMaxStackSize(configuration.getInt(path + "max-stack-size", 0));
menuItemStack.setMaxDamage(configuration.getInt(path + "max-damage", 0));
menuItemStack.setDamage(configuration.getInt(path + "damage", 0));
menuItemStack.setRepairCost(configuration.getInt(path + "repair-cost", 0));
menuItemStack.setUnbreakableEnabled(getOrNull(configuration.get(path + "unbreakable", null)));
menuItemStack.setUnbreakableShowInTooltip(getOrNull(configuration.get(path + "unbreakable-show-in-tooltip", null)));
menuItemStack.setFireResistant(getOrNull(configuration.get(path + "fire-resistant", null)));
menuItemStack.setHideTooltip(getOrNull(configuration.get(path + "hide-tooltip", null)));
menuItemStack.setHideAdditionalTooltip(getOrNull(configuration.get(path + "hide-additional-tooltip", null)));
menuItemStack.setEnchantmentGlint(getOrNull(configuration.get(path + "enchantment-glint", null)));
menuItemStack.setEnchantmentShowInTooltip(getOrNull(configuration.get(path + "enchantment-show-in-tooltip", null)));
menuItemStack.setAttributeShowInTooltip(getOrNull(configuration.get(path + "attribute-show-in-tooltip", null)));
String rarityString = configuration.getString("item-rarity", null);
if (rarityString != null) {
menuItemStack.setItemRarity(MenuItemRarity.valueOf(rarityString.toUpperCase()));
}
String tooltypestyleString = configuration.getString(path + "tooltip-style", null);
if (tooltypestyleString != null) {
menuItemStack.setToolTipStyle(tooltypestyleString);
}
this.loadItemModel(configuration, menuItemStack, path, file);
String equippedModel = configuration.getString(path + "equipped-model", null);
if (equippedModel != null) {
menuItemStack.setEquippedModel(equippedModel);
}
}
private void loadTrims(ZMenuItemStack menuItemStack, YamlConfiguration configuration, String path, File file) {
boolean enableTrim = configuration.getBoolean(path + "trim.enable", false);
if (enableTrim) {
String patternKey = configuration.getString(path + "trim.pattern", "").toLowerCase();
TrimPattern trimPattern = null;
try {
NamespacedKey patternNamespace = NamespacedKey.fromString(patternKey);
if (patternNamespace != null) {
trimPattern = Registry.TRIM_PATTERN.get(patternNamespace);
}
if (trimPattern == null) {
enableTrim = false;
String joinedNames = Registry.TRIM_PATTERN.stream()
.map(TrimPattern::getKey)
.map(NamespacedKey::toString)
.collect(Collectors.joining(", "));
Logger.info("Trim pattern '" + patternKey + "' was not found for item '" + file.getAbsolutePath() + "'. Available patterns: " + joinedNames, Logger.LogType.ERROR);
}
} catch (Exception e) {
enableTrim = false;
Logger.info("Invalid namespace for trim pattern: '" + patternKey + "' in file '" + file.getAbsolutePath() + "'", Logger.LogType.ERROR);
}
String materialKey = configuration.getString(path + "trim.material", "").toLowerCase();
TrimMaterial trimMaterial = null;
try {
NamespacedKey materialNamespace = NamespacedKey.fromString(materialKey);
if (materialNamespace != null) {
trimMaterial = Registry.TRIM_MATERIAL.get(materialNamespace);
}
if (trimMaterial == null) {
enableTrim = false;
String joinedNames = Registry.TRIM_MATERIAL.stream()
.map(TrimMaterial::getKey)
.map(NamespacedKey::toString)
.collect(Collectors.joining(", "));
Logger.info("Trim material '" + materialKey + "' was not found for item '" + file.getAbsolutePath() + "'. Available materials: " + joinedNames, Logger.LogType.ERROR);
}
} catch (Exception e) {
enableTrim = false;
Logger.info("Invalid namespace for trim material: '" + materialKey + "' in file '" + file.getAbsolutePath() + "'", Logger.LogType.ERROR);
}
if (trimMaterial != null && trimPattern != null)
menuItemStack.setTrimConfiguration(new TrimConfiguration(enableTrim, trimMaterial, trimPattern));
}
}
/**
* Parse a color from a YamlConfiguration.
* <p>
* The color may be specified in either RGB (three integers) or ARGB (four integers) format.
* If the configuration does not contain the specified key, or if the value is not a valid color,
* the specified default value is returned.
* </p>
*
* @param configuration the configuration to read the color from
* @param key the key to read the color from
* @param def the default value to return if the color cannot be parsed
* @return the parsed color, or the default value if the color cannot be parsed
*/
private Color getColor(YamlConfiguration configuration, String key, Color def) {
String[] split = configuration.getString(key, "").split(",");
try {
if (split.length == 3) {
return Color.fromRGB(Integer.parseInt(split[0]), Integer.parseInt(split[1]), Integer.parseInt(split[2]));
}
if (split.length == 4) {
return Color.fromARGB(Integer.parseInt(split[0]), Integer.parseInt(split[1]), Integer.parseInt(split[2]), Integer.parseInt(split[3]));
}
} catch (NumberFormatException ignored) {
}
return def;
}
/**
* Reads a list of colors from a ConfigurationSection.
* <p>
* Colors may be specified in either RGB (three integers) or ARGB (four integers) format.
* If a color is not a valid color, it is ignored.
* </p>
*
* @param section the configuration section to read the colors from
* @param key the key to read the colors from
* @return a list of colors
*/
private List<Color> getColors(ConfigurationSection section, String key) {
List<Color> colors = new ArrayList<>();
for (String color : section.getStringList(key)) {
String[] split = color.split(",");
if (split.length == 3) {
colors.add(Color.fromRGB(Integer.parseInt(split[0]), Integer.parseInt(split[1]), Integer.parseInt(split[2])));
} else if (split.length == 4) {
colors.add(Color.fromARGB(Integer.parseInt(split[0]), Integer.parseInt(split[1]), Integer.parseInt(split[2]), Integer.parseInt(split[3])));
}
}
return colors;
}
/**
* Saves the properties of a MenuItemStack to a YamlConfiguration file.
* <p>
* This method serializes various properties of the given MenuItemStack, such as
* material, display name, lore, model ID, durability, amount, URL, potion effects,
* firework effects, leather armor color, banner patterns, enchantments, and flags.
* These properties are stored in the provided YamlConfiguration under the specified path.
* <p>
* Additionally, it attempts to save the configuration to the provided file, logging
* any IOExceptions that occur during the process.
*
* @param item the MenuItemStack whose properties are to be saved
* @param configuration the YamlConfiguration to store the properties in
* @param path the path within the configuration to store the properties under
* @param file the file in which to save the configuration
* @param objects additional objects for potential future use
*/
public void save(MenuItemStack item, @NonNull YamlConfiguration configuration, @NonNull String path, File file, Object... objects) {
configuration.set(path + "material", item.getMaterial());
if (item.getDisplayName() != null) configuration.set(path + "name", item.getDisplayName());
if (!item.getLore().isEmpty()) configuration.set(path + "lore", item.getLore());
if (item.isGlowing()) configuration.set(path + "glow", item.isGlowing());
if (item.getModelID() != null && !item.getModelID().equalsIgnoreCase("0")) {
configuration.set(path + "model-id", item.getModelID());
}
try {
if (Integer.parseInt(item.getData()) > 0) configuration.set(path + "data", item.getData());
} catch (Exception ignored) {
configuration.set(path + "data", item.getData());
}
if (item.getDurability() > 0) configuration.set(path + "durability", item.getDurability());
if (item.getAmount() != null) {
try {
int value = Integer.parseInt(item.getAmount());
if (value > 1) {
configuration.set(path + "amount", value);
}
} catch (Exception exception) {
configuration.set(path + "amount", item.getAmount());
}
}
if (item.getUrl() != null) configuration.set(path + "url", item.getUrl());
Potion potion = item.getPotion();
Firework firework = item.getFirework();
LeatherArmor leatherArmor = item.getLeatherArmor();
Banner banner = item.getBanner();
if (potion != null) {
Color potionColor = potion.getColor();
configuration.set(path + "potion", potion.getType().toString());
if (potion.getLevel() != 0) configuration.set(path + "level", potion.getLevel());
if (potion.isSplash()) configuration.set(path + "splash", true);
if (potion.hasExtendedDuration()) configuration.set(path + "extended", true);
if (potionColor != null) {
configuration.set("color", potionColor.getAlpha() + "," + potionColor.getRed() + "," + potionColor.getGreen() + "," + potionColor.getBlue());
}
}
if (firework != null) {
ConfigurationSection fireworkSection = configuration.createSection(path + "firework");
FireworkEffect effect = firework.getEffect();
List<String> stringColors = new ArrayList<>();
effect.getColors().forEach(color -> stringColors.add(color.getAlpha() + "," + color.getRed() + "," + color.getGreen() + "," + color.getBlue()));
List<String> stringFadeColors = new ArrayList<>();
effect.getColors().forEach(color -> stringFadeColors.add(color.getAlpha() + "," + color.getRed() + "," + color.getGreen() + "," + color.getBlue()));
fireworkSection.set("star", firework.isStar());
fireworkSection.set("flicker", effect.hasFlicker());
fireworkSection.set("trail", effect.hasTrail());
fireworkSection.set("type", effect.getType().toString());
fireworkSection.set("colors", stringColors);
fireworkSection.set("fadeColors", stringFadeColors);
}
if (leatherArmor != null) {
Color leatherArmorColor = leatherArmor.getColor();
if (leatherArmorColor != null) {
configuration.set("color", leatherArmorColor.getAlpha() + "," + leatherArmorColor.getRed() + "," + leatherArmorColor.getGreen() + "," + leatherArmorColor.getBlue());
}
}
if (banner != null) {
List<Pattern> patterns = banner.getPatterns();
configuration.set(path + "banner", banner.getBaseColor().toString());
if (patterns != null && !patterns.isEmpty()) {
List<String> stringPatterns = new ArrayList<>();
for (Pattern p : patterns) {
stringPatterns.add(p.getColor() + ":" + p.getPattern());
}
configuration.set(path + "patterns", stringPatterns);
}
}
if (item.getEnchantments() != null && !item.getEnchantments().isEmpty()) {
List<String> stringEnchantments = new ArrayList<>(item.getEnchantments().size());
item.getEnchantments().forEach((enchantment, level) -> stringEnchantments.add(enchantment.getName() + "," + level));
configuration.set(path + "enchants", stringEnchantments);
}
if (item.getFlags() != null && !item.getFlags().isEmpty()) {
List<String> flags = new ArrayList<>(item.getFlags().size());
for (ItemFlag flag : item.getFlags()) {
flags.add(flag.toString());
}
configuration.set(path + "flags", flags);
}
try {
configuration.save(file);
} catch (IOException exception) {
exception.printStackTrace();
}
}
}