From a496b1fd71ada2c00182875cfdfc5d863244c923 Mon Sep 17 00:00:00 2001 From: nossr50 Date: Mon, 29 Apr 2019 18:54:00 -0700 Subject: [PATCH 1/7] Dev mode --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index bcf4dd2ec..b4f17d084 100755 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 com.gmail.nossr50.mcMMO mcMMO - 2.1.50 + 2.1.51-SNAPSHOT mcMMO https://github.com/mcMMO-Dev/mcMMO From c2d4aeaf8509fa06e3c9101fee28e71136b2b027 Mon Sep 17 00:00:00 2001 From: Mark Vainomaa Date: Mon, 29 Apr 2019 01:25:00 +0300 Subject: [PATCH 2/7] Try to load locale from plugin data folder first --- .../gmail/nossr50/locale/LocaleLoader.java | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java b/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java index c48fd66bc..b5b65d928 100644 --- a/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java +++ b/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java @@ -4,10 +4,17 @@ import com.gmail.nossr50.config.Config; import com.gmail.nossr50.mcMMO; import org.bukkit.ChatColor; +import java.io.IOException; +import java.io.Reader; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.text.MessageFormat; import java.util.Locale; import java.util.MissingResourceException; +import java.util.PropertyResourceBundle; import java.util.ResourceBundle; +import java.util.logging.Level; public final class LocaleLoader { private static final String BUNDLE_ROOT = "com.gmail.nossr50.locale.locale"; @@ -85,7 +92,21 @@ public final class LocaleLoader { locale = new Locale(myLocale[0], myLocale[1]); } - bundle = ResourceBundle.getBundle(BUNDLE_ROOT, locale); + if (locale == null) { + throw new IllegalStateException("Failed to parse locale string '" + Config.getInstance().getLocale() + "'"); + } + + Path localePath = Paths.get(mcMMO.getMainDirectory() + "locale_" + locale.toString() + ".properties"); + if (Files.exists(localePath) && Files.isRegularFile(localePath)) { + try (Reader localeReader = Files.newBufferedReader(localePath)) { + bundle = new PropertyResourceBundle(localeReader); + } catch (IOException e) { + mcMMO.p.getLogger().log(Level.WARNING, "Failed to load locale from " + localePath, e); + bundle = ResourceBundle.getBundle(BUNDLE_ROOT, locale); + } + } else { + bundle = ResourceBundle.getBundle(BUNDLE_ROOT, locale); + } enBundle = ResourceBundle.getBundle(BUNDLE_ROOT, Locale.US); } } From 0f2e1ea74084ce49eeb10a0447696179e375aaf4 Mon Sep 17 00:00:00 2001 From: Mark Vainomaa Date: Mon, 29 Apr 2019 20:58:54 +0300 Subject: [PATCH 3/7] Fall back to locale loaded from JAR when filesystem one doesn't have requested key --- .../com/gmail/nossr50/locale/LocaleLoader.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java b/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java index b5b65d928..906397931 100644 --- a/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java +++ b/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java @@ -19,6 +19,7 @@ import java.util.logging.Level; public final class LocaleLoader { private static final String BUNDLE_ROOT = "com.gmail.nossr50.locale.locale"; private static ResourceBundle bundle = null; + private static ResourceBundle filesystemBundle = null; private static ResourceBundle enBundle = null; private LocaleLoader() {}; @@ -39,6 +40,13 @@ public final class LocaleLoader { initialize(); } + if (filesystemBundle != null) { + try { + return getString(key, filesystemBundle, messageArguments); + } + catch (MissingResourceException ignored) {} + } + try { return getString(key, bundle, messageArguments); } @@ -99,14 +107,12 @@ public final class LocaleLoader { Path localePath = Paths.get(mcMMO.getMainDirectory() + "locale_" + locale.toString() + ".properties"); if (Files.exists(localePath) && Files.isRegularFile(localePath)) { try (Reader localeReader = Files.newBufferedReader(localePath)) { - bundle = new PropertyResourceBundle(localeReader); + filesystemBundle = new PropertyResourceBundle(localeReader); } catch (IOException e) { mcMMO.p.getLogger().log(Level.WARNING, "Failed to load locale from " + localePath, e); - bundle = ResourceBundle.getBundle(BUNDLE_ROOT, locale); } - } else { - bundle = ResourceBundle.getBundle(BUNDLE_ROOT, locale); } + bundle = ResourceBundle.getBundle(BUNDLE_ROOT, locale); enBundle = ResourceBundle.getBundle(BUNDLE_ROOT, Locale.US); } } From 1ca48051add5f289ff79bc7584ee083a6b1a53f9 Mon Sep 17 00:00:00 2001 From: Mark Vainomaa Date: Tue, 30 Apr 2019 09:31:33 +0300 Subject: [PATCH 4/7] Dedicated directory for locales --- src/main/java/com/gmail/nossr50/locale/LocaleLoader.java | 2 +- src/main/java/com/gmail/nossr50/mcMMO.java | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java b/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java index 906397931..11522164b 100644 --- a/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java +++ b/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java @@ -104,7 +104,7 @@ public final class LocaleLoader { throw new IllegalStateException("Failed to parse locale string '" + Config.getInstance().getLocale() + "'"); } - Path localePath = Paths.get(mcMMO.getMainDirectory() + "locale_" + locale.toString() + ".properties"); + Path localePath = Paths.get(mcMMO.getLocalesDirectory() + "locale_" + locale.toString() + ".properties"); if (Files.exists(localePath) && Files.isRegularFile(localePath)) { try (Reader localeReader = Files.newBufferedReader(localePath)) { filesystemBundle = new PropertyResourceBundle(localeReader); diff --git a/src/main/java/com/gmail/nossr50/mcMMO.java b/src/main/java/com/gmail/nossr50/mcMMO.java index 644ad1bce..9b39ecd60 100644 --- a/src/main/java/com/gmail/nossr50/mcMMO.java +++ b/src/main/java/com/gmail/nossr50/mcMMO.java @@ -79,6 +79,7 @@ public class mcMMO extends JavaPlugin { /* File Paths */ private static String mainDirectory; + private static String localesDirectory; private static String flatFileDirectory; private static String usersFile; private static String modDirectory; @@ -357,6 +358,10 @@ public class mcMMO extends JavaPlugin { return mainDirectory; } + public static String getLocalesDirectory() { + return localesDirectory; + } + public static String getFlatFileDirectory() { return flatFileDirectory; } @@ -432,6 +437,7 @@ public class mcMMO extends JavaPlugin { private void setupFilePaths() { mcmmo = getFile(); mainDirectory = getDataFolder().getPath() + File.separator; + localesDirectory = mainDirectory + "locales" + File.separator; flatFileDirectory = mainDirectory + "flatfile" + File.separator; usersFile = flatFileDirectory + "mcmmo.users"; modDirectory = mainDirectory + "mods" + File.separator; @@ -485,6 +491,8 @@ public class mcMMO extends JavaPlugin { File currentFlatfilePath = new File(flatFileDirectory); currentFlatfilePath.mkdirs(); + File localesDirectoryPath = new File(localesDirectory); + localesDirectoryPath.mkdirs(); } private void loadConfigFiles() { From ec574a6b63874d18ecd397b6b36cba1ebbd64788 Mon Sep 17 00:00:00 2001 From: Mark Vainomaa Date: Tue, 30 Apr 2019 17:58:33 +0300 Subject: [PATCH 5/7] Cache raw messages from resource bundles Should speed things up a bit as next message lookups are from single hashmap instead --- .../gmail/nossr50/locale/LocaleLoader.java | 34 +++++++++++-------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java b/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java index 11522164b..c16f2c425 100644 --- a/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java +++ b/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java @@ -10,7 +10,9 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.text.MessageFormat; +import java.util.HashMap; import java.util.Locale; +import java.util.Map; import java.util.MissingResourceException; import java.util.PropertyResourceBundle; import java.util.ResourceBundle; @@ -18,6 +20,7 @@ import java.util.logging.Level; public final class LocaleLoader { private static final String BUNDLE_ROOT = "com.gmail.nossr50.locale.locale"; + private static Map bundleCache = new HashMap<>(); private static ResourceBundle bundle = null; private static ResourceBundle filesystemBundle = null; private static ResourceBundle enBundle = null; @@ -40,32 +43,33 @@ public final class LocaleLoader { initialize(); } + String rawMessage = bundleCache.computeIfAbsent(key, LocaleLoader::getRawString); + return formatString(rawMessage, messageArguments); + } + + private static String getRawString(String key) { if (filesystemBundle != null) { try { - return getString(key, filesystemBundle, messageArguments); + return filesystemBundle.getString(key); } catch (MissingResourceException ignored) {} } try { - return getString(key, bundle, messageArguments); + return bundle.getString(key); } - catch (MissingResourceException ex) { - try { - return getString(key, enBundle, messageArguments); - } - catch (MissingResourceException ex2) { - if (!key.contains("Guides")) { - mcMMO.p.getLogger().warning("Could not find locale string: " + key); - } + catch (MissingResourceException ignored) {} - return '!' + key + '!'; - } + try { + return enBundle.getString(key); } - } + catch (MissingResourceException ignored) { + if (!key.contains("Guides")) { + mcMMO.p.getLogger().warning("Could not find locale string: " + key); + } - private static String getString(String key, ResourceBundle bundle, Object... messageArguments) throws MissingResourceException { - return formatString(bundle.getString(key), messageArguments); + return '!' + key + '!'; + } } public static String formatString(String string, Object... messageArguments) { From 5dc9c3f732a38fe01255cba96989f8fa15d22f73 Mon Sep 17 00:00:00 2001 From: Mark Vainomaa Date: Tue, 30 Apr 2019 20:14:42 +0300 Subject: [PATCH 6/7] Note user that locale is loaded from locales directory --- src/main/java/com/gmail/nossr50/locale/LocaleLoader.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java b/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java index c16f2c425..15b31c36b 100644 --- a/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java +++ b/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java @@ -111,6 +111,7 @@ public final class LocaleLoader { Path localePath = Paths.get(mcMMO.getLocalesDirectory() + "locale_" + locale.toString() + ".properties"); if (Files.exists(localePath) && Files.isRegularFile(localePath)) { try (Reader localeReader = Files.newBufferedReader(localePath)) { + mcMMO.p.getLogger().log(Level.INFO, "Loading locale from {0}", localePath); filesystemBundle = new PropertyResourceBundle(localeReader); } catch (IOException e) { mcMMO.p.getLogger().log(Level.WARNING, "Failed to load locale from " + localePath, e); From 40ea101bf6ed9ea9fc75924e4ccd6051ada090e2 Mon Sep 17 00:00:00 2001 From: Mark Vainomaa Date: Tue, 30 Apr 2019 21:19:55 +0300 Subject: [PATCH 7/7] Add a command to reload locale --- .../admin/McmmoReloadLocaleCommand.java | 30 +++++++++++++++++++ .../gmail/nossr50/locale/LocaleLoader.java | 11 +++++++ .../com/gmail/nossr50/util/Permissions.java | 1 + .../commands/CommandRegistrationManager.java | 13 ++++++++ .../resources/locale/locale_en_US.properties | 4 ++- src/main/resources/plugin.yml | 5 ++++ 6 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/gmail/nossr50/commands/admin/McmmoReloadLocaleCommand.java diff --git a/src/main/java/com/gmail/nossr50/commands/admin/McmmoReloadLocaleCommand.java b/src/main/java/com/gmail/nossr50/commands/admin/McmmoReloadLocaleCommand.java new file mode 100644 index 000000000..da15b8297 --- /dev/null +++ b/src/main/java/com/gmail/nossr50/commands/admin/McmmoReloadLocaleCommand.java @@ -0,0 +1,30 @@ +package com.gmail.nossr50.commands.admin; + +import com.gmail.nossr50.locale.LocaleLoader; +import com.gmail.nossr50.util.Permissions; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; + +/** + * @author Mark Vainomaa + */ +public final class McmmoReloadLocaleCommand implements CommandExecutor { + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + switch (args.length) { + case 0: + if (!Permissions.reloadlocale(sender)) { + sender.sendMessage(command.getPermissionMessage()); + return true; + } + + LocaleLoader.reloadLocale(); + sender.sendMessage(LocaleLoader.getString("Locale.Reloaded")); + + return true; + default: + return false; + } + } +} diff --git a/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java b/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java index 15b31c36b..2bde9ea52 100644 --- a/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java +++ b/src/main/java/com/gmail/nossr50/locale/LocaleLoader.java @@ -47,6 +47,17 @@ public final class LocaleLoader { return formatString(rawMessage, messageArguments); } + /** + * Reloads locale + */ + public static void reloadLocale() { + bundle = null; + filesystemBundle = null; + enBundle = null; + bundleCache = new HashMap<>(); // Cheaper to replace than clear() + initialize(); + } + private static String getRawString(String key) { if (filesystemBundle != null) { try { diff --git a/src/main/java/com/gmail/nossr50/util/Permissions.java b/src/main/java/com/gmail/nossr50/util/Permissions.java index dbbeef94f..cd5118e3b 100644 --- a/src/main/java/com/gmail/nossr50/util/Permissions.java +++ b/src/main/java/com/gmail/nossr50/util/Permissions.java @@ -101,6 +101,7 @@ public final class Permissions { public static boolean mcpurge(Permissible permissible) { return permissible.hasPermission("mcmmo.commands.mcpurge"); } public static boolean mcremove(Permissible permissible) { return permissible.hasPermission("mcmmo.commands.mcremove"); } public static boolean mmoupdate(Permissible permissible) { return permissible.hasPermission("mcmmo.commands.mmoupdate"); } + public static boolean reloadlocale(Permissible permissible) { return permissible.hasPermission("mcmmo.commands.reloadlocale"); } /* * PERKS diff --git a/src/main/java/com/gmail/nossr50/util/commands/CommandRegistrationManager.java b/src/main/java/com/gmail/nossr50/util/commands/CommandRegistrationManager.java index d05de745b..2d3ab2360 100644 --- a/src/main/java/com/gmail/nossr50/util/commands/CommandRegistrationManager.java +++ b/src/main/java/com/gmail/nossr50/util/commands/CommandRegistrationManager.java @@ -1,6 +1,7 @@ package com.gmail.nossr50.util.commands; import com.gmail.nossr50.commands.*; +import com.gmail.nossr50.commands.admin.McmmoReloadLocaleCommand; import com.gmail.nossr50.commands.chat.AdminChatCommand; import com.gmail.nossr50.commands.chat.McChatSpy; import com.gmail.nossr50.commands.chat.PartyChatCommand; @@ -400,6 +401,15 @@ public final class CommandRegistrationManager { command.setExecutor(new McImportCommand()); } + private static void registerReloadLocaleCommand() { + PluginCommand command = mcMMO.p.getCommand("mcmmoreloadlocale"); + command.setDescription("Reloads locale"); // TODO: Localize + command.setPermission("mcmmo.commands.reloadlocale"); + command.setPermissionMessage(permissionsMessage); + command.setUsage(LocaleLoader.formatString("Commands.Usage.0", "mcmmoreloadlocale")); + command.setExecutor(new McmmoReloadLocaleCommand()); + } + public static void registerCommands() { // Generic Commands registerMmoInfoCommand(); @@ -447,5 +457,8 @@ public final class CommandRegistrationManager { // Skill Commands registerSkillCommands(); + + // Admin commands + registerReloadLocaleCommand(); } } diff --git a/src/main/resources/locale/locale_en_US.properties b/src/main/resources/locale/locale_en_US.properties index c9e1f4e82..c4d6876e7 100644 --- a/src/main/resources/locale/locale_en_US.properties +++ b/src/main/resources/locale/locale_en_US.properties @@ -1076,4 +1076,6 @@ Profile.Loading.FailureNotice=[[DARK_RED]][A][[RED]] mcMMO was unable to load th Holiday.AprilFools.Levelup=[[GOLD]]{0} is now level [[GREEN]]{1}[[GOLD]]! Holiday.Anniversary=[[BLUE]]Happy {0} Year Anniversary!\n[[BLUE]]In honor of all of nossr50's work and all the devs, here's a firework show! #Reminder Messages -Reminder.Squelched=[[GRAY]]Reminder: You are currently not receiving notifications from mcMMO, to enable notifications please run the /mcnotify command again. This is an automated hourly reminder. \ No newline at end of file +Reminder.Squelched=[[GRAY]]Reminder: You are currently not receiving notifications from mcMMO, to enable notifications please run the /mcnotify command again. This is an automated hourly reminder. +#Locale +Locale.Reloaded=[[GREEN]]Locale reloaded! \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index a42dbfbfd..1da6ec409 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -168,6 +168,10 @@ commands: aliases: [macho, jumping, throwing, wrecking, crafting, walking, swimming, falling, climbing, flying, diving, piggy] description: Deploy jokes permission: mcmmo.commands.mcfools + mcmmoreloadlocale: + aliases: [mcreloadlocale] + description: Reloads locale + permission: mcmmo.commands.reloadlocale permissions: mcmmo.*: default: false @@ -807,6 +811,7 @@ permissions: mcmmo.commands.mmoedit.others: true mcmmo.commands.mmoshowdb: true mcmmo.commands.ptp.world.all: true + mcmmo.commands.reloadlocale: true mcmmo.commands.skillreset.all: true mcmmo.commands.vampirism.all: true mcmmo.commands.xprate.all: true