From 40ea101bf6ed9ea9fc75924e4ccd6051ada090e2 Mon Sep 17 00:00:00 2001 From: Mark Vainomaa Date: Tue, 30 Apr 2019 21:19:55 +0300 Subject: [PATCH] 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