Decouples formatting from translations

This commit is contained in:
2022-06-03 16:00:03 +02:00
parent c4cdab8b7f
commit a19dd16bad
10 changed files with 328 additions and 68 deletions

View File

@@ -4,6 +4,7 @@ import net.TheDgtl.Stargate.api.StargateAPI;
import net.TheDgtl.Stargate.config.ConfigurationOption;
import net.knarcraft.stargatecommand.command.CommandStarGateCommand;
import net.knarcraft.stargatecommand.command.StargateCommandTabCompleter;
import net.knarcraft.stargatecommand.formatting.StringFormatter;
import net.knarcraft.stargatecommand.formatting.Translator;
import net.knarcraft.stargatecommand.listener.StargateListener;
import net.knarcraft.stargatecommand.manager.IconManager;
@@ -82,6 +83,7 @@ public class StargateCommand extends JavaPlugin {
*/
private void loadConfiguration(FileConfiguration fileConfiguration) {
Translator.loadLanguages(fileConfiguration.getString("language"));
StringFormatter.loadStringFormats();
//Load all icons from config
for (Icon icon : Icon.values()) {
String iconString = fileConfiguration.getString(icon.getConfigNode());

View File

@@ -3,9 +3,9 @@ package net.knarcraft.stargatecommand.command;
import net.TheDgtl.Stargate.config.ConfigurationAPI;
import net.TheDgtl.Stargate.config.ConfigurationOption;
import net.TheDgtl.Stargate.config.OptionDataType;
import net.knarcraft.stargatecommand.formatting.StringFormat;
import net.knarcraft.stargatecommand.formatting.StringFormatter;
import net.knarcraft.stargatecommand.formatting.TranslatableMessage;
import net.knarcraft.stargatecommand.formatting.Translator;
import net.knarcraft.stargatecommand.property.StargateCommandCommand;
import net.md_5.bungee.api.ChatColor;
import org.apache.commons.lang.StringUtils;
@@ -231,9 +231,9 @@ public class CommandConfig implements CommandExecutor {
private void printConfigOptionValue(CommandSender sender, ConfigurationOption option) {
Object value = configurationAPI.getConfigurationOptionValue(option);
String description = getOptionDescription(option);
String currentValue = StringFormatter.replacePlaceholder(Translator.getTranslatedMessage(
TranslatableMessage.CONFIG_OPTION_CURRENT_VALUE), "{value}", String.valueOf(value));
sender.sendMessage(StringFormatter.formatInfoMessage(description + "\n" + currentValue));
String currentValue = StringFormatter.replacePlaceholder(StringFormatter.getStringFormat(
StringFormat.CONFIG_OPTION_CURRENT_VALUE_FORMAT), "{value}", String.valueOf(value));
sender.sendMessage(StringFormatter.formatInfoMessage(description + currentValue));
}
/**
@@ -243,11 +243,11 @@ public class CommandConfig implements CommandExecutor {
*/
private void displayConfigValues(CommandSender sender) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(StringFormatter.getTranslatedInfoMessage(TranslatableMessage.CONFIG_VALUES_HEADER));
stringBuilder.append(StringFormatter.getStringFormat(StringFormat.CONFIG_VALUES_HEADER_FORMAT));
for (ConfigurationOption option : ConfigurationOption.values()) {
if (!bannedConfigOptions.contains(option)) {
stringBuilder.append("\n").append(getOptionDescription(option));
stringBuilder.append(getOptionDescription(option));
}
}
sender.sendMessage(stringBuilder.toString());
@@ -265,9 +265,9 @@ public class CommandConfig implements CommandExecutor {
if (option.getDataType() == OptionDataType.STRING_LIST) {
stringValue = "[" + StringUtils.join((String[]) defaultValue, ",") + "]";
}
return StringFormatter.replacePlaceholders(Translator.getTranslatedMessage(
TranslatableMessage.CONFIG_OPTION_DESCRIPTION), new String[]{"{name}", "{description}",
"{value}"}, new String[]{option.name(), option.getDescription(), stringValue});
return StringFormatter.replacePlaceholders(StringFormatter.getStringFormat(
StringFormat.CONFIG_OPTION_DESCRIPTION_FORMAT), new String[]{"{name}", "{description}", "{value}"},
new String[]{option.name(), option.getDescription(), stringValue});
}
}

View File

@@ -2,12 +2,11 @@ package net.knarcraft.stargatecommand.command;
import net.TheDgtl.Stargate.network.Network;
import net.TheDgtl.Stargate.network.RegistryAPI;
import net.TheDgtl.Stargate.network.portal.FixedPortal;
import net.TheDgtl.Stargate.network.portal.Portal;
import net.TheDgtl.Stargate.network.portal.PortalFlag;
import net.knarcraft.stargatecommand.formatting.StringFormat;
import net.knarcraft.stargatecommand.formatting.StringFormatter;
import net.knarcraft.stargatecommand.formatting.TranslatableMessage;
import net.knarcraft.stargatecommand.formatting.Translator;
import net.knarcraft.stargatecommand.manager.IconManager;
import net.knarcraft.stargatecommand.property.Icon;
import net.knarcraft.stargatecommand.property.StargateCommandCommand;
@@ -55,11 +54,11 @@ public class CommandVisualizer implements CommandExecutor {
}
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(Translator.getTranslatedMessage(TranslatableMessage.COMMAND_VISUALIZER_FORMAT));
stringBuilder.append(StringFormatter.getStringFormat(StringFormat.COMMAND_VISUALIZER_FORMAT));
//Print info about all portals in the network
for (Portal portal : network.getAllPortals()) {
stringBuilder.append("\n");
StringBuilder iconBuilder = new StringBuilder();
if (portal.hasFlag(PortalFlag.HIDDEN)) {
iconBuilder.append(Icon.HIDDEN.getPlaceholder());
@@ -77,18 +76,15 @@ public class CommandVisualizer implements CommandExecutor {
iconBuilder.append(Icon.NOT_RANDOM.getPlaceholder());
}
String fixedString = "";
//TODO: Look for the fixed flag instead of FixedPortal once it's fixed
if (portal instanceof FixedPortal) {
fixedString = StringFormatter.replacePlaceholder(Translator.getTranslatedMessage(
TranslatableMessage.COMMAND_VISUALIZER_FIXED_FORMAT), "{portal}",
if (portal.hasFlag(PortalFlag.FIXED)) {
fixedString = StringFormatter.replacePlaceholder(StringFormatter.getStringFormat(
StringFormat.COMMAND_VISUALIZER_FIXED_FORMAT), "{portal}",
portal.getDestinationName());
}
stringBuilder.append(StringFormatter.replacePlaceholders(
Translator.getTranslatedMessage(TranslatableMessage.COMMAND_VISUALIZER_PORTAL_FORMAT),
new String[]{"{icons}", "{portal}", "{fixed}"}, new String[]{iconBuilder.toString(),
portal.getName(), fixedString}));
stringBuilder.append(StringFormatter.replacePlaceholders(StringFormatter.getStringFormat(
StringFormat.COMMAND_VISUALIZER_PORTAL_FORMAT), new String[]{"{icons}", "{portal}", "{fixed}"},
new String[]{iconBuilder.toString(), portal.getName(), fixedString}));
}
commandSender.sendMessage(IconManager.replaceIconsInString(

View File

@@ -3,9 +3,9 @@ package net.knarcraft.stargatecommand.command;
import net.TheDgtl.Stargate.network.RegistryAPI;
import net.TheDgtl.Stargate.network.portal.Portal;
import net.TheDgtl.Stargate.network.portal.PortalFlag;
import net.knarcraft.stargatecommand.formatting.StringFormat;
import net.knarcraft.stargatecommand.formatting.StringFormatter;
import net.knarcraft.stargatecommand.formatting.TranslatableMessage;
import net.knarcraft.stargatecommand.formatting.Translator;
import net.knarcraft.stargatecommand.property.StargateCommandCommand;
import net.knarcraft.stargatecommand.util.PortalFinderHelper;
import org.apache.commons.lang.StringUtils;
@@ -65,9 +65,9 @@ public class TabCommandInfo implements TabExecutor {
Set<PortalFlag> portalFlags = PortalFlag.parseFlags(portal.getAllFlagsString());
String flags = StringUtils.join(portalFlags, ", ");
String infoMessage = StringFormatter.replacePlaceholders(Translator.getTranslatedMessage(
TranslatableMessage.COMMAND_INFO_FORMAT), new String[]{"{portal}", "{destination}", "{network}",
"{owner}", "{flags}"}, new String[]{name, destination, network, ownerName, flags});
String infoMessage = StringFormatter.replacePlaceholders(StringFormatter.getStringFormat(
StringFormat.COMMAND_INFO_FORMAT), new String[]{"{portal}", "{destination}", "{network}", "{owner}",
"{flags}"}, new String[]{name, destination, network, ownerName, flags});
player.sendMessage(StringFormatter.formatInfoMessage(infoMessage));
return true;

View File

@@ -0,0 +1,43 @@
package net.knarcraft.stargatecommand.formatting;
/**
* An enum representing the formats of various output messages
*/
public enum StringFormat {
/**
* The format describing the visualizer's output format
*/
COMMAND_VISUALIZER_FORMAT,
/**
* The format describing one portal line in the visualizer's output format
*/
COMMAND_VISUALIZER_PORTAL_FORMAT,
/**
* The format used for displaying a fixed portal's destination
*/
COMMAND_VISUALIZER_FIXED_FORMAT,
/**
* The format describing the output from the info command
*/
COMMAND_INFO_FORMAT,
/**
* The format used when describing a configuration option
*/
CONFIG_OPTION_DESCRIPTION_FORMAT,
/**
* The format describing a configuration option's current value
*/
CONFIG_OPTION_CURRENT_VALUE_FORMAT,
/**
* The format describing the header when displaying configuration values
*/
CONFIG_VALUES_HEADER_FORMAT,
}

View File

@@ -1,14 +1,60 @@
package net.knarcraft.stargatecommand.formatting;
import net.knarcraft.stargatecommand.StargateCommand;
import net.knarcraft.stargatecommand.util.FileHelper;
import net.md_5.bungee.api.ChatColor;
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.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* A formatter for formatting displayed messages
*/
public class StringFormatter {
public final class StringFormatter {
private static final String formatFileName = "format.yml";
private static Map<StringFormat, String> loadedFormats;
private static Map<StringFormat, String> backupLoadedFormats;
private StringFormatter() {
}
/**
* Loads string formats from disk
*/
public static void loadStringFormats() {
loadedFormats = loadCustomStringFormat();
backupLoadedFormats = loadStringFormat();
}
/**
* Gets a string format from the format file
*
* @param stringFormat <p>The string format to get</p>
* @return <p>The translated message</p>
*/
public static String getStringFormat(StringFormat stringFormat) {
String translatedMessage;
if (loadedFormats != null && loadedFormats.containsKey(stringFormat)) {
translatedMessage = loadedFormats.get(stringFormat);
} else if (backupLoadedFormats != null && backupLoadedFormats.containsKey(stringFormat)) {
translatedMessage = backupLoadedFormats.get(stringFormat);
} else {
return "String formats not loaded";
}
return replaceTranslatablePlaceholders(translateAllColorCodes(translatedMessage));
}
/**
* Replaces a placeholder in a string
@@ -77,17 +123,6 @@ public class StringFormatter {
return ChatColor.DARK_RED + formatMessage(message);
}
/**
* Formats a message by adding the prefix and text color
*
* @param message <p>The message to format</p>
* @return <p>The formatted message</p>
*/
private static String formatMessage(String message) {
return Translator.getTranslatedMessage(TranslatableMessage.PREFIX) + " " +
ChatColor.RESET + message;
}
/**
* Translates all found color codes to formatting in a string
*
@@ -104,4 +139,87 @@ public class StringFormatter {
return message;
}
/**
* Formats a message by adding the prefix and text color
*
* @param message <p>The message to format</p>
* @return <p>The formatted message</p>
*/
private static String formatMessage(String message) {
return Translator.getTranslatedMessage(TranslatableMessage.PREFIX) + " " +
ChatColor.RESET + message;
}
/**
* Loads all string formats
*
* @return <p>A mapping of all string formats</p>
*/
public static Map<StringFormat, String> loadStringFormat() {
try {
BufferedReader reader = FileHelper.getBufferedReaderForInternalFile("/" + formatFileName);
return loadFormats(reader);
} catch (FileNotFoundException e) {
StargateCommand.getInstance().getLogger().log(Level.SEVERE, "Unable to load string formats from " +
formatFileName);
return null;
}
}
/**
* Tries to load translated messages from a custom en-US.yml file
*
* @return <p>The loaded translated strings, or null if no custom language file exists</p>
*/
public static Map<StringFormat, String> loadCustomStringFormat() {
File formatFile = new File(StargateCommand.getInstance().getDataFolder(), formatFileName);
if (!formatFile.exists()) {
return null;
}
try {
StargateCommand.getInstance().getLogger().log(Level.INFO, "Loading custom formats from " +
formatFileName);
return loadFormats(new BufferedReader(new InputStreamReader(new FileInputStream(formatFile),
StandardCharsets.UTF_8)));
} catch (FileNotFoundException e) {
StargateCommand.getInstance().getLogger().log(Level.WARNING,
"Unable to load custom formats from " + formatFileName);
return null;
}
}
/**
* Replaces all placeholders corresponding to translatable messages in the given input
*
* @param input <p>The input to replace placeholders for</p>
* @return <p>The input with placeholders replaced</p>
*/
private static String replaceTranslatablePlaceholders(String input) {
for (TranslatableMessage translatableMessage : TranslatableMessage.values()) {
input = StringFormatter.replacePlaceholder(input, "{" + translatableMessage.name() + "}",
Translator.getTranslatedMessage(translatableMessage));
}
return input;
}
/**
* Loads string formats from the given reader
*
* @param reader <p>The buffered reader to read from</p>
* @return <p>The loaded formats strings</p>
*/
private static Map<StringFormat, String> loadFormats(BufferedReader reader) {
Map<StringFormat, String> stringFormats = new HashMap<>();
YamlConfiguration configuration = YamlConfiguration.loadConfiguration(reader);
for (StringFormat message : StringFormat.values()) {
String format = configuration.getString(message.toString());
if (format != null) {
stringFormats.put(message, format);
}
}
return stringFormats;
}
}

View File

@@ -40,21 +40,6 @@ public enum TranslatableMessage {
*/
INVALID_NUMBER_GIVEN,
/**
* The message to display when displaying the current value of a configuration option
*/
CONFIG_OPTION_CURRENT_VALUE,
/**
* The header to display when showing all configuration options and their values
*/
CONFIG_VALUES_HEADER,
/**
* The message to display when showing a full description of a configuration option
*/
CONFIG_OPTION_DESCRIPTION,
/**
* The message to display when a command is used from the console, but requires a player
*/
@@ -96,23 +81,98 @@ public enum TranslatableMessage {
COMMAND_VISUALIZER_ARGUMENTS,
/**
* The format used to display the visualization of a Stargate network
* The message to use when displaying that something is Stargate related
*/
COMMAND_VISUALIZER_FORMAT,
STARGATE,
/**
* The format used to display one portal int the visualized network
* The configuration values text displayed when displaying all configuration values
*/
COMMAND_VISUALIZER_PORTAL_FORMAT,
CONFIGURATION_VALUES_PROMPT,
/**
* The format used to display info about a portal's fixed destination
* The current value text displayed when displaying a configuration option's current value
*/
COMMAND_VISUALIZER_FIXED_FORMAT,
CURRENT_VALUE_PROMPT,
/**
* The format used to display information about a Stargate
* The default text displayed when displaying a configuration option's default value
*/
COMMAND_INFO_FORMAT,
DEFAULT_PROMPT,
/**
* The header displayed at the beginning of the info command's output
*/
COMMAND_INFO_HEADER,
/**
* The name text displayed when displaying information about a Stargate
*/
NAME_PROMPT,
/**
* The destination text displayed when displaying information about a Stargate
*/
DESTINATION_PROMPT,
/**
* The network text displayed when displaying information about a Stargate
*/
NETWORK_PROMPT,
/**
* The owner text displayed when displaying information about a Stargate
*/
OWNER_PROMPT,
/**
* The flags text displayed when displaying information about a Stargate
*/
FLAGS_PROMPT,
/**
* The header of the visualizer's symbol explanation section
*/
VISUALIZER_SYMBOL_EXPLANATION,
/**
* The text used to explain the symbol used for hidden portals
*/
VISUALIZER_HIDDEN,
/**
* The text used to explain the symbol used for non-hidden portals
*/
VISUALIZER_NOT_HIDDEN,
/**
* The text used to explain the symbol used for always open portals
*/
VISUALIZER_ALWAYS_OPEN,
/**
* The text used to explain the symbol used for not always open portals
*/
VISUALIZER_NOT_ALWAYS_OPEN,
/**
* The text used to explain the symbol used for random portals
*/
VISUALIZER_RANDOM,
/**
* The text used to explain the symbol used for non-random portals
*/
VISUALIZER_NOT_RANDOM,
/**
* The text used to explain the symbol used for fixed portals
*/
VISUALIZER_FIXED,
/**
* The header of the visualizer's Stargate list section
*/
VISUALIZER_LIST_HEADER,
}

View File

@@ -51,6 +51,8 @@ public final class Translator {
} else if (backupTranslatedMessages != null && backupTranslatedMessages.containsKey(translatableMessage)) {
translatedMessage = backupTranslatedMessages.get(translatableMessage);
} else {
StargateCommand.getInstance().getLogger().log(Level.WARNING,
"No translation found for translatable message " + translatableMessage.name());
return "Translated strings not loaded";
}
return StringFormatter.translateAllColorCodes(translatedMessage);

View File

@@ -0,0 +1,27 @@
# The format for visualizing a network
COMMAND_VISUALIZER_FORMAT: |
{VISUALIZER_SYMBOL_EXPLANATION}:
&6{icon_h}&r = {VISUALIZER_HIDDEN}, &6{icon_nh}&r = {VISUALIZER_NOT_HIDDEN}
&6{icon_a}&r = {VISUALIZER_ALWAYS_OPEN}, &6{icon_na}&r = {VISUALIZER_NOT_ALWAYS_OPEN}
&6{icon_r}&r = {VISUALIZER_RANDOM}, &6{icon_nr}&r = {VISUALIZER_NOT_RANDOM}
&6{icon_arrow_right}&r = {VISUALIZER_LIST_HEADER}
|
{VISUALIZER_LIST_HEADER} &a{network}&r:
# The format for visualizing a single portal on a network
COMMAND_VISUALIZER_PORTAL_FORMAT: "\n&6{icons} &a{portal}&r{fixed}"
# The format for displaying a fixed Stargate's destination during visualization
COMMAND_VISUALIZER_FIXED_FORMAT: " &6{icon_arrow_right}&r &a{portal}"
# The format for displaying information about a Stargate
COMMAND_INFO_FORMAT: |
{STARGATE} {COMMAND_INFO_HEADER}:
|- &6{NAME_PROMPT}&r: &a{portal}&r
|- &6{DESTINATION_PROMPT}&r: &a{destination}&r
|- &6{NETWORK_PROMPT}&r: &a{network}&r
|- &6{OWNER_PROMPT}&r: &a{owner}&r
|- &6{FLAGS_PROMPT}&r: &a{flags}&r
# The format for displaying a configuration option's description
CONFIG_OPTION_DESCRIPTION_FORMAT: "\n&6{name}&r - &a{description}&8 (&r{DEFAULT_PROMPT}}: &7{value}&8)"
# The format for displaying a configuration option's current value
CONFIG_OPTION_CURRENT_VALUE_FORMAT: "\n&a{CURRENT_VALUE_PROMPT}: &6{value}"
# The format for the header displayed before the list of configuration options
CONFIG_VALUES_HEADER_FORMAT: "&a{STARGATE} &6{CONFIGURATION_VALUES_PROMPT}:"

View File

@@ -1,13 +1,11 @@
PREFIX: "[StargateCommand]"
STARGATE: "Stargate"
PERMISSION_DENIED: "Permission Denied"
INVALID_CONFIGURATION_OPTION: "Invalid configuration option specified"
INVALID_DATATYPE_GIVEN: "Invalid {datatype} given"
CONFIG_UPDATED: "Configuration updated"
POSITIVE_NUMBER_REQUIRED: "This config option cannot be negative"
INVALID_NUMBER_GIVEN: "Invalid number given"
CONFIG_OPTION_CURRENT_VALUE: "&aCurrent value: &6{value}"
CONFIG_VALUES_HEADER: "&aStargate &6Config values:"
CONFIG_OPTION_DESCRIPTION: "&6{name}&r - &a{description}&8 (Default: &7{value}&8)"
COMMAND_PLAYER_ONLY: "This command can only be used by players"
COMMAND_DIAL_ARGUMENTS: "You need to provide a network name and a Stargate name to dial"
INVALID_NETWORK_GIVEN: "Invalid network specified in input"
@@ -16,7 +14,21 @@ PORTAL_NO_ACCESS: "You don't have access to the selected Stargate"
NO_PORTAL_IN_SIGHT: "You need to look at a Stargate to use this command"
DIAL_SUCCESSFUL: "Your Stargate has been prepared"
COMMAND_VISUALIZER_ARGUMENTS: "A network must be provided to visualize"
COMMAND_VISUALIZER_FORMAT: "Symbol explanation: \n&6{icon_h}&r = hidden, &6{icon_nh}&r = not hidden\n&6{icon_a}&r = always open, &6{icon_na}&r = not always open\n&6{icon_r}&r = random destination, &6{icon_nr}&r = non-random destination\n&6{icon_arrow_right}&r = fixed Stargate going to the specified Stargate\n|\nAll Stargates in network &a{network}&r:"
COMMAND_VISUALIZER_PORTAL_FORMAT: "&6{icons} &a{portal}&r{fixed}"
COMMAND_VISUALIZER_FIXED_FORMAT: " {icon_arrow_right} &a{portal}"
COMMAND_INFO_FORMAT: "Stargate info:\n|- &6Name&r: &a{portal}&r\n|- &6Destination&r: &a{destination}&r\n|- &6Network&r: &a{network}&r\n|- &6Owner&r: &a{owner}&r\n|- &6Flags&r: &a{flags}&r"
CONFIGURATION_VALUES_PROMPT: "Config values"
CURRENT_VALUE_PROMPT: "Current value"
DEFAULT_PROMPT: "Default"
NAME_PROMPT: "Name"
COMMAND_INFO_HEADER: "Information"
DESTINATION_PROMPT: "Destination"
NETWORK_PROMPT: "Network"
OWNER_PROMPT: "Owner"
FLAGS_PROMPT: "Flags"
VISUALIZER_SYMBOL_EXPLANATION: "Symbol explanation"
VISUALIZER_HIDDEN: "hidden"
VISUALIZER_NOT_HIDDEN: "not hidden"
VISUALIZER_ALWAYS_OPEN: "always open"
VISUALIZER_NOT_ALWAYS_OPEN: "not always open"
VISUALIZER_RANDOM: "random destination"
VISUALIZER_NOT_RANDOM: "non-random destination"
VISUALIZER_FIXED: "fixed Stargate going to the specified Stargate"
VISUALIZER_LIST_HEADER: "All Stargates in network"