Locale override now uses a specific file instead of weird name matching

This commit is contained in:
nossr50 2021-04-16 13:50:41 -07:00
parent c29f311f1e
commit 16f79b9fbc
8 changed files with 167 additions and 24 deletions

View File

@ -1,5 +1,11 @@
Version 2.1.194
Fixed a bug where mcMMO didn't properly setup file paths for locale override
Locale override files are now named locale_override.properties (converted automatically/generated automatically)
Existing in use locale override files will be renamed to locale_override.properties and have some useful text put in them
mcMMO will now generate a locale override file with some detailed instructions if one doesn't exist (will be found in /plugins/mcMMO/locales/locale_override.properties)
NOTES:
If you were overriding locale before this update mcMMO will just rename the existing override file to locale_override.properties add some useful text and then load it
Remember you can use /mcreloadlocale to swap the edits in without restarting the server
Version 2.1.193
Fixed another bug where mcrank/mctop/leaderboards weren't loading

View File

@ -9,7 +9,6 @@ import com.gmail.nossr50.util.commands.CommandUtils;
import com.gmail.nossr50.util.player.UserManager;
import com.gmail.nossr50.util.skills.SkillTools;
import com.google.common.collect.ImmutableList;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
@ -19,7 +18,6 @@ import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public abstract class ExperienceCommand implements TabExecutor {
@Override

View File

@ -22,7 +22,6 @@ import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
* This class mirrors the structure of ExperienceCommand, except the

View File

@ -1,6 +1,5 @@
package com.gmail.nossr50.database;
import com.gmail.nossr50.api.exceptions.InvalidPlayerException;
import com.gmail.nossr50.api.exceptions.InvalidSkillException;
import com.gmail.nossr50.datatypes.database.DatabaseType;
import com.gmail.nossr50.datatypes.database.PlayerStat;

View File

@ -10,7 +10,6 @@ import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
import com.gmail.nossr50.datatypes.skills.SuperAbilityType;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.blockmeta.HashChunkManager;
import com.gmail.nossr50.util.skills.SkillTools;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
@ -18,7 +17,6 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.*;
import java.security.InvalidParameterException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
@ -1074,7 +1072,7 @@ public final class FlatFileDatabaseManager implements DatabaseManager {
try {
// Open the file to write the player
bufferedWriter = new BufferedWriter(new FileWriter(usersFilePath, true));
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm");
LocalDateTime localDateTime = LocalDateTime.now();
bufferedWriter.append("# mcMMO Database created on ").append(localDateTime.format(dateTimeFormatter)).append("\r\n"); //Empty file
} catch (Exception e) {

View File

@ -4,18 +4,21 @@ import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.util.text.TextUtils;
import net.kyori.adventure.text.TextComponent;
import org.bukkit.ChatColor;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.io.Reader;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.MessageFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.logging.Level;
public final class LocaleLoader {
private static final String BUNDLE_ROOT = "com.gmail.nossr50.locale.locale";
private static final String OVERRIDE_FILE_NAME = "locale_override.properties";
private static Map<String, String> bundleCache = new HashMap<>();
private static ResourceBundle bundle = null;
private static ResourceBundle filesystemBundle = null;
@ -30,8 +33,9 @@ public final class LocaleLoader {
/**
* Gets the appropriate string from the Locale files.
*
* @param key The key to look up the string with
* @param key The key to look up the string with
* @param messageArguments Any arguments to be added to the string
*
* @return The properly formatted locale string
*/
public static String getString(String key, Object... messageArguments) {
@ -44,11 +48,13 @@ public final class LocaleLoader {
}
//TODO: Remove this hacky crap with something better later
/**
* Gets the appropriate TextComponent representation of a formatted string from the Locale files.
*
* @param key The key to look up the string with
* @param key The key to look up the string with
* @param messageArguments Any arguments to be added to the text component
*
* @return The properly formatted text component
*/
public static TextComponent getTextComponent(String key, Object... messageArguments) {
@ -75,19 +81,18 @@ public final class LocaleLoader {
if (filesystemBundle != null) {
try {
return filesystemBundle.getString(key);
} catch (MissingResourceException ignored) {
}
catch (MissingResourceException ignored) {}
}
try {
return bundle.getString(key);
} catch (MissingResourceException ignored) {
}
catch (MissingResourceException ignored) {}
try {
return enBundle.getString(key);
}
catch (MissingResourceException ignored) {
} catch (MissingResourceException ignored) {
if (!key.contains("Guides")) {
mcMMO.p.getLogger().warning("Could not find locale string: " + key);
}
@ -134,8 +139,7 @@ public final class LocaleLoader {
if (myLocale.length == 1) {
locale = new Locale(myLocale[0]);
}
else if (myLocale.length >= 2) {
} else if (myLocale.length >= 2) {
locale = new Locale(myLocale[0], myLocale[1]);
}
@ -144,18 +148,155 @@ public final class LocaleLoader {
}
Path localePath = Paths.get(mcMMO.getLocalesDirectory() + "locale_" + locale.toString() + ".properties");
Path overridePath = Paths.get(mcMMO.getLocalesDirectory() + OVERRIDE_FILE_NAME);
File overrideFile = overridePath.toFile();
if (Files.exists(localePath) && Files.isRegularFile(localePath)) {
try (Reader localeReader = Files.newBufferedReader(localePath)) {
mcMMO.p.getLogger().log(Level.INFO, "Loading locale from {0}", localePath);
File oldOverrideFile = localePath.toFile();
try {
//Copy the file
com.google.common.io.Files.copy(oldOverrideFile, overrideFile);
//Remove the old file now
oldOverrideFile.delete();
//Insert our helpful text
StringBuilder stringBuilder = new StringBuilder();
try(BufferedReader bufferedReader = new BufferedReader(new FileReader(overrideFile.getPath()))) {
// Open the file
String line;
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm");
LocalDateTime localDateTime = LocalDateTime.now();
stringBuilder.append("# mcMMO Locale Override File created on ").append(localDateTime.format(dateTimeFormatter)).append("\r\n"); //Empty file
stringBuilder.append(getLocaleHelpTextWithoutExamples()); //Add our helpful text
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line).append("\r\n");
}
} catch (IOException e) {
e.printStackTrace();
}
try(FileWriter fileWriter = new FileWriter(overrideFile.getPath())) {
fileWriter.write(stringBuilder.toString());
} catch (IOException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
//Use the new locale file
if (Files.exists(overridePath) && Files.isRegularFile(overridePath)) {
try (Reader localeReader = Files.newBufferedReader(overridePath)) {
mcMMO.p.getLogger().log(Level.INFO, "Loading locale from {0}", overridePath);
filesystemBundle = new PropertyResourceBundle(localeReader);
} catch (IOException e) {
mcMMO.p.getLogger().log(Level.WARNING, "Failed to load locale from " + localePath, e);
mcMMO.p.getLogger().log(Level.WARNING, "Failed to load locale from " + overridePath, e);
}
} else {
//Create a blank file and fill it in with some helpful text
try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(overrideFile, true))) {
// Open the file to write the player
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm");
LocalDateTime localDateTime = LocalDateTime.now();
bufferedWriter.append("# mcMMO Locale Override File created on ").append(localDateTime.format(dateTimeFormatter)).append("\r\n"); //Empty file
String localeExplanation = getLocaleHelpText();
bufferedWriter.append(localeExplanation);
} catch (Exception e) {
e.printStackTrace();
}
}
bundle = ResourceBundle.getBundle(BUNDLE_ROOT, locale);
enBundle = ResourceBundle.getBundle(BUNDLE_ROOT, Locale.US);
}
enBundle = ResourceBundle.getBundle(BUNDLE_ROOT, Locale.US);
}
@NotNull
private static String getLocaleHelpText() {
String localeExplanation =
"# -- Are you looking to change the language of mcMMO but without editing it yourself? --\n" +
"\n" +
"# mcMMO has quite a few built in translations, you can choose which translation by editing config.yml with the appropriate locale code. The setting is `General.Locale` in config.yml\n" +
"# Odds are, if you speak a popular language on earth we already have a translation for it.\n" +
"# However our translations are done by the community, and update infrequently. (Please help us out <3)\n" +
"# We would love more people to help update our locales, submit any updated translation file to our GitHub or email it to me at business@neetgames.com\n" +
"# For a list of built in translations, view this link: https://github.com/mcMMO-Dev/mcMMO/tree/master/src/main/resources/locale\n" +
"\n" +
"\n" +
"# -- Using a built in translation -- \n" +
"# Assuming you read the above section, edit config.yml's General.Locale from en_US to the locale code that we support (see the above link), then reboot your server\n" +
"\n" +
"\n" +
"# -- Do you want to change the text in mcMMO? Including adding colors? ( Locale Override ) -- \n" +
"# First, a brief explanation.\n" +
"# Locales are the language files used by mcMMO, they also contain color codes and most of the styling used by mcMMO.\n" +
"# You can customize a locale outside of the JAR in version 2.1.51 and up.\n" +
"#\n" +
"# Locales can be overridden by editing this file\n" +
"# You can find the up to date current locale files here https://github.com/mcMMO-Dev/mcMMO/tree/master/src/main/resources/locale\n" +
"# The master file is en_US, if a translation is missing entries (as they often are) it will pull from the en_US file https://github.com/mcMMO-Dev/mcMMO/blob/master/src/main/resources/locale/locale_en_US.properties\n" +
"#\n" +
"# To override a locale, add entries to this file and copy ** only ** the strings you want to replace, otherwise you will not see any updated strings when mcMMO updates and will have to manually change them and read patch notes carefully.\n" +
"# If you wish to replace every line in some way, feel free to copy the entire contents of this file, just be advised that you will need to be on top of locale updates in mcMMO and follow our changelog closely.\n" +
"\n" +
"\n" +
"# WARNING: Locales only support ASCII and UTF16 characters at the moment, so you'll need to run special characters through a UTF16 converter (google it) to get them to work. This will be fixed in the future!\n" +
"# FIND KEYS HERE: On our github repo (en_US is our master file and has ALL the keys) -> https://github.com/mcMMO-Dev/mcMMO/tree/master/src/main/resources/locale\n" +
"# WARNING: Some keys in our master file are unused, make gratuitous use of Ctrl+F\n" +
"# HOW TO APPLY: You can either restart the server for these changes to take effect or run /mcreloadlocale.\n" +
"# -- Add Keys Below --\n" +
getExamples();
return localeExplanation;
}
@NotNull
private static String getExamples() {
return "This.Is.An.Example.Put.Locale.Keys.Here.One=&aExample text using hex color codes\n" +
"This.Is.An.Example.Put.Locale.Keys.Here.Two=[[DARK_AQUA]]Example text using our own color codes\n" +
"This.Is.An.Example.Put.Locale.Keys.Here.Three=Example text with no colors\n";
}
@NotNull
private static String getLocaleHelpTextWithoutExamples() {
String localeExplanation =
"# -- Are you looking to change the language of mcMMO but without editing it yourself? --\n" +
"\n" +
"# mcMMO has quite a few built in translations, you can choose which translation by editing config.yml with the appropriate locale code. The setting is `General.Locale` in config.yml\n" +
"# Odds are, if you speak a popular language on earth we already have a translation for it.\n" +
"# However our translations are done by the community, and update infrequently. (Please help us out <3)\n" +
"# We would love more people to help update our locales, submit any updated translation file to our GitHub or email it to me at business@neetgames.com\n" +
"# For a list of built in translations, view this link: https://github.com/mcMMO-Dev/mcMMO/tree/master/src/main/resources/locale\n" +
"\n" +
"\n" +
"# -- Using a built in translation -- \n" +
"# Assuming you read the above section, edit config.yml's General.Locale from en_US to the locale code that we support (see the above link), then reboot your server\n" +
"\n" +
"\n" +
"# -- Do you want to change the text in mcMMO? Including adding colors? ( Locale Override ) -- \n" +
"# First, a brief explanation.\n" +
"# Locales are the language files used by mcMMO, they also contain color codes and most of the styling used by mcMMO.\n" +
"# You can customize a locale outside of the JAR in version 2.1.51 and up.\n" +
"#\n" +
"# Locales can be overridden by editing this file\n" +
"# You can find the up to date current locale files here https://github.com/mcMMO-Dev/mcMMO/tree/master/src/main/resources/locale\n" +
"# The master file is en_US, if a translation is missing entries (as they often are) it will pull from the en_US file https://github.com/mcMMO-Dev/mcMMO/blob/master/src/main/resources/locale/locale_en_US.properties\n" +
"#\n" +
"# To override a locale, add entries to this file and copy ** only ** the strings you want to replace, otherwise you will not see any updated strings when mcMMO updates and will have to manually change them and read patch notes carefully.\n" +
"# If you wish to replace every line in some way, feel free to copy the entire contents of this file, just be advised that you will need to be on top of locale updates in mcMMO and follow our changelog closely.\n" +
"\n" +
"\n" +
"# WARNING: Locales only support ASCII and UTF16 characters at the moment, so you'll need to run special characters through a UTF16 converter (google it) to get them to work. This will be fixed in the future!\n" +
"# FIND KEYS HERE: On our github repo (en_US is our master file and has ALL the keys) -> https://github.com/mcMMO-Dev/mcMMO/tree/master/src/main/resources/locale\n" +
"# WARNING: Some keys in our master file are unused, make gratuitous use of Ctrl+F\n" +
"# HOW TO APPLY: You can either restart the server for these changes to take effect or run /mcreloadlocale.\n" +
"# -- Add Keys Below --\n";
return localeExplanation;
}
public static String addColors(String input) {

View File

@ -18,7 +18,6 @@ import com.gmail.nossr50.database.DatabaseManagerFactory;
import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
import com.gmail.nossr50.datatypes.skills.subskills.acrobatics.Roll;
import com.gmail.nossr50.listeners.*;
import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.party.PartyManager;
import com.gmail.nossr50.runnables.SaveTimerTask;
import com.gmail.nossr50.runnables.backups.CleanBackupsTask;

View File

@ -17,7 +17,10 @@ import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.*;
import java.net.URI;