Fix TLocale

This commit is contained in:
sky 2020-01-16 12:25:56 +08:00
parent 00416255f1
commit 3831158a6b
4 changed files with 47 additions and 24 deletions

View File

@ -64,10 +64,22 @@ public class TConfigWatcher {
addOnListen(file, obj, (Consumer<Object>) consumer); addOnListen(file, obj, (Consumer<Object>) consumer);
} }
public boolean hasListener(File file) {
synchronized (map) {
return map.values().stream().anyMatch(t -> t.getLeft().getPath().equals(file.getPath()));
}
}
public void runListener(File file) {
synchronized (map) {
map.values().stream().filter(t -> t.getLeft().getPath().equals(file.getPath())).forEach(f -> f.getRight().accept(null));
}
}
public void removeListener(File file) { public void removeListener(File file) {
synchronized (map) { synchronized (map) {
map.entrySet().removeIf(entry -> { map.entrySet().removeIf(entry -> {
if (entry.getValue().getLeft().equals(file)) { if (entry.getValue().getLeft().getPath().equals(file.getPath())) {
try { try {
entry.getKey().close(); entry.getKey().close();
} catch (IOException ignored) { } catch (IOException ignored) {

View File

@ -68,13 +68,9 @@ public class TInjectLoader implements TabooLibLoader.Loader {
} else { } else {
localePriority.add(String.valueOf(config.get(args.locale()))); localePriority.add(String.valueOf(config.get(args.locale())));
} }
if (TLocaleLoader.getLocalePriority(plugin).equals(localePriority)) {
return;
}
TLocaleLoader.setLocalePriority(plugin, localePriority); TLocaleLoader.setLocalePriority(plugin, localePriority);
TLocaleLoader.load(plugin, true, true); TLocaleLoader.load(plugin, true, true);
}); }).runListener();
config.runListener();
} }
if (Strings.nonEmpty(args.reload())) { if (Strings.nonEmpty(args.reload())) {
try { try {

View File

@ -19,6 +19,7 @@ import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.*; import java.util.*;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
public class TLocaleLoader { public class TLocaleLoader {
@ -81,22 +82,22 @@ public class TLocaleLoader {
public static void load(Plugin plugin, boolean isCover, boolean hideMessage) { public static void load(Plugin plugin, boolean isCover, boolean hideMessage) {
try { try {
if (isLoadLocale(plugin, isCover)) { if (isLoadLocale(plugin, isCover)) {
// 获取文件 for (File localeFile : getLocaleFile(plugin)) {
File localeFile = getLocaleFile(plugin); if (!TConfigWatcher.getInst().hasListener(localeFile)) {
if (localeFile == null) { Runnable listener = () -> {
return; if (localeFile.getName().equals(getLocalPriorityFirst(plugin) + ".yml")) {
YamlConfiguration localeConfiguration = Files.loadYaml(localeFile);
YamlConfiguration localeConfigurationAtStream = getLocaleAsPlugin(plugin, localeFile);
loadPluginLocale(plugin, localeFile, localeConfiguration, localeConfigurationAtStream, hideMessage);
}
};
TConfigWatcher.getInst().addListener(localeFile, null, obj -> listener.run());
}
TConfigWatcher.getInst().runListener(localeFile);
} }
// 加载文件
YamlConfiguration localeConfiguration = Files.loadYaml(localeFile);
YamlConfiguration localeConfigurationAtStream = getLocaleAsPlugin(plugin, localeFile);
// 载入配置
loadPluginLocale(plugin, localeFile, localeConfiguration, localeConfigurationAtStream, hideMessage);
// 注册监听
TConfigWatcher.getInst().removeListener(localeFile);
TConfigWatcher.getInst().addListener(localeFile, null, obj -> loadPluginLocale(plugin, localeFile, Files.loadYaml(localeFile), getLocaleAsPlugin(plugin, localeFile), false));
} }
} catch (Exception e) { } catch (Exception e) {
errorLogger("ERROR-LOADING-LANG", plugin.getName(), e.toString() + "\n" + e.getStackTrace()[0].toString()); errorLogger(plugin.getName(), e.toString() + "\n" + e.getStackTrace()[0].toString());
} }
} }
@ -112,6 +113,11 @@ public class TLocaleLoader {
return plugin.getClass().getSuperclass().getSimpleName().equals("TabooPlugin"); return plugin.getClass().getSuperclass().getSimpleName().equals("TabooPlugin");
} }
public static String getLocalPriorityFirst(Plugin plugin) {
List<String> localePriority = getLocalePriority(plugin);
return localePriority.isEmpty() ? "zh_CN" : localePriority.get(0);
}
public static List<String> getLocalePriority(Plugin plugin) { public static List<String> getLocalePriority(Plugin plugin) {
return localePriority.getOrDefault(plugin.getName(), TabooLib.getConfig().contains("LOCALE.PRIORITY") ? TabooLib.getConfig().getStringList("LOCALE.PRIORITY") : Collections.singletonList("zh_CN")); return localePriority.getOrDefault(plugin.getName(), TabooLib.getConfig().contains("LOCALE.PRIORITY") ? TabooLib.getConfig().getStringList("LOCALE.PRIORITY") : Collections.singletonList("zh_CN"));
} }
@ -128,13 +134,17 @@ public class TLocaleLoader {
TLogger.getGlobalLogger().info(Strings.replaceWithOrder(TabooLib.getInst().getInternal().getString(path), args)); TLogger.getGlobalLogger().info(Strings.replaceWithOrder(TabooLib.getInst().getInternal().getString(path), args));
} }
private static void errorLogger(String path, String... args) { private static void errorLogger(String... args) {
TLogger.getGlobalLogger().error(Strings.replaceWithOrder(TabooLib.getInst().getInternal().getString(path), args)); TLogger.getGlobalLogger().error(Strings.replaceWithOrder(TabooLib.getInst().getInternal().getString("ERROR-LOADING-LANG"), args));
} }
private static File getLocaleFile(Plugin plugin) { private static File getLocalFile(Plugin plugin, String locale) {
getLocalePriority(plugin).forEach(localeName -> Files.releaseResource(plugin, "lang/" + localeName + ".yml", false)); List<File> localeFile = getLocaleFile(plugin);
return getLocalePriority(plugin).stream().map(localeName -> new File("plugins/" + plugin.getName() + "/lang/" + localeName + ".yml")).filter(File::exists).findFirst().orElse(null); return localeFile.stream().filter(f -> f.getName().equals(localeFile + ".yml")).findFirst().orElse(null);
}
private static List<File> getLocaleFile(Plugin plugin) {
return getLocalePriority(plugin).stream().map(file -> Files.releaseResource(plugin, "lang/" + file + ".yml")).filter(File::exists).collect(Collectors.toList());
} }
private static TLocaleInstance getLocaleInstance(Plugin plugin) { private static TLocaleInstance getLocaleInstance(Plugin plugin) {

View File

@ -86,6 +86,11 @@ public class Files {
return null; return null;
} }
public static File releaseResource(Plugin plugin, String path) {
releaseResource(plugin, path, false);
return new File(plugin.getDataFolder(), path);
}
public static void releaseResource(Plugin plugin, String path, boolean replace) { public static void releaseResource(Plugin plugin, String path, boolean replace) {
File file = new File(plugin.getDataFolder(), path); File file = new File(plugin.getDataFolder(), path);
if (!file.exists() || replace) { if (!file.exists() || replace) {