Minor fixes and improvements

Uses translateColors from ColorHelper in StringFormatter
Uses a primitive double where the Double class was used
Adds improved description to the translator class
Adds a stripColorCodes class to the ColorHelper
Uses color conversion from ColorHelper in readKeyValuePairs
This commit is contained in:
Kristian Knarvik 2022-11-07 10:30:07 +01:00
parent 8b4f841ead
commit f10e2caed2
5 changed files with 27 additions and 16 deletions

View File

@ -1,6 +1,8 @@
package net.knarcraft.knarlib.formatting; package net.knarcraft.knarlib.formatting;
import net.knarcraft.knarlib.KnarLib; import net.knarcraft.knarlib.KnarLib;
import net.knarcraft.knarlib.property.ColorConversion;
import net.knarcraft.knarlib.util.ColorHelper;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -62,17 +64,7 @@ public final class StringFormatter {
* @return <p>The formatted message</p> * @return <p>The formatted message</p>
*/ */
private static String getFormattedMessage(String message) { private static String getFormattedMessage(String message) {
return "[" + pluginName + "] " + ChatColor.RESET + translateColors(message); return "[" + pluginName + "] " + ChatColor.RESET + ColorHelper.translateColorCodes(message, ColorConversion.NORMAL);
}
/**
* Translates & color codes to proper colors
*
* @param input <p>The input string to translate colors for</p>
* @return <p>The input with color codes translated</p>
*/
private static String translateColors(String input) {
return ChatColor.translateAlternateColorCodes('&', input);
} }
/** /**

View File

@ -32,7 +32,7 @@ public final class TimeFormatter {
if (sortedUnits == null) { if (sortedUnits == null) {
initializeUnits(); initializeUnits();
} }
for (Double unit : sortedUnits) { for (double unit : sortedUnits) {
if (duration / unit >= 1) { if (duration / unit >= 1) {
double units = round(duration / unit); double units = round(duration / unit);
return formatDurationString(units, timeUnits.get(unit)[units == 1 ? 0 : 1], return formatDurationString(units, timeUnits.get(unit)[units == 1 ? 0 : 1],

View File

@ -20,6 +20,11 @@ import java.util.logging.Level;
/** /**
* A tool to get strings translated to the correct language * A tool to get strings translated to the correct language
*
* <p>To add translatable messages, create a class extending TranslatableMessage, and register it by calling
* "registerMessageCategory" on one of its values.
* Add a file strings.yml in your resources directory with data: `en.ENUM: "Text"`. You must have a `en` language as
* it's used as the fallback, but you can additionally add any language code you want.</p>
*/ */
public final class Translator { public final class Translator {

View File

@ -50,6 +50,17 @@ public final class ColorHelper {
ColorHelper.requireAmpersandInHexColors = requireAmpersandInHexColors; ColorHelper.requireAmpersandInHexColors = requireAmpersandInHexColors;
} }
/**
* Strips all color codes from the given message
*
* @param message <p>The message to strip color codes from</p>
* @param colorConversion <p>The type of color conversion to apply before stripping</p>
* @return <p>The message without color codes</p>
*/
public static String stripColorCodes(String message, ColorConversion colorConversion) {
return ChatColor.stripColor(translateColorCodes(message, colorConversion));
}
/** /**
* Translates color codes according to the given color conversion setting * Translates color codes according to the given color conversion setting
* *

View File

@ -1,6 +1,6 @@
package net.knarcraft.knarlib.util; package net.knarcraft.knarlib.util;
import net.md_5.bungee.api.ChatColor; import net.knarcraft.knarlib.property.ColorConversion;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.BufferedWriter; import java.io.BufferedWriter;
@ -91,10 +91,13 @@ public final class FileHelper {
* Reads key/value pairs from an input stream * Reads key/value pairs from an input stream
* *
* @param bufferedReader <p>The buffered reader to read</p> * @param bufferedReader <p>The buffered reader to read</p>
* @param separator <p>The separator separating a key from a value</p>
* @param colorConversion <p>The color conversion to use for any color codes encountered</p>
* @return <p>A map containing the read pairs</p> * @return <p>A map containing the read pairs</p>
* @throws IOException <p>If unable to read from the stream</p> * @throws IOException <p>If unable to read from the stream</p>
*/ */
public static Map<String, String> readKeyValuePairs(BufferedReader bufferedReader, String separator, boolean translateColorCodes) throws IOException { public static Map<String, String> readKeyValuePairs(BufferedReader bufferedReader, String separator,
ColorConversion colorConversion) throws IOException {
Map<String, String> readPairs = new HashMap<>(); Map<String, String> readPairs = new HashMap<>();
String line = bufferedReader.readLine(); String line = bufferedReader.readLine();
@ -114,7 +117,7 @@ public final class FileHelper {
//Read the line //Read the line
String key = line.substring(0, separatorIndex); String key = line.substring(0, separatorIndex);
String value = ChatColor.translateAlternateColorCodes('&', line.substring(separatorIndex + 1)); String value = ColorHelper.translateColorCodes(line.substring(separatorIndex + 1), colorConversion);
readPairs.put(key, value); readPairs.put(key, value);
line = bufferedReader.readLine(); line = bufferedReader.readLine();