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 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 <p>The translator to use</p>
*/
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()) {

View File

@ -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;
}

View File

@ -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, '&')));
}
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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 <p>The message to display</p>
*/
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 <p>The string to display to a user</p>
*/
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 <p>The string to display to a user</p>
*/
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 <p>The string to display to a user</p>
*/
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 <p>The string to display to a user</p>
*/
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});
}

View File

@ -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);
};
}

View File

@ -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 <p>Text describing the time interval</p>
*/
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(",")) {

View File

@ -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;
}

View File

@ -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;
}