一棒打死重构改名的
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package com.ilummc.tlib.resources;
|
||||
|
||||
import com.google.common.io.Files;
|
||||
import com.ilummc.tlib.TLib;
|
||||
import com.ilummc.tlib.annotations.TLocalePlugin;
|
||||
import com.ilummc.tlib.logger.TLogger;
|
||||
@@ -14,14 +15,14 @@ import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.configuration.serialization.ConfigurationSerialization;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.yaml.snakeyaml.DumperOptions;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class TLocaleLoader {
|
||||
@@ -75,23 +76,20 @@ public class TLocaleLoader {
|
||||
if (isLoadLocale(plugin, isCover)) {
|
||||
// 获取文件
|
||||
File localeFile = getLocaleFile(plugin);
|
||||
if (localeFile == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 加载文件
|
||||
infoLogger("TRY-LOADING-LANG", plugin.getName(), localeFile.getName());
|
||||
YamlConfiguration localeConfiguration = ConfigUtils.loadYaml(plugin, localeFile);
|
||||
YamlConfiguration localeConfigurationAtStream = getLocaleAtStream(plugin, localeFile);
|
||||
Map<String, Object> originMap = getLocaleAtStream(plugin, localeFile);
|
||||
|
||||
TLib.getTLib().getConfigWatcher().removeListener(localeFile);
|
||||
|
||||
// 载入配置
|
||||
loadPluginLocale(plugin, localeFile, localeConfiguration, localeConfigurationAtStream);
|
||||
updateAndLoad(plugin, localeFile, originMap);
|
||||
|
||||
// 注册监听
|
||||
TLib.getTLib().getConfigWatcher().removeListener(localeFile);
|
||||
TLib.getTLib().getConfigWatcher().addListener(localeFile, null, obj -> {
|
||||
infoLogger("RELOADING-LANG", plugin.getName());
|
||||
loadPluginLocale(plugin, localeFile, ConfigUtils.loadYaml(plugin, localeFile), getLocaleAtStream(plugin, localeFile));
|
||||
updateAndLoad(plugin, localeFile, getLocaleAtStream(plugin, localeFile));
|
||||
});
|
||||
}
|
||||
} catch (Exception e) {
|
||||
@@ -117,22 +115,22 @@ public class TLocaleLoader {
|
||||
|
||||
private static File getLocaleFile(Plugin plugin) {
|
||||
releaseLocales(plugin);
|
||||
return getLocalePriority().stream().map(localeName -> new File(plugin.getDataFolder(), "lang/" + localeName + ".yml")).filter(File::exists).findFirst().orElse(null);
|
||||
return getLocalePriority().stream().map(localeName -> new File(plugin.getDataFolder(), "lang/" + localeName + ".yml")).filter(File::exists).findFirst().orElseThrow(NullPointerException::new);
|
||||
}
|
||||
|
||||
private static void releaseLocales(Plugin plugin) {
|
||||
getLocalePriority().stream().filter(localeName -> !new File(plugin.getDataFolder(), "lang/" + localeName + ".yml").exists() && plugin.getResource("lang/" + localeName + ".yml") != null).forEach(localeName -> plugin.saveResource("lang/" + localeName + ".yml", true));
|
||||
}
|
||||
|
||||
public static boolean isLocaleLoaded(Plugin plugin) {
|
||||
private static boolean isLocaleLoaded(Plugin plugin) {
|
||||
return map.containsKey(plugin.getName());
|
||||
}
|
||||
|
||||
public static boolean isDependWithTabooLib(Plugin plugin) {
|
||||
private static boolean isDependWithTabooLib(Plugin plugin) {
|
||||
return plugin.getClass().getAnnotation(TLocalePlugin.class) != null || plugin.getDescription().getDepend().contains(Main.getInst().getName()) || plugin.getDescription().getSoftDepend().contains(Main.getInst().getName());
|
||||
}
|
||||
|
||||
public static List<String> getLocalePriority() {
|
||||
private static List<String> getLocalePriority() {
|
||||
return Main.getInst().getConfig().contains("LOCALE.PRIORITY") ? Main.getInst().getConfig().getStringList("LOCALE.PRIORITY") : Collections.singletonList("zh_CN");
|
||||
}
|
||||
|
||||
@@ -142,29 +140,63 @@ public class TLocaleLoader {
|
||||
return instance;
|
||||
}
|
||||
|
||||
private static YamlConfiguration getLocaleAtStream(Plugin plugin, File localeFile) {
|
||||
private static Map<String, Object> getLocaleAtStream(Plugin plugin, File localeFile) {
|
||||
InputStream localeInputSteam = plugin.getClass().getResourceAsStream("/lang/" + localeFile.getName());
|
||||
try {
|
||||
String yamlText = new String(IO.readFully(localeInputSteam), Charset.forName("utf-8"));
|
||||
YamlConfiguration yaml = new YamlConfiguration();
|
||||
yaml.loadFromString(yamlText);
|
||||
return yaml;
|
||||
} catch (Exception ignored) {
|
||||
return null;
|
||||
Object load = new Yaml().load(yamlText);
|
||||
return load instanceof Map ? (Map<String, Object>) load : new HashMap<>(0);
|
||||
} catch (Exception e) {
|
||||
return new HashMap<>(0);
|
||||
}
|
||||
}
|
||||
|
||||
private static void loadPluginLocale(Plugin plugin, File localeFile, YamlConfiguration localeConfiguration, YamlConfiguration localeConfigurationAtStream) {
|
||||
TLocaleInstance localeInstance = getLocaleInstance(plugin);
|
||||
boolean versionOutOfDate = isVersionOutOfDate(localeConfiguration, localeConfigurationAtStream);
|
||||
if (versionOutOfDate) {
|
||||
localeInstance.load(localeConfigurationAtStream);
|
||||
private static Map<String, Object> currentLocaleMap(File localeFile) {
|
||||
try {
|
||||
Object load = new Yaml().load(Files.toString(localeFile, Charset.forName("utf-8")));
|
||||
return load instanceof Map ? (Map<String, Object>) load : new HashMap<>(0);
|
||||
} catch (Exception e) {
|
||||
return new HashMap<>(0);
|
||||
}
|
||||
}
|
||||
|
||||
private static int compareAndSet(Map<String, Object> origin, Map<String, Object> current, File file) {
|
||||
int i = compareMaps(origin, current);
|
||||
DumperOptions options = new DumperOptions();
|
||||
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
||||
options.setAllowUnicode(false);
|
||||
Yaml yaml = new Yaml(options);
|
||||
String dump = yaml.dump(current);
|
||||
try {
|
||||
Files.write(dump.getBytes(Charset.forName("utf-8")), file);
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static int compareMaps(Map<String, Object> origin, Map<String, Object> current) {
|
||||
int res = 0;
|
||||
for (Map.Entry<String, Object> entry : origin.entrySet()) {
|
||||
if (current.putIfAbsent(entry.getKey(), entry.getValue()) != null) {
|
||||
if (entry.getValue() instanceof Map && !((Map) entry.getValue()).containsKey("==") && current.get(entry.getKey()) instanceof Map) {
|
||||
res += compareMaps((Map<String, Object>) entry.getValue(), (Map<String, Object>) current.get(entry.getKey()));
|
||||
}
|
||||
} else ++res;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
private static void updateAndLoad(Plugin plugin, File localeFile, Map<String, Object> originMap) {
|
||||
Map<String, Object> currentMap = currentLocaleMap(localeFile);
|
||||
int update = compareAndSet(originMap, currentMap, localeFile);
|
||||
TLocaleInstance localeInstance = getLocaleInstance(plugin);
|
||||
YamlConfiguration localeConfiguration = ConfigUtils.loadYaml(plugin, localeFile);
|
||||
localeInstance.load(localeConfiguration);
|
||||
if (!versionOutOfDate || localeInstance.size() - localeInstance.getUpdateNodes() == 0) {
|
||||
if (update == 0) {
|
||||
infoLogger("SUCCESS-LOADING-LANG-NORMAL", plugin.getName(), localeFile.getName().split("\\.")[0], String.valueOf(localeInstance.size()));
|
||||
} else {
|
||||
infoLogger("SUCCESS-LOADING-LANG-UPDATE", plugin.getName(), localeFile.getName().split("\\.")[0], String.valueOf(localeInstance.size()), String.valueOf(localeInstance.size() - localeInstance.getUpdateNodes()));
|
||||
infoLogger("SUCCESS-LOADING-LANG-UPDATE", plugin.getName(), localeFile.getName().split("\\.")[0], String.valueOf(localeInstance.size()), String.valueOf(update));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user