Merge 2.1.51

This commit is contained in:
nossr50 2019-04-30 17:01:45 -07:00
commit 3997a44fce
7 changed files with 119 additions and 17 deletions

View File

@ -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;
}
}
}

View File

@ -3,14 +3,25 @@ package com.gmail.nossr50.locale;
import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.mcMMO;
import org.bukkit.ChatColor; 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.text.MessageFormat;
import java.util.HashMap;
import java.util.Locale; import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException; import java.util.MissingResourceException;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import java.util.logging.Level;
public final class LocaleLoader { public final class LocaleLoader {
private static final String BUNDLE_ROOT = "com.gmail.nossr50.locale.locale"; private static final String BUNDLE_ROOT = "com.gmail.nossr50.locale.locale";
private static Map<String, String> bundleCache = new HashMap<>();
private static ResourceBundle bundle = null; private static ResourceBundle bundle = null;
private static ResourceBundle filesystemBundle = null;
private static ResourceBundle enBundle = null; private static ResourceBundle enBundle = null;
private LocaleLoader() { private LocaleLoader() {
@ -32,12 +43,38 @@ public final class LocaleLoader {
initialize(); initialize();
} }
String rawMessage = bundleCache.computeIfAbsent(key, LocaleLoader::getRawString);
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 { try {
return getString(key, bundle, messageArguments); return filesystemBundle.getString(key);
} catch (MissingResourceException ex) { }
catch (MissingResourceException ignored) {}
}
try { try {
return getString(key, enBundle, messageArguments); return bundle.getString(key);
} catch (MissingResourceException ex2) { }
catch (MissingResourceException ignored) {}
try {
return enBundle.getString(key);
}
catch (MissingResourceException ignored) {
if (!key.contains("Guides")) { if (!key.contains("Guides")) {
mcMMO.p.getLogger().warning("Could not find locale string: " + key); mcMMO.p.getLogger().warning("Could not find locale string: " + key);
} }
@ -45,11 +82,6 @@ public final class LocaleLoader {
return '!' + key + '!'; return '!' + key + '!';
} }
} }
}
private static String getString(String key, ResourceBundle bundle, Object... messageArguments) throws MissingResourceException {
return formatString(bundle.getString(key), messageArguments);
}
public static String formatString(String string, Object... messageArguments) { public static String formatString(String string, Object... messageArguments) {
if (messageArguments != null) { if (messageArguments != null) {
@ -82,6 +114,19 @@ public final class LocaleLoader {
locale = new Locale(myLocale[0], myLocale[1]); locale = new Locale(myLocale[0], myLocale[1]);
} }
if (locale == null) {
throw new IllegalStateException("Failed to parse locale string '" + mcMMO.getConfigManager().getConfigLanguage().getTargetLanguage() + "'");
}
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);
}
}
bundle = ResourceBundle.getBundle(BUNDLE_ROOT, locale); bundle = ResourceBundle.getBundle(BUNDLE_ROOT, locale);
enBundle = ResourceBundle.getBundle(BUNDLE_ROOT, Locale.US); enBundle = ResourceBundle.getBundle(BUNDLE_ROOT, Locale.US);
} }

View File

@ -90,6 +90,7 @@ public class mcMMO extends JavaPlugin {
private static MaterialMapStore materialMapStore; private static MaterialMapStore materialMapStore;
/* File Paths */ /* File Paths */
private static String mainDirectory; private static String mainDirectory;
private static String localesDirectory;
private static String flatFileDirectory; private static String flatFileDirectory;
private static String usersFile; private static String usersFile;
private static String modDirectory; private static String modDirectory;
@ -421,13 +422,16 @@ public class mcMMO extends JavaPlugin {
} }
databaseManager.onDisable(); databaseManager.onDisable();
//Unload configs last //Unload configs last
configManager.unloadAllConfigsAndRegisters(); configManager.unloadAllConfigsAndRegisters();
debug("Was disabled."); // How informative! debug("Was disabled."); // How informative!
} }
public static String getLocalesDirectory() {
return localesDirectory;
}
public boolean isXPEventEnabled() { public boolean isXPEventEnabled() {
return xpEventEnabled; return xpEventEnabled;
} }
@ -458,6 +462,7 @@ public class mcMMO extends JavaPlugin {
private void setupFilePaths() { private void setupFilePaths() {
mcmmo = getFile(); mcmmo = getFile();
mainDirectory = getDataFolder().getPath() + File.separator; mainDirectory = getDataFolder().getPath() + File.separator;
localesDirectory = mainDirectory + "locales" + File.separator;
flatFileDirectory = mainDirectory + "flatfile" + File.separator; flatFileDirectory = mainDirectory + "flatfile" + File.separator;
usersFile = flatFileDirectory + "mcmmo.users"; usersFile = flatFileDirectory + "mcmmo.users";
modDirectory = mainDirectory + "mods" + File.separator; modDirectory = mainDirectory + "mods" + File.separator;
@ -511,6 +516,8 @@ public class mcMMO extends JavaPlugin {
File currentFlatfilePath = new File(flatFileDirectory); File currentFlatfilePath = new File(flatFileDirectory);
currentFlatfilePath.mkdirs(); currentFlatfilePath.mkdirs();
File localesDirectoryPath = new File(localesDirectory);
localesDirectoryPath.mkdirs();
} }
private void loadConfigFiles() { private void loadConfigFiles() {

View File

@ -233,6 +233,8 @@ public final class Permissions {
return permissible.hasPermission("mcmmo.commands.mmoupdate"); return permissible.hasPermission("mcmmo.commands.mmoupdate");
} }
public static boolean reloadlocale(Permissible permissible) { return permissible.hasPermission("mcmmo.commands.reloadlocale"); }
/* /*
* PERKS * PERKS
*/ */

View File

@ -1,6 +1,7 @@
package com.gmail.nossr50.util.commands; package com.gmail.nossr50.util.commands;
import com.gmail.nossr50.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.AdminChatCommand;
import com.gmail.nossr50.commands.chat.McChatSpy; import com.gmail.nossr50.commands.chat.McChatSpy;
import com.gmail.nossr50.commands.chat.PartyChatCommand; import com.gmail.nossr50.commands.chat.PartyChatCommand;
@ -408,6 +409,15 @@ public final class CommandRegistrationManager {
command.setExecutor(new McImportCommand()); 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.getString("Commands.Usage.0", "mcmmoreloadlocale"));
command.setExecutor(new McmmoReloadLocaleCommand());
}
public static void registerCommands() { public static void registerCommands() {
// Generic Commands // Generic Commands
registerMmoInfoCommand(); registerMmoInfoCommand();
@ -458,5 +468,7 @@ public final class CommandRegistrationManager {
//Config Commands //Config Commands
registerMcmmoReloadCommand(); registerMcmmoReloadCommand();
// Admin commands
registerReloadLocaleCommand();
} }
} }

View File

@ -1086,3 +1086,4 @@ Reminder.Squelched=[[GRAY]]Reminder: You are currently not receiving notificatio
#Misc #Misc
Commands.Reload.Start=mcMMO is reloading... this may take a moment Commands.Reload.Start=mcMMO is reloading... this may take a moment
Commands.Reload.Finished=mcMMO has finished reloading! Commands.Reload.Finished=mcMMO has finished reloading!
Locale.Reloaded=[[GREEN]]Locale reloaded!

View File

@ -163,6 +163,10 @@ commands:
aliases: [macho, jumping, throwing, wrecking, crafting, walking, swimming, falling, climbing, flying, diving, piggy] aliases: [macho, jumping, throwing, wrecking, crafting, walking, swimming, falling, climbing, flying, diving, piggy]
description: Deploy jokes description: Deploy jokes
permission: mcmmo.commands.mcfools permission: mcmmo.commands.mcfools
mcmmoreloadlocale:
aliases: [mcreloadlocale]
description: Reloads locale
permission: mcmmo.commands.reloadlocale
permissions: permissions:
mcmmo.*: mcmmo.*:
default: false default: false
@ -800,6 +804,7 @@ permissions:
mcmmo.commands.mmoedit.others: true mcmmo.commands.mmoedit.others: true
mcmmo.commands.mmoshowdb: true mcmmo.commands.mmoshowdb: true
mcmmo.commands.ptp.world.all: true mcmmo.commands.ptp.world.all: true
mcmmo.commands.reloadlocale: true
mcmmo.commands.skillreset.all: true mcmmo.commands.skillreset.all: true
mcmmo.commands.xprate.all: true mcmmo.commands.xprate.all: true
mcmmo.commands.acrobatics: mcmmo.commands.acrobatics: