From a501a3cbb4b9c289f95832dc66575343250a2a3c Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Mon, 7 Nov 2022 15:20:30 +0100 Subject: [PATCH] Changes things for the non-static Translator --- .../blacksmith/BlacksmithPlugin.java | 19 ++++++++++++---- .../command/BlackSmithConfigCommand.java | 3 ++- .../command/BlackSmithEditCommand.java | 13 ++++++----- .../blacksmith/command/PresetCommand.java | 13 ++++++----- .../blacksmith/command/ReloadCommand.java | 3 ++- .../BlacksmithTranslatableMessage.java | 22 +++++++++++-------- .../blacksmith/formatting/ItemType.java | 8 ++++--- .../blacksmith/formatting/TimeFormatter.java | 6 ++--- .../blacksmith/listener/NPCClickListener.java | 4 +++- .../blacksmith/util/TypeValidationHelper.java | 16 +++++++++----- 10 files changed, 70 insertions(+), 37 deletions(-) diff --git a/src/main/java/net/knarcraft/blacksmith/BlacksmithPlugin.java b/src/main/java/net/knarcraft/blacksmith/BlacksmithPlugin.java index 4491a24..0511308 100644 --- a/src/main/java/net/knarcraft/blacksmith/BlacksmithPlugin.java +++ b/src/main/java/net/knarcraft/blacksmith/BlacksmithPlugin.java @@ -31,6 +31,7 @@ public class BlacksmithPlugin extends JavaPlugin { private static BlacksmithPlugin instance; private GlobalSettings config; + private static Translator translator; /** * Gets an instance of the Blacksmith plugin @@ -56,7 +57,16 @@ public class BlacksmithPlugin extends JavaPlugin { public void reload() { config.load(); this.reloadConfig(); - Translator.loadLanguages(this.getConfig().getString("language", "en")); + translator.loadLanguages(this.getConfig().getString("language", "en")); + } + + /** + * Gets the translator to use for translation + * + * @return

The translator to use

+ */ + public static Translator getTranslator() { + return BlacksmithPlugin.translator; } @Override @@ -81,9 +91,10 @@ public class BlacksmithPlugin extends JavaPlugin { config.load(); //Prepare the translator - Translator.registerMessageCategory(TranslatableTimeUnit.UNIT_SECOND); - Translator.registerMessageCategory(BlacksmithTranslatableMessage.ITEM_TYPE_ENCHANTMENT); - Translator.loadLanguages(fileConfiguration.getString("language", "en")); + translator = new Translator(); + translator.registerMessageCategory(TranslatableTimeUnit.UNIT_SECOND); + translator.registerMessageCategory(BlacksmithTranslatableMessage.ITEM_TYPE_ENCHANTMENT); + translator.loadLanguages(fileConfiguration.getString("language", "en")); //Set up Vault integration if (!setUpVault()) { diff --git a/src/main/java/net/knarcraft/blacksmith/command/BlackSmithConfigCommand.java b/src/main/java/net/knarcraft/blacksmith/command/BlackSmithConfigCommand.java index 40ff6bd..4e6d6de 100644 --- a/src/main/java/net/knarcraft/blacksmith/command/BlackSmithConfigCommand.java +++ b/src/main/java/net/knarcraft/blacksmith/command/BlackSmithConfigCommand.java @@ -42,7 +42,8 @@ public class BlackSmithConfigCommand implements CommandExecutor { //Changing reforge-able items' default isn't recommended if (commandName.equalsIgnoreCase(NPCSetting.REFORGE_ABLE_ITEMS.getCommandName())) { - StringFormatter.displayErrorMessage(sender, BlacksmithTranslatableMessage.DEFAULT_REFORGE_ABLE_ITEMS_UNCHANGEABLE); + StringFormatter.displayErrorMessage(sender, BlacksmithPlugin.getTranslator(), + BlacksmithTranslatableMessage.DEFAULT_REFORGE_ABLE_ITEMS_UNCHANGEABLE); return false; } diff --git a/src/main/java/net/knarcraft/blacksmith/command/BlackSmithEditCommand.java b/src/main/java/net/knarcraft/blacksmith/command/BlackSmithEditCommand.java index c58a8ce..411f809 100644 --- a/src/main/java/net/knarcraft/blacksmith/command/BlackSmithEditCommand.java +++ b/src/main/java/net/knarcraft/blacksmith/command/BlackSmithEditCommand.java @@ -10,7 +10,6 @@ import net.knarcraft.blacksmith.trait.BlacksmithTrait; import net.knarcraft.blacksmith.util.InputParsingHelper; import net.knarcraft.blacksmith.util.TypeValidationHelper; import net.knarcraft.knarlib.formatting.StringFormatter; -import net.knarcraft.knarlib.formatting.Translator; import net.md_5.bungee.api.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -32,7 +31,8 @@ public class BlackSmithEditCommand implements CommandExecutor { @NotNull String[] args) { NPC npc = CitizensAPI.getDefaultNPCSelector().getSelected(sender); if (npc == null || !npc.hasTrait(BlacksmithTrait.class)) { - StringFormatter.displayErrorMessage(sender, BlacksmithTranslatableMessage.NO_NPC_SELECTED); + StringFormatter.displayErrorMessage(sender, BlacksmithPlugin.getTranslator(), + BlacksmithTranslatableMessage.NO_NPC_SELECTED); return true; } @@ -107,11 +107,14 @@ public class BlackSmithEditCommand implements CommandExecutor { StringFormatter.displaySuccessMessage(sender, getCurrentValueMessage(npcSetting.getCommandName(), rawValue)); } else { //Add a marker if the value has been customized - String marker = Translator.getTranslatedMessage(BlacksmithTranslatableMessage.SETTING_OVERRIDDEN_MARKER); - StringFormatter.displaySuccessMessage(sender, getCurrentValueMessage(npcSetting.getCommandName(), rawValue) + marker); + String marker = BlacksmithPlugin.getTranslator().getTranslatedMessage( + BlacksmithTranslatableMessage.SETTING_OVERRIDDEN_MARKER); + StringFormatter.displaySuccessMessage(sender, + getCurrentValueMessage(npcSetting.getCommandName(), rawValue) + marker); } if (npcSetting.getPath().startsWith("defaults.messages")) { - sender.sendMessage(BlacksmithTranslatableMessage.getRawValueMessage(rawValue.replace(ChatColor.COLOR_CHAR, '&'))); + sender.sendMessage(BlacksmithTranslatableMessage.getRawValueMessage( + rawValue.replace(ChatColor.COLOR_CHAR, '&'))); } } diff --git a/src/main/java/net/knarcraft/blacksmith/command/PresetCommand.java b/src/main/java/net/knarcraft/blacksmith/command/PresetCommand.java index da8f702..9ec17ec 100644 --- a/src/main/java/net/knarcraft/blacksmith/command/PresetCommand.java +++ b/src/main/java/net/knarcraft/blacksmith/command/PresetCommand.java @@ -1,10 +1,10 @@ package net.knarcraft.blacksmith.command; +import net.knarcraft.blacksmith.BlacksmithPlugin; import net.knarcraft.blacksmith.config.SmithPreset; import net.knarcraft.blacksmith.config.SmithPresetFilter; import net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage; import net.knarcraft.knarlib.formatting.StringFormatter; -import net.knarcraft.knarlib.formatting.Translator; import org.bukkit.Material; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -37,7 +37,8 @@ public class PresetCommand implements CommandExecutor { SmithPresetFilter filter = SmithPresetFilter.valueOf(parts[1]); if (!smithPreset.supportsFilter(filter)) { - StringFormatter.displayErrorMessage(sender, BlacksmithTranslatableMessage.INVALID_FILTER_FOR_PRESET); + StringFormatter.displayErrorMessage(sender, BlacksmithPlugin.getTranslator(), + BlacksmithTranslatableMessage.INVALID_FILTER_FOR_PRESET); return false; } includedMaterials = smithPreset.getFilteredMaterials(filter); @@ -45,7 +46,8 @@ public class PresetCommand implements CommandExecutor { includedMaterials = SmithPreset.valueOf(presetName).getMaterials(); } } catch (IllegalArgumentException exception) { - StringFormatter.displayErrorMessage(sender, BlacksmithTranslatableMessage.INVALID_PRESET_OR_FILTER); + StringFormatter.displayErrorMessage(sender, BlacksmithPlugin.getTranslator(), + BlacksmithTranslatableMessage.INVALID_PRESET_OR_FILTER); return false; } @@ -54,8 +56,9 @@ public class PresetCommand implements CommandExecutor { for (Material material : includedMaterials) { materialNames.add(material.name()); } - StringFormatter.displaySuccessMessage(sender, StringFormatter.replacePlaceholder(Translator.getTranslatedMessage( - BlacksmithTranslatableMessage.PRESET_MATERIALS), "{materials}", String.join(", ", materialNames))); + StringFormatter.displaySuccessMessage(sender, StringFormatter.replacePlaceholder( + BlacksmithPlugin.getTranslator().getTranslatedMessage(BlacksmithTranslatableMessage.PRESET_MATERIALS), + "{materials}", String.join(", ", materialNames))); return true; } diff --git a/src/main/java/net/knarcraft/blacksmith/command/ReloadCommand.java b/src/main/java/net/knarcraft/blacksmith/command/ReloadCommand.java index 3b56633..832d0ac 100644 --- a/src/main/java/net/knarcraft/blacksmith/command/ReloadCommand.java +++ b/src/main/java/net/knarcraft/blacksmith/command/ReloadCommand.java @@ -21,7 +21,8 @@ public class ReloadCommand implements TabExecutor { public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { BlacksmithPlugin.getInstance().reload(); - StringFormatter.displaySuccessMessage(sender, BlacksmithTranslatableMessage.PLUGIN_RELOADED); + StringFormatter.displaySuccessMessage(sender, BlacksmithPlugin.getTranslator(), + BlacksmithTranslatableMessage.PLUGIN_RELOADED); return true; } diff --git a/src/main/java/net/knarcraft/blacksmith/formatting/BlacksmithTranslatableMessage.java b/src/main/java/net/knarcraft/blacksmith/formatting/BlacksmithTranslatableMessage.java index 6e6ff71..32efa2a 100644 --- a/src/main/java/net/knarcraft/blacksmith/formatting/BlacksmithTranslatableMessage.java +++ b/src/main/java/net/knarcraft/blacksmith/formatting/BlacksmithTranslatableMessage.java @@ -1,8 +1,8 @@ package net.knarcraft.blacksmith.formatting; +import net.knarcraft.blacksmith.BlacksmithPlugin; import net.knarcraft.knarlib.formatting.StringFormatter; import net.knarcraft.knarlib.formatting.TranslatableMessage; -import net.knarcraft.knarlib.formatting.Translator; /** * An enum containing all translatable global messages @@ -144,8 +144,8 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage { * @return

The message to display

*/ public static String getRawValueMessage(String rawValue) { - return StringFormatter.replacePlaceholder(Translator.getTranslatedMessage(BlacksmithTranslatableMessage.RAW_VALUE), - "{rawValue}", rawValue); + return StringFormatter.replacePlaceholder(BlacksmithPlugin.getTranslator().getTranslatedMessage( + BlacksmithTranslatableMessage.RAW_VALUE), "{rawValue}", rawValue); } /** @@ -156,8 +156,9 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage { * @return

The string to display to a user

*/ public static String getValueChangedMessage(String setting, String newValue) { - return StringFormatter.replacePlaceholders(Translator.getTranslatedMessage(BlacksmithTranslatableMessage.VALUE_CHANGED), - new String[]{"{setting}", "{newValue}"}, new String[]{setting, newValue}); + return StringFormatter.replacePlaceholders(BlacksmithPlugin.getTranslator().getTranslatedMessage( + BlacksmithTranslatableMessage.VALUE_CHANGED), new String[]{"{setting}", "{newValue}"}, + new String[]{setting, newValue}); } /** @@ -170,7 +171,8 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage { * @return

The string to display to a user

*/ public static String getItemValueChangedMessage(String setting, ItemType itemType, String item, String newValue) { - return StringFormatter.replacePlaceholders(Translator.getTranslatedMessage(BlacksmithTranslatableMessage.VALUE_FOR_ITEM_CHANGED), + return StringFormatter.replacePlaceholders(BlacksmithPlugin.getTranslator().getTranslatedMessage( + BlacksmithTranslatableMessage.VALUE_FOR_ITEM_CHANGED), new String[]{"{setting}", "{itemType}", "{item}", "{newValue}"}, new String[]{setting, itemType.getItemTypeName(), item, newValue}); } @@ -183,8 +185,9 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage { * @return

The string to display to a user

*/ public static String getCurrentValueMessage(String setting, String currentValue) { - return StringFormatter.replacePlaceholders(Translator.getTranslatedMessage(BlacksmithTranslatableMessage.CURRENT_VALUE), - new String[]{"{setting}", "{currentValue}"}, new String[]{setting, currentValue}); + return StringFormatter.replacePlaceholders(BlacksmithPlugin.getTranslator().getTranslatedMessage( + BlacksmithTranslatableMessage.CURRENT_VALUE), new String[]{"{setting}", "{currentValue}"}, + new String[]{setting, currentValue}); } /** @@ -197,7 +200,8 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage { * @return

The string to display to a user

*/ public static String getItemCurrentValueMessage(String setting, ItemType itemType, String item, String currentValue) { - return StringFormatter.replacePlaceholders(Translator.getTranslatedMessage(BlacksmithTranslatableMessage.CURRENT_VALUE_FOR_ITEM), + return StringFormatter.replacePlaceholders(BlacksmithPlugin.getTranslator().getTranslatedMessage( + BlacksmithTranslatableMessage.CURRENT_VALUE_FOR_ITEM), new String[]{"{setting}", "{itemType}", "{item}", "{currentValue}"}, new String[]{setting, itemType.getItemTypeName(), item, currentValue}); } diff --git a/src/main/java/net/knarcraft/blacksmith/formatting/ItemType.java b/src/main/java/net/knarcraft/blacksmith/formatting/ItemType.java index 3719469..c0d24d4 100644 --- a/src/main/java/net/knarcraft/blacksmith/formatting/ItemType.java +++ b/src/main/java/net/knarcraft/blacksmith/formatting/ItemType.java @@ -1,6 +1,6 @@ package net.knarcraft.blacksmith.formatting; -import net.knarcraft.knarlib.formatting.Translator; +import net.knarcraft.blacksmith.BlacksmithPlugin; /** * An enum representing all item types used in messages @@ -17,8 +17,10 @@ public enum ItemType { */ public String getItemTypeName() { return switch (this) { - case MATERIAL -> Translator.getTranslatedMessage(BlacksmithTranslatableMessage.ITEM_TYPE_MATERIAL); - case ENCHANTMENT -> Translator.getTranslatedMessage(BlacksmithTranslatableMessage.ITEM_TYPE_ENCHANTMENT); + case MATERIAL -> BlacksmithPlugin.getTranslator().getTranslatedMessage( + BlacksmithTranslatableMessage.ITEM_TYPE_MATERIAL); + case ENCHANTMENT -> BlacksmithPlugin.getTranslator().getTranslatedMessage( + BlacksmithTranslatableMessage.ITEM_TYPE_ENCHANTMENT); }; } diff --git a/src/main/java/net/knarcraft/blacksmith/formatting/TimeFormatter.java b/src/main/java/net/knarcraft/blacksmith/formatting/TimeFormatter.java index a1f39c0..ddea841 100644 --- a/src/main/java/net/knarcraft/blacksmith/formatting/TimeFormatter.java +++ b/src/main/java/net/knarcraft/blacksmith/formatting/TimeFormatter.java @@ -1,6 +1,6 @@ package net.knarcraft.blacksmith.formatting; -import net.knarcraft.knarlib.formatting.Translator; +import net.knarcraft.blacksmith.BlacksmithPlugin; import java.util.Arrays; import java.util.List; @@ -24,7 +24,7 @@ public final class TimeFormatter { */ public static String formatTime(boolean exact, int seconds) { if (exact) { - return net.knarcraft.knarlib.formatting.TimeFormatter.getDurationString(seconds); + return net.knarcraft.knarlib.formatting.TimeFormatter.getDurationString(BlacksmithPlugin.getTranslator(), seconds); } else { return formatUnclearTime(seconds); } @@ -53,7 +53,7 @@ public final class TimeFormatter { * @return

Text describing the time interval

*/ private static String getMessageFromInterval(TimeInterval interval) { - String text = Translator.getTranslatedMessage(BlacksmithTranslatableMessage.valueOf(interval.name())); + String text = BlacksmithPlugin.getTranslator().getTranslatedMessage(BlacksmithTranslatableMessage.valueOf(interval.name())); //Choose a random entry if a comma-separated list is provided if (text != null && text.contains(",")) { diff --git a/src/main/java/net/knarcraft/blacksmith/listener/NPCClickListener.java b/src/main/java/net/knarcraft/blacksmith/listener/NPCClickListener.java index c17e783..5aa29fb 100644 --- a/src/main/java/net/knarcraft/blacksmith/listener/NPCClickListener.java +++ b/src/main/java/net/knarcraft/blacksmith/listener/NPCClickListener.java @@ -1,5 +1,6 @@ package net.knarcraft.blacksmith.listener; +import net.knarcraft.blacksmith.BlacksmithPlugin; import net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage; import net.knarcraft.blacksmith.trait.BlacksmithTrait; import net.knarcraft.knarlib.formatting.StringFormatter; @@ -25,7 +26,8 @@ public class NPCClickListener implements Listener { //Permission check if (!player.hasPermission("blacksmith.use")) { - StringFormatter.displayErrorMessage(player, BlacksmithTranslatableMessage.PERMISSION_DENIED); + StringFormatter.displayErrorMessage(player, BlacksmithPlugin.getTranslator(), + BlacksmithTranslatableMessage.PERMISSION_DENIED); return; } diff --git a/src/main/java/net/knarcraft/blacksmith/util/TypeValidationHelper.java b/src/main/java/net/knarcraft/blacksmith/util/TypeValidationHelper.java index bda0fad..4ec3263 100644 --- a/src/main/java/net/knarcraft/blacksmith/util/TypeValidationHelper.java +++ b/src/main/java/net/knarcraft/blacksmith/util/TypeValidationHelper.java @@ -1,5 +1,6 @@ package net.knarcraft.blacksmith.util; +import net.knarcraft.blacksmith.BlacksmithPlugin; import net.knarcraft.blacksmith.config.SettingValueType; import net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage; import net.knarcraft.knarlib.formatting.StringFormatter; @@ -51,7 +52,8 @@ public final class TypeValidationHelper { private static boolean isStringList(Object value, CommandSender sender) { boolean isStringList = value instanceof String[] || value instanceof List || value instanceof String; if (!isStringList && sender != null) { - StringFormatter.displayErrorMessage(sender, BlacksmithTranslatableMessage.INPUT_STRING_LIST_REQUIRED); + StringFormatter.displayErrorMessage(sender, BlacksmithPlugin.getTranslator(), + BlacksmithTranslatableMessage.INPUT_STRING_LIST_REQUIRED); } return isStringList; } @@ -69,7 +71,8 @@ public final class TypeValidationHelper { return intValue >= 0 && intValue <= 100; } catch (NumberFormatException | NullPointerException exception) { if (sender != null) { - StringFormatter.displayErrorMessage(sender, BlacksmithTranslatableMessage.INPUT_PERCENTAGE_REQUIRED); + StringFormatter.displayErrorMessage(sender, BlacksmithPlugin.getTranslator(), + BlacksmithTranslatableMessage.INPUT_PERCENTAGE_REQUIRED); } return false; } @@ -85,7 +88,8 @@ public final class TypeValidationHelper { private static boolean isNonEmptyString(Object value, CommandSender sender) { boolean isString = value instanceof String string && !string.strip().isEmpty(); if (!isString && sender != null) { - StringFormatter.displayErrorMessage(sender, BlacksmithTranslatableMessage.INPUT_STRING_REQUIRED); + StringFormatter.displayErrorMessage(sender, BlacksmithPlugin.getTranslator(), + BlacksmithTranslatableMessage.INPUT_STRING_REQUIRED); } return isString; } @@ -102,7 +106,8 @@ public final class TypeValidationHelper { return ConfigHelper.asDouble(value) >= 0.0; } catch (NumberFormatException | NullPointerException exception) { if (sender != null) { - StringFormatter.displayErrorMessage(sender, BlacksmithTranslatableMessage.INPUT_POSITIVE_DOUBLE_REQUIRED); + StringFormatter.displayErrorMessage(sender, BlacksmithPlugin.getTranslator(), + BlacksmithTranslatableMessage.INPUT_POSITIVE_DOUBLE_REQUIRED); } return false; } @@ -120,7 +125,8 @@ public final class TypeValidationHelper { return ConfigHelper.asInt(value) >= 0; } catch (NumberFormatException | NullPointerException exception) { if (sender != null) { - StringFormatter.displayErrorMessage(sender, BlacksmithTranslatableMessage.INPUT_POSITIVE_INTEGER_REQUIRED); + StringFormatter.displayErrorMessage(sender, BlacksmithPlugin.getTranslator(), + BlacksmithTranslatableMessage.INPUT_POSITIVE_INTEGER_REQUIRED); } return false; }