This commit is contained in:
坏黑 2018-05-12 22:14:10 +08:00
parent 71547865f9
commit eb616fd5bd
3 changed files with 60 additions and 84 deletions

View File

@ -15,7 +15,7 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
@ThreadSafe
@ -24,6 +24,7 @@ class TLocaleInstance {
private final Map<String, List<TLocaleSerialize>> map = new HashMap<>();
private final Plugin plugin;
private final AtomicInteger latestUpdateNodes = new AtomicInteger();
TLocaleInstance(Plugin plugin) {
this.plugin = plugin;
@ -46,6 +47,10 @@ class TLocaleInstance {
return plugin;
}
public AtomicInteger getLatestUpdateNodes() {
return latestUpdateNodes;
}
public void sendTo(String path, CommandSender sender, String... args) {
try {
map.getOrDefault(path, ImmutableList.of(TLocaleSerialize.getEmpty(path))).forEach(tSender -> {
@ -80,20 +85,34 @@ class TLocaleInstance {
}
public void load(YamlConfiguration configuration) {
configuration.getKeys(true).forEach(s -> {
Object object = configuration.get(s);
if (object instanceof TLocaleSerialize) {
map.put(s, Collections.singletonList((TLocaleSerialize) object));
} else if (object instanceof List && !((List) object).isEmpty()) {
if (isListString((List) object)) {
map.put(s, Collections.singletonList(TLocaleText.of(object)));
load(configuration, false);
}
public void load(YamlConfiguration configuration, boolean cleanup) {
int originNodes = map.size();
int updateNodes = 0;
if (cleanup) {
map.clear();
}
for (String s : configuration.getKeys(true)) {
boolean updated = false;
Object value = configuration.get(s);
if (value instanceof TLocaleSerialize) {
updated = map.put(s, Collections.singletonList((TLocaleSerialize) value)) != null;
} else if (value instanceof List && !((List) value).isEmpty()) {
if (isListString((List) value)) {
updated = map.put(s, Collections.singletonList(TLocaleText.of(value))) != null;
} else {
map.put(s, ((List<?>) object).stream().map(o -> o instanceof TLocaleSerialize ? (TLocaleSerialize) o : TLocaleText.of(String.valueOf(o))).collect(Collectors.toList()));
updated = map.put(s, ((List<?>) value).stream().map(o -> o instanceof TLocaleSerialize ? (TLocaleSerialize) o : TLocaleText.of(String.valueOf(o))).collect(Collectors.toList())) != null;
}
} else if (!(object instanceof ConfigurationSection)) {
String str = String.valueOf(object);
map.put(s, Collections.singletonList(str.length() == 0 ? TLocaleSerialize.getEmpty() : TLocaleText.of(str)));
} else if (!(value instanceof ConfigurationSection)) {
String str = String.valueOf(value);
updated = map.put(s, Collections.singletonList(str.length() == 0 ? TLocaleSerialize.getEmpty() : TLocaleText.of(str))) != null;
}
});
if (updated) {
updateNodes++;
}
}
latestUpdateNodes.set(originNodes - updateNodes);
}
}

View File

@ -1,6 +1,5 @@
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,13 +13,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.Yaml;
import sun.security.krb5.Config;
import java.io.File;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.*;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
public class TLocaleLoader {
@ -74,20 +74,23 @@ public class TLocaleLoader {
if (isLoadLocale(plugin, isCover)) {
// 获取文件
File localeFile = getLocaleFile(plugin);
if (localeFile == null) {
return;
}
// 加载文件
infoLogger("TRY-LOADING-LANG", plugin.getName(), localeFile.getName());
Map<String, Object> originMap = getLocaleAtStream(plugin, localeFile);
TLib.getTLib().getConfigWatcher().removeListener(localeFile);
YamlConfiguration localeConfiguration = ConfigUtils.loadYaml(plugin, localeFile);
YamlConfiguration localeConfigurationAtStream = getLocaleAtStream(plugin, localeFile);
// 载入配置
updateAndLoad(plugin, localeFile, originMap);
loadPluginLocale(plugin, localeFile, localeConfiguration, localeConfigurationAtStream);
// 注册监听
TLib.getTLib().getConfigWatcher().removeListener(localeFile);
TLib.getTLib().getConfigWatcher().addListener(localeFile, null, obj -> {
infoLogger("RELOADING-LANG", plugin.getName());
updateAndLoad(plugin, localeFile, getLocaleAtStream(plugin, localeFile));
loadPluginLocale(plugin, localeFile, ConfigUtils.loadYaml(plugin, localeFile), getLocaleAtStream(plugin, localeFile));
});
}
} catch (Exception e) {
@ -107,28 +110,24 @@ public class TLocaleLoader {
TLogger.getGlobalLogger().error(Strings.replaceWithOrder(TLib.getInternalLanguage().getString(path), args));
}
private static boolean isVersionOutOfDate(YamlConfiguration configuration, YamlConfiguration configurationAtSteam) {
return configurationAtSteam != null && configurationAtSteam.getDouble("VERSION", 0) > configuration.getDouble("VERSION", 0);
}
private static File getLocaleFile(Plugin plugin) {
releaseLocales(plugin);
return getLocalePriority().stream().map(localeName -> new File(plugin.getDataFolder(), "lang/" + localeName + ".yml")).filter(File::exists).findFirst().orElseThrow(NullPointerException::new);
return getLocalePriority().stream().map(localeName -> new File(plugin.getDataFolder(), "lang/" + localeName + ".yml")).filter(File::exists).findFirst().orElse(null);
}
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));
}
private static boolean isLocaleLoaded(Plugin plugin) {
public static boolean isLocaleLoaded(Plugin plugin) {
return map.containsKey(plugin.getName());
}
private static boolean isDependWithTabooLib(Plugin plugin) {
public 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());
}
private static List<String> getLocalePriority() {
public static List<String> getLocalePriority() {
return Main.getInst().getConfig().contains("LOCALE.PRIORITY") ? Main.getInst().getConfig().getStringList("LOCALE.PRIORITY") : Collections.singletonList("zh_CN");
}
@ -138,65 +137,28 @@ public class TLocaleLoader {
return instance;
}
private static Map<String, Object> getLocaleAtStream(Plugin plugin, File localeFile) {
private static YamlConfiguration 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"));
Object load = new Yaml().load(yamlText);
return load instanceof Map ? (Map<String, Object>) load : new HashMap<>(0);
} catch (Exception e) {
return new HashMap<>(0);
YamlConfiguration yaml = new YamlConfiguration();
yaml.loadFromString(yamlText);
return yaml;
} catch (Exception ignored) {
return null;
}
}
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);
private static void loadPluginLocale(Plugin plugin, File localeFile, YamlConfiguration localeConfiguration, YamlConfiguration localeConfigurationAtStream) {
TLocaleInstance localeInstance = getLocaleInstance(plugin);
YamlConfiguration localeConfiguration = (YamlConfiguration) ConfigUtils.mapToConf(currentMap);
if (localeConfigurationAtStream != null) {
localeInstance.load(localeConfigurationAtStream);
}
localeInstance.load(localeConfiguration);
if (update == 0) {
if (localeInstance.getLatestUpdateNodes().get() <= 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(update));
infoLogger("SUCCESS-LOADING-LANG-UPDATE", plugin.getName(), localeFile.getName().split("\\.")[0], String.valueOf(localeInstance.size()), String.valueOf(localeInstance.getLatestUpdateNodes().get()));
}
}
}

View File

@ -51,11 +51,6 @@ public class ListenerItemListCommand implements Listener {
meta.setLore(lore);
item.setItemMeta(meta);
inventory.setItem(slot, item);
System.out.println("lore: " + TLocale.asString("COMMANDS.TABOOLIB.ITEMLIST.MENU.LORE", name));
System.out.println("lore: " + TLocale.asStringList("COMMANDS.TABOOLIB.ITEMLIST.MENU.LORE", name));
TLocale.sendTo(player, "COMMANDS.TABOOLIB.ITEMLIST.MENU.LORE", name);
holder.ITEMS_DATA.put(slot, name);
} else {
break;