Changes things for the non-static Translator

This commit is contained in:
Kristian Knarvik 2022-11-07 15:20:30 +01:00
parent 7875e9a705
commit a501a3cbb4
10 changed files with 70 additions and 37 deletions

View File

@ -31,6 +31,7 @@ public class BlacksmithPlugin extends JavaPlugin {
private static BlacksmithPlugin instance; private static BlacksmithPlugin instance;
private GlobalSettings config; private GlobalSettings config;
private static Translator translator;
/** /**
* Gets an instance of the Blacksmith plugin * Gets an instance of the Blacksmith plugin
@ -56,7 +57,16 @@ public class BlacksmithPlugin extends JavaPlugin {
public void reload() { public void reload() {
config.load(); config.load();
this.reloadConfig(); this.reloadConfig();
Translator.loadLanguages(this.getConfig().getString("language", "en")); translator.loadLanguages(this.getConfig().getString("language", "en"));
}
/**
* Gets the translator to use for translation
*
* @return <p>The translator to use</p>
*/
public static Translator getTranslator() {
return BlacksmithPlugin.translator;
} }
@Override @Override
@ -81,9 +91,10 @@ public class BlacksmithPlugin extends JavaPlugin {
config.load(); config.load();
//Prepare the translator //Prepare the translator
Translator.registerMessageCategory(TranslatableTimeUnit.UNIT_SECOND); translator = new Translator();
Translator.registerMessageCategory(BlacksmithTranslatableMessage.ITEM_TYPE_ENCHANTMENT); translator.registerMessageCategory(TranslatableTimeUnit.UNIT_SECOND);
Translator.loadLanguages(fileConfiguration.getString("language", "en")); translator.registerMessageCategory(BlacksmithTranslatableMessage.ITEM_TYPE_ENCHANTMENT);
translator.loadLanguages(fileConfiguration.getString("language", "en"));
//Set up Vault integration //Set up Vault integration
if (!setUpVault()) { if (!setUpVault()) {

View File

@ -42,7 +42,8 @@ public class BlackSmithConfigCommand implements CommandExecutor {
//Changing reforge-able items' default isn't recommended //Changing reforge-able items' default isn't recommended
if (commandName.equalsIgnoreCase(NPCSetting.REFORGE_ABLE_ITEMS.getCommandName())) { 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; return false;
} }

View File

@ -10,7 +10,6 @@ import net.knarcraft.blacksmith.trait.BlacksmithTrait;
import net.knarcraft.blacksmith.util.InputParsingHelper; import net.knarcraft.blacksmith.util.InputParsingHelper;
import net.knarcraft.blacksmith.util.TypeValidationHelper; import net.knarcraft.blacksmith.util.TypeValidationHelper;
import net.knarcraft.knarlib.formatting.StringFormatter; import net.knarcraft.knarlib.formatting.StringFormatter;
import net.knarcraft.knarlib.formatting.Translator;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
@ -32,7 +31,8 @@ public class BlackSmithEditCommand implements CommandExecutor {
@NotNull String[] args) { @NotNull String[] args) {
NPC npc = CitizensAPI.getDefaultNPCSelector().getSelected(sender); NPC npc = CitizensAPI.getDefaultNPCSelector().getSelected(sender);
if (npc == null || !npc.hasTrait(BlacksmithTrait.class)) { 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; return true;
} }
@ -107,11 +107,14 @@ public class BlackSmithEditCommand implements CommandExecutor {
StringFormatter.displaySuccessMessage(sender, getCurrentValueMessage(npcSetting.getCommandName(), rawValue)); StringFormatter.displaySuccessMessage(sender, getCurrentValueMessage(npcSetting.getCommandName(), rawValue));
} else { } else {
//Add a marker if the value has been customized //Add a marker if the value has been customized
String marker = Translator.getTranslatedMessage(BlacksmithTranslatableMessage.SETTING_OVERRIDDEN_MARKER); String marker = BlacksmithPlugin.getTranslator().getTranslatedMessage(
StringFormatter.displaySuccessMessage(sender, getCurrentValueMessage(npcSetting.getCommandName(), rawValue) + marker); BlacksmithTranslatableMessage.SETTING_OVERRIDDEN_MARKER);
StringFormatter.displaySuccessMessage(sender,
getCurrentValueMessage(npcSetting.getCommandName(), rawValue) + marker);
} }
if (npcSetting.getPath().startsWith("defaults.messages")) { if (npcSetting.getPath().startsWith("defaults.messages")) {
sender.sendMessage(BlacksmithTranslatableMessage.getRawValueMessage(rawValue.replace(ChatColor.COLOR_CHAR, '&'))); sender.sendMessage(BlacksmithTranslatableMessage.getRawValueMessage(
rawValue.replace(ChatColor.COLOR_CHAR, '&')));
} }
} }

View File

@ -1,10 +1,10 @@
package net.knarcraft.blacksmith.command; package net.knarcraft.blacksmith.command;
import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.blacksmith.config.SmithPreset; import net.knarcraft.blacksmith.config.SmithPreset;
import net.knarcraft.blacksmith.config.SmithPresetFilter; import net.knarcraft.blacksmith.config.SmithPresetFilter;
import net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage; import net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage;
import net.knarcraft.knarlib.formatting.StringFormatter; import net.knarcraft.knarlib.formatting.StringFormatter;
import net.knarcraft.knarlib.formatting.Translator;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
@ -37,7 +37,8 @@ public class PresetCommand implements CommandExecutor {
SmithPresetFilter filter = SmithPresetFilter.valueOf(parts[1]); SmithPresetFilter filter = SmithPresetFilter.valueOf(parts[1]);
if (!smithPreset.supportsFilter(filter)) { if (!smithPreset.supportsFilter(filter)) {
StringFormatter.displayErrorMessage(sender, BlacksmithTranslatableMessage.INVALID_FILTER_FOR_PRESET); StringFormatter.displayErrorMessage(sender, BlacksmithPlugin.getTranslator(),
BlacksmithTranslatableMessage.INVALID_FILTER_FOR_PRESET);
return false; return false;
} }
includedMaterials = smithPreset.getFilteredMaterials(filter); includedMaterials = smithPreset.getFilteredMaterials(filter);
@ -45,7 +46,8 @@ public class PresetCommand implements CommandExecutor {
includedMaterials = SmithPreset.valueOf(presetName).getMaterials(); includedMaterials = SmithPreset.valueOf(presetName).getMaterials();
} }
} catch (IllegalArgumentException exception) { } catch (IllegalArgumentException exception) {
StringFormatter.displayErrorMessage(sender, BlacksmithTranslatableMessage.INVALID_PRESET_OR_FILTER); StringFormatter.displayErrorMessage(sender, BlacksmithPlugin.getTranslator(),
BlacksmithTranslatableMessage.INVALID_PRESET_OR_FILTER);
return false; return false;
} }
@ -54,8 +56,9 @@ public class PresetCommand implements CommandExecutor {
for (Material material : includedMaterials) { for (Material material : includedMaterials) {
materialNames.add(material.name()); materialNames.add(material.name());
} }
StringFormatter.displaySuccessMessage(sender, StringFormatter.replacePlaceholder(Translator.getTranslatedMessage( StringFormatter.displaySuccessMessage(sender, StringFormatter.replacePlaceholder(
BlacksmithTranslatableMessage.PRESET_MATERIALS), "{materials}", String.join(", ", materialNames))); BlacksmithPlugin.getTranslator().getTranslatedMessage(BlacksmithTranslatableMessage.PRESET_MATERIALS),
"{materials}", String.join(", ", materialNames)));
return true; return true;
} }

View File

@ -21,7 +21,8 @@ public class ReloadCommand implements TabExecutor {
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
@NotNull String[] args) { @NotNull String[] args) {
BlacksmithPlugin.getInstance().reload(); BlacksmithPlugin.getInstance().reload();
StringFormatter.displaySuccessMessage(sender, BlacksmithTranslatableMessage.PLUGIN_RELOADED); StringFormatter.displaySuccessMessage(sender, BlacksmithPlugin.getTranslator(),
BlacksmithTranslatableMessage.PLUGIN_RELOADED);
return true; return true;
} }

View File

@ -1,8 +1,8 @@
package net.knarcraft.blacksmith.formatting; package net.knarcraft.blacksmith.formatting;
import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.knarlib.formatting.StringFormatter; import net.knarcraft.knarlib.formatting.StringFormatter;
import net.knarcraft.knarlib.formatting.TranslatableMessage; import net.knarcraft.knarlib.formatting.TranslatableMessage;
import net.knarcraft.knarlib.formatting.Translator;
/** /**
* An enum containing all translatable global messages * An enum containing all translatable global messages
@ -144,8 +144,8 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage {
* @return <p>The message to display</p> * @return <p>The message to display</p>
*/ */
public static String getRawValueMessage(String rawValue) { public static String getRawValueMessage(String rawValue) {
return StringFormatter.replacePlaceholder(Translator.getTranslatedMessage(BlacksmithTranslatableMessage.RAW_VALUE), return StringFormatter.replacePlaceholder(BlacksmithPlugin.getTranslator().getTranslatedMessage(
"{rawValue}", rawValue); BlacksmithTranslatableMessage.RAW_VALUE), "{rawValue}", rawValue);
} }
/** /**
@ -156,8 +156,9 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage {
* @return <p>The string to display to a user</p> * @return <p>The string to display to a user</p>
*/ */
public static String getValueChangedMessage(String setting, String newValue) { public static String getValueChangedMessage(String setting, String newValue) {
return StringFormatter.replacePlaceholders(Translator.getTranslatedMessage(BlacksmithTranslatableMessage.VALUE_CHANGED), return StringFormatter.replacePlaceholders(BlacksmithPlugin.getTranslator().getTranslatedMessage(
new String[]{"{setting}", "{newValue}"}, new String[]{setting, newValue}); BlacksmithTranslatableMessage.VALUE_CHANGED), new String[]{"{setting}", "{newValue}"},
new String[]{setting, newValue});
} }
/** /**
@ -170,7 +171,8 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage {
* @return <p>The string to display to a user</p> * @return <p>The string to display to a user</p>
*/ */
public static String getItemValueChangedMessage(String setting, ItemType itemType, String item, String newValue) { 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}", "{item}", "{newValue}"},
new String[]{setting, itemType.getItemTypeName(), item, newValue}); new String[]{setting, itemType.getItemTypeName(), item, newValue});
} }
@ -183,8 +185,9 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage {
* @return <p>The string to display to a user</p> * @return <p>The string to display to a user</p>
*/ */
public static String getCurrentValueMessage(String setting, String currentValue) { public static String getCurrentValueMessage(String setting, String currentValue) {
return StringFormatter.replacePlaceholders(Translator.getTranslatedMessage(BlacksmithTranslatableMessage.CURRENT_VALUE), return StringFormatter.replacePlaceholders(BlacksmithPlugin.getTranslator().getTranslatedMessage(
new String[]{"{setting}", "{currentValue}"}, new String[]{setting, currentValue}); BlacksmithTranslatableMessage.CURRENT_VALUE), new String[]{"{setting}", "{currentValue}"},
new String[]{setting, currentValue});
} }
/** /**
@ -197,7 +200,8 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage {
* @return <p>The string to display to a user</p> * @return <p>The string to display to a user</p>
*/ */
public static String getItemCurrentValueMessage(String setting, ItemType itemType, String item, String currentValue) { 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}", "{item}", "{currentValue}"},
new String[]{setting, itemType.getItemTypeName(), item, currentValue}); new String[]{setting, itemType.getItemTypeName(), item, currentValue});
} }

View File

@ -1,6 +1,6 @@
package net.knarcraft.blacksmith.formatting; 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 * An enum representing all item types used in messages
@ -17,8 +17,10 @@ public enum ItemType {
*/ */
public String getItemTypeName() { public String getItemTypeName() {
return switch (this) { return switch (this) {
case MATERIAL -> Translator.getTranslatedMessage(BlacksmithTranslatableMessage.ITEM_TYPE_MATERIAL); case MATERIAL -> BlacksmithPlugin.getTranslator().getTranslatedMessage(
case ENCHANTMENT -> Translator.getTranslatedMessage(BlacksmithTranslatableMessage.ITEM_TYPE_ENCHANTMENT); BlacksmithTranslatableMessage.ITEM_TYPE_MATERIAL);
case ENCHANTMENT -> BlacksmithPlugin.getTranslator().getTranslatedMessage(
BlacksmithTranslatableMessage.ITEM_TYPE_ENCHANTMENT);
}; };
} }

View File

@ -1,6 +1,6 @@
package net.knarcraft.blacksmith.formatting; package net.knarcraft.blacksmith.formatting;
import net.knarcraft.knarlib.formatting.Translator; import net.knarcraft.blacksmith.BlacksmithPlugin;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -24,7 +24,7 @@ public final class TimeFormatter {
*/ */
public static String formatTime(boolean exact, int seconds) { public static String formatTime(boolean exact, int seconds) {
if (exact) { if (exact) {
return net.knarcraft.knarlib.formatting.TimeFormatter.getDurationString(seconds); return net.knarcraft.knarlib.formatting.TimeFormatter.getDurationString(BlacksmithPlugin.getTranslator(), seconds);
} else { } else {
return formatUnclearTime(seconds); return formatUnclearTime(seconds);
} }
@ -53,7 +53,7 @@ public final class TimeFormatter {
* @return <p>Text describing the time interval</p> * @return <p>Text describing the time interval</p>
*/ */
private static String getMessageFromInterval(TimeInterval 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 //Choose a random entry if a comma-separated list is provided
if (text != null && text.contains(",")) { if (text != null && text.contains(",")) {

View File

@ -1,5 +1,6 @@
package net.knarcraft.blacksmith.listener; package net.knarcraft.blacksmith.listener;
import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage; import net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage;
import net.knarcraft.blacksmith.trait.BlacksmithTrait; import net.knarcraft.blacksmith.trait.BlacksmithTrait;
import net.knarcraft.knarlib.formatting.StringFormatter; import net.knarcraft.knarlib.formatting.StringFormatter;
@ -25,7 +26,8 @@ public class NPCClickListener implements Listener {
//Permission check //Permission check
if (!player.hasPermission("blacksmith.use")) { if (!player.hasPermission("blacksmith.use")) {
StringFormatter.displayErrorMessage(player, BlacksmithTranslatableMessage.PERMISSION_DENIED); StringFormatter.displayErrorMessage(player, BlacksmithPlugin.getTranslator(),
BlacksmithTranslatableMessage.PERMISSION_DENIED);
return; return;
} }

View File

@ -1,5 +1,6 @@
package net.knarcraft.blacksmith.util; package net.knarcraft.blacksmith.util;
import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.blacksmith.config.SettingValueType; import net.knarcraft.blacksmith.config.SettingValueType;
import net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage; import net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage;
import net.knarcraft.knarlib.formatting.StringFormatter; import net.knarcraft.knarlib.formatting.StringFormatter;
@ -51,7 +52,8 @@ public final class TypeValidationHelper {
private static boolean isStringList(Object value, CommandSender sender) { private static boolean isStringList(Object value, CommandSender sender) {
boolean isStringList = value instanceof String[] || value instanceof List<?> || value instanceof String; boolean isStringList = value instanceof String[] || value instanceof List<?> || value instanceof String;
if (!isStringList && sender != null) { if (!isStringList && sender != null) {
StringFormatter.displayErrorMessage(sender, BlacksmithTranslatableMessage.INPUT_STRING_LIST_REQUIRED); StringFormatter.displayErrorMessage(sender, BlacksmithPlugin.getTranslator(),
BlacksmithTranslatableMessage.INPUT_STRING_LIST_REQUIRED);
} }
return isStringList; return isStringList;
} }
@ -69,7 +71,8 @@ public final class TypeValidationHelper {
return intValue >= 0 && intValue <= 100; return intValue >= 0 && intValue <= 100;
} catch (NumberFormatException | NullPointerException exception) { } catch (NumberFormatException | NullPointerException exception) {
if (sender != null) { if (sender != null) {
StringFormatter.displayErrorMessage(sender, BlacksmithTranslatableMessage.INPUT_PERCENTAGE_REQUIRED); StringFormatter.displayErrorMessage(sender, BlacksmithPlugin.getTranslator(),
BlacksmithTranslatableMessage.INPUT_PERCENTAGE_REQUIRED);
} }
return false; return false;
} }
@ -85,7 +88,8 @@ public final class TypeValidationHelper {
private static boolean isNonEmptyString(Object value, CommandSender sender) { private static boolean isNonEmptyString(Object value, CommandSender sender) {
boolean isString = value instanceof String string && !string.strip().isEmpty(); boolean isString = value instanceof String string && !string.strip().isEmpty();
if (!isString && sender != null) { if (!isString && sender != null) {
StringFormatter.displayErrorMessage(sender, BlacksmithTranslatableMessage.INPUT_STRING_REQUIRED); StringFormatter.displayErrorMessage(sender, BlacksmithPlugin.getTranslator(),
BlacksmithTranslatableMessage.INPUT_STRING_REQUIRED);
} }
return isString; return isString;
} }
@ -102,7 +106,8 @@ public final class TypeValidationHelper {
return ConfigHelper.asDouble(value) >= 0.0; return ConfigHelper.asDouble(value) >= 0.0;
} catch (NumberFormatException | NullPointerException exception) { } catch (NumberFormatException | NullPointerException exception) {
if (sender != null) { if (sender != null) {
StringFormatter.displayErrorMessage(sender, BlacksmithTranslatableMessage.INPUT_POSITIVE_DOUBLE_REQUIRED); StringFormatter.displayErrorMessage(sender, BlacksmithPlugin.getTranslator(),
BlacksmithTranslatableMessage.INPUT_POSITIVE_DOUBLE_REQUIRED);
} }
return false; return false;
} }
@ -120,7 +125,8 @@ public final class TypeValidationHelper {
return ConfigHelper.asInt(value) >= 0; return ConfigHelper.asInt(value) >= 0;
} catch (NumberFormatException | NullPointerException exception) { } catch (NumberFormatException | NullPointerException exception) {
if (sender != null) { if (sender != null) {
StringFormatter.displayErrorMessage(sender, BlacksmithTranslatableMessage.INPUT_POSITIVE_INTEGER_REQUIRED); StringFormatter.displayErrorMessage(sender, BlacksmithPlugin.getTranslator(),
BlacksmithTranslatableMessage.INPUT_POSITIVE_INTEGER_REQUIRED);
} }
return false; return false;
} }