From 52960173de813b244e1b40b34e85a16d24e21ca9 Mon Sep 17 00:00:00 2001 From: zhangYi Date: Sat, 27 Feb 2021 06:53:35 +0800 Subject: [PATCH 01/10] Added LangUtils support. --- pom.xml | 11 + .../world/bentobox/bentobox/BentoBox.java | 2 + .../bentobox/hooks/LangUtilsHook.java | 494 ++++++++++++++++++ .../flags/clicklisteners/GeoMobLimitTab.java | 3 +- .../InvincibleVisitorsListener.java | 2 + .../panels/BlueprintManagementPanel.java | 23 +- src/main/resources/plugin.yml | 2 +- 7 files changed, 531 insertions(+), 6 deletions(-) create mode 100644 src/main/java/world/bentobox/bentobox/hooks/LangUtilsHook.java diff --git a/pom.xml b/pom.xml index 2046621c7..2671cb081 100644 --- a/pom.xml +++ b/pom.xml @@ -182,6 +182,10 @@ papermc https://papermc.io/repo/repository/maven-public/ + + langutils-api-repo + https://raw.githubusercontent.com/apachezy/LangUtils-API/master/repo/ + @@ -292,6 +296,13 @@ 1.0.6 compile + + + com.meowj + LangUtils-API + 3.0.0 + provided + diff --git a/src/main/java/world/bentobox/bentobox/BentoBox.java b/src/main/java/world/bentobox/bentobox/BentoBox.java index 9949b6a39..427a3f279 100644 --- a/src/main/java/world/bentobox/bentobox/BentoBox.java +++ b/src/main/java/world/bentobox/bentobox/BentoBox.java @@ -24,6 +24,7 @@ import world.bentobox.bentobox.hooks.MultiverseCoreHook; import world.bentobox.bentobox.hooks.VaultHook; import world.bentobox.bentobox.hooks.WorldEditHook; +import world.bentobox.bentobox.hooks.LangUtilsHook; import world.bentobox.bentobox.hooks.placeholders.PlaceholderAPIHook; import world.bentobox.bentobox.listeners.BannedCommands; import world.bentobox.bentobox.listeners.BlockEndDragon; @@ -228,6 +229,7 @@ private void completeSetup(long loadTime) { // Register additional hooks hooksManager.registerHook(new DynmapHook()); hooksManager.registerHook(new WorldEditHook()); + hooksManager.registerHook(new LangUtilsHook()); webManager = new WebManager(this); diff --git a/src/main/java/world/bentobox/bentobox/hooks/LangUtilsHook.java b/src/main/java/world/bentobox/bentobox/hooks/LangUtilsHook.java new file mode 100644 index 000000000..1b4fdffed --- /dev/null +++ b/src/main/java/world/bentobox/bentobox/hooks/LangUtilsHook.java @@ -0,0 +1,494 @@ +package world.bentobox.bentobox.hooks; + +import com.meowj.langutils.lang.LanguageHelper; +import org.bukkit.DyeColor; +import org.bukkit.Material; +import org.bukkit.block.Biome; +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.plugin.Plugin; +import org.bukkit.potion.PotionEffect; +import org.bukkit.potion.PotionEffectType; +import org.bukkit.potion.PotionType; +import world.bentobox.bentobox.api.hooks.Hook; +import world.bentobox.bentobox.api.user.User; +import world.bentobox.bentobox.util.Util; + +import java.util.Map.Entry; + +/** + * @author ApacheZy + * @since 1.6.0 + */ +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. + boolean classExists; + try { + Class.forName("com.meowj.langutils.lang.LanguageHelper"); + classExists = true; + } catch (ClassNotFoundException e) { + classExists = false; + } + + hooked = plugin != null && plugin.isEnabled() && classExists; + return hooked; + } + + @Override + public boolean hook() { + return LangUtilsHook.doHook(getPlugin()); + } + + @Override + public String getFailureCause() { + return null; + } + + 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. + *

+ * 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. + *

+ * 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. + */ + @SuppressWarnings("unused") + 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 + */ + @SuppressWarnings("unused") + 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)) + : 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} + * @param user The User's locale will be used for translation. + * @return Translated enchanted name with level. + */ + public static String getEnchantDisplayName(Entry entry, User user) { + return hooked + ? LanguageHelper.getEnchantmentDisplayName(entry, getUserLocale(user)) + : 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)) + : 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)); + } + switch (potionType) { + case UNCRAFTABLE: return "Uncraftable Potion"; + case WATER: return "Water Bottle"; + case MUNDANE: return "Mundane Potion"; + case THICK: return "Thick Potion"; + case AWKWARD: return "Awkward Potion"; + case NIGHT_VISION: return "Potion of Night Vision"; + case INVISIBILITY: return "Potion of Invisibility"; + case JUMP: return "Potion of Leaping"; + case FIRE_RESISTANCE: return "Potion of Fire Resistance"; + case SPEED: return "Potion of Swiftness"; + case SLOWNESS: return "Potion of Slowness"; + case WATER_BREATHING: return "Potion of Water Breathing"; + case INSTANT_HEAL: return "Potion of Healing"; + case INSTANT_DAMAGE: return "Potion of Harming"; + case POISON: return "Potion of Poison"; + case REGEN: return "Potion of Regeneration"; + case STRENGTH: return "Potion of Strength"; + case WEAKNESS: return "Potion of Weakness"; + case LUCK: return "Potion of Luck"; + case TURTLE_MASTER: return "Potion of the Turtle Master"; + case SLOW_FALLING: return "Potion of Slow Falling"; + default: + throw new IllegalStateException("Unexpected value: getPotionTypeName " + potionType); + } + + } + + /** + * 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)); + } + switch (potionType) { + case UNCRAFTABLE: return "Splash Uncraftable Potion"; + case WATER: return "Splash Water Bottle"; + case MUNDANE: return "Mundane Splash Potion"; + case THICK: return "Thick Splash Potion"; + case AWKWARD: return "Awkward Splash Potion"; + case NIGHT_VISION: return "Splash Potion of Night Vision"; + case INVISIBILITY: return "Splash Potion of Invisibility"; + case JUMP: return "Splash Potion of Leaping"; + case FIRE_RESISTANCE: return "Splash Potion of Fire Resistance"; + case SPEED: return "Splash Potion of Swiftness"; + case SLOWNESS: return "Splash Potion of Slowness"; + case WATER_BREATHING: return "Splash Potion of Water Breathing"; + case INSTANT_HEAL: return "Splash Potion of Healing"; + case INSTANT_DAMAGE: return "Splash Potion of Harming"; + case POISON: return "Splash Potion of Poison"; + case REGEN: return "Splash Potion of Regeneration"; + case STRENGTH: return "Splash Potion of Strength"; + case WEAKNESS: return "Splash Potion of Weakness"; + case LUCK: return "Splash Potion of Luck"; + case TURTLE_MASTER: return "Splash Potion of the Turtle Master"; + case SLOW_FALLING: return "Splash Potion of Slow Falling"; + default: + throw new IllegalStateException("Unexpected value: getSplashPotionName " + potionType); + } + } + + /** + * 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)); + } + switch (potionType) { + case UNCRAFTABLE: return "Lingering Uncraftable Potion"; + case WATER: return "Lingering Water Bottle"; + case MUNDANE: return "Mundane Lingering Potion"; + case THICK: return "Thick Lingering Potion"; + case AWKWARD: return "Awkward Lingering Potion"; + case NIGHT_VISION: return "Lingering Potion of Night Vision"; + case INVISIBILITY: return "Lingering Potion of Invisibility"; + case JUMP: return "Lingering Potion of Leaping"; + case FIRE_RESISTANCE: return "Lingering Potion of Fire Resistance"; + case SPEED: return "Lingering Potion of Swiftness"; + case SLOWNESS: return "Lingering Potion of Slowness"; + case WATER_BREATHING: return "Lingering Potion of Water Breathing"; + case INSTANT_HEAL: return "Lingering Potion of Healing"; + case INSTANT_DAMAGE: return "Lingering Potion of Harming"; + case POISON: return "Lingering Potion of Poison"; + case REGEN: return "Lingering Potion of Regeneration"; + case STRENGTH: return "Lingering Potion of Strength"; + case WEAKNESS: return "Lingering Potion of Weakness"; + case LUCK: return "Lingering Potion of Luck"; + case TURTLE_MASTER: return "Lingering Potion of the Turtle Master"; + case SLOW_FALLING: return "Lingering Potion of Slow Falling"; + default: + throw new IllegalStateException("Unexpected value: getLingeringPotionName " + potionType); + } + } + + /** + * 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)); + } + switch (potionType) { + case UNCRAFTABLE: return "Uncraftable Tipped Arrow"; + case WATER: return "Arrow of Splashing"; + case MUNDANE: + case THICK: + case AWKWARD: return "Tipped Arrow"; + case NIGHT_VISION: return "Arrow of Night Vision"; + case INVISIBILITY: return "Arrow of Invisibility"; + case JUMP: return "Arrow of Leaping"; + case FIRE_RESISTANCE: return "Arrow of Fire Resistance"; + case SPEED: return "Arrow of Swiftness"; + case SLOWNESS: return "Arrow of Slowness"; + case WATER_BREATHING: return "Arrow of Water Breathing"; + case INSTANT_HEAL: return "Arrow of Healing"; + case INSTANT_DAMAGE: return "Arrow of Harming"; + case POISON: return "Arrow of Poison"; + case REGEN: return "Arrow of Regeneration"; + case STRENGTH: return "Arrow of Strength"; + case WEAKNESS: return "Arrow of Weakness"; + case LUCK: return "Arrow of Luck"; + case TURTLE_MASTER: return "Arrow of the Turtle Master"; + case SLOW_FALLING: return "Arrow of Slow Falling"; + default: + throw new IllegalStateException("Unexpected value: " + potionType); + } + } + + /** + * 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 time = String.format("%d:%02d", m, s); + effecName = String.format("%s (%s)", effecName, time); + } + + 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()); + } + + /** + * 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()); + } + +} diff --git a/src/main/java/world/bentobox/bentobox/listeners/flags/clicklisteners/GeoMobLimitTab.java b/src/main/java/world/bentobox/bentobox/listeners/flags/clicklisteners/GeoMobLimitTab.java index 0cb242f8c..b4d3179a8 100644 --- a/src/main/java/world/bentobox/bentobox/listeners/flags/clicklisteners/GeoMobLimitTab.java +++ b/src/main/java/world/bentobox/bentobox/listeners/flags/clicklisteners/GeoMobLimitTab.java @@ -23,6 +23,7 @@ import world.bentobox.bentobox.api.panels.TabbedPanel; import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder; import world.bentobox.bentobox.api.user.User; +import world.bentobox.bentobox.hooks.LangUtilsHook; import world.bentobox.bentobox.util.Util; /** @@ -120,7 +121,7 @@ public String getPermission() { private PanelItem getPanelItem(EntityType c, User user) { PanelItemBuilder pib = new PanelItemBuilder(); - pib.name(Util.prettifyText(c.toString())); + pib.name(LangUtilsHook.getEntityName(c, user)); pib.clickHandler(this); if (type == EntityLimitTabType.MOB_LIMIT) { if (!BentoBox.getInstance().getIWM().getMobLimitSettings(world).contains(c.name())) { diff --git a/src/main/java/world/bentobox/bentobox/listeners/flags/worldsettings/InvincibleVisitorsListener.java b/src/main/java/world/bentobox/bentobox/listeners/flags/worldsettings/InvincibleVisitorsListener.java index 0185fa44c..23f219924 100644 --- a/src/main/java/world/bentobox/bentobox/listeners/flags/worldsettings/InvincibleVisitorsListener.java +++ b/src/main/java/world/bentobox/bentobox/listeners/flags/worldsettings/InvincibleVisitorsListener.java @@ -82,6 +82,8 @@ private void openPanel(User user, String ivPanelName) { private PanelItem getPanelItem(DamageCause c, User user) { PanelItemBuilder pib = new PanelItemBuilder(); + // todo: Please consider adding translation fields for each entry + // of "DamageCause" in the language file in the future. pib.name(Util.prettifyText(c.toString())); pib.clickHandler(this); if (getIWM().getIvSettings(user.getWorld()).contains(c.name())) { diff --git a/src/main/java/world/bentobox/bentobox/panels/BlueprintManagementPanel.java b/src/main/java/world/bentobox/bentobox/panels/BlueprintManagementPanel.java index 3428323b8..304ae8c79 100644 --- a/src/main/java/world/bentobox/bentobox/panels/BlueprintManagementPanel.java +++ b/src/main/java/world/bentobox/bentobox/panels/BlueprintManagementPanel.java @@ -255,12 +255,27 @@ protected PanelItem getBundleIcon(BlueprintBundle bb) { private PanelItem getWorldInstrTile(Environment env) { Material icon; - if (env.equals(Environment.NORMAL)) icon = Material.GRASS_BLOCK; - else if (env.equals(Environment.NETHER)) icon = Material.NETHERRACK; - else icon = Material.END_STONE; + String worldName; + switch (env) { + case NORMAL: + icon = Material.GRASS_BLOCK; + worldName = normalBlueprint.getName(); + break; + case NETHER: + icon = Material.NETHERRACK; + worldName = netherBlueprint.getName(); + break; + case THE_END: + icon = Material.END_STONE; + worldName = endBlueprint.getName(); + break; + default: + icon = Material.STONE; + worldName = Util.prettifyText(env.name()); + } return new PanelItemBuilder() - .name(t("world-name-syntax", TextVariables.NAME, Util.prettifyText(env.name()))) + .name(t("world-name-syntax", TextVariables.NAME, worldName)) .description(t("world-instructions")) .glow(true) .icon(icon) diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 54b15bc78..012003fb4 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -12,7 +12,7 @@ load: STARTUP loadbefore: [Multiverse-Core, Residence] -softdepend: [Vault, PlaceholderAPI, dynmap, WorldEdit, WorldBorderAPI, BsbMongo, WorldGeneratorApi] +softdepend: [Vault, PlaceholderAPI, dynmap, WorldEdit, WorldBorderAPI, BsbMongo, WorldGeneratorApi, LangUtils] permissions: bentobox.admin: From 6ab75b426516581e0911fa31e2bc73ff6a87d09a Mon Sep 17 00:00:00 2001 From: apacheZy Date: Sun, 28 Feb 2021 02:07:43 +0800 Subject: [PATCH 02/10] Added predefined tropical fish to LangUtilsHook. --- pom.xml | 12 ++- .../bentobox/hooks/LangUtilsHook.java | 73 +++++++++++++++++++ 2 files changed, 81 insertions(+), 4 deletions(-) diff --git a/pom.xml b/pom.xml index 2671cb081..6b0506a54 100644 --- a/pom.xml +++ b/pom.xml @@ -183,8 +183,12 @@ https://papermc.io/repo/repository/maven-public/ - langutils-api-repo - https://raw.githubusercontent.com/apachezy/LangUtils-API/master/repo/ + langutils-github-repo + https://raw.githubusercontent.com/apachezy/LangUtils/master/repo/ + + + langutils-gitee-repo + https://gitee.com/apachezy/LangUtils/raw/master/repo/ @@ -299,8 +303,8 @@ com.meowj - LangUtils-API - 3.0.0 + LangUtils + 3.0.0-mc1.16.5-SNAPSHOT provided diff --git a/src/main/java/world/bentobox/bentobox/hooks/LangUtilsHook.java b/src/main/java/world/bentobox/bentobox/hooks/LangUtilsHook.java index 1b4fdffed..708d4559a 100644 --- a/src/main/java/world/bentobox/bentobox/hooks/LangUtilsHook.java +++ b/src/main/java/world/bentobox/bentobox/hooks/LangUtilsHook.java @@ -11,14 +11,17 @@ 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 world.bentobox.bentobox.api.hooks.Hook; import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.util.Util; +import java.lang.reflect.Field; import java.util.Map.Entry; /** @@ -73,6 +76,7 @@ private static String getUserLocale(User user) { * @param user the User's locale will be used for translation. * @return The Display-Name of the item. */ + @SuppressWarnings("unused") public static String getItemDisplayName(ItemStack item, User user) { if (hooked) { return LanguageHelper.getItemDisplayName(item, getUserLocale(user)); @@ -97,6 +101,7 @@ public static String getItemDisplayName(ItemStack item, User user) { * @param user the User's locale will be used for translation. * @return The translated item name. */ + @SuppressWarnings("unused") public static String getItemName(ItemStack itemStack, User user) { return hooked ? LanguageHelper.getItemName(itemStack, getUserLocale(user)) @@ -168,6 +173,7 @@ public static String getEntityName(Entity entity, User user) { * @param user the User's locale will be used for translation. * @return The translated Biome name. */ + @SuppressWarnings("unused") public static String getBiomeName(Biome biome, User user) { return hooked ? LanguageHelper.getBiomeName(biome, getUserLocale(user)) @@ -182,6 +188,7 @@ public static String getBiomeName(Biome biome, User user) { * @param user The User's locale will be used for translation. * @return Translated enchanted name with level. */ + @SuppressWarnings("unused") public static String getEnchantDisplayName(Enchantment ench, int level, User user) { return hooked ? LanguageHelper.getEnchantmentDisplayName(ench, level, getUserLocale(user)) @@ -196,6 +203,7 @@ public static String getEnchantDisplayName(Enchantment ench, int level, User use * @param user The User's locale will be used for translation. * @return Translated enchanted name with level. */ + @SuppressWarnings("unused") public static String getEnchantDisplayName(Entry entry, User user) { return hooked ? LanguageHelper.getEnchantmentDisplayName(entry, getUserLocale(user)) @@ -209,6 +217,7 @@ public static String getEnchantDisplayName(Entry entry, Us * @param user The User's locale will be used for translation. * @return The translated enchant name. */ + @SuppressWarnings("unused") public static String getEnchantName(Enchantment enchant, User user) { return hooked ? LanguageHelper.getEnchantmentName(enchant, getUserLocale(user)) @@ -223,6 +232,7 @@ public static String getEnchantName(Enchantment enchant, User user) { * @param user The user's language will be used for translation. * @return The converted enchantment level. */ + @SuppressWarnings("unused") public static String getEnchantLevelName(int level, User user) { return hooked ? LanguageHelper.getEnchantmentLevelName(level, getUserLocale(user)) @@ -236,6 +246,7 @@ public static String getEnchantLevelName(int level, User user) { * @param user The user's language will be used for translation. * @return Translated potion name. */ + @SuppressWarnings("unused") public static String getPotionTypeName(PotionType potionType, User user) { if (hooked) { return LanguageHelper.getPotionName(potionType, getUserLocale(user)); @@ -275,6 +286,7 @@ public static String getPotionTypeName(PotionType potionType, User user) { * @param user The user's language will be used for translation. * @return Translated splash potion name. */ + @SuppressWarnings("unused") public static String getSplashPotionName(PotionType potionType, User user) { if (hooked) { return LanguageHelper.getSplashPotionName(potionType, getUserLocale(user)); @@ -313,6 +325,7 @@ public static String getSplashPotionName(PotionType potionType, User user) { * @param user The user's language will be used for translation. * @return Translated lingering potion name. */ + @SuppressWarnings("unused") public static String getLingeringPotionName(PotionType potionType, User user) { if (hooked) { return LanguageHelper.getLingeringPotionName(potionType, getUserLocale(user)); @@ -351,6 +364,7 @@ public static String getLingeringPotionName(PotionType potionType, User user) { * @param user The user's language will be used for translation. * @return Translated tipped arrow name. */ + @SuppressWarnings("unused") public static String getTippedArrowName(PotionType potionType, User user) { if (hooked) { return LanguageHelper.getTippedArrowName(potionType, getUserLocale(user)); @@ -416,6 +430,7 @@ public static String getEffectAmplifierName(int amplifier, User user) { * @param user The user's language will be used for translation. * @return The translated and formatted potion effect name, level, and duration. */ + @SuppressWarnings("unused") public static String getPotionEffectDisplay(PotionEffect effect, User user) { if (hooked) { return LanguageHelper.getPotionEffectDisplay(effect, getUserLocale(user)); @@ -446,12 +461,67 @@ public static String getPotionEffectDisplay(PotionEffect effect, User user) { * @param user The user's language will be used for translation. * @return The translated name of the tropical fish type. */ + @SuppressWarnings("unused") 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. + */ + @SuppressWarnings("unused") + @Nullable + public static String getPredefinedTropicalFishName(TropicalFishBucketMeta meta, User user) { + if (hooked) { + return LanguageHelper.getPredefinedTropicalFishName(meta, getUserLocale(user)); + } + + if (meta.hasVariant()) { + try { + Field variantField = meta.getClass().getDeclaredField("variant"); + variantField.setAccessible(true); + Integer variant = (Integer) variantField.get(meta); + + 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"; + } + } catch (NoSuchFieldException | IllegalAccessException ignored) { + // nothing + } + } + return null; + } + /** * Translate the name of the dye color. * @@ -459,6 +529,7 @@ public static String getTropicalFishTypeName(TropicalFish.Pattern fishPattern, U * @param user The user's language will be used for translation. * @return The name of the dye color that has been translated. */ + @SuppressWarnings("unused") public static String getDyeColorName(DyeColor color, User user) { return hooked ? LanguageHelper.getDyeColorName(color, getUserLocale(user)) @@ -472,6 +543,7 @@ public static String getDyeColorName(DyeColor color, User user) { * @param user The user's language will be used for translation. * @return Translated name of merchant's level. */ + @SuppressWarnings("unused") public static String getVillagerLevelName(int level, User user) { return hooked ? LanguageHelper.getVillagerLevelName(level, getUserLocale(user)) @@ -485,6 +557,7 @@ public static String getVillagerLevelName(int level, User user) { * @param user The user's language will be used for translation. * @return The translated profession name of the villager. */ + @SuppressWarnings("unused") public static String getVillagerProfessionName(Villager.Profession profession, User user) { return hooked ? LanguageHelper.getVillagerProfessionName(profession, getUserLocale(user)) From 159c6243798c99552a8e8e49d53da02dec0c996e Mon Sep 17 00:00:00 2001 From: ApacheZy Date: Sun, 28 Feb 2021 03:56:30 +0800 Subject: [PATCH 03/10] Adding LangUtils to soft dependency --- src/main/resources/plugin.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 253a80375..1ca589839 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -12,7 +12,7 @@ load: STARTUP loadbefore: [Multiverse-Core, Residence] -softdepend: [Vault, PlaceholderAPI, dynmap, WorldEdit, WorldBorderAPI, BsbMongo, WorldGeneratorApi, AdvancedChests] +softdepend: [Vault, PlaceholderAPI, dynmap, WorldEdit, WorldBorderAPI, BsbMongo, WorldGeneratorApi, AdvancedChests, LangUtils] permissions: bentobox.admin: From 644119f0e8e106167742321208e5bc69c96c2b2a Mon Sep 17 00:00:00 2001 From: ApacheZy Date: Sun, 28 Feb 2021 15:52:50 +0800 Subject: [PATCH 04/10] #discussion_r584202658 #issuecomment-787202315 --- .../bentobox/hooks/LangUtilsHook.java | 75 ++++++++++--------- 1 file changed, 39 insertions(+), 36 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/hooks/LangUtilsHook.java b/src/main/java/world/bentobox/bentobox/hooks/LangUtilsHook.java index 708d4559a..a4a34b15a 100644 --- a/src/main/java/world/bentobox/bentobox/hooks/LangUtilsHook.java +++ b/src/main/java/world/bentobox/bentobox/hooks/LangUtilsHook.java @@ -21,7 +21,6 @@ import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.util.Util; -import java.lang.reflect.Field; import java.util.Map.Entry; /** @@ -274,7 +273,7 @@ public static String getPotionTypeName(PotionType potionType, User user) { case TURTLE_MASTER: return "Potion of the Turtle Master"; case SLOW_FALLING: return "Potion of Slow Falling"; default: - throw new IllegalStateException("Unexpected value: getPotionTypeName " + potionType); + return Util.prettifyText(potionType.name()); } } @@ -314,7 +313,7 @@ public static String getSplashPotionName(PotionType potionType, User user) { case TURTLE_MASTER: return "Splash Potion of the Turtle Master"; case SLOW_FALLING: return "Splash Potion of Slow Falling"; default: - throw new IllegalStateException("Unexpected value: getSplashPotionName " + potionType); + return Util.prettifyText(potionType.name()); } } @@ -353,7 +352,7 @@ public static String getLingeringPotionName(PotionType potionType, User user) { case TURTLE_MASTER: return "Lingering Potion of the Turtle Master"; case SLOW_FALLING: return "Lingering Potion of Slow Falling"; default: - throw new IllegalStateException("Unexpected value: getLingeringPotionName " + potionType); + return Util.prettifyText(potionType.name()); } } @@ -392,7 +391,7 @@ public static String getTippedArrowName(PotionType potionType, User user) { case TURTLE_MASTER: return "Arrow of the Turtle Master"; case SLOW_FALLING: return "Arrow of Slow Falling"; default: - throw new IllegalStateException("Unexpected value: " + potionType); + return Util.prettifyText(potionType.name()); } } @@ -486,37 +485,41 @@ public static String getPredefinedTropicalFishName(TropicalFishBucketMeta meta, } if (meta.hasVariant()) { - try { - Field variantField = meta.getClass().getDeclaredField("variant"); - variantField.setAccessible(true); - Integer variant = (Integer) variantField.get(meta); - - 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"; - } - } catch (NoSuchFieldException | IllegalAccessException ignored) { - // nothing + 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 : return null; } } return null; From 17aaca11ad2e19727834571ee46c172811c1c52d Mon Sep 17 00:00:00 2001 From: ApacheZy Date: Mon, 1 Mar 2021 23:09:43 +0800 Subject: [PATCH 05/10] Add isHooked(), Delete redundant @SuppressWarnings("unused") --- .../bentobox/hooks/LangUtilsHook.java | 30 +++++++------------ 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/hooks/LangUtilsHook.java b/src/main/java/world/bentobox/bentobox/hooks/LangUtilsHook.java index a4a34b15a..74dd4eacb 100644 --- a/src/main/java/world/bentobox/bentobox/hooks/LangUtilsHook.java +++ b/src/main/java/world/bentobox/bentobox/hooks/LangUtilsHook.java @@ -27,6 +27,7 @@ * @author ApacheZy * @since 1.6.0 */ +@SuppressWarnings("unused") public class LangUtilsHook extends Hook { private static boolean hooked; @@ -60,6 +61,16 @@ public String getFailureCause() { return null; } + /** + * 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(); } @@ -75,7 +86,6 @@ private static String getUserLocale(User user) { * @param user the User's locale will be used for translation. * @return The Display-Name of the item. */ - @SuppressWarnings("unused") public static String getItemDisplayName(ItemStack item, User user) { if (hooked) { return LanguageHelper.getItemDisplayName(item, getUserLocale(user)); @@ -100,7 +110,6 @@ public static String getItemDisplayName(ItemStack item, User user) { * @param user the User's locale will be used for translation. * @return The translated item name. */ - @SuppressWarnings("unused") public static String getItemName(ItemStack itemStack, User user) { return hooked ? LanguageHelper.getItemName(itemStack, getUserLocale(user)) @@ -117,7 +126,6 @@ public static String getItemName(ItemStack itemStack, User user) { * @param user the User's locale will be used for translation. * @return The translated material name. */ - @SuppressWarnings("unused") public static String getMaterialName(Material material, User user) { return hooked ? LanguageHelper.getMaterialName(material, getUserLocale(user)) @@ -131,7 +139,6 @@ public static String getMaterialName(Material material, User user) { * @param user the User's locale will be used for translation. * @return The name of the entity */ - @SuppressWarnings("unused") public static String getEntityDisplayName(Entity entity, User user) { return entity.getCustomName() != null ? entity.getCustomName() @@ -172,7 +179,6 @@ public static String getEntityName(Entity entity, User user) { * @param user the User's locale will be used for translation. * @return The translated Biome name. */ - @SuppressWarnings("unused") public static String getBiomeName(Biome biome, User user) { return hooked ? LanguageHelper.getBiomeName(biome, getUserLocale(user)) @@ -187,7 +193,6 @@ public static String getBiomeName(Biome biome, User user) { * @param user The User's locale will be used for translation. * @return Translated enchanted name with level. */ - @SuppressWarnings("unused") public static String getEnchantDisplayName(Enchantment ench, int level, User user) { return hooked ? LanguageHelper.getEnchantmentDisplayName(ench, level, getUserLocale(user)) @@ -202,7 +207,6 @@ public static String getEnchantDisplayName(Enchantment ench, int level, User use * @param user The User's locale will be used for translation. * @return Translated enchanted name with level. */ - @SuppressWarnings("unused") public static String getEnchantDisplayName(Entry entry, User user) { return hooked ? LanguageHelper.getEnchantmentDisplayName(entry, getUserLocale(user)) @@ -216,7 +220,6 @@ public static String getEnchantDisplayName(Entry entry, Us * @param user The User's locale will be used for translation. * @return The translated enchant name. */ - @SuppressWarnings("unused") public static String getEnchantName(Enchantment enchant, User user) { return hooked ? LanguageHelper.getEnchantmentName(enchant, getUserLocale(user)) @@ -231,7 +234,6 @@ public static String getEnchantName(Enchantment enchant, User user) { * @param user The user's language will be used for translation. * @return The converted enchantment level. */ - @SuppressWarnings("unused") public static String getEnchantLevelName(int level, User user) { return hooked ? LanguageHelper.getEnchantmentLevelName(level, getUserLocale(user)) @@ -245,7 +247,6 @@ public static String getEnchantLevelName(int level, User user) { * @param user The user's language will be used for translation. * @return Translated potion name. */ - @SuppressWarnings("unused") public static String getPotionTypeName(PotionType potionType, User user) { if (hooked) { return LanguageHelper.getPotionName(potionType, getUserLocale(user)); @@ -285,7 +286,6 @@ public static String getPotionTypeName(PotionType potionType, User user) { * @param user The user's language will be used for translation. * @return Translated splash potion name. */ - @SuppressWarnings("unused") public static String getSplashPotionName(PotionType potionType, User user) { if (hooked) { return LanguageHelper.getSplashPotionName(potionType, getUserLocale(user)); @@ -324,7 +324,6 @@ public static String getSplashPotionName(PotionType potionType, User user) { * @param user The user's language will be used for translation. * @return Translated lingering potion name. */ - @SuppressWarnings("unused") public static String getLingeringPotionName(PotionType potionType, User user) { if (hooked) { return LanguageHelper.getLingeringPotionName(potionType, getUserLocale(user)); @@ -363,7 +362,6 @@ public static String getLingeringPotionName(PotionType potionType, User user) { * @param user The user's language will be used for translation. * @return Translated tipped arrow name. */ - @SuppressWarnings("unused") public static String getTippedArrowName(PotionType potionType, User user) { if (hooked) { return LanguageHelper.getTippedArrowName(potionType, getUserLocale(user)); @@ -429,7 +427,6 @@ public static String getEffectAmplifierName(int amplifier, User user) { * @param user The user's language will be used for translation. * @return The translated and formatted potion effect name, level, and duration. */ - @SuppressWarnings("unused") public static String getPotionEffectDisplay(PotionEffect effect, User user) { if (hooked) { return LanguageHelper.getPotionEffectDisplay(effect, getUserLocale(user)); @@ -460,7 +457,6 @@ public static String getPotionEffectDisplay(PotionEffect effect, User user) { * @param user The user's language will be used for translation. * @return The translated name of the tropical fish type. */ - @SuppressWarnings("unused") public static String getTropicalFishTypeName(TropicalFish.Pattern fishPattern, User user) { return hooked ? LanguageHelper.getTropicalFishTypeName(fishPattern, getUserLocale(user)) @@ -477,7 +473,6 @@ public static String getTropicalFishTypeName(TropicalFish.Pattern fishPattern, U * @return If variant is predefined, return the name of the * tropical fish, otherwise return null. */ - @SuppressWarnings("unused") @Nullable public static String getPredefinedTropicalFishName(TropicalFishBucketMeta meta, User user) { if (hooked) { @@ -532,7 +527,6 @@ public static String getPredefinedTropicalFishName(TropicalFishBucketMeta meta, * @param user The user's language will be used for translation. * @return The name of the dye color that has been translated. */ - @SuppressWarnings("unused") public static String getDyeColorName(DyeColor color, User user) { return hooked ? LanguageHelper.getDyeColorName(color, getUserLocale(user)) @@ -546,7 +540,6 @@ public static String getDyeColorName(DyeColor color, User user) { * @param user The user's language will be used for translation. * @return Translated name of merchant's level. */ - @SuppressWarnings("unused") public static String getVillagerLevelName(int level, User user) { return hooked ? LanguageHelper.getVillagerLevelName(level, getUserLocale(user)) @@ -560,7 +553,6 @@ public static String getVillagerLevelName(int level, User user) { * @param user The user's language will be used for translation. * @return The translated profession name of the villager. */ - @SuppressWarnings("unused") public static String getVillagerProfessionName(Villager.Profession profession, User user) { return hooked ? LanguageHelper.getVillagerProfessionName(profession, getUserLocale(user)) From 72ca61ba6408b78aea167e0436cb966b0f7c56b7 Mon Sep 17 00:00:00 2001 From: ApacheZy Date: Sun, 7 Mar 2021 04:22:04 +0800 Subject: [PATCH 06/10] Add getBannerPatternName(); Use jitpack.io repository. --- pom.xml | 12 ++-------- .../bentobox/hooks/LangUtilsHook.java | 23 ++++++++++++++++++- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/pom.xml b/pom.xml index 6b0506a54..79fe0ef94 100644 --- a/pom.xml +++ b/pom.xml @@ -182,14 +182,6 @@ papermc https://papermc.io/repo/repository/maven-public/ - - langutils-github-repo - https://raw.githubusercontent.com/apachezy/LangUtils/master/repo/ - - - langutils-gitee-repo - https://gitee.com/apachezy/LangUtils/raw/master/repo/ - @@ -302,9 +294,9 @@ - com.meowj + com.gitee.apachezy LangUtils - 3.0.0-mc1.16.5-SNAPSHOT + 3.1.1 provided diff --git a/src/main/java/world/bentobox/bentobox/hooks/LangUtilsHook.java b/src/main/java/world/bentobox/bentobox/hooks/LangUtilsHook.java index 74dd4eacb..a3e352ad9 100644 --- a/src/main/java/world/bentobox/bentobox/hooks/LangUtilsHook.java +++ b/src/main/java/world/bentobox/bentobox/hooks/LangUtilsHook.java @@ -4,6 +4,7 @@ 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; @@ -21,6 +22,7 @@ import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.util.Util; +import java.util.Locale; import java.util.Map.Entry; /** @@ -364,7 +366,7 @@ public static String getLingeringPotionName(PotionType potionType, User user) { */ public static String getTippedArrowName(PotionType potionType, User user) { if (hooked) { - return LanguageHelper.getTippedArrowName(potionType, getUserLocale(user)); + return LanguageHelper.getTippedArrowName(potionType, getUserLocale(user)); } switch (potionType) { case UNCRAFTABLE: return "Uncraftable Tipped Arrow"; @@ -559,4 +561,23 @@ public static String getVillagerProfessionName(Villager.Profession profession, U : 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) { + String patName; + if (hooked) { + patName = LanguageHelper.getBannerPatternName(pattern, getUserLocale(user)); + } else { + patName = pattern.getColor().name().toLowerCase(Locale.ROOT) + + "_" + + pattern.getPattern().name().toLowerCase(Locale.ROOT); + } + return patName; + } + } From a85fce4fbc4372e96503ebc907d727919e112c8f Mon Sep 17 00:00:00 2001 From: ApacheZy Date: Sun, 7 Mar 2021 14:04:14 +0800 Subject: [PATCH 07/10] 3.1.1 released to github. --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 79fe0ef94..631219f11 100644 --- a/pom.xml +++ b/pom.xml @@ -294,7 +294,7 @@ - com.gitee.apachezy + com.github.apachezy LangUtils 3.1.1 provided From 8555d5091a699009c01a0bf61e3d53f638c23949 Mon Sep 17 00:00:00 2001 From: zhangYi Date: Mon, 8 Mar 2021 16:50:28 +0800 Subject: [PATCH 08/10] Added checkMethods(). --- .../bentobox/hooks/LangUtilsHook.java | 58 +++++++++++++++---- 1 file changed, 47 insertions(+), 11 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/hooks/LangUtilsHook.java b/src/main/java/world/bentobox/bentobox/hooks/LangUtilsHook.java index 74dd4eacb..dea3dcd08 100644 --- a/src/main/java/world/bentobox/bentobox/hooks/LangUtilsHook.java +++ b/src/main/java/world/bentobox/bentobox/hooks/LangUtilsHook.java @@ -17,11 +17,13 @@ import org.bukkit.potion.PotionEffectType; import org.bukkit.potion.PotionType; import org.jetbrains.annotations.Nullable; +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; import java.util.Map.Entry; +import java.util.logging.Logger; /** * @author ApacheZy @@ -37,18 +39,53 @@ public LangUtilsHook() { } 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. - boolean classExists; - try { - Class.forName("com.meowj.langutils.lang.LanguageHelper"); - classExists = true; - } catch (ClassNotFoundException e) { - classExists = false; + + if (plugin != null && plugin.isEnabled()) { + + Class langHelperClass; + try { + langHelperClass = Class.forName("com.meowj.langutils.lang.LanguageHelper"); + } catch (ClassNotFoundException e) { + langHelperClass = null; + } + + if (langHelperClass != null && checkMethods(langHelperClass)) { + hooked = true; + return true; + } + + Logger logger = BentoBox.getInstance().getLogger(); + logger.warning("The LangUtils version is too old to be applicable to BentoBox."); + logger.warning("Please go here to download the latest version:"); + logger.warning("https://github.com/apachezy/LangUtils/releases"); } - hooked = plugin != null && plugin.isEnabled() && classExists; - return hooked; + hooked = false; + return false; + } + + private static boolean checkMethods(Class clazz) { + try { + clazz.getMethod("getMaterialName", Material .class, String.class); + clazz.getMethod("getPotionName", PotionType .class, String.class); + clazz.getMethod("getSplashPotionName", PotionType .class, String.class); + clazz.getMethod("getLingeringPotionName", PotionType .class, String.class); + clazz.getMethod("getTippedArrowName", PotionType .class, String.class); + clazz.getMethod("getPotionEffectName", PotionEffectType .class, String.class); + clazz.getMethod("getEffectAmplifierName", int .class, String.class); + clazz.getMethod("getPotionEffectDisplay", PotionEffect .class, String.class); + clazz.getMethod("getTropicalFishTypeName", TropicalFish.Pattern .class, String.class); + clazz.getMethod("getPredefinedTropicalFishName", TropicalFishBucketMeta.class, String.class); + clazz.getMethod("getDyeColorName", DyeColor .class, String.class); + clazz.getMethod("getVillagerLevelName", int .class, String.class); + clazz.getMethod("getVillagerProfessionName", Villager.Profession .class, String.class); + return true; + } catch (NoSuchMethodException e) { + return false; + } } @Override @@ -58,7 +95,7 @@ public boolean hook() { @Override public String getFailureCause() { - return null; + return "The LangUtils version does not apply to BentoBox."; } /** @@ -158,7 +195,6 @@ public static String getEntityName(EntityType entityType, User user) { : Util.prettifyText(entityType.toString()); } - /** * Translate the name of the entity type. * @@ -364,7 +400,7 @@ public static String getLingeringPotionName(PotionType potionType, User user) { */ public static String getTippedArrowName(PotionType potionType, User user) { if (hooked) { - return LanguageHelper.getTippedArrowName(potionType, getUserLocale(user)); + return LanguageHelper.getTippedArrowName(potionType, getUserLocale(user)); } switch (potionType) { case UNCRAFTABLE: return "Uncraftable Tipped Arrow"; From 1e085c586090280cf25b1842cefe145181c41970 Mon Sep 17 00:00:00 2001 From: ApacheZy Date: Tue, 9 Mar 2021 08:31:32 +0800 Subject: [PATCH 09/10] LangUtils updated to 3.1.2 --- pom.xml | 2 +- .../bentobox/hooks/LangUtilsHook.java | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 631219f11..e038cf052 100644 --- a/pom.xml +++ b/pom.xml @@ -296,7 +296,7 @@ com.github.apachezy LangUtils - 3.1.1 + 3.1.2 provided diff --git a/src/main/java/world/bentobox/bentobox/hooks/LangUtilsHook.java b/src/main/java/world/bentobox/bentobox/hooks/LangUtilsHook.java index 0db0d389e..bda2c3273 100644 --- a/src/main/java/world/bentobox/bentobox/hooks/LangUtilsHook.java +++ b/src/main/java/world/bentobox/bentobox/hooks/LangUtilsHook.java @@ -431,6 +431,26 @@ public static String getTippedArrowName(PotionType potionType, User user) { } } + /** + * 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. * From 31d4e43e94a98df837b23d33d3e91fef485842e1 Mon Sep 17 00:00:00 2001 From: ApacheZy Date: Thu, 11 Mar 2021 18:19:26 +0800 Subject: [PATCH 10/10] Change the method of checking LangUtils. --- pom.xml | 2 +- .../bentobox/hooks/LangUtilsHook.java | 33 ++----------------- 2 files changed, 4 insertions(+), 31 deletions(-) diff --git a/pom.xml b/pom.xml index 4eb338e9a..1b1cfa421 100644 --- a/pom.xml +++ b/pom.xml @@ -296,7 +296,7 @@ com.github.apachezy LangUtils - 3.1.2 + 3.1.3 provided diff --git a/src/main/java/world/bentobox/bentobox/hooks/LangUtilsHook.java b/src/main/java/world/bentobox/bentobox/hooks/LangUtilsHook.java index bda2c3273..74f5a8357 100644 --- a/src/main/java/world/bentobox/bentobox/hooks/LangUtilsHook.java +++ b/src/main/java/world/bentobox/bentobox/hooks/LangUtilsHook.java @@ -47,20 +47,14 @@ private static boolean doHook(Plugin plugin) { if (plugin != null && plugin.isEnabled()) { - Class langHelperClass; - try { - langHelperClass = Class.forName("com.meowj.langutils.lang.LanguageHelper"); - } catch (ClassNotFoundException e) { - langHelperClass = null; - } - - if (langHelperClass != null && checkMethods(langHelperClass)) { + String tag = plugin.getConfig().getString("Extra-TAG"); + if ("tag_r72EhIAL".equals(tag)) { hooked = true; return true; } Logger logger = BentoBox.getInstance().getLogger(); - logger.warning("The LangUtils version is too old to be applicable to BentoBox."); + 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"); } @@ -69,27 +63,6 @@ private static boolean doHook(Plugin plugin) { return false; } - private static boolean checkMethods(Class clazz) { - try { - clazz.getMethod("getMaterialName", Material .class, String.class); - clazz.getMethod("getPotionName", PotionType .class, String.class); - clazz.getMethod("getSplashPotionName", PotionType .class, String.class); - clazz.getMethod("getLingeringPotionName", PotionType .class, String.class); - clazz.getMethod("getTippedArrowName", PotionType .class, String.class); - clazz.getMethod("getPotionEffectName", PotionEffectType .class, String.class); - clazz.getMethod("getEffectAmplifierName", int .class, String.class); - clazz.getMethod("getPotionEffectDisplay", PotionEffect .class, String.class); - clazz.getMethod("getTropicalFishTypeName", TropicalFish.Pattern .class, String.class); - clazz.getMethod("getPredefinedTropicalFishName", TropicalFishBucketMeta.class, String.class); - clazz.getMethod("getDyeColorName", DyeColor .class, String.class); - clazz.getMethod("getVillagerLevelName", int .class, String.class); - clazz.getMethod("getVillagerProfessionName", Villager.Profession .class, String.class); - return true; - } catch (NoSuchMethodException e) { - return false; - } - } - @Override public boolean hook() { return LangUtilsHook.doHook(getPlugin());