Adds a Message enum to avoid string keys

This commit is contained in:
2024-02-20 16:18:38 +01:00
parent a9e5855194
commit e57aa4097c
20 changed files with 369 additions and 95 deletions

View File

@@ -192,10 +192,10 @@ public final class EconomyConfig {
this.vault = vault;
return true;
} else {
Stargate.logInfo(Stargate.getString("ecoLoadError"));
Stargate.logInfo(Stargate.getString(Message.ECONOMY_LOAD_ERROR));
}
} else {
Stargate.logInfo(Stargate.getString("vaultLoadError"));
Stargate.logInfo(Stargate.getString(Message.VAULT_LOAD_ERROR));
}
configOptions.put(ConfigOption.USE_ECONOMY, false);
return false;

View File

@@ -21,9 +21,9 @@ import java.util.Set;
public final class LanguageLoader {
private final String languageFolder;
private final Map<String, String> loadedBackupStrings;
private final Map<Message, String> loadedBackupStrings;
private String chosenLanguage;
private Map<String, String> loadedStringTranslations;
private Map<Message, String> loadedStringTranslations;
/**
* Instantiates a new language loader
@@ -62,34 +62,34 @@ public final class LanguageLoader {
}
/**
* Gets the string to display given its name/key
* Gets the string to display given its message key
*
* @param name <p>The name/key of the string to display</p>
* @return <p>The string in the user's preferred language</p>
* @param message <p>The message to display</p>
* @return <p>The message in the user's preferred language</p>
*/
@NotNull
public String getString(@NotNull String name) {
public String getString(@NotNull Message message) {
String value = null;
if (loadedStringTranslations != null) {
value = loadedStringTranslations.get(name);
value = loadedStringTranslations.get(message);
}
if (value == null) {
value = getBackupString(name);
value = getBackupString(message);
}
return value;
}
/**
* Gets the string to display given its name/key
* Gets the string to display given its message key
*
* @param name <p>The name/key of the string to display</p>
* @param message <p>The message to display</p>
* @return <p>The string in the backup language (English)</p>
*/
@NotNull
public String getBackupString(@NotNull String name) {
public String getBackupString(@NotNull Message message) {
String value = null;
if (loadedBackupStrings != null) {
value = loadedBackupStrings.get(name);
value = loadedBackupStrings.get(message);
}
if (value == null) {
return "";
@@ -112,7 +112,7 @@ public final class LanguageLoader {
* @param language <p>The language to update</p>
*/
private void updateLanguage(@NotNull String language) {
Map<String, String> currentLanguageValues = load(language);
Map<Message, String> currentLanguageValues = load(language);
InputStream inputStream = getClass().getResourceAsStream("/lang/" + language + ".txt");
if (inputStream == null) {
@@ -138,11 +138,11 @@ public final class LanguageLoader {
* @throws IOException <p>if unable to read a language file</p>
*/
private void readChangedLanguageStrings(@NotNull InputStream inputStream, @NotNull String language,
@Nullable Map<String, String> currentLanguageValues) throws IOException {
@Nullable Map<Message, String> currentLanguageValues) throws IOException {
//Get language values
BufferedReader bufferedReader = FileHelper.getBufferedReaderFromInputStream(inputStream);
Map<String, String> internalLanguageValues = FileHelper.readKeyValuePairs(bufferedReader, "=",
ColorConversion.NORMAL);
Map<Message, String> internalLanguageValues = fromStringMap(FileHelper.readKeyValuePairs(bufferedReader,
"=", ColorConversion.NORMAL));
//If currentLanguageValues is null; the chosen language has not been used before
if (currentLanguageValues == null) {
@@ -153,9 +153,9 @@ public final class LanguageLoader {
//If a key is not found in the language file, add the one in the internal file. Must update the external file
if (!internalLanguageValues.keySet().equals(currentLanguageValues.keySet())) {
Map<String, String> newLanguageValues = new HashMap<>();
Map<Message, String> newLanguageValues = new HashMap<>();
boolean updateNecessary = false;
for (String key : internalLanguageValues.keySet()) {
for (Message key : internalLanguageValues.keySet()) {
if (currentLanguageValues.get(key) == null) {
newLanguageValues.put(key, internalLanguageValues.get(key));
//Found at least one value in the internal file not in the external file. Need to update
@@ -181,19 +181,19 @@ public final class LanguageLoader {
* @param customLanguageStrings <p>Any custom language strings not recognized</p>
* @throws IOException <p>If unable to write to the language file</p>
*/
private void updateLanguageFile(@NotNull String language, @NotNull Map<String, String> languageStrings,
@Nullable Map<String, String> customLanguageStrings) throws IOException {
private void updateLanguageFile(@NotNull String language, @NotNull Map<Message, String> languageStrings,
@Nullable Map<Message, String> customLanguageStrings) throws IOException {
BufferedWriter bufferedWriter = FileHelper.getBufferedWriterFromString(languageFolder + language + ".txt");
//Output normal Language data
for (String key : languageStrings.keySet()) {
for (Message key : languageStrings.keySet()) {
bufferedWriter.write(key + "=" + languageStrings.get(key));
bufferedWriter.newLine();
}
bufferedWriter.newLine();
//Output any custom language strings the user had
if (customLanguageStrings != null) {
for (String key : customLanguageStrings.keySet()) {
for (Message key : customLanguageStrings.keySet()) {
bufferedWriter.write(key + "=" + customLanguageStrings.get(key));
bufferedWriter.newLine();
}
@@ -207,8 +207,7 @@ public final class LanguageLoader {
* @param lang <p>The language to load</p>
* @return <p>A mapping between loaded string indexes and the strings to display</p>
*/
@Nullable
private Map<String, String> load(@NotNull String lang) {
private Map<Message, String> load(@NotNull String lang) {
return load(lang, null);
}
@@ -219,9 +218,7 @@ public final class LanguageLoader {
* @param inputStream <p>An optional input stream to use. Defaults to using a file input stream</p>
* @return <p>A mapping between loaded string indexes and the strings to display</p>
*/
@Nullable
private Map<String, String> load(@NotNull String lang, @Nullable InputStream inputStream) {
Map<String, String> strings;
private Map<Message, String> load(@NotNull String lang, @Nullable InputStream inputStream) {
BufferedReader bufferedReader;
try {
if (inputStream == null) {
@@ -229,14 +226,13 @@ public final class LanguageLoader {
} else {
bufferedReader = FileHelper.getBufferedReaderFromInputStream(inputStream);
}
strings = FileHelper.readKeyValuePairs(bufferedReader, "=", ColorConversion.NORMAL);
return fromStringMap(FileHelper.readKeyValuePairs(bufferedReader, "=", ColorConversion.NORMAL));
} catch (Exception exception) {
if (Stargate.getStargateConfig().isDebuggingEnabled()) {
Stargate.logInfo("Unable to load language " + lang);
}
return null;
}
return strings;
}
/**
@@ -244,8 +240,8 @@ public final class LanguageLoader {
*/
public void debug() {
if (loadedStringTranslations != null) {
Set<String> keys = loadedStringTranslations.keySet();
for (String key : keys) {
Set<Message> keys = loadedStringTranslations.keySet();
for (Message key : keys) {
Stargate.debug("LanguageLoader::Debug::loadedStringTranslations", key + " => " +
loadedStringTranslations.get(key));
}
@@ -253,11 +249,27 @@ public final class LanguageLoader {
if (loadedBackupStrings == null) {
return;
}
Set<String> keys = loadedBackupStrings.keySet();
for (String key : keys) {
Set<Message> keys = loadedBackupStrings.keySet();
for (Message key : keys) {
Stargate.debug("LanguageLoader::Debug::loadedBackupStrings", key + " => " +
loadedBackupStrings.get(key));
}
}
@NotNull
private Map<Message, String> fromStringMap(@NotNull Map<String, String> configurationStrings) {
Map<Message, String> output = new HashMap<>();
for (Map.Entry<String, String> entry : configurationStrings.entrySet()) {
Message message = Message.getFromKey(entry.getKey());
if (message == null) {
Stargate.logWarning("Found unrecognized language key " + entry.getKey());
continue;
}
output.put(message, entry.getValue());
}
return output;
}
}

View File

@@ -0,0 +1,251 @@
package net.knarcraft.stargate.config;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* Translated messages displayed to players
*/
public enum Message {
/**
* The prefix displayed in front of all messages shown in the chat
*/
PREFIX("prefix"),
/**
* The message displayed when a player is teleported
*/
TELEPORTED("teleportMsg"),
/**
* The message displayed when a player destroys a Stargate
*/
DESTROYED("destroyMsg"),
/**
* The message displayed when the currently selected Stargate destination is invalid
*/
INVALID_DESTINATION("invalidMsg"),
/**
* The message displayed when the destination portal is busy with another player
*/
DESTINATION_BLOCKED("blockMsg"),
/**
* The message displayed when the Stargate has no destinations available to the player
*/
NO_DESTINATION("destEmpty"),
/**
* The message displayed when a player is denied access to any action
*/
ACCESS_DENIED("denyMsg"),
/**
* The message displayed when the plugin is reloaded
*/
RELOADED("reloaded"),
/**
* The message displayed when a player has some currency deducted from their account
*/
ECONOMY_DEDUCTED("ecoDeduct"),
/**
* The message displayed when a player has some currency refunded to their account
*/
ECONOMY_REFUNDED("ecoRefund"),
/**
* The message displayed when a player obtains some currency to their account (from portal usage)
*/
ECONOMY_OBTAINED("ecoObtain"),
/**
* The message displayed when the player has an insufficient amount of currency to perform an action
*/
ECONOMY_INSUFFICIENT("ecoInFunds"),
/**
* The message displayed when economy fails to load
*/
ECONOMY_LOAD_ERROR("ecoLoadError"),
/**
* The message displayed when Vault fails to load
*/
VAULT_LOAD_ERROR("vaultLoadError"),
/**
* The message displayed when Vault successfully loads
*/
VAULT_LOADED("vaultLoaded"),
/**
* The message displayed when a Stargate is successfully created
*/
CREATED("createMsg"),
/**
* The message displayed when a player is denied from creating a Stargate on the selected network
*/
CREATION_NETWORK_DENIED("createNetDeny"),
/**
* The message displayed when a player is denied from creating a Stargate of the given gate type
*/
CREATION_GATE_DENIED("createGateDeny"),
/**
* The message displayed when a Stargate is created on the player's personal network
*/
CREATION_PERSONAL("createPersonal"),
/**
* The message displayed when the name of a Stargate is too short or too long
*/
CREATION_NAME_LENGTH("createNameLength"),
/**
* The message displayed when another Stargate on the network has the same name as the new Stargate
*/
CREATION_NAME_COLLISION("createExists"),
/**
* The message displayed when the specified network is full
*/
CREATION_NETWORK_FULL("createFull"),
/**
* The message displayed when a player is denied from creating a Stargate in the current world
*/
CREATION_WORLD_DENIED("createWorldDeny"),
/**
* The message displayed when a gate is physically conflicting with another
*/
CREATION_CONFLICT("createConflict"),
/**
* The right-click prompt displayed on Stargate signs
*/
SIGN_RIGHT_CLICK("signRightClick"),
/**
* The to use prompt displayed on Stargate signs
*/
SIGN_TO_USE("signToUse"),
/**
* The random string displayed on Stargate signs
*/
SIGN_RANDOM("signRandom"),
/**
* The disconnected string displayed on Stargate signs
*/
SIGN_DISCONNECTED("signDisconnected"),
/**
* The invalid gate string displayed on Stargate signs
*/
SIGN_INVALID("signInvalidGate"),
/**
* The message displayed if trying to create a bungee gate when bungee is disabled
*/
BUNGEE_DISABLED("bungeeDisabled"),
/**
* The message displayed when a player is denied from creating a bungee Stargate
*/
BUNGEE_CREATION_DENIED("bungeeDeny"),
/**
* The message displayed if a Stargate is missing the destination, the network or both
*/
BUNGEE_MISSING_INFO("bungeeEmpty"),
/**
* The teleportation prompt shown on bungee signs
*/
BUNGEE_SIGN("bungeeSign"),
/**
* The format of the title of the portal info shown in chat
*/
PORTAL_INFO_TITLE("portalInfoTitle"),
/**
* The format of the name of the portal info shown in chat
*/
PORTAL_INFO_NAME("portalInfoName"),
/**
* The format of the destination of the portal info shown in chat
*/
PORTAL_INFO_DESTINATION("portalInfoDestination"),
/**
* The format of the network of the portal info shown in chat
*/
PORTAL_INFO_NETWORK("portalInfoNetwork"),
/**
* The format of the server of the portal info shown in chat
*/
PORTAL_INFO_SERVER("portalInfoServer"),
/**
* The author that created the loaded translation
*/
AUTHOR("author"),
;
private final String key;
/**
* Instantiates a new message
*
* @param key <p>The key of the message in the language files</p>
*/
Message(@NotNull String key) {
this.key = key;
}
/**
* Gets the language file key for this message
*
* @return <p>This message's key</p>
*/
@NotNull
public String getKey() {
return this.key;
}
/**
* Gets the message corresponding to the given key
*
* @param key <p>The key to get a message from</p>
* @return <p>The message, or null if not found</p>
*/
@Nullable
public static Message getFromKey(@NotNull String key) {
for (Message message : Message.values()) {
if (message.getKey().equalsIgnoreCase(key)) {
return message;
}
}
return null;
}
@Override
@NotNull
public String toString() {
return this.getKey();
}
}

View File

@@ -53,9 +53,9 @@ public final class MessageSender {
}
message = ChatColor.translateAlternateColorCodes('&', message);
if (error) {
sender.sendMessage(ChatColor.RED + languageLoader.getString("prefix") + ChatColor.WHITE + message);
sender.sendMessage(ChatColor.RED + languageLoader.getString(Message.PREFIX) + ChatColor.WHITE + message);
} else {
sender.sendMessage(ChatColor.GREEN + languageLoader.getString("prefix") + ChatColor.WHITE + message);
sender.sendMessage(ChatColor.GREEN + languageLoader.getString(Message.PREFIX) + ChatColor.WHITE + message);
}
}

View File

@@ -244,7 +244,7 @@ public final class StargateConfig {
//Reload portal markers
DynmapManager.addAllPortalMarkers();
messageSender.sendErrorMessage(sender, languageLoader.getString("reloaded"));
messageSender.sendErrorMessage(sender, languageLoader.getString(Message.RELOADED));
}
/**
@@ -541,7 +541,7 @@ public final class StargateConfig {
if (economyConfig.setupEconomy(Stargate.getPluginManager()) && economyConfig.getEconomy() != null &&
economyConfig.getVault() != null) {
String vaultVersion = economyConfig.getVault().getDescription().getVersion();
Stargate.logInfo(Stargate.replacePlaceholders(Stargate.getString("vaultLoaded"), "%version%", vaultVersion));
Stargate.logInfo(Stargate.replacePlaceholders(Stargate.getString(Message.VAULT_LOADED), "%version%", vaultVersion));
}
}