-
-
Notifications
You must be signed in to change notification settings - Fork 134
/
LangUtilsHook.java
633 lines (577 loc) · 24.2 KB
/
LangUtilsHook.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
package world.bentobox.bentobox.hooks;
import java.util.Locale;
import java.util.Map.Entry;
import java.util.logging.Logger;
import org.bukkit.DyeColor;
import org.bukkit.Material;
import org.bukkit.block.Biome;
import org.bukkit.block.banner.Pattern;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.TropicalFish;
import org.bukkit.entity.Villager;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.TropicalFishBucketMeta;
import org.bukkit.plugin.Plugin;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.potion.PotionType;
import org.jetbrains.annotations.Nullable;
import com.meowj.langutils.lang.LanguageHelper;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.hooks.Hook;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.util.Util;
/**
* @author ApacheZy
* @since 1.6.0
*/
@SuppressWarnings("unused")
public class LangUtilsHook extends Hook {
private static boolean hooked;
public LangUtilsHook() {
super("LangUtils", Material.BOOK);
}
private static boolean doHook(Plugin plugin) {
// Because there are other plugins with the same name,
// we should check here whether it is the plugin we need.
if (plugin != null && plugin.isEnabled()) {
String tag = plugin.getConfig().getString("Extra-TAG");
if ("tag_r72EhIAL".equals(tag)) {
hooked = true;
return true;
}
Logger logger = BentoBox.getInstance().getLogger();
logger.warning("This LangUtils version is not available for BentoBox.");
logger.warning("Please go here to download the latest version:");
logger.warning("https://github.com/apachezy/LangUtils/releases");
}
hooked = false;
return false;
}
@Override
public boolean hook() {
return LangUtilsHook.doHook(getPlugin());
}
@Override
public String getFailureCause() {
return "The LangUtils version does not apply to BentoBox.";
}
/**
* Sometimes it is necessary to check whether "LangUtils" exists
* first to decide what method to use to complete the work.
*
* @return LangUtils is loaded correctly.
*/
public static boolean isHooked() {
return hooked;
}
private static String getUserLocale(User user) {
return user.getLocale().toLanguageTag();
}
/**
* Get the item display name.
*
* If the item contains a custom name, return its custom name.
* If the item itself does not have a custom name, the material
* name will be translated and returned.
*
* @param item The item
* @param user the User's locale will be used for translation.
* @return The Display-Name of the item.
*/
public static String getItemDisplayName(ItemStack item, User user) {
if (hooked) {
return LanguageHelper.getItemDisplayName(item, getUserLocale(user));
}
ItemMeta meta = item.getItemMeta();
if (meta != null && meta.hasDisplayName()) {
String dname = meta.getDisplayName();
if (!dname.isEmpty()) {
return dname;
}
}
return Util.prettifyText(item.getType().name());
}
/**
* Name translation of ItemStack.
* <p>
* Translate the material names of the items so that players can
* see the names they know well.
*
* @param itemStack the ItemStack whose name will be translated.
* @param user the User's locale will be used for translation.
* @return The translated item name.
*/
public static String getItemName(ItemStack itemStack, User user) {
return hooked
? LanguageHelper.getItemName(itemStack, getUserLocale(user))
: Util.prettifyText(itemStack.getType().name());
}
/**
* Name translation of Bukkit material.
* <p>
* Translate the material names of the items so that players can
* see the names they know well.
*
* @param material the Bukkit material whose name will be translated.
* @param user the User's locale will be used for translation.
* @return The translated material name.
*/
public static String getMaterialName(Material material, User user) {
return hooked
? LanguageHelper.getMaterialName(material, getUserLocale(user))
: Util.prettifyText(material.name());
}
/**
* Return the display name of the entity.
*
* @param entity The entity
* @param user the User's locale will be used for translation.
* @return The name of the entity
*/
public static String getEntityDisplayName(Entity entity, User user) {
return entity.getCustomName() != null
? entity.getCustomName()
: getEntityName(entity, user);
}
/**
* Translate the name of the entity type.
*
* @param entityType the EntityType whose name will be translated.
* @param user the User's locale will be used for translation.
* @return The translated EntityType name.
*/
public static String getEntityName(EntityType entityType, User user) {
return hooked
? LanguageHelper.getEntityName(entityType, getUserLocale(user))
: Util.prettifyText(entityType.toString());
}
/**
* Translate the name of the entity type.
*
* @param entity the EntityType whose name will be translated.
* @param user the User's locale will be used for translation.
* @return The translated EntityType name.
*/
public static String getEntityName(Entity entity, User user) {
return hooked
? LanguageHelper.getEntityName(entity, getUserLocale(user))
: Util.prettifyText(entity.getType().toString());
}
/**
* Translate the name of the Biome.
*
* @param biome the Biome whose name will be translated.
* @param user the User's locale will be used for translation.
* @return The translated Biome name.
*/
public static String getBiomeName(Biome biome, User user) {
return hooked
? LanguageHelper.getBiomeName(biome, getUserLocale(user))
: Util.prettifyText(biome.name());
}
/**
* Return the display name of the enchantment(with level).
*
* @param ench The enchantment.
* @param level The enchantment level.
* @param user The User's locale will be used for translation.
* @return Translated enchanted name with level.
*/
public static String getEnchantDisplayName(Enchantment ench, int level, User user) {
return hooked
? LanguageHelper.getEnchantmentDisplayName(ench, level, getUserLocale(user))
: Util.prettifyText(ench.getKey().getKey()) + " " + level;
}
/**
* Return the display name of the enchantment(with level).
*
* @param entry The Entry of an enchantment with level The type
* is {@code Map.Entry<Enchantment, Integer>}
* @param user The User's locale will be used for translation.
* @return Translated enchanted name with level.
*/
public static String getEnchantDisplayName(Entry<Enchantment, Integer> entry, User user) {
return hooked
? LanguageHelper.getEnchantmentDisplayName(entry, getUserLocale(user))
: Util.prettifyText(entry.getKey().getKey().getKey()) + " " + entry.getValue();
}
/**
* Return the name of the enchantment.
*
* @param enchant The enchantment.
* @param user The User's locale will be used for translation.
* @return The translated enchant name.
*/
public static String getEnchantName(Enchantment enchant, User user) {
return hooked
? LanguageHelper.getEnchantmentName(enchant, getUserLocale(user))
: Util.prettifyText(enchant.getKey().getKey());
}
/**
* Return the enchantment level indicated by Roman numerals.
* Can only get Roman numerals within 10.
*
* @param level The enchantment level.
* @param user The user's language will be used for translation.
* @return The converted enchantment level.
*/
public static String getEnchantLevelName(int level, User user) {
return hooked
? LanguageHelper.getEnchantmentLevelName(level, getUserLocale(user))
: String.valueOf(level);
}
/**
* Translate the name of the potion.
*
* @param potionType The type of the potion.
* @param user The user's language will be used for translation.
* @return Translated potion name.
*/
public static String getPotionTypeName(PotionType potionType, User user) {
if (hooked) {
return LanguageHelper.getPotionName(potionType, getUserLocale(user));
}
return switch (potionType) {
case UNCRAFTABLE -> "Uncraftable Potion";
case WATER -> "Water Bottle";
case MUNDANE -> "Mundane Potion";
case THICK -> "Thick Potion";
case AWKWARD -> "Awkward Potion";
case NIGHT_VISION -> "Potion of Night Vision";
case INVISIBILITY -> "Potion of Invisibility";
case JUMP -> "Potion of Leaping";
case FIRE_RESISTANCE -> "Potion of Fire Resistance";
case SPEED -> "Potion of Swiftness";
case SLOWNESS -> "Potion of Slowness";
case WATER_BREATHING -> "Potion of Water Breathing";
case INSTANT_HEAL -> "Potion of Healing";
case INSTANT_DAMAGE -> "Potion of Harming";
case POISON -> "Potion of Poison";
case REGEN -> "Potion of Regeneration";
case STRENGTH -> "Potion of Strength";
case WEAKNESS -> "Potion of Weakness";
case LUCK -> "Potion of Luck";
case TURTLE_MASTER -> "Potion of the Turtle Master";
case SLOW_FALLING -> "Potion of Slow Falling";
};
}
/**
* Translate the name of the splash potion.
*
* @param potionType The type of the splash potion.
* @param user The user's language will be used for translation.
* @return Translated splash potion name.
*/
public static String getSplashPotionName(PotionType potionType, User user) {
if (hooked) {
return LanguageHelper.getSplashPotionName(potionType, getUserLocale(user));
}
return switch (potionType) {
case UNCRAFTABLE -> "Splash Uncraftable Potion";
case WATER -> "Splash Water Bottle";
case MUNDANE -> "Mundane Splash Potion";
case THICK -> "Thick Splash Potion";
case AWKWARD -> "Awkward Splash Potion";
case NIGHT_VISION -> "Splash Potion of Night Vision";
case INVISIBILITY -> "Splash Potion of Invisibility";
case JUMP -> "Splash Potion of Leaping";
case FIRE_RESISTANCE -> "Splash Potion of Fire Resistance";
case SPEED -> "Splash Potion of Swiftness";
case SLOWNESS -> "Splash Potion of Slowness";
case WATER_BREATHING -> "Splash Potion of Water Breathing";
case INSTANT_HEAL -> "Splash Potion of Healing";
case INSTANT_DAMAGE -> "Splash Potion of Harming";
case POISON -> "Splash Potion of Poison";
case REGEN -> "Splash Potion of Regeneration";
case STRENGTH -> "Splash Potion of Strength";
case WEAKNESS -> "Splash Potion of Weakness";
case LUCK -> "Splash Potion of Luck";
case TURTLE_MASTER -> "Splash Potion of the Turtle Master";
case SLOW_FALLING -> "Splash Potion of Slow Falling";
};
}
/**
* Translate the name of the lingering potion.
*
* @param potionType The type of lingering potion.
* @param user The user's language will be used for translation.
* @return Translated lingering potion name.
*/
public static String getLingeringPotionName(PotionType potionType, User user) {
if (hooked) {
return LanguageHelper.getLingeringPotionName(potionType, getUserLocale(user));
}
return switch (potionType) {
case UNCRAFTABLE -> "Lingering Uncraftable Potion";
case WATER -> "Lingering Water Bottle";
case MUNDANE -> "Mundane Lingering Potion";
case THICK -> "Thick Lingering Potion";
case AWKWARD -> "Awkward Lingering Potion";
case NIGHT_VISION -> "Lingering Potion of Night Vision";
case INVISIBILITY -> "Lingering Potion of Invisibility";
case JUMP -> "Lingering Potion of Leaping";
case FIRE_RESISTANCE -> "Lingering Potion of Fire Resistance";
case SPEED -> "Lingering Potion of Swiftness";
case SLOWNESS -> "Lingering Potion of Slowness";
case WATER_BREATHING -> "Lingering Potion of Water Breathing";
case INSTANT_HEAL -> "Lingering Potion of Healing";
case INSTANT_DAMAGE -> "Lingering Potion of Harming";
case POISON -> "Lingering Potion of Poison";
case REGEN -> "Lingering Potion of Regeneration";
case STRENGTH -> "Lingering Potion of Strength";
case WEAKNESS -> "Lingering Potion of Weakness";
case LUCK -> "Lingering Potion of Luck";
case TURTLE_MASTER -> "Lingering Potion of the Turtle Master";
case SLOW_FALLING -> "Lingering Potion of Slow Falling";
};
}
/**
* Translate the name of the tipped arrow.
*
* @param potionType Potion type of tipped arrow.
* @param user The user's language will be used for translation.
* @return Translated tipped arrow name.
*/
public static String getTippedArrowName(PotionType potionType, User user) {
if (hooked) {
return LanguageHelper.getTippedArrowName(potionType, getUserLocale(user));
}
return switch (potionType) {
case UNCRAFTABLE -> "Uncraftable Tipped Arrow";
case WATER -> "Arrow of Splashing";
case MUNDANE, THICK, AWKWARD -> "Tipped Arrow";
case NIGHT_VISION -> "Arrow of Night Vision";
case INVISIBILITY -> "Arrow of Invisibility";
case JUMP -> "Arrow of Leaping";
case FIRE_RESISTANCE -> "Arrow of Fire Resistance";
case SPEED -> "Arrow of Swiftness";
case SLOWNESS -> "Arrow of Slowness";
case WATER_BREATHING -> "Arrow of Water Breathing";
case INSTANT_HEAL -> "Arrow of Healing";
case INSTANT_DAMAGE -> "Arrow of Harming";
case POISON -> "Arrow of Poison";
case REGEN -> "Arrow of Regeneration";
case STRENGTH -> "Arrow of Strength";
case WEAKNESS -> "Arrow of Weakness";
case LUCK -> "Arrow of Luck";
case TURTLE_MASTER -> "Arrow of the Turtle Master";
case SLOW_FALLING -> "Arrow of Slow Falling";
};
}
/**
* Translate the name of the base effect of the potion. If the PotionType
* has no base effect, the translation of "No Effects" is returned. e.g.
* Water Bottle, Mundane Potion.
*
* @param potionType The basic effect of PotionType.
* @param user The user's language will be used for translation.
* @return Return the translation result.
*/
public static String getPotionBaseEffectName(PotionType potionType, User user) {
if (hooked) {
return LanguageHelper.getPotionBaseEffectName(potionType, getUserLocale(user));
}
PotionEffectType effectType = potionType.getEffectType();
if (effectType == null) {
return "No Effects";
}
return Util.prettifyText(effectType.getName());
}
/**
* Translate the name of the potion effect.
*
* @param effectType The potion effect.
* @param user The user's language will be used for translation.
* @return Translated name of potion effect.
*/
public static String getPotionEffectName(PotionEffectType effectType, User user) {
return hooked
? LanguageHelper.getPotionEffectName(effectType, getUserLocale(user))
: Util.prettifyText(effectType.getName());
}
/**
* Translate the name of the potion level.
*
* @param amplifier The potion level.
* @param user The user's language will be used for translation.
* @return The translated name of the potion level.
*/
public static String getEffectAmplifierName(int amplifier, User user) {
if (hooked) {
return LanguageHelper.getEffectAmplifierName(amplifier, getUserLocale(user));
}
return amplifier > 0 ? Integer.toString(amplifier) : "";
}
/**
* Make the custom potion effect like the player usually sees.
*
* @param effect The potion effect.
* @param user The user's language will be used for translation.
* @return The translated and formatted potion effect name, level, and duration.
*/
public static String getPotionEffectDisplay(PotionEffect effect, User user) {
if (hooked) {
return LanguageHelper.getPotionEffectDisplay(effect, getUserLocale(user));
}
String effecName = getPotionEffectName(effect.getType(), user);
String amplifier = getEffectAmplifierName(effect.getAmplifier(), user);
if (amplifier.length() > 0) {
effecName = effecName + " " + amplifier;
}
int duration = effect.getDuration();
if (duration > 20) {
int m = duration / 20 / 60;
int s = duration / 20 % 60;
String ts = String.format("%d:%02d", m, s);
effecName = String.format("%s (%s)", effecName, ts);
}
return effecName;
}
/**
* Translate the type name of tropical fish.
*
* @param fishPattern The type of tropical fish.
* @param user The user's language will be used for translation.
* @return The translated name of the tropical fish type.
*/
public static String getTropicalFishTypeName(TropicalFish.Pattern fishPattern, User user) {
return hooked
? LanguageHelper.getTropicalFishTypeName(fishPattern, getUserLocale(user))
: Util.prettifyText(fishPattern.name());
}
/**
* Get the names of 22 predefined tropical fish according to the
* 'variant' tag of TropicalFish.
*
* @param meta Metadata carrying information about tropical fish.
* @param user The return value is localized according to the
* user's locale.
* @return If variant is predefined, return the name of the
* tropical fish, otherwise return null.
*/
@Nullable
public static String getPredefinedTropicalFishName(TropicalFishBucketMeta meta, User user) {
if (hooked) {
return LanguageHelper.getPredefinedTropicalFishName(meta, getUserLocale(user));
}
if (meta.hasVariant()) {
TropicalFish.Pattern pattern = meta.getPattern();
// https://minecraft.gamepedia.com/Tropical_Fish#Entity_data
int type = pattern.ordinal() > 5 ? 1 : 0;
int patt = pattern.ordinal() % 6;
int bcol = meta.getBodyColor().ordinal();
int pcol = meta.getPatternColor().ordinal();
int variant = (pcol & 255) << 24 | (bcol & 255) << 16 | (patt & 255) << 8 | type;
switch (variant) {
case 117506305: return "Anemone";
case 117899265: return "Black Tang";
case 185008129: return "Blue Tang";
case 117441793: return "Butterflyfish";
case 118161664: return "Cichlid";
case 65536 : return "Clownfish";
case 50726144 : return "Cotton Candy Betta";
case 67764993 : return "Dottyback";
case 234882305: return "Emperor Red Snapper";
case 67110144 : return "Goatfish";
case 117441025: return "Moorish Idol";
case 16778497 : return "Ornate Butterflyfish";
case 101253888: return "Parrotfish";
case 50660352 : return "Queen Angelfish";
case 918529 : return "Red Cichlid";
case 235340288: return "Red Lipped Blenny";
case 918273 : return "Red Snapper";
case 67108865 : return "Threadfin";
case 917504 : return "Tomato Clownfish";
case 459008 : return "Triggerfish";
case 67699456 : return "Yellowtail Parrotfish";
case 67371009 : return "Yellow Tang";
default : break;
}
}
return null;
}
/**
* Translate the name of the dye color.
*
* @param color The color of the dye.
* @param user The user's language will be used for translation.
* @return The name of the dye color that has been translated.
*/
public static String getDyeColorName(DyeColor color, User user) {
return hooked
? LanguageHelper.getDyeColorName(color, getUserLocale(user))
: Util.prettifyText(color.name());
}
/**
* Translate merchant's level name.
*
* @param level The merchant's level.
* @param user The user's language will be used for translation.
* @return Translated name of merchant's level.
*/
public static String getVillagerLevelName(int level, User user) {
return hooked
? LanguageHelper.getVillagerLevelName(level, getUserLocale(user))
: Integer.toString(level);
}
/**
* Translate the profession name of the villager.
*
* @param profession The villager's profession.
* @param user The user's language will be used for translation.
* @return The translated profession name of the villager.
*/
public static String getVillagerProfessionName(Villager.Profession profession, User user) {
return hooked
? LanguageHelper.getVillagerProfessionName(profession, getUserLocale(user))
: Util.prettifyText(profession.name());
}
/**
* Translate the name of the banner pattern.
*
* @param pattern Contains the color banner pattern.
* @param user The user's language will be used for translation.
* @return The translated name of banner pattern.
*/
public static String getBannerPatternName(Pattern pattern, User user) {
return hooked
? LanguageHelper.getBannerPatternName(pattern, getUserLocale(user))
: pattern.getColor().name().toLowerCase(Locale.ROOT)
+ "_"
+ pattern.getPattern().name().toLowerCase(Locale.ROOT);
}
/**
* Get the description of the music disk.
*
* @param material Material for music records.
* @param user This parameter is temporarily useless and is reserved for
* possible future changes.
* @return If the given material is a music disk, the description of the
* music disk is returned. Otherwise, return NULL.
*/
@Nullable
public static String getMusicDiskDesc(Material material, User user) {
// The description of the music record is the same in any language,
// so directly output it here.
return switch (material) {
case MUSIC_DISC_13 -> "C418 - 13";
case MUSIC_DISC_CAT -> "C418 - cat";
case MUSIC_DISC_BLOCKS -> "C418 - blocks";
case MUSIC_DISC_CHIRP -> "C418 - chirp";
case MUSIC_DISC_FAR -> "C418 - far";
case MUSIC_DISC_MALL -> "C418 - mall";
case MUSIC_DISC_MELLOHI -> "C418 - mellohi";
case MUSIC_DISC_STAL -> "C418 - stal";
case MUSIC_DISC_STRAD -> "C418 - strad";
case MUSIC_DISC_WARD -> "C418 - ward";
case MUSIC_DISC_11 -> "C418 - 11";
case MUSIC_DISC_WAIT -> "C418 - wait";
case MUSIC_DISC_PIGSTEP -> "Lena Raine - Pigstep";
default -> null;
};
}
}