From 5e8b13785de5c896481008d9ca802ea1b470d2e6 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Wed, 25 Jan 2023 16:38:56 +0100 Subject: [PATCH] Makes translator color conversion configurable --- .../knarlib/formatting/Translator.java | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/knarcraft/knarlib/formatting/Translator.java b/src/main/java/net/knarcraft/knarlib/formatting/Translator.java index 1692c55..858910d 100644 --- a/src/main/java/net/knarcraft/knarlib/formatting/Translator.java +++ b/src/main/java/net/knarcraft/knarlib/formatting/Translator.java @@ -31,6 +31,7 @@ public final class Translator { private final List messageCategories = new ArrayList<>(); private Map translatedMessages; private Map backupTranslatedMessages; + private ColorConversion colorConversion = ColorConversion.RGB; /** * Registers all translatable messages in the given message category @@ -44,6 +45,23 @@ public final class Translator { messageCategories.add(translatableMessage); } + /** + * Sets the color conversion to use for translated messages + * + *

See {@link net.knarcraft.knarlib.property.ColorConversion} and + * {@link net.knarcraft.knarlib.util.ColorHelper#translateColorCodes(String, ColorConversion)} for details, but + * basically, this allows setting which color codes should be converted, and which should not be.

+ * + * @param colorConversion

The color conversion to be used

+ */ + public void setColorConversion(ColorConversion colorConversion) { + if (colorConversion != null) { + this.colorConversion = colorConversion; + } else { + throw new IllegalArgumentException("Color conversion cannot be null"); + } + } + /** * Loads the languages used by this translator * @@ -77,7 +95,7 @@ public final class Translator { } else { translatedMessage = translatableMessage.toString(); } - return ColorHelper.translateColorCodes(translatedMessage, ColorConversion.RGB); + return ColorHelper.translateColorCodes(translatedMessage, colorConversion); } /** @@ -115,7 +133,8 @@ public final class Translator { try { Bukkit.getLogger().log(Level.INFO, "Loading custom strings..."); - return loadTranslatableMessages(language, new BufferedReader(new InputStreamReader(new FileInputStream(strings)))); + return loadTranslatableMessages(language, new BufferedReader(new InputStreamReader( + new FileInputStream(strings)))); } catch (FileNotFoundException e) { Bukkit.getLogger().log(Level.WARNING, "Unable to load custom messages"); return null;