-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Utils.java
925 lines (766 loc) · 23.8 KB
/
Utils.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
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
package world.bentobox.challenges.utils;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.Statistic;
import org.bukkit.World;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.potion.PotionData;
import org.bukkit.potion.PotionType;
import org.eclipse.jdt.annotation.Nullable;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.hooks.LangUtilsHook;
import world.bentobox.bentobox.util.Util;
/**
* Util methods used in different situations.
*/
public class Utils
{
/**
* This method checks if 2 given item stacks are similar without durability check.
* @param input First item.
* @param stack Second item.
* @return {@code true} if items are equal, {@code false} otherwise.
*/
public static boolean isSimilarNoDurability(@Nullable ItemStack input, @Nullable ItemStack stack)
{
if (stack == null || input == null)
{
return false;
}
else if (stack == input)
{
return true;
}
else
{
return input.getType() == stack.getType() &&
input.hasItemMeta() == stack.hasItemMeta() &&
(!input.hasItemMeta() || Bukkit.getItemFactory().equals(input.getItemMeta(), stack.getItemMeta()));
}
}
/**
* This method groups input items in single itemstack with correct amount and returns it.
* Allows to remove duplicate items from list.
* @param requiredItems Input item list
* @return List that contains unique items that cannot be grouped.
*/
public static List<ItemStack> groupEqualItems(List<ItemStack> requiredItems, Set<Material> ignoreMetaData)
{
List<ItemStack> returnItems = new ArrayList<>(requiredItems.size());
// Group all equal items in single stack, as otherwise it will be too complicated to check if all
// items are in players inventory.
for (ItemStack item : requiredItems)
{
boolean isUnique = item != null;
int i = 0;
final int requiredSize = returnItems.size();
while (i < requiredSize && isUnique)
{
ItemStack required = returnItems.get(i);
// Merge items which meta can be ignored or is similar to item in required list.
if (Utils.isSimilarNoDurability(required, item) ||
ignoreMetaData.contains(item.getType()) && item.getType().equals(required.getType()))
{
required.setAmount(required.getAmount() + item.getAmount());
isUnique = false;
}
i++;
}
if (isUnique)
{
// The same issue as in other places. Clone prevents from changing original item.
returnItems.add(item.clone());
}
}
return returnItems;
}
/**
* This method transforms given World into GameMode name. If world is not a GameMode
* world then it returns null.
* @param world World which gameMode name must be found out.
* @return GameMode name or null.
*/
public static String getGameMode(World world)
{
return BentoBox.getInstance().getIWM().getAddon(world).
map(gameModeAddon -> gameModeAddon.getDescription().getName()).
orElse(null);
}
/**
* This method allows to get next value from array list after given value.
* @param values Array that should be searched for given value.
* @param currentValue Value which next element should be found.
* @param <T> Instance of given object.
* @return Next value after currentValue in values array.
*/
public static <T> T getNextValue(T[] values, T currentValue)
{
for (int i = 0; i < values.length; i++)
{
if (values[i].equals(currentValue))
{
if (i + 1 == values.length)
{
return values[0];
}
else
{
return values[i + 1];
}
}
}
return currentValue;
}
/**
* This method allows to get previous value from array list after given value.
* @param values Array that should be searched for given value.
* @param currentValue Value which previous element should be found.
* @param <T> Instance of given object.
* @return Previous value before currentValue in values array.
*/
public static <T> T getPreviousValue(T[] values, T currentValue)
{
for (int i = 0; i < values.length; i++)
{
if (values[i].equals(currentValue))
{
if (i > 0)
{
return values[i - 1];
}
else
{
return values[values.length - 1];
}
}
}
return currentValue;
}
/**
* Sanitizes the provided input. It replaces spaces and hyphens with underscores and lower cases the input.
* This code also removes all color codes from the input.
* @param input input to sanitize
* @return sanitized input
*/
public static String sanitizeInput(String input)
{
return ChatColor.stripColor(
Util.translateColorCodes(input.toLowerCase(Locale.ENGLISH).
replace(" ", "_").
replace("-", "_")));
}
/**
* Send given message to user and add prefix to the start of the message.
*
* @param user User who need to receive message.
* @param world Reference to world where message must be send.
* @param translation String of message that must be send.
* @param parameters Parameters that must be added to translation.
*/
public static void sendMessage(User user, World world, String translation, String... parameters)
{
user.sendMessage(user.getTranslation(world, Constants.CONVERSATIONS + "prefix") +
user.getTranslation(world, translation, parameters));
}
/**
* Prettify World.Environment object for user.
* @param object Object that must be pretty.
* @param user User who will see the object.
* @return Prettified string for World.Environment.
*/
public static String prettifyObject(World.Environment object, User user)
{
// Find addon structure with:
// [addon]:
// environments:
// [environment]:
// name: [name]
String translation = user.getTranslationOrNothing(Constants.ENVIRONMENTS + object.name().toLowerCase() + ".name");
if (!translation.isEmpty())
{
// We found our translation.
return translation;
}
// Find addon structure with:
// [addon]:
// environments:
// [environment]: [name]
translation = user.getTranslationOrNothing(Constants.ENVIRONMENTS + object.name().toLowerCase());
if (!translation.isEmpty())
{
// We found our translation.
return translation;
}
// Find general structure with:
// environments:
// [environment]: [name]
translation = user.getTranslationOrNothing("environments." + object.name().toLowerCase());
if (!translation.isEmpty())
{
// We found our translation.
return translation;
}
// Lang Utils do not have Environment :(
//LangUtilsHook.getEnvrionmentName(object, user);
// Nothing was found. Use just a prettify text function.
return Util.prettifyText(object.name());
}
/**
* Prettify World.Environment object description for user.
* @param object Object that must be pretty.
* @param user User who will see the object.
* @return Prettified description string for World.Environment.
*/
public static String prettifyDescription(World.Environment object, User user)
{
// Find addon structure with:
// [addon]:
// environments:
// [environment]:
// description: [text]
String translation = user.getTranslationOrNothing(Constants.ENVIRONMENTS + object.name().toLowerCase() + ".description");
if (!translation.isEmpty())
{
// We found our translation.
return translation;
}
// No text to return.
return "";
}
/**
* Prettify Material object for user.
* @param object Object that must be pretty.
* @param user User who will see the object.
* @return Prettified string for Material.
*/
public static String prettifyObject(@Nullable Material object, User user)
{
// Nothing to translate
if (object == null)
{
return "";
}
// Find addon structure with:
// [addon]:
// materials:
// [material]:
// name: [name]
String translation = user.getTranslationOrNothing(Constants.MATERIALS + object.name().toLowerCase() + ".name");
if (!translation.isEmpty())
{
// We found our translation.
return translation;
}
// Find addon structure with:
// [addon]:
// materials:
// [material]: [name]
translation = user.getTranslationOrNothing(Constants.MATERIALS + object.name().toLowerCase());
if (!translation.isEmpty())
{
// We found our translation.
return translation;
}
// Find general structure with:
// materials:
// [material]: [name]
translation = user.getTranslationOrNothing("materials." + object.name().toLowerCase());
if (!translation.isEmpty())
{
// We found our translation.
return translation;
}
// Use Lang Utils Hook to translate material
return LangUtilsHook.getMaterialName(object, user);
}
/**
* Prettify Material object description for user.
* @param object Object that must be pretty.
* @param user User who will see the object.
* @return Prettified description string for Material.
*/
public static String prettifyDescription(@Nullable Material object, User user)
{
// Nothing to translate
if (object == null)
{
return "";
}
// Find addon structure with:
// [addon]:
// materials:
// [material]:
// description: [text]
String translation = user.getTranslationOrNothing(Constants.MATERIALS + object.name().toLowerCase() + ".description");
if (!translation.isEmpty())
{
// We found our translation.
return translation;
}
// No text to return.
return "";
}
/**
* Prettify EntityType object for user.
* @param object Object that must be pretty.
* @param user User who will see the object.
* @return Prettified string for EntityType.
*/
public static String prettifyObject(@Nullable EntityType object, User user)
{
// Nothing to translate
if (object == null)
{
return "";
}
// Find addon structure with:
// [addon]:
// entities:
// [entity]:
// name: [name]
String translation = user.getTranslationOrNothing(Constants.ENTITIES + object.name().toLowerCase() + ".name");
if (!translation.isEmpty())
{
// We found our translation.
return translation;
}
// Find addon structure with:
// [addon]:
// entities:
// [entity]: [name]
translation = user.getTranslationOrNothing(Constants.ENTITIES + object.name().toLowerCase());
if (!translation.isEmpty())
{
// We found our translation.
return translation;
}
// Find general structure with:
// entities:
// [entity]: [name]
translation = user.getTranslationOrNothing("entities." + object.name().toLowerCase());
if (!translation.isEmpty())
{
// We found our translation.
return translation;
}
// Use Lang Utils Hook to translate material
return LangUtilsHook.getEntityName(object, user);
}
/**
* Prettify EntityType object description for user.
* @param object Object that must be pretty.
* @param user User who will see the object.
* @return Prettified description string for EntityType.
*/
public static String prettifyDescription(@Nullable EntityType object, User user)
{
// Nothing to translate
if (object == null)
{
return "";
}
// Find addon structure with:
// [addon]:
// entities:
// [entity]:
// description: [text]
String translation = user.getTranslationOrNothing(Constants.ENTITIES + object.name().toLowerCase() + ".description");
if (!translation.isEmpty())
{
// We found our translation.
return translation;
}
// No text to return.
return "";
}
/**
* Prettify Statistic object for user.
* @param object Object that must be pretty.
* @param user User who will see the object.
* @return Prettified string for Statistic.
*/
public static String prettifyObject(@Nullable Statistic object, User user)
{
// Nothing to translate
if (object == null)
{
return "";
}
// Find addon structure with:
// [addon]:
// statistics:
// [statistic]:
// name: [name]
String translation = user.getTranslationOrNothing(Constants.STATISTICS + object.name().toLowerCase() + ".name");
if (!translation.isEmpty())
{
// We found our translation.
return translation;
}
// Find addon structure with:
// [addon]:
// statistics:
// [statistic]: [name]
translation = user.getTranslationOrNothing(Constants.STATISTICS + object.name().toLowerCase());
if (!translation.isEmpty())
{
// We found our translation.
return translation;
}
// Find general structure with:
// statistics:
// [statistic]: [name]
translation = user.getTranslationOrNothing("statistics." + object.name().toLowerCase());
if (!translation.isEmpty())
{
// We found our translation.
return translation;
}
// Use Lang Utils Hook to translate material
//return LangUtilsHook.getStatisticName(object, user);
return Util.prettifyText(object.name());
}
/**
* Prettify Statistic object description for user.
* @param object Object that must be pretty.
* @param user User who will see the object.
* @return Prettified description string for Statistic.
*/
public static String prettifyDescription(@Nullable Statistic object, User user)
{
// Nothing to translate
if (object == null)
{
return "";
}
// Find addon structure with:
// [addon]:
// statistics:
// [statistic]:
// description: [text]
String translation = user.getTranslationOrNothing(Constants.STATISTICS + object.name().toLowerCase() + ".description");
if (!translation.isEmpty())
{
// We found our translation.
return translation;
}
// No text to return.
return "";
}
/**
* Prettify ItemStack object for user.
* @param object Object that must be pretty.
* @param user User who will see the object.
* @return Prettified string for ItemStack.
*/
public static String prettifyObject(@Nullable ItemStack object, User user)
{
// Nothing to translate
if (object == null)
{
return "";
}
// Find addon structure with:
// [addon]:
// item-stacks:
// [material]: ...
// meta:
// potion-type: ...
// ...
// generic: [amount] [name] [meta]
String translation;
switch (object.getType())
{
case POTION, SPLASH_POTION, LINGERING_POTION, TIPPED_ARROW ->
// Get Potion Meta
translation = prettifyObject(object, (PotionMeta) object.getItemMeta(), user);
case PLAYER_HEAD, PLAYER_WALL_HEAD ->
translation = prettifyObject(object, (SkullMeta) object.getItemMeta(), user);
case ENCHANTED_BOOK ->
translation = prettifyObject(object, (EnchantmentStorageMeta) object.getItemMeta(), user);
case WRITTEN_BOOK, WRITABLE_BOOK ->
translation = prettifyObject(object, (BookMeta) object.getItemMeta(), user);
case LEATHER_BOOTS,LEATHER_CHESTPLATE,LEATHER_HELMET,LEATHER_LEGGINGS,LEATHER_HORSE_ARMOR,
TRIDENT,CROSSBOW,CHAINMAIL_HELMET,CHAINMAIL_CHESTPLATE,CHAINMAIL_LEGGINGS,CHAINMAIL_BOOTS,IRON_HELMET,
IRON_CHESTPLATE,IRON_LEGGINGS,IRON_BOOTS,DIAMOND_HELMET,DIAMOND_CHESTPLATE,DIAMOND_LEGGINGS,DIAMOND_BOOTS,
GOLDEN_HELMET,GOLDEN_CHESTPLATE,GOLDEN_LEGGINGS,GOLDEN_BOOTS,NETHERITE_HELMET,NETHERITE_CHESTPLATE,
NETHERITE_LEGGINGS,NETHERITE_BOOTS,WOODEN_SWORD,WOODEN_SHOVEL,WOODEN_PICKAXE,WOODEN_AXE,WOODEN_HOE,
STONE_SWORD,STONE_SHOVEL,STONE_PICKAXE,STONE_AXE,STONE_HOE,GOLDEN_SWORD,GOLDEN_SHOVEL,GOLDEN_PICKAXE,
GOLDEN_AXE,GOLDEN_HOE,IRON_SWORD,IRON_SHOVEL,IRON_PICKAXE,IRON_AXE,IRON_HOE,DIAMOND_SWORD,DIAMOND_SHOVEL,
DIAMOND_PICKAXE,DIAMOND_AXE,DIAMOND_HOE,NETHERITE_SWORD,NETHERITE_SHOVEL,NETHERITE_PICKAXE,NETHERITE_AXE,
NETHERITE_HOE,TURTLE_HELMET,SHEARS,SHIELD,FLINT_AND_STEEL,BOW ->
translation = prettifyObject(object, object.getItemMeta(), user);
default ->
translation = "";
}
if (!translation.isEmpty())
{
// We found our translation.
return translation;
}
// Find addon structure with:
// [addon]:
// materials:
// [material]: [name]
translation = user.getTranslationOrNothing(Constants.MATERIALS + object.getType().name().toLowerCase());
if (!translation.isEmpty())
{
// We found our translation.
return translation;
}
// Find general structure with:
// materials:
// [material]: [name]
translation = user.getTranslationOrNothing("materials." + object.getType().name().toLowerCase());
if (!translation.isEmpty())
{
// We found our translation.
return translation;
}
// Use Lang Utils
return LangUtilsHook.getItemDisplayName(object, user);
}
/**
* Prettify enchant string.
*
* @param enchantment the enchantment
* @param user the user
* @return the string
*/
public static String prettifyObject(Enchantment enchantment, User user)
{
if (enchantment == null)
{
return "";
}
String type = user.getTranslationOrNothing(Constants.ITEM_STACKS + "enchant." + enchantment.getKey().getKey());
if (type.isEmpty())
{
type = LangUtilsHook.getEnchantName(enchantment, user);
}
return type;
}
/**
* Prettify type string.
*
* @param type the potion type
* @param user the user
* @return the string
*/
public static String prettifyObject(PotionType type, User user)
{
if (type == null)
{
return "";
}
String text = user.getTranslationOrNothing(Constants.ITEM_STACKS + "potion-type." + type.name().toLowerCase());
if (text.isEmpty())
{
text = LangUtilsHook.getPotionBaseEffectName(type, user);
}
return text;
}
/**
* Prettify potion item string.
*
* @param item the item
* @param potionMeta the potion meta
* @param user the user
* @return the string
*/
public static String prettifyObject(ItemStack item, @Nullable PotionMeta potionMeta, User user)
{
if (potionMeta == null)
{
return "";
}
Material itemType = item.getType();
final String itemReference = Constants.ITEM_STACKS + itemType.name().toLowerCase() + ".";
final String metaReference = Constants.ITEM_STACKS + "meta.";
PotionData potionData = potionMeta.getBasePotionData();
// Check custom translation for potions.
String type = user.getTranslationOrNothing(itemReference + potionData.getType().name().toLowerCase());
if (type.isEmpty())
{
// Check potion types translation.
type = prettifyObject(potionData.getType(), user);
}
String upgraded = user.getTranslationOrNothing(metaReference + "upgraded");
String extended = user.getTranslationOrNothing(metaReference + "extended");
// Get item specific translation.
String specific = user.getTranslationOrNothing(itemReference + "name",
"[type]", type,
"[upgraded]", (potionData.isUpgraded() ? upgraded : ""),
"[extended]", (potionData.isExtended() ? extended : ""));
if (specific.isEmpty())
{
// Use generic translation.
String meta = user.getTranslationOrNothing(metaReference + "potion-meta",
"[type]", type,
"[upgraded]", (potionData.isUpgraded() ? upgraded : ""),
"[extended]", (potionData.isExtended() ? extended : ""));
specific = user.getTranslationOrNothing(Constants.ITEM_STACKS + "generic",
"[type]", prettifyObject(itemType, user),
"[meta]", meta);
}
return specific;
}
/**
* Prettify skull item string.
*
* @param item the item
* @param skullMeta the skull meta
* @param user the user
* @return the string
*/
public static String prettifyObject(ItemStack item, @Nullable SkullMeta skullMeta, User user)
{
if (skullMeta == null)
{
return "";
}
Material itemType = item.getType();
final String metaReference = Constants.ITEM_STACKS + "meta.";
String meta = user.getTranslationOrNothing(metaReference + "skull-meta",
"[player-name]", skullMeta.getDisplayName());
return user.getTranslationOrNothing(Constants.ITEM_STACKS + "generic",
"[type]", prettifyObject(itemType, user),
"[meta]", meta);
}
/**
* Prettify item string.
*
* @param item the item
* @param itemMeta the item meta
* @param user the user
* @return the string
*/
public static String prettifyObject(ItemStack item, @Nullable ItemMeta itemMeta, User user)
{
if (itemMeta == null)
{
return "";
}
StringBuilder builder = new StringBuilder();
itemMeta.getEnchants().forEach((enchantment, level) -> {
builder.append("\n");
builder.append(user.getTranslationOrNothing(Constants.ITEM_STACKS + "meta.enchant-meta",
"[type]", prettifyObject(enchantment, user),
"[level]", String.valueOf(level)));
});
Material itemType = item.getType();
final String itemReference = Constants.ITEM_STACKS + itemType.name().toLowerCase() + ".";
String translation = user.getTranslationOrNothing(itemReference + "name",
"[type]", prettifyObject(itemType, user),
"[enchant]", builder.toString());
if (translation.isEmpty())
{
translation = user.getTranslationOrNothing(Constants.ITEM_STACKS + "generic",
"[type]", prettifyObject(itemType, user),
"[meta]", builder.toString());
}
return translation;
}
/**
* Prettify enchantment storage string.
*
* @param item the item
* @param enchantmentMeta the enchantment storage meta
* @param user the user
* @return the string
*/
public static String prettifyObject(ItemStack item, @Nullable EnchantmentStorageMeta enchantmentMeta, User user)
{
if (enchantmentMeta == null)
{
return "";
}
StringBuilder builder = new StringBuilder();
enchantmentMeta.getStoredEnchants().forEach((enchantment, level) -> {
builder.append("\n");
builder.append(user.getTranslationOrNothing(Constants.ITEM_STACKS + "meta.enchant-meta",
"[type]", prettifyObject(enchantment, user),
"[level]", String.valueOf(level)));
});
Material itemType = item.getType();
final String itemReference = Constants.ITEM_STACKS + itemType.name().toLowerCase() + ".";
String translation = user.getTranslationOrNothing(itemReference + "name",
"[type]", prettifyObject(itemType, user),
"[enchant]", builder.toString());
if (translation.isEmpty())
{
translation = user.getTranslationOrNothing(Constants.ITEM_STACKS + "generic",
"[type]", prettifyObject(itemType, user),
"[meta]", builder.toString());
}
return translation;
}
/**
* Prettify book item string.
*
* @param item the item
* @param bookMeta the book meta
* @param user the user
* @return the string
*/
public static String prettifyObject(ItemStack item, @Nullable BookMeta bookMeta, User user)
{
if (bookMeta == null)
{
return "";
}
Material itemType = item.getType();
final String metaReference = Constants.ITEM_STACKS + "meta.";
String meta = user.getTranslationOrNothing(metaReference + "book-meta",
"[title]", bookMeta.hasTitle() ? bookMeta.getTitle() : "",
"[author]", bookMeta.hasAuthor() ? bookMeta.getAuthor() : "");
return user.getTranslationOrNothing(Constants.ITEM_STACKS + "generic",
"[type]", prettifyObject(itemType, user),
"[meta]", meta);
}
/**
* This method parses duration to a readable format.
* @param duration that needs to be parsed.
* @return parsed duration string.
*/
public static String parseDuration(Duration duration, User user)
{
final String reference = Constants.DESCRIPTIONS + "challenge.cooldown.";
String returnString = "";
if (duration.toDays() > 0)
{
returnString += user.getTranslationOrNothing(reference + "in-days",
Constants.PARAMETER_NUMBER, String.valueOf(duration.toDays()));
}
if (duration.toHoursPart() > 0)
{
returnString += user.getTranslationOrNothing(reference + "in-hours",
Constants.PARAMETER_NUMBER, String.valueOf(duration.toHoursPart()));
}
if (duration.toMinutesPart() > 0)
{
returnString += user.getTranslationOrNothing(reference + "in-minutes",
Constants.PARAMETER_NUMBER, String.valueOf(duration.toMinutesPart()));
}
if (duration.toSecondsPart() > 0 || returnString.isBlank())
{
returnString += user.getTranslationOrNothing(reference + "in-seconds",
Constants.PARAMETER_NUMBER, String.valueOf(duration.toSecondsPart()));
}
return returnString;
}
}