forked from Maxlego08/zMenu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZMenuItemStack.java
More file actions
1200 lines (995 loc) · 38.6 KB
/
ZMenuItemStack.java
File metadata and controls
1200 lines (995 loc) · 38.6 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
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package fr.maxlego08.menu;
import com.google.common.collect.ArrayListMultimap;
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.AttributeUtil;
import fr.maxlego08.menu.api.attribute.AttributeWrapper;
import fr.maxlego08.menu.api.configuration.Configuration;
import fr.maxlego08.menu.api.context.BuildContext;
import fr.maxlego08.menu.api.enchantment.Enchantments;
import fr.maxlego08.menu.api.enums.MenuItemRarity;
import fr.maxlego08.menu.api.exceptions.ItemEnchantException;
import fr.maxlego08.menu.api.font.FontImage;
import fr.maxlego08.menu.api.itemstack.*;
import fr.maxlego08.menu.api.loader.MaterialLoader;
import fr.maxlego08.menu.api.utils.LoreType;
import fr.maxlego08.menu.api.utils.MapConfiguration;
import fr.maxlego08.menu.api.utils.OfflinePlayerCache;
import fr.maxlego08.menu.api.utils.Placeholders;
import fr.maxlego08.menu.common.context.ZBuildContext;
import fr.maxlego08.menu.common.utils.ZUtils;
import fr.maxlego08.menu.common.utils.itemstack.MenuItemStackFromItemStack;
import fr.maxlego08.menu.common.utils.nms.NmsVersion;
import fr.maxlego08.menu.zcore.logger.Logger;
import fr.maxlego08.menu.zcore.utils.PerformanceDebug;
import org.bukkit.*;
import org.bukkit.block.banner.Pattern;
import org.bukkit.block.banner.PatternType;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.*;
import org.bukkit.inventory.meta.trim.ArmorTrim;
import org.bukkit.potion.PotionType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jspecify.annotations.NonNull;
import javax.annotation.Nullable;
import java.io.File;
import java.util.*;
public class ZMenuItemStack extends ZUtils implements MenuItemStack {
private final InventoryManager inventoryManager;
private final String filePath;
private final String path;
private final List<ItemComponent> itemComponents = new ArrayList<>();
private String material;
private String targetPlayer;
private String amount;
private String url;
private String data;
private String tooltipStyle;
private int durability;
private Potion potion;
private List<String> lore = new ArrayList<>();
private List<ItemFlag> flags = new ArrayList<>();
private String displayName;
private Map<String, String> translatedDisplayName = new HashMap<>();
private Map<String, List<String>> translatedLore = new HashMap<>();
private boolean isGlowing;
private String modelID;
private NamespacedKey itemModel;
private String equippedModel;
private Map<Enchantment, Integer> enchantments = new HashMap<>();
private boolean clearDefaultAttributes = false;
private List<AttributeWrapper> attributes = new ArrayList<>();
private AttributeMergeStrategy attributeMergeStrategy;
private Banner banner;
private Firework firework;
private LeatherArmor leatherArmor;
private boolean needPlaceholderAPI = false;
private ItemStack cacheItemStack;
private boolean centerName;
private boolean centerLore;
private LoreType loreType = LoreType.REPLACE;
private int maxStackSize;
private int maxDamage;
private int damage;
private int repairCost;
private Boolean unbreakableEnabled;
private Boolean unbreakableShowInTooltip;
private Boolean fireResistant;
private Boolean hideTooltip;
private Boolean hideAdditionalTooltip;
private Boolean enchantmentGlint;
private Boolean enchantmentShowInTooltip;
private Boolean attributeShowInTooltip;
private MenuItemRarity itemRarity;
private TrimConfiguration trimConfiguration;
public ZMenuItemStack(InventoryManager inventoryManager, String filePath, String path) {
this.inventoryManager = inventoryManager;
this.filePath = filePath;
this.path = path;
}
public static ZMenuItemStack fromItemStack(InventoryManager manager, ItemStack itemStack) {
return MenuItemStackFromItemStack.fromItemStack(manager, itemStack);
}
/**
* @return the inventoryManager
*/
public @NonNull InventoryManager getInventoryManager() {
return inventoryManager;
}
@Override
public @NonNull ItemStack build(Player player) {
return build(player, true);
}
@Override
public ItemStack build(BuildContext context) {
Player player = context.getPlayer();
boolean useCache = context.isUseCache();
Placeholders placeholders = context.getPlaceholders();
var performanceDebug = PerformanceDebug.create("menuItemStack:" + path + ":" + filePath);
performanceDebug.start("build.shouldUseCache");
if (shouldUseCache(useCache)) {
performanceDebug.end();
performanceDebug.printSummary();
return this.cacheItemStack;
}
performanceDebug.end();
performanceDebug.start("build.createDefaultMaterial");
if (this.material == null) {
this.material = "STONE";
}
performanceDebug.end();
performanceDebug.start("build.resolveOfflinePlayer");
OfflinePlayer offlinePlayer = resolveOfflinePlayer(player, placeholders);
performanceDebug.end();
performanceDebug.start("build.parseAmount");
int amount = this.parseAmount(offlinePlayer == null ? player : offlinePlayer, placeholders);
performanceDebug.end();
performanceDebug.start("build.createItemStack");
ItemStack itemStack = createItemStack(player, placeholders, offlinePlayer, amount);
performanceDebug.end();
performanceDebug.start("build.applyItemMeta");
applyItemMeta(player, placeholders, offlinePlayer, useCache, itemStack);
performanceDebug.end();
performanceDebug.start("build.applyAttributes");
applyAttributes(itemStack);
performanceDebug.end();
performanceDebug.start("build.itemComponents");
if (!this.itemComponents.isEmpty()) {
for (ItemComponent metadata : this.itemComponents) {
try {
performanceDebug.start("build.itemComponents.apply." + metadata.getParentLoader().getComponentName());
metadata.apply(context, itemStack, player);
performanceDebug.end();
} catch (Exception e) {
if (Configuration.enableDebug) {
Logger.info("Error while applying ItemComponent '" + metadata.getParentLoader().getComponentName() + "' for item " + path + " in file " + filePath + " (" + player + ")", Logger.LogType.ERROR);
e.printStackTrace();
}
}
}
}
performanceDebug.end();
if (!needPlaceholderAPI && Configuration.enableCacheItemStack) {
this.cacheItemStack = itemStack;
}
performanceDebug.printSummary();
return itemStack;
}
@Override
public @NonNull ItemStack build(Player player, boolean useCache) {
return build(player, useCache, new Placeholders());
}
@Override
public @NonNull ItemStack build(Player player, boolean useCache, Placeholders placeholders) {
return this.build(new ZBuildContext.Builder().player(player).useCache(useCache).placeholders(placeholders).build());
}
private boolean shouldUseCache(boolean useCache) {
return !this.needPlaceholderAPI && this.cacheItemStack != null && Configuration.enableCacheItemStack && useCache;
}
private OfflinePlayer resolveOfflinePlayer(Player player, Placeholders placeholders) {
return this.targetPlayer != null ? OfflinePlayerCache.get(papi(placeholders.parse(this.targetPlayer), player, false)) : null;
}
private ItemStack createItemStack(Player player, Placeholders placeholders, OfflinePlayer offlinePlayer, int amount) {
Material material = resolveMaterial(player, placeholders, offlinePlayer);
ItemStack itemStack = resolveCustomItem(player, placeholders, material);
if (itemStack == null) {
itemStack = createDefaultItemStack(player, material, amount);
}
itemStack = applySpecialItemStack(player, amount, itemStack);
if (itemStack == null) {
itemStack = new ItemStack(Material.STONE);
}
itemStack.setAmount(Math.max(1, amount));
if (this.durability != 0) {
itemStack.setDurability((short) this.durability);
}
return itemStack;
}
private Material resolveMaterial(Player player, Placeholders placeholders, OfflinePlayer offlinePlayer) {
String papiMaterial = papi(placeholders.parse(this.material), offlinePlayer == null ? player : offlinePlayer, true);
try {
return getMaterial(Integer.parseInt(papiMaterial));
} catch (Exception ignored) {
}
try {
return papiMaterial != null ? Material.getMaterial(papiMaterial.toUpperCase()) : null;
} catch (Exception ignored) {
return null;
}
}
private ItemStack resolveCustomItem(Player player, Placeholders placeholders, Material material) {
String papiMaterial = papi(placeholders.parse(this.material), player, true);
if (material != null && !material.equals(Material.AIR)) {
return null;
}
if (papiMaterial != null && papiMaterial.contains(":")) {
String[] values = papiMaterial.split(":", 2);
if (values.length == 2) {
String key = values[0];
String value = values[1];
Optional<MaterialLoader> optional = this.inventoryManager.getMaterialLoader(key);
if (optional.isPresent()) {
MaterialLoader loader = optional.get();
return loader.load(player, null, path, value);
}
}
}
return null;
}
private ItemStack createDefaultItemStack(Player player, Material material, int amount) {
if (material == null) {
material = Material.STONE;
}
return data != null && !data.isEmpty() ? new ItemStack(material, amount, Byte.parseByte(this.papi(this.data, player, false))) : new ItemStack(material);
}
private ItemStack applySpecialItemStack(Player player, int amount, ItemStack itemStack) {
if (this.url != null && !url.equalsIgnoreCase("null")) {
String urlResult = this.papi(this.url, player, false);
if (urlResult != null) {
itemStack = this.createSkull(urlResult);
}
}
if (this.potion != null) {
itemStack = potion.toItemStack(amount);
}
if (this.banner != null) {
itemStack = banner.toItemStack(amount);
}
if (this.firework != null) {
itemStack = firework.toItemStack(amount);
}
if (this.leatherArmor != null) {
itemStack = leatherArmor.toItemStack(amount);
}
return itemStack;
}
private void applyItemMeta(Player player, Placeholders placeholders, OfflinePlayer offlinePlayer, boolean useCache, ItemStack itemStack) {
ItemMeta itemMeta = itemStack.getItemMeta();
if (itemMeta == null) {
return;
}
Material finalMaterial = itemStack.getType();
String locale = findPlayerLocale(player);
FontImage fontImage = this.inventoryManager.getFontImage();
applyDisplayNameLore(player, placeholders, itemMeta, offlinePlayer, locale, fontImage, useCache);
applyGlowing(itemMeta);
applyCustomModelData(player, placeholders, offlinePlayer, itemMeta);
applyEnchantments(finalMaterial, itemMeta);
applyFlags(itemMeta);
applyVersionSpecificMeta(itemStack, itemMeta, player, placeholders);
itemStack.setItemMeta(itemMeta);
}
private void applyGlowing(ItemMeta itemMeta) {
if (!this.isGlowing) {
return;
}
Enchantments helperEnchantments = inventoryManager.getEnchantments();
helperEnchantments.getEnchantments("power").ifPresent(menuEnchantment -> {
itemMeta.addEnchant(menuEnchantment.enchantment(), 1, true);
itemMeta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
});
}
private void applyCustomModelData(Player player, Placeholders placeholders, OfflinePlayer offlinePlayer, ItemMeta itemMeta) {
try {
int customModelData = Integer.parseInt(papi(placeholders.parse(this.modelID), offlinePlayer == null ? player : offlinePlayer, true));
if (customModelData != 0) {
itemMeta.setCustomModelData(customModelData);
}
} catch (NumberFormatException ignored) {
}
}
private void applyEnchantments(Material finalMaterial, ItemMeta itemMeta) {
this.enchantments.forEach((enchantment, level) -> {
if (finalMaterial.equals(Material.ENCHANTED_BOOK)) {
((EnchantmentStorageMeta) itemMeta).addStoredEnchant(enchantment, level, true);
} else {
itemMeta.addEnchant(enchantment, level, true);
}
});
}
private void applyFlags(ItemMeta itemMeta) {
for (ItemFlag flag : this.flags) {
itemMeta.addItemFlags(flag);
}
}
private void applyVersionSpecificMeta(ItemStack itemStack, ItemMeta itemMeta, Player player, Placeholders placeholders) {
if (NmsVersion.getCurrentVersion().isNewItemStackAPI()) {
this.buildNewItemStackAPI(itemStack, itemMeta, player, placeholders);
}
if (NmsVersion.getCurrentVersion().isNewHeadApi()) {
this.buildTrimAPI(itemStack, itemMeta, player, placeholders);
}
if (this.clearDefaultAttributes && attributes.isEmpty() && NmsVersion.getCurrentVersion().getVersion() >= NmsVersion.V_1_20_4.getVersion()) {
itemMeta.setAttributeModifiers(ArrayListMultimap.create());
}
}
private void applyAttributes(@NotNull ItemStack itemStack) {
if (this.attributes.isEmpty() || itemStack.getType() == Material.AIR) return;
AttributeUtil.applyAttributes(itemStack, this.attributes, this.inventoryManager.getPlugin(), this.attributeMergeStrategy);
}
/**
* Applies the display name and lore to the item stack using the given Placeholders,
* FontImage, and locale.
*
* @param player the player viewing the item stack.
* @param placeholders the Placeholders to use.
* @param itemMeta the item meta to update.
* @param offlinePlayer the offline player viewing the item stack, or null if not applicable.
* @param locale the locale to use for translation.
* @param fontImage the FontImage to use for replacing font codes.
* @param useCache whether to use the cache for the placeholders.
*/
private void applyDisplayNameLore(Player player, Placeholders placeholders, ItemMeta itemMeta, OfflinePlayer offlinePlayer, String locale, FontImage fontImage, boolean useCache) {
String itemName = null;
List<String> itemLore = new ArrayList<>();
if (this.displayName != null) {
try {
String displayName = locale == null ? this.displayName : this.translatedDisplayName.getOrDefault(locale, this.displayName);
if (displayName != null)
itemName = fontImage.replace(papi(placeholders.parse(displayName), offlinePlayer == null ? player : offlinePlayer, useCache));
} catch (Exception exception) {
Logger.info("Error with update display name for item " + path + " in file " + filePath + " (" + player + ", " + this.displayName + ")", Logger.LogType.ERROR);
exception.printStackTrace();
}
}
if (!this.lore.isEmpty()) {
List<String> lore = papi(placeholders.parse(locale == null ? this.lore : this.translatedLore.getOrDefault(locale, this.lore)), offlinePlayer == null ? player : offlinePlayer, useCache);
List<String> flattened = new ArrayList<>();
for (String str : lore) {
String[] parts = str.split("\n");
for (String part : parts) {
flattened.add(fontImage.replace(part));
}
}
itemLore = flattened;
}
if (itemName != null) {
this.inventoryManager.getMeta().updateDisplayName(itemMeta, itemName, player);
}
if (!itemLore.isEmpty()) {
this.inventoryManager.getMeta().updateLore(itemMeta, itemLore, this.loreType);
}
}
private void buildNewItemStackAPI(ItemStack itemStack, ItemMeta itemMeta, Player player, Placeholders placeholders) {
if (this.maxStackSize > 0 && this.maxStackSize != itemStack.getMaxStackSize()) {
itemMeta.setMaxStackSize(this.maxStackSize);
}
if (itemMeta instanceof Damageable damageable) {
if (this.maxDamage > 0) {
damageable.setMaxDamage(this.maxDamage);
}
if (this.damage != 0) {
damageable.setDamage(this.damage);
}
if (this.unbreakableEnabled != null) {
damageable.setUnbreakable(this.unbreakableEnabled);
if (this.unbreakableShowInTooltip != null && !this.unbreakableShowInTooltip) {
itemMeta.addItemFlags(ItemFlag.HIDE_UNBREAKABLE);
}
}
}
if (itemMeta instanceof Repairable && this.repairCost > 0) {
((Repairable) itemMeta).setRepairCost(this.repairCost);
}
if (this.hideTooltip != null) {
itemMeta.setHideTooltip(this.hideTooltip);
}
if (this.hideAdditionalTooltip != null && this.hideAdditionalTooltip) {
for (ItemFlag value : ItemFlag.values()) {
itemMeta.addItemFlags(value);
}
}
if (this.enchantmentShowInTooltip != null && !this.enchantmentShowInTooltip) {
itemMeta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
}
if (this.enchantmentGlint != null) {
itemMeta.setEnchantmentGlintOverride(this.enchantmentGlint);
}
if (this.fireResistant != null) {
itemMeta.setFireResistant(this.fireResistant);
}
if (this.attributeShowInTooltip != null && !this.attributeShowInTooltip) {
itemMeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
}
if (this.itemRarity != null) {
itemMeta.setRarity(this.itemRarity.getItemRarity());
}
if (this.tooltipStyle != null) {
String[] tooltipstyleSplit = this.tooltipStyle.split(":", 2);
if (tooltipstyleSplit.length == 2) {
itemMeta.setTooltipStyle(new NamespacedKey(tooltipstyleSplit[0], tooltipstyleSplit[1]));
}
}
if (this.itemModel != null) {
itemMeta.setItemModel(this.itemModel);
}
if (this.equippedModel != null) {
var equippedModelSplit = this.equippedModel.split(":", 2);
if (equippedModelSplit.length == 2) {
var equippable = itemMeta.getEquippable();
equippable.setModel(new NamespacedKey(equippedModelSplit[0], equippedModelSplit[1]));
itemMeta.setEquippable(equippable);
}
}
}
private void buildTrimAPI(ItemStack itemStack, ItemMeta itemMeta, Player player, Placeholders placeholders) {
if (itemMeta instanceof ArmorMeta && this.trimConfiguration != null) {
((ArmorMeta) itemMeta).setTrim(new ArmorTrim(this.trimConfiguration.getMaterial(), this.trimConfiguration.getPattern()));
}
}
/**
* @return the target player
*/
public String getTargetPlayer() {
return targetPlayer;
}
/**
* @param targetPlayer the targetPlayer to set
*/
public void setTargetPlayer(String targetPlayer) {
this.targetPlayer = targetPlayer;
}
/**
* @return the material
*/
public String getMaterial() {
return material;
}
/**
* @param material the material to set
*/
public void setMaterial(String material) {
this.material = material;
this.updatePlaceholder(material);
}
/**
* @return the amount
*/
public String getAmount() {
return amount;
}
/**
* @param amount the amount to set
*/
public void setAmount(String amount) {
this.amount = amount;
this.updatePlaceholder(amount);
}
/**
* @return the url
*/
@Nullable
public String getUrl() {
return url;
}
/**
* @param url the url to set
*/
public void setUrl(String url) {
this.url = url;
}
/**
* @return the data
*/
public String getData() {
return data;
}
/**
* @param data the data to set
*/
public void setData(String data) {
this.data = data;
}
/**
* @return the durability
*/
public int getDurability() {
return durability;
}
/**
* @param durability the durability to set
*/
public void setDurability(int durability) {
this.durability = durability;
}
/**
* @return the potion
*/
public Potion getPotion() {
return potion;
}
/**
* @param potion the potion to set
*/
public void setPotion(Potion potion) {
this.potion = potion;
}
/**
* @return the lore
*/
public List<String> getLore() {
return lore;
}
/**
* @param lore the lore to set
*/
public void setLore(List<String> lore) {
this.lore = lore;
lore.forEach(this::updatePlaceholder);
}
/**
* @return the flags
*/
public List<ItemFlag> getFlags() {
return flags;
}
/**
* @param flags the flags to set
*/
public void setFlags(List<ItemFlag> flags) {
this.flags = flags;
}
/**
* @return the displayName
*/
public String getDisplayName() {
return displayName;
}
/**
* @param displayName the displayName to set
*/
public void setDisplayName(String displayName) {
this.displayName = displayName;
this.updatePlaceholder(displayName);
}
/**
* @return the isGlowing
*/
public boolean isGlowing() {
return isGlowing;
}
/**
* @param isGlowing the isGlowing to set
*/
public void setGlowing(boolean isGlowing) {
this.isGlowing = isGlowing;
}
/**
* @return the modelID
*/
public String getModelID() {
return modelID;
}
/**
* @param modelID the modelID to set
*/
public void setModelID(String modelID) {
this.modelID = modelID;
this.updatePlaceholder(modelID);
}
/**
* @param modelID the modelID to set
*/
public void setModelID(int modelID) {
this.modelID = String.valueOf(modelID);
}
/**
* @return the enchantments
*/
public Map<Enchantment, Integer> getEnchantments() {
return enchantments;
}
/**
* @param enchantments the enchantments to set
*/
public void setEnchantments(Map<Enchantment, Integer> enchantments) {
this.enchantments = enchantments;
}
/**
* @return the attributes
*/
public List<AttributeWrapper> getAttributes() {
return attributes;
}
/**
* @param attributes the attributes to set.
*/
public void setAttributes(List<AttributeWrapper> attributes) {
this.attributes = attributes;
}
@Override
public AttributeMergeStrategy getAttributeMergeStrategy() {
return attributeMergeStrategy;
}
@Override
public void setAttributeMergeStrategy(AttributeMergeStrategy attributeMergeStrategy) {
this.attributeMergeStrategy = attributeMergeStrategy;
}
/**
* @return the banner
*/
public Banner getBanner() {
return banner;
}
/**
* @param banner the banner to set
*/
public void setBanner(Banner banner) {
this.banner = banner;
}
/**
* @return the firework
*/
public Firework getFirework() {
return firework;
}
/**
* @param firework the firework to set
*/
public void setFirework(Firework firework) {
this.firework = firework;
}
/**
* @return the leather armor
*/
public LeatherArmor getLeatherArmor() {
return leatherArmor;
}
/**
* @param leatherArmor the leather armor to set
*/
public void setLeatherArmor(LeatherArmor leatherArmor) {
this.leatherArmor = leatherArmor;
}
@Override
public String getToolTipStyle() {
return tooltipStyle;
}
@Override
public void setToolTipStyle(String toolTipStyle) {
this.tooltipStyle = toolTipStyle;
}
@Override
public NamespacedKey getItemModel() {
return itemModel;
}
@Override
public void setItemModel(NamespacedKey itemModel) {
this.itemModel = itemModel;
}
@Override
public @NonNull String getFilePath() {
return filePath;
}
@Override
public String getPath() {
return path;
}
@Override
public int parseAmount(Player player) {
int amount = 1;
try {
amount = Integer.parseInt(papi(this.amount, player, true));
} catch (Exception ignored) {
}
return amount;
}
@Override
public int parseAmount(Player player, Placeholders placeholders) {
int amount = 1;
try {
amount = Integer.parseInt(papi(placeholders.parse(this.amount), player, true));
} catch (Exception ignored) {
}
return amount;
}
@Override
public int parseAmount(OfflinePlayer offlinePlayer, Placeholders placeholders) {
int amount = 1;
try {
amount = Integer.parseInt(papi(placeholders.parse(this.amount), offlinePlayer, true));
} catch (Exception ignored) {
}
return amount;
}
/**
* Let's know if the ItemStack needs a placeholder, if not then the ItemStack will be cached
*
* @param string - Current string
*/
private void updatePlaceholder(String string) {
if (string == null || needPlaceholderAPI) return;
needPlaceholderAPI = string.contains("%");
}
public void setTypeMapAccessor(MapConfiguration configuration) {
setData(configuration.getString("data", "0"));
setDurability(configuration.getInt("durability", 0));
setAmount(configuration.getString("amount", "1"));
setMaterial(configuration.getString("material", null));
setTargetPlayer(configuration.getString("target", null));
setUrl(configuration.getString("url", null));
Color potionColor = getColor(configuration, "color", null);
try {
Material material = Material.valueOf(configuration.getString("material", "").toUpperCase());
String materialName = material.toString();
if (materialName.startsWith("LEATHER_")) {
Color armorColor = getColor(configuration, "color", Color.fromRGB(160, 101, 64));
String type = materialName.replace("LEATHER_", "");
setLeatherArmor(new LeatherArmor(LeatherArmor.ArmorType.valueOf(type), armorColor));
}
} catch (Exception ignored) {
}
if (configuration.contains("potion")) {
PotionType type = PotionType.valueOf(configuration.getString("potion", "REGEN").toUpperCase());
int level = configuration.getInt("level", 1);
boolean splash = configuration.getBoolean("splash", false);
boolean extended = configuration.getBoolean("extended", false);
boolean arrow = configuration.getBoolean("arrow", false);
Potion potion = new Potion(type, level, splash, extended, arrow);
potion.setColor(potionColor);
setPotion(potion);
}
if (configuration.contains("banner")) {
DyeColor dyeColor = DyeColor.valueOf(configuration.getString("banner", "WHITE").toUpperCase());
List<String> stringPattern = configuration.getStringList("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])));
}
setBanner(new Banner(dyeColor, patterns));
}
/*if (configuration.contains("firework")) {
ConfigurationSection section = configuration.getConfigurationSection("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"));
setFirework(new Firework(isStar, builder.build()));
}
}*/
List<String> lore = configuration.getStringList("lore");
if (lore.isEmpty()) {
Object object = configuration.getObject("lore", null);
if (object instanceof String loreString) {
lore = Arrays.asList(loreString.split("\n"));
}
}
setLore(lore);
setDisplayName(configuration.getString("name", null));
setGlowing(configuration.getBoolean("glow"));
setModelID(configuration.getString("modelID", configuration.getString("model-id", configuration.getString("modelId", configuration.getString("customModelId", configuration.getString("customModelData", "0"))))));
List<String> enchants = configuration.getStringList("enchants");
Map<Enchantment, Integer> enchantments = new HashMap<>();
for (String enchantString : enchants) {
try {
String[] splitEnchant = enchantString.split(",");
if (splitEnchant.length == 1) throw new ItemEnchantException("an error occurred while loading the enchantment " + enchantString);
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);
}
Enchantment enchantment = Enchantment.getByName(enchant);
if (enchantment == null) {
throw new ItemEnchantException("an error occurred while loading the enchantment " + enchantString);
}
enchantments.put(enchantment, level);
} catch (ItemEnchantException e) {
e.printStackTrace();
}
}
List<String> flagStrings = configuration.getStringList("flags");
List<ItemFlag> flags = new ArrayList<>(flagStrings.size());
for (String flag : flagStrings) {
flags.add(this.getFlag(flag));
}
List<AttributeWrapper> attributeModifiers = new ArrayList<>();
if (configuration.contains("attributes")) {
List<Map<String, Object>> attributesFromConfig = (List<Map<String, Object>>) configuration.getList("attributes");
if (attributesFromConfig != null) {
for (Map<String, Object> attributeMap : attributesFromConfig) {
attributeModifiers.add(AttributeWrapper.deserialize(attributeMap));
}
}
}
setEnchantments(enchantments);
setFlags(flags);
setAttributes(attributeModifiers);
setAttributeMergeStrategy(AttributeMergeStrategy.valueOf(configuration.getString("attribute-merge-strategy", "REPLACE").toUpperCase()));
}
private Color getColor(MapConfiguration configuration, String key, Color def) {
String[] split = configuration.getString(key, "").split(",");
if (split.length == 3) {
return Color.fromRGB(Integer.parseInt(split[0]), Integer.parseInt(split[1]), Integer.parseInt(split[2]));
} else if (split.length == 4) {
return Color.fromARGB(Integer.parseInt(split[0]), Integer.parseInt(split[1]), Integer.parseInt(split[2]), Integer.parseInt(split[3]));
}
return def;
}
private List<Color> getColors(MapConfiguration 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;
}
public Map<String, String> getTranslatedDisplayName() {
return translatedDisplayName;
}
public void setTranslatedDisplayName(Map<String, String> translatedDisplayName) {
this.translatedDisplayName = translatedDisplayName;
}
public Map<String, List<String>> getTranslatedLore() {
return translatedLore;