Uses KnarLib for common tasks
This commit is contained in:
@ -0,0 +1,30 @@
|
||||
package net.knarcraft.blacksmith.formatting;
|
||||
|
||||
import net.citizensnpcs.api.npc.NPC;
|
||||
import net.knarcraft.knarlib.property.ColorConversion;
|
||||
import net.knarcraft.knarlib.util.ColorHelper;
|
||||
import net.md_5.bungee.api.ChatColor;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
/**
|
||||
* A formatter for formatting displayed messages
|
||||
*/
|
||||
public final class BlacksmithStringFormatter {
|
||||
|
||||
private BlacksmithStringFormatter() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a message from a blacksmith NPC to a player
|
||||
*
|
||||
* @param npc <p>The NPC sending the message</p>
|
||||
* @param player <p>The player to send the message to</p>
|
||||
* @param message <p>The message to send</p>
|
||||
*/
|
||||
public static void sendNPCMessage(NPC npc, Player player, String message) {
|
||||
player.sendMessage(ChatColor.GREEN + "[" + npc.getName() + "] -> You:" + ChatColor.RESET + " " +
|
||||
ColorHelper.translateColorCodes(message, ColorConversion.NORMAL));
|
||||
}
|
||||
|
||||
}
|
@ -1,6 +1,8 @@
|
||||
package net.knarcraft.blacksmith.formatting;
|
||||
|
||||
import static net.knarcraft.blacksmith.formatting.StringFormatter.replacePlaceholders;
|
||||
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
|
||||
@ -8,7 +10,7 @@ import static net.knarcraft.blacksmith.formatting.StringFormatter.replacePlaceho
|
||||
* <p>This does not include NPC messages as they are configurable per-npc, and can be translated by changing the
|
||||
* default message values in the main config file.</p>
|
||||
*/
|
||||
public enum TranslatableMessage {
|
||||
public enum BlacksmithTranslatableMessage implements TranslatableMessage {
|
||||
|
||||
/**
|
||||
* The message displayed when a configuration value has been successfully changed
|
||||
@ -105,36 +107,6 @@ public enum TranslatableMessage {
|
||||
*/
|
||||
PRESET_MATERIALS,
|
||||
|
||||
/**
|
||||
* The format for displaying the exact duration of a blacksmith's cool-down or delay
|
||||
*/
|
||||
DURATION_FORMAT,
|
||||
|
||||
/**
|
||||
* The text to display for 0 seconds
|
||||
*/
|
||||
UNIT_NOW,
|
||||
|
||||
/**
|
||||
* The text to display for 1 second
|
||||
*/
|
||||
UNIT_SECOND,
|
||||
|
||||
/**
|
||||
* The text to display for a number of seconds
|
||||
*/
|
||||
UNIT_SECONDS,
|
||||
|
||||
/**
|
||||
* The text to display for 1 minute
|
||||
*/
|
||||
UNIT_MINUTE,
|
||||
|
||||
/**
|
||||
* The text to display for a number of minutes
|
||||
*/
|
||||
UNIT_MINUTES,
|
||||
|
||||
/**
|
||||
* The text to display when describing less than 10 seconds remaining
|
||||
*/
|
||||
@ -172,7 +144,7 @@ public enum TranslatableMessage {
|
||||
* @return <p>The message to display</p>
|
||||
*/
|
||||
public static String getRawValueMessage(String rawValue) {
|
||||
return StringFormatter.replacePlaceholder(Translator.getTranslatedMessage(TranslatableMessage.RAW_VALUE),
|
||||
return StringFormatter.replacePlaceholder(Translator.getTranslatedMessage(BlacksmithTranslatableMessage.RAW_VALUE),
|
||||
"{rawValue}", rawValue);
|
||||
}
|
||||
|
||||
@ -184,7 +156,7 @@ public enum TranslatableMessage {
|
||||
* @return <p>The string to display to a user</p>
|
||||
*/
|
||||
public static String getValueChangedMessage(String setting, String newValue) {
|
||||
return replacePlaceholders(Translator.getTranslatedMessage(TranslatableMessage.VALUE_CHANGED),
|
||||
return StringFormatter.replacePlaceholders(Translator.getTranslatedMessage(BlacksmithTranslatableMessage.VALUE_CHANGED),
|
||||
new String[]{"{setting}", "{newValue}"}, new String[]{setting, newValue});
|
||||
}
|
||||
|
||||
@ -198,7 +170,7 @@ public enum TranslatableMessage {
|
||||
* @return <p>The string to display to a user</p>
|
||||
*/
|
||||
public static String getItemValueChangedMessage(String setting, ItemType itemType, String item, String newValue) {
|
||||
return replacePlaceholders(Translator.getTranslatedMessage(TranslatableMessage.VALUE_FOR_ITEM_CHANGED),
|
||||
return StringFormatter.replacePlaceholders(Translator.getTranslatedMessage(BlacksmithTranslatableMessage.VALUE_FOR_ITEM_CHANGED),
|
||||
new String[]{"{setting}", "{itemType}", "{item}", "{newValue}"},
|
||||
new String[]{setting, itemType.getItemTypeName(), item, newValue});
|
||||
}
|
||||
@ -211,7 +183,7 @@ public enum TranslatableMessage {
|
||||
* @return <p>The string to display to a user</p>
|
||||
*/
|
||||
public static String getCurrentValueMessage(String setting, String currentValue) {
|
||||
return replacePlaceholders(Translator.getTranslatedMessage(TranslatableMessage.CURRENT_VALUE),
|
||||
return StringFormatter.replacePlaceholders(Translator.getTranslatedMessage(BlacksmithTranslatableMessage.CURRENT_VALUE),
|
||||
new String[]{"{setting}", "{currentValue}"}, new String[]{setting, currentValue});
|
||||
}
|
||||
|
||||
@ -225,9 +197,13 @@ public enum TranslatableMessage {
|
||||
* @return <p>The string to display to a user</p>
|
||||
*/
|
||||
public static String getItemCurrentValueMessage(String setting, ItemType itemType, String item, String currentValue) {
|
||||
return replacePlaceholders(Translator.getTranslatedMessage(TranslatableMessage.CURRENT_VALUE_FOR_ITEM),
|
||||
return StringFormatter.replacePlaceholders(Translator.getTranslatedMessage(BlacksmithTranslatableMessage.CURRENT_VALUE_FOR_ITEM),
|
||||
new String[]{"{setting}", "{itemType}", "{item}", "{currentValue}"},
|
||||
new String[]{setting, itemType.getItemTypeName(), item, currentValue});
|
||||
}
|
||||
|
||||
@Override
|
||||
public TranslatableMessage[] getAllMessages() {
|
||||
return BlacksmithTranslatableMessage.values();
|
||||
}
|
||||
}
|
@ -1,5 +1,7 @@
|
||||
package net.knarcraft.blacksmith.formatting;
|
||||
|
||||
import net.knarcraft.knarlib.formatting.Translator;
|
||||
|
||||
/**
|
||||
* An enum representing all item types used in messages
|
||||
*/
|
||||
@ -15,8 +17,8 @@ public enum ItemType {
|
||||
*/
|
||||
public String getItemTypeName() {
|
||||
return switch (this) {
|
||||
case MATERIAL -> Translator.getTranslatedMessage(TranslatableMessage.ITEM_TYPE_MATERIAL);
|
||||
case ENCHANTMENT -> Translator.getTranslatedMessage(TranslatableMessage.ITEM_TYPE_ENCHANTMENT);
|
||||
case MATERIAL -> Translator.getTranslatedMessage(BlacksmithTranslatableMessage.ITEM_TYPE_MATERIAL);
|
||||
case ENCHANTMENT -> Translator.getTranslatedMessage(BlacksmithTranslatableMessage.ITEM_TYPE_ENCHANTMENT);
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -1,128 +0,0 @@
|
||||
package net.knarcraft.blacksmith.formatting;
|
||||
|
||||
import net.citizensnpcs.api.npc.NPC;
|
||||
import net.knarcraft.blacksmith.BlacksmithPlugin;
|
||||
import net.md_5.bungee.api.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* A formatter for formatting displayed messages
|
||||
*/
|
||||
public final class StringFormatter {
|
||||
|
||||
private final static String pluginName = BlacksmithPlugin.getInstance().getDescription().getName();
|
||||
|
||||
private StringFormatter() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a message from a blacksmith NPC to a player
|
||||
*
|
||||
* @param npc <p>The NPC sending the message</p>
|
||||
* @param player <p>The player to send the message to</p>
|
||||
* @param message <p>The message to send</p>
|
||||
*/
|
||||
public static void sendNPCMessage(NPC npc, Player player, String message) {
|
||||
player.sendMessage(ChatColor.GREEN + "[" + npc.getName() + "] -> You:" + ChatColor.RESET + " " +
|
||||
translateColors(message));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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>
|
||||
*/
|
||||
public static void displaySuccessMessage(CommandSender sender, TranslatableMessage message) {
|
||||
sender.sendMessage(ChatColor.GREEN + getFormattedMessage(Translator.getTranslatedMessage(message)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays a message signifying a successful action
|
||||
*
|
||||
* @param sender <p>The command sender to display the message to</p>
|
||||
* @param message <p>The raw message to display</p>
|
||||
*/
|
||||
public static void displaySuccessMessage(CommandSender sender, String message) {
|
||||
sender.sendMessage(ChatColor.GREEN + getFormattedMessage(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>
|
||||
*/
|
||||
public static void displayErrorMessage(CommandSender sender, TranslatableMessage message) {
|
||||
sender.sendMessage(ChatColor.DARK_RED + getFormattedMessage(Translator.getTranslatedMessage(message)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the formatted version of any chat message
|
||||
*
|
||||
* @param message <p>The message to format</p>
|
||||
* @return <p>The formatted message</p>
|
||||
*/
|
||||
private static String getFormattedMessage(String message) {
|
||||
return "[" + pluginName + "] " + ChatColor.RESET + translateColors(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces a placeholder in a string
|
||||
*
|
||||
* @param input <p>The input string 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(String input, String placeholder, String replacement) {
|
||||
return input.replace(placeholder, replacement);
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces placeholders in a string
|
||||
*
|
||||
* @param input <p>The input string 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(String input, String[] placeholders, String[] replacements) {
|
||||
for (int i = 0; i < Math.min(placeholders.length, replacements.length); i++) {
|
||||
input = replacePlaceholder(input, placeholders[i], replacements[i]);
|
||||
}
|
||||
return input;
|
||||
}
|
||||
|
||||
/**
|
||||
* Translates all found color codes to formatting in a string
|
||||
*
|
||||
* @param message <p>The string to search for color codes</p>
|
||||
* @return <p>The message with color codes translated</p>
|
||||
*/
|
||||
public static String translateAllColorCodes(String message) {
|
||||
message = ChatColor.translateAlternateColorCodes('&', message);
|
||||
Pattern pattern = Pattern.compile("(#[a-fA-F0-9]{6})");
|
||||
Matcher matcher = pattern.matcher(message);
|
||||
while (matcher.find()) {
|
||||
message = message.replace(matcher.group(), "" + ChatColor.of(matcher.group()));
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
}
|
@ -1,23 +1,16 @@
|
||||
package net.knarcraft.blacksmith.formatting;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import net.knarcraft.knarlib.formatting.Translator;
|
||||
|
||||
import static net.knarcraft.blacksmith.formatting.StringFormatter.replacePlaceholder;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* A utility for formatting a string specifying an amount of time
|
||||
*/
|
||||
public final class TimeFormatter {
|
||||
|
||||
private static Map<Double, TranslatableMessage[]> timeUnits;
|
||||
private static List<Double> sortedUnits;
|
||||
|
||||
private TimeFormatter() {
|
||||
|
||||
}
|
||||
@ -31,7 +24,7 @@ public final class TimeFormatter {
|
||||
*/
|
||||
public static String formatTime(boolean exact, int seconds) {
|
||||
if (exact) {
|
||||
return getDurationString(seconds);
|
||||
return net.knarcraft.knarlib.formatting.TimeFormatter.getDurationString(seconds);
|
||||
} else {
|
||||
return formatUnclearTime(seconds);
|
||||
}
|
||||
@ -60,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(TranslatableMessage.valueOf(interval.name()));
|
||||
String text = Translator.getTranslatedMessage(BlacksmithTranslatableMessage.valueOf(interval.name()));
|
||||
|
||||
//Choose a random entry if a comma-separated list is provided
|
||||
if (text != null && text.contains(",")) {
|
||||
@ -79,68 +72,4 @@ public final class TimeFormatter {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the string used for displaying this sign's duration
|
||||
*
|
||||
* @return <p>The string used for displaying this sign's duration</p>
|
||||
*/
|
||||
public static String getDurationString(int duration) {
|
||||
if (duration == 0) {
|
||||
return Translator.getTranslatedMessage(TranslatableMessage.UNIT_NOW);
|
||||
} else {
|
||||
if (sortedUnits == null) {
|
||||
initializeUnits();
|
||||
}
|
||||
for (Double unit : sortedUnits) {
|
||||
if (duration / unit >= 1) {
|
||||
double units = round(duration / unit);
|
||||
return formatDurationString(units, timeUnits.get(unit)[units == 1 ? 0 : 1],
|
||||
(units * 10) % 10 == 0);
|
||||
}
|
||||
}
|
||||
return formatDurationString(duration, TranslatableMessage.UNIT_SECONDS, false);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Rounds a number to its last two digits
|
||||
*
|
||||
* @param number <p>The number to round</p>
|
||||
* @return <p>The rounded number</p>
|
||||
*/
|
||||
private static double round(double number) {
|
||||
return Math.round(number * 100.0) / 100.0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats a duration string
|
||||
*
|
||||
* @param duration <p>The duration to display</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, TranslatableMessage translatableMessage, boolean castToInt) {
|
||||
String durationFormat = Translator.getTranslatedMessage(TranslatableMessage.DURATION_FORMAT);
|
||||
durationFormat = replacePlaceholder(durationFormat, "{unit}",
|
||||
Translator.getTranslatedMessage(translatableMessage));
|
||||
return replacePlaceholder(durationFormat, "{time}", castToInt ? String.valueOf((int) duration) :
|
||||
String.valueOf(duration));
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the mapping of available time units for formatting permission sign duration
|
||||
*/
|
||||
private static void initializeUnits() {
|
||||
double minute = 60;
|
||||
|
||||
timeUnits = new HashMap<>();
|
||||
timeUnits.put(minute, new TranslatableMessage[]{TranslatableMessage.UNIT_MINUTE, TranslatableMessage.UNIT_MINUTES});
|
||||
timeUnits.put(1D, new TranslatableMessage[]{TranslatableMessage.UNIT_SECOND, TranslatableMessage.UNIT_SECONDS});
|
||||
|
||||
sortedUnits = new ArrayList<>(timeUnits.keySet());
|
||||
Collections.sort(sortedUnits);
|
||||
Collections.reverse(sortedUnits);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,120 +0,0 @@
|
||||
package net.knarcraft.blacksmith.formatting;
|
||||
|
||||
import net.knarcraft.blacksmith.BlacksmithPlugin;
|
||||
import net.knarcraft.blacksmith.util.FileHelper;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Level;
|
||||
|
||||
/**
|
||||
* A tool to get strings translated to the correct language
|
||||
*/
|
||||
public final class Translator {
|
||||
|
||||
private static Map<TranslatableMessage, String> translatedMessages;
|
||||
private static Map<TranslatableMessage, String> backupTranslatedMessages;
|
||||
|
||||
private Translator() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the languages used by this translator
|
||||
*/
|
||||
public static void loadLanguages(String selectedLanguage) {
|
||||
backupTranslatedMessages = loadTranslatedMessages("en");
|
||||
translatedMessages = loadCustomTranslatedMessages(selectedLanguage);
|
||||
if (translatedMessages == null) {
|
||||
translatedMessages = loadTranslatedMessages(selectedLanguage);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a translated version of the given translatable message
|
||||
*
|
||||
* @param translatableMessage <p>The message to translate</p>
|
||||
* @return <p>The translated message</p>
|
||||
*/
|
||||
public static String getTranslatedMessage(TranslatableMessage translatableMessage) {
|
||||
if (translatedMessages == null) {
|
||||
return "Translated strings not loaded";
|
||||
}
|
||||
String translatedMessage;
|
||||
if (translatedMessages.containsKey(translatableMessage)) {
|
||||
translatedMessage = translatedMessages.get(translatableMessage);
|
||||
} else if (backupTranslatedMessages.containsKey(translatableMessage)) {
|
||||
translatedMessage = backupTranslatedMessages.get(translatableMessage);
|
||||
} else {
|
||||
translatedMessage = translatableMessage.toString();
|
||||
}
|
||||
return StringFormatter.translateAllColorCodes(translatedMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads all translated messages for the given language
|
||||
*
|
||||
* @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) {
|
||||
try {
|
||||
BufferedReader reader = FileHelper.getBufferedReaderForInternalFile("/strings.yml");
|
||||
return loadTranslatableMessages(language, reader);
|
||||
} catch (FileNotFoundException e) {
|
||||
BlacksmithPlugin.getInstance().getLogger().log(Level.SEVERE, "Unable to load translated messages");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to load translated messages from a custom strings.yml file
|
||||
*
|
||||
* @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) {
|
||||
BlacksmithPlugin instance = BlacksmithPlugin.getInstance();
|
||||
|
||||
File strings = new File(instance.getDataFolder(), "strings.yml");
|
||||
if (!strings.exists()) {
|
||||
instance.getLogger().log(Level.FINEST, "Strings file not found");
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
instance.getLogger().log(Level.INFO, "Loading custom strings...");
|
||||
return loadTranslatableMessages(language, new BufferedReader(new InputStreamReader(new FileInputStream(strings))));
|
||||
} catch (FileNotFoundException e) {
|
||||
instance.getLogger().log(Level.WARNING, "Unable to load custom messages");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads translatable messages from the given reader
|
||||
*
|
||||
* @param language <p>The selected language</p>
|
||||
* @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) {
|
||||
Map<TranslatableMessage, String> translatedMessages = new HashMap<>();
|
||||
YamlConfiguration configuration = YamlConfiguration.loadConfiguration(reader);
|
||||
|
||||
for (TranslatableMessage message : TranslatableMessage.values()) {
|
||||
String translated = configuration.getString(language + "." + message.toString());
|
||||
if (translated != null) {
|
||||
translatedMessages.put(message, translated);
|
||||
}
|
||||
}
|
||||
return translatedMessages;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user