Makes the translator non-static to fix #1

This commit is contained in:
Kristian Knarvik 2022-11-07 15:00:02 +01:00
parent f10e2caed2
commit 7299e86afc
7 changed files with 68 additions and 32 deletions

View File

@ -4,6 +4,8 @@ import org.bukkit.plugin.java.JavaPlugin;
/**
* KnarLib's main class
*
* <p>Make sure you initialize KnarLib by running setPlugin() during onEnable! Some methods will not work without it.</p>
*/
public final class KnarLib {

View File

@ -20,11 +20,12 @@ public final class StringFormatter {
/**
* Displays a message signifying a successful action
*
* @param sender <p>The command sender to display the message to</p>
* @param message <p>The translatable message to display</p>
* @param sender <p>The command sender to display the message to</p>
* @param translator <p>The translator to use for the translation</p>
* @param message <p>The translatable message to display</p>
*/
public static void displaySuccessMessage(CommandSender sender, TranslatableMessage message) {
sender.sendMessage(ChatColor.GREEN + getFormattedMessage(Translator.getTranslatedMessage(message)));
public static void displaySuccessMessage(CommandSender sender, Translator translator, TranslatableMessage message) {
sender.sendMessage(ChatColor.GREEN + getFormattedMessage(translator.getTranslatedMessage(message)));
}
/**
@ -40,18 +41,19 @@ public final class StringFormatter {
/**
* Displays a message signifying an unsuccessful action
*
* @param sender <p>The command sender to display the message to</p>
* @param message <p>The translatable message to display</p>
* @param sender <p>The command sender to display the message to</p>
* @param translator <p>The translator to use for the translation</p>
* @param message <p>The translatable message to display</p>
*/
public static void displayErrorMessage(CommandSender sender, TranslatableMessage message) {
sender.sendMessage(ChatColor.DARK_RED + getFormattedMessage(Translator.getTranslatedMessage(message)));
public static void displayErrorMessage(CommandSender sender, Translator translator, TranslatableMessage message) {
sender.sendMessage(ChatColor.DARK_RED + getFormattedMessage(translator.getTranslatedMessage(message)));
}
/**
* Displays a message signifying an unsuccessful action
*
* @param sender <p>The command sender to display the message to</p>
* @param message <p>The translatable message to display</p>
* @param message <p>The raw message to display</p>
*/
public static void displayErrorMessage(CommandSender sender, String message) {
sender.sendMessage(ChatColor.DARK_RED + getFormattedMessage(message));
@ -67,6 +69,34 @@ public final class StringFormatter {
return "[" + pluginName + "] " + ChatColor.RESET + ColorHelper.translateColorCodes(message, ColorConversion.NORMAL);
}
/**
* Replaces a placeholder in a string
*
* @param translator <p>The translator to use for the translation</p>
* @param translatableMessage <p>The translatable message to replace in</p>
* @param placeholder <p>The placeholder to replace</p>
* @param replacement <p>The replacement value</p>
* @return <p>The input string with the placeholder replaced</p>
*/
public static String replacePlaceholder(Translator translator, TranslatableMessage translatableMessage, String placeholder,
String replacement) {
return replacePlaceholder(translator.getTranslatedMessage(translatableMessage), placeholder, replacement);
}
/**
* Replaces placeholders in a string
*
* @param translator <p>The translator to use for the translation</p>
* @param translatableMessage <p>The translatable message to replace in</p>
* @param placeholders <p>The placeholders to replace</p>
* @param replacements <p>The replacement values</p>
* @return <p>The input string with placeholders replaced</p>
*/
public static String replacePlaceholders(Translator translator, TranslatableMessage translatableMessage, String[] placeholders,
String[] replacements) {
return replacePlaceholders(translator.getTranslatedMessage(translatableMessage), placeholders, replacements);
}
/**
* Replaces a placeholder in a string
*

View File

@ -23,11 +23,13 @@ public final class TimeFormatter {
/**
* Gets the string used for displaying this sign's duration
*
* @param translator <p>The translator to use for translation</p>
* @param duration <p>The duration, in seconds, to display</p>
* @return <p>The string used for displaying this sign's duration</p>
*/
public static String getDurationString(long duration) {
public static String getDurationString(Translator translator, long duration) {
if (duration == 0) {
return Translator.getTranslatedMessage(TranslatableTimeUnit.UNIT_NOW);
return translator.getTranslatedMessage(TranslatableTimeUnit.UNIT_NOW);
} else {
if (sortedUnits == null) {
initializeUnits();
@ -35,11 +37,11 @@ public final class TimeFormatter {
for (double unit : sortedUnits) {
if (duration / unit >= 1) {
double units = round(duration / unit);
return formatDurationString(units, timeUnits.get(unit)[units == 1 ? 0 : 1],
return formatDurationString(units, translator, timeUnits.get(unit)[units == 1 ? 0 : 1],
(units * 10) % 10 == 0);
}
}
return formatDurationString(duration, TranslatableTimeUnit.UNIT_SECONDS, false);
return formatDurationString(duration, translator, TranslatableTimeUnit.UNIT_SECONDS, false);
}
}
@ -57,14 +59,16 @@ public final class TimeFormatter {
* Formats a duration string
*
* @param duration <p>The duration to display</p>
* @param translator <p>The translator to use for translation</p>
* @param translatableMessage <p>The time unit to display</p>
* @param castToInt <p>Whether to cast the duration to an int</p>
* @return <p>The formatted duration string</p>
*/
private static String formatDurationString(double duration, TranslatableTimeUnit translatableMessage, boolean castToInt) {
String durationFormat = Translator.getTranslatedMessage(TranslatableTimeUnit.DURATION_FORMAT);
private static String formatDurationString(double duration, Translator translator,
TranslatableTimeUnit translatableMessage, boolean castToInt) {
String durationFormat = translator.getTranslatedMessage(TranslatableTimeUnit.DURATION_FORMAT);
durationFormat = replacePlaceholder(durationFormat, "{unit}",
Translator.getTranslatedMessage(translatableMessage));
translator.getTranslatedMessage(translatableMessage));
return replacePlaceholder(durationFormat, "{time}", castToInt ? String.valueOf((int) duration) :
String.valueOf(duration));
}

View File

@ -28,13 +28,9 @@ import java.util.logging.Level;
*/
public final class Translator {
private static final List<TranslatableMessage> messageCategories = new ArrayList<>();
private static Map<TranslatableMessage, String> translatedMessages;
private static Map<TranslatableMessage, String> backupTranslatedMessages;
private Translator() {
}
private final List<TranslatableMessage> messageCategories = new ArrayList<>();
private Map<TranslatableMessage, String> translatedMessages;
private Map<TranslatableMessage, String> backupTranslatedMessages;
/**
* Registers all translatable messages in the given message category
@ -44,14 +40,16 @@ public final class Translator {
*
* @param translatableMessage <p>A translatable message in the category to register</p>
*/
public static void registerMessageCategory(TranslatableMessage translatableMessage) {
public void registerMessageCategory(TranslatableMessage translatableMessage) {
messageCategories.add(translatableMessage);
}
/**
* Loads the languages used by this translator
*
* @param selectedLanguage <p>The currently selected language</p>
*/
public static void loadLanguages(String selectedLanguage) {
public void loadLanguages(String selectedLanguage) {
backupTranslatedMessages = loadTranslatedMessages("en");
translatedMessages = loadCustomTranslatedMessages(selectedLanguage);
if (translatedMessages == null) {
@ -65,7 +63,7 @@ public final class Translator {
* @param translatableMessage <p>The message to translate</p>
* @return <p>The translated message</p>
*/
public static String getTranslatedMessage(TranslatableMessage translatableMessage) {
public String getTranslatedMessage(TranslatableMessage translatableMessage) {
if (translatedMessages == null) {
return "Translated strings not loaded";
}
@ -86,7 +84,7 @@ public final class Translator {
* @param language <p>The language chosen by the user</p>
* @return <p>A mapping of all strings for the given language</p>
*/
public static Map<TranslatableMessage, String> loadTranslatedMessages(String language) {
public Map<TranslatableMessage, String> loadTranslatedMessages(String language) {
try {
BufferedReader reader = FileHelper.getBufferedReaderForInternalFile("/strings.yml");
return loadTranslatableMessages(language, reader);
@ -102,7 +100,7 @@ public final class Translator {
* @param language <p>The selected language</p>
* @return <p>The loaded translated strings, or null if no custom language file exists</p>
*/
public static Map<TranslatableMessage, String> loadCustomTranslatedMessages(String language) {
public Map<TranslatableMessage, String> loadCustomTranslatedMessages(String language) {
JavaPlugin instance = KnarLib.getPlugin();
File strings = new File(instance.getDataFolder(), "strings.yml");
@ -127,7 +125,7 @@ public final class Translator {
* @param reader <p>The buffered reader to read from</p>
* @return <p>The loaded translated strings</p>
*/
private static Map<TranslatableMessage, String> loadTranslatableMessages(String language, BufferedReader reader) {
private Map<TranslatableMessage, String> loadTranslatableMessages(String language, BufferedReader reader) {
Map<TranslatableMessage, String> translatedMessages = new HashMap<>();
YamlConfiguration configuration = YamlConfiguration.loadConfiguration(reader);

View File

@ -21,7 +21,7 @@ public enum ColorConversion {
RGB,
/**
* Only hexadecimal color codes are converted into colors, not &[a-f0-9] color codes.
* Only hexadecimal color codes are converted into colors, not &amp;[a-f0-9] color codes.
*/
RGB_ONLY,

View File

@ -41,7 +41,7 @@ public final class ColorHelper {
/**
* Sets whether to require ampersand in hex color
*
* <p>If set to true, &#f35336 will be treated as a color code, but #f35336 won't. By default, this is set to false,
* <p>If set to true, &amp;#f35336 will be treated as a color code, but #f35336 won't. By default, this is set to false,
* meaning that both are treated as color codes.</p>
*
* @param requireAmpersandInHexColors <p>True if hex colors should require an ampersand</p>

View File

@ -25,8 +25,10 @@ public final class FileHelper {
}
/**
* Gets a buffered reader for
* Gets a buffered reader for an internal file
*
* @param file <p>The name of the file to get a buffered reader for (start with a '/'). The file should reside in
* the resources directory.</p>
* @return <p>A buffered read for reading the file</p>
* @throws FileNotFoundException <p>If unable to get an input stream for the given file</p>
*/