Fix TLocale
This commit is contained in:
parent
00416255f1
commit
3831158a6b
@ -64,10 +64,22 @@ public class TConfigWatcher {
|
||||
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) {
|
||||
synchronized (map) {
|
||||
map.entrySet().removeIf(entry -> {
|
||||
if (entry.getValue().getLeft().equals(file)) {
|
||||
if (entry.getValue().getLeft().getPath().equals(file.getPath())) {
|
||||
try {
|
||||
entry.getKey().close();
|
||||
} catch (IOException ignored) {
|
||||
|
@ -68,13 +68,9 @@ public class TInjectLoader implements TabooLibLoader.Loader {
|
||||
} else {
|
||||
localePriority.add(String.valueOf(config.get(args.locale())));
|
||||
}
|
||||
if (TLocaleLoader.getLocalePriority(plugin).equals(localePriority)) {
|
||||
return;
|
||||
}
|
||||
TLocaleLoader.setLocalePriority(plugin, localePriority);
|
||||
TLocaleLoader.load(plugin, true, true);
|
||||
});
|
||||
config.runListener();
|
||||
}).runListener();
|
||||
}
|
||||
if (Strings.nonEmpty(args.reload())) {
|
||||
try {
|
||||
|
@ -19,6 +19,7 @@ import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class TLocaleLoader {
|
||||
|
||||
@ -81,22 +82,22 @@ public class TLocaleLoader {
|
||||
public static void load(Plugin plugin, boolean isCover, boolean hideMessage) {
|
||||
try {
|
||||
if (isLoadLocale(plugin, isCover)) {
|
||||
// 获取文件
|
||||
File localeFile = getLocaleFile(plugin);
|
||||
if (localeFile == null) {
|
||||
return;
|
||||
for (File localeFile : getLocaleFile(plugin)) {
|
||||
if (!TConfigWatcher.getInst().hasListener(localeFile)) {
|
||||
Runnable listener = () -> {
|
||||
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) {
|
||||
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");
|
||||
}
|
||||
|
||||
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) {
|
||||
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));
|
||||
}
|
||||
|
||||
private static void errorLogger(String path, String... args) {
|
||||
TLogger.getGlobalLogger().error(Strings.replaceWithOrder(TabooLib.getInst().getInternal().getString(path), args));
|
||||
private static void errorLogger(String... args) {
|
||||
TLogger.getGlobalLogger().error(Strings.replaceWithOrder(TabooLib.getInst().getInternal().getString("ERROR-LOADING-LANG"), args));
|
||||
}
|
||||
|
||||
private static File getLocaleFile(Plugin plugin) {
|
||||
getLocalePriority(plugin).forEach(localeName -> Files.releaseResource(plugin, "lang/" + localeName + ".yml", false));
|
||||
return getLocalePriority(plugin).stream().map(localeName -> new File("plugins/" + plugin.getName() + "/lang/" + localeName + ".yml")).filter(File::exists).findFirst().orElse(null);
|
||||
private static File getLocalFile(Plugin plugin, String locale) {
|
||||
List<File> localeFile = getLocaleFile(plugin);
|
||||
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) {
|
||||
|
@ -86,6 +86,11 @@ public class Files {
|
||||
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) {
|
||||
File file = new File(plugin.getDataFolder(), path);
|
||||
if (!file.exists() || replace) {
|
||||
|
Loading…
Reference in New Issue
Block a user