diff --git a/pom.xml b/pom.xml index 543e26a..53a3bcd 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ me.pikamug.localelib LocaleLib - 1.6 + 1.7 UTF-8 diff --git a/src/main/java/me/pikamug/localelib/LocaleManager.java b/src/main/java/me/pikamug/localelib/LocaleManager.java index ed56812..b6acb52 100644 --- a/src/main/java/me/pikamug/localelib/LocaleManager.java +++ b/src/main/java/me/pikamug/localelib/LocaleManager.java @@ -21,20 +21,18 @@ import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.PotionMeta; -import me.pikamug.localelib.LocaleKeys; - public class LocaleManager{ private static Class craftMagicNumbers = null; private static Class itemClazz = null; private static Class localeClazz = null; private static boolean oldVersion = false; private static boolean hasBasePotionData = false; - private Map oldBlocks = LocaleKeys.getBlockKeys(); - private Map oldItems = LocaleKeys.getItemKeys(); - private Map oldPotions = LocaleKeys.getPotionKeys(); - private Map oldLingeringPotions = LocaleKeys.getLingeringPotionKeys(); - private Map oldSplashPotions = LocaleKeys.getSplashPotionKeys(); - private Map oldEntities = LocaleKeys.getEntityKeys(); + private final Map oldBlocks = LocaleKeys.getBlockKeys(); + private final Map oldItems = LocaleKeys.getItemKeys(); + private final Map oldPotions = LocaleKeys.getPotionKeys(); + private final Map oldLingeringPotions = LocaleKeys.getLingeringPotionKeys(); + private final Map oldSplashPotions = LocaleKeys.getSplashPotionKeys(); + private final Map oldEntities = LocaleKeys.getEntityKeys(); public LocaleManager() { oldVersion = isBelow113(); @@ -42,12 +40,12 @@ public LocaleManager() { // Bukkit version is 1.9+ hasBasePotionData = true; } - String version = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3]; + final String version = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3]; try { craftMagicNumbers = Class.forName("org.bukkit.craftbukkit.{v}.util.CraftMagicNumbers".replace("{v}", version)); itemClazz = Class.forName("net.minecraft.server.{v}.Item".replace("{v}", version)); localeClazz = Class.forName("net.minecraft.server.{v}.LocaleLanguage".replace("{v}", version)); - } catch (ClassNotFoundException e) { + } catch (final ClassNotFoundException e) { e.printStackTrace(); } } @@ -60,7 +58,8 @@ public LocaleManager() { * but note that most Potions use meta for 1.13+.

* * Message should contain {@code } string for replacement by - * this method (along with applicable {@code } strings). + * this method (along with applicable {@code } and/or + * {@code } strings). * * @param player The player whom the message is to be sent to * @param message The message to be sent to the player @@ -69,23 +68,25 @@ public LocaleManager() { * @param enchantments Enchantments for the item being translated * @param meta ItemMeta for the item being translated */ - public boolean sendMessage(Player player, String message, Material material, short durability, Map enchantments, ItemMeta meta) { + public boolean sendMessage(final Player player, final String message, final Material material, final short durability, final Map enchantments, final ItemMeta meta) { if (player == null || material == null) { return false; } String matKey = ""; try { matKey = queryMaterial(material, durability, meta); - } catch (Exception ex) { + } catch (final Exception ex) { Bukkit.getLogger().severe("[LocaleLib] Unable to query Material: " + material.name()); return false; } - - Collection enchKeys = queryEnchantments(enchantments).values(); - + final Collection enchKeys = queryEnchantments(enchantments).values(); + final Collection lvlKeys = queryLevels(enchantments).values(); String msg = message.replace("", "\",{\"translate\":\"" + matKey + "\"},\""); - for (String ek : enchKeys) { - msg.replaceFirst("", "\",{\"translate\":\"" + ek + "\"},\""); + for (final String ek : enchKeys) { + msg = msg.replaceFirst("", "\",{\"translate\":\"" + ek + "\"},\""); + } + for (final String lk : lvlKeys) { + msg = msg.replaceFirst("", "\",{\"translate\":\"" + lk + "\"},\""); } Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "tellraw " + player.getName() + " [\"" + msg + "\"]"); return true; @@ -98,7 +99,8 @@ public boolean sendMessage(Player player, String message, Material material, sho * Enchantments are optional and may be left null or empty.

* * Message should contain {@code } string for replacement by - * this method (along with applicable {@code } strings). + * this method (along with applicable {@code } and/or + * {@code } strings). * * @param player The player whom the message is to be sent to * @param message The message to be sent to the player @@ -106,7 +108,7 @@ public boolean sendMessage(Player player, String message, Material material, sho * @param durability Durability for the item being translated * @param enchantments Enchantments for the item being translated */ - public boolean sendMessage(Player player, String message, Material material, short durability, Map enchantments) { + public boolean sendMessage(final Player player, final String message, final Material material, final short durability, final Map enchantments) { return sendMessage(player, message, material, durability, enchantments, null); } @@ -114,21 +116,28 @@ public boolean sendMessage(Player player, String message, Material material, sho * Send message with enchantments translated to the client's locale. * Map of Enchantment+level is required. * - * Message should contain one {@code } string for each - * replacement by this method. + * Message should contain {@code } string for replacement by + * this method (along with applicable {@code } and/or + * {@code } strings). * * @param player The player whom the message is to be sent to * @param message The message to be sent to the player * @param enchantments Enchantments for the item being translated */ - public boolean sendMessage(Player player, String message, Map enchantments) { + public boolean sendMessage(final Player player, final String message, final Map enchantments) { if (player == null || message == null || enchantments == null) { return false; } - Collection enchKeys = queryEnchantments(enchantments).values(); - String msg = message; - for (String ek : enchKeys) { - msg.replaceFirst("", "\",{\"translate\":\"" + ek + "\"},\""); + final Collection enchKeys = queryEnchantments(enchantments).values(); + final Collection lvlKeys = queryLevels(enchantments).values(); + final String msg = message; + if (enchKeys != null && !enchKeys.isEmpty()) { + for (final String ek : enchKeys) { + msg.replaceFirst("", "\",{\"translate\":\"" + ek + "\"},\""); + } + for (final String lk : lvlKeys) { + msg.replaceFirst("", "\",{\"translate\":\"" + lk + "\"},\""); + } } Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "tellraw " + player.getName() + " [\"" + msg + "\"]"); return true; @@ -138,15 +147,15 @@ public boolean sendMessage(Player player, String message, Map * - * Message should contain {@code } - * string for replacement by this method. + * Message should contain {@code }string for replacement by + * this method. * * @param player The player whom the message is to be sent to * @param message The message to be sent to the player * @param type The entity type to be translated * @param extra Career, Ocelot, or Rabbit type if applicable */ - public boolean sendMessage(Player player, String message, EntityType type, String extra) { + public boolean sendMessage(final Player player, final String message, final EntityType type, final String extra) { if (player == null || message == null || type == null) { return false; } @@ -169,7 +178,7 @@ public boolean sendMessage(Player player, String message, EntityType type, Strin key = "entity.minecraft." + type.toString().toLowerCase(); } } - String msg = message.replace("", "\",{\"translate\":\"" + key + "\"},\""); + final String msg = message.replace("", "\",{\"translate\":\"" + key + "\"},\""); Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "tellraw " + player.getName() + " [\"" + msg + "\"]"); return true; } @@ -181,7 +190,7 @@ public boolean sendMessage(Player player, String message, EntityType type, Strin * @return the raw key * @throws IllegalArgumentException if an item with that material could not be found */ - public String queryMaterial(Material material) throws IllegalArgumentException, NullArgumentException { + public String queryMaterial(final Material material) throws IllegalArgumentException, NullArgumentException { return queryMaterial(material, (short) 0, null); } @@ -195,7 +204,7 @@ public String queryMaterial(Material material) throws IllegalArgumentException, * @throws NullArgumentException if the specified material parameter is null */ @SuppressWarnings("deprecation") - public String queryMaterial(Material material, short durability, ItemMeta meta) throws IllegalArgumentException, NullArgumentException { + public String queryMaterial(final Material material, final short durability, final ItemMeta meta) throws IllegalArgumentException, NullArgumentException { if (material == null) { throw new NullArgumentException("[LocaleLib] Material cannot be null"); } @@ -210,7 +219,7 @@ public String queryMaterial(Material material, short durability, ItemMeta meta) throw new IllegalArgumentException("[LocaleLib] Block not found: " + material.name() + "." + durability); } } else { - ItemStack i = new ItemStack(material, 1, durability); + final ItemStack i = new ItemStack(material, 1, durability); if (durability >= 0 && i.getItemMeta() instanceof PotionMeta) { if (hasBasePotionData) { if (material.equals(Material.POTION)) { @@ -231,15 +240,14 @@ public String queryMaterial(Material material, short durability, ItemMeta meta) } } else { try { - Object item = null; - Method m = craftMagicNumbers.getDeclaredMethod("getItem", material.getClass()); + final Method m = craftMagicNumbers.getDeclaredMethod("getItem", material.getClass()); m.setAccessible(true); - item = m.invoke(craftMagicNumbers, material); + final Object item = m.invoke(craftMagicNumbers, material); if (item == null) { throw new IllegalArgumentException(material.name() + " material could not be queried!"); } matKey = (String) itemClazz.getMethod("getName").invoke(item); - } catch (Exception ex) { + } catch (final Exception ex) { throw new IllegalArgumentException("[LocaleLib] Unable to query Material: " + material.name()); } if (meta != null && meta instanceof PotionMeta) { @@ -258,32 +266,49 @@ public String queryMaterial(Material material, short durability, ItemMeta meta) * @return the raw keys of the enchantments */ @SuppressWarnings("deprecation") - public Map queryEnchantments(Map enchantments) { - Map enchKeys = new HashMap(); + public Map queryEnchantments(final Map enchantments) { + final Map enchKeys = new HashMap(); if (enchantments == null || enchantments.isEmpty()) { return enchKeys; } if (oldVersion) { - for (Enchantment e : enchantments.keySet()) { + for (final Enchantment e : enchantments.keySet()) { enchKeys.put(e, "enchantment." + e.getName().toLowerCase().replace("_", ".") .replace("environmental", "all").replace("protection", "protect")); } } else { - for (Enchantment e : enchantments.keySet()) { + for (final Enchantment e : enchantments.keySet()) { enchKeys.put(e, "enchantment.minecraft." + e.toString().toLowerCase()); } } return enchKeys; } + /** + * Gets the key name of the specified enchantment levels as it would appear in a Minecraft lang file. + * + * @param enchantments Enchantment levels to get the keys of + * @return the raw keys of the enchantment levels + */ + public Map queryLevels(final Map enchantments) { + final Map lvlKeys = new HashMap(); + if (enchantments == null || enchantments.isEmpty()) { + return lvlKeys; + } + for (final Integer i : enchantments.values()) { + lvlKeys.put(i, "enchantment.level." + i); + } + return lvlKeys; + } + /** * Gets the display name of the specified material as it would appear in a Minecraft lang file. * * @param key the raw key for the object name * @return the display name of the specified key within the server locale file */ - public String toServerLocale(String key) throws IllegalAccessException, InvocationTargetException { - Method trans = Arrays.stream(localeClazz.getMethods()) + public String toServerLocale(final String key) throws IllegalAccessException, InvocationTargetException { + final Method trans = Arrays.stream(localeClazz.getMethods()) .filter(m -> m.getReturnType().equals(String.class)) .filter(m -> m.getParameterCount() == 1) .filter(m -> m.getParameters()[0].getType().equals(String.class)) @@ -310,7 +335,7 @@ public boolean isBelow113() { return _isBelow113(Bukkit.getServer().getBukkitVersion().split("-")[0]); } - private boolean _isBelow113(String bukkitVersion) { + private boolean _isBelow113(final String bukkitVersion) { if (bukkitVersion.matches("^[0-9.]+$")) { switch(bukkitVersion) { case "1.12.2" : diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index ad66161..3fe7e6d 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,6 +1,6 @@ name: LocaleLib main: me.pikamug.localelib.LocaleLib -version: 1.6 +version: 1.7 api-version: 1.13 description: Show translated names of items, entities & more in client's language website: https://www.spigotmc.org/resources/quests.3711/