Merge branch 'dev' of https://github.com/Bkm016/TabooLib
# Conflicts: # src/main/java/com/ilummc/tlib/resources/TLocaleLoader.java
This commit is contained in:
@@ -1,17 +1,20 @@
|
||||
package com.ilummc.tlib.resources;
|
||||
|
||||
import com.ilummc.tlib.TLib;
|
||||
import com.ilummc.tlib.bungee.api.ChatColor;
|
||||
import com.ilummc.tlib.inject.TLoggerManager;
|
||||
import com.ilummc.tlib.util.Ref;
|
||||
import com.ilummc.tlib.util.Strings;
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import me.skymc.taboolib.Main;
|
||||
import me.skymc.taboolib.TabooLib;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class TLocale {
|
||||
|
||||
@@ -20,15 +23,15 @@ public class TLocale {
|
||||
}
|
||||
|
||||
static String asString(String path, Class<?> callerClass, String... args) {
|
||||
return TLocaleLoader.asString(getCallerPlugin(callerClass), path, args);
|
||||
return TLocaleLoader.asString(Ref.getCallerPlugin(callerClass), path, args);
|
||||
}
|
||||
|
||||
static List<String> asStringList(String path, Class<?> callerClass, String... args) {
|
||||
return TLocaleLoader.asStringList(getCallerPlugin(callerClass), path, args);
|
||||
return TLocaleLoader.asStringList(Ref.getCallerPlugin(callerClass), path, args);
|
||||
}
|
||||
|
||||
private static void sendTo(String path, CommandSender sender, String[] args, Class<?> callerClass) {
|
||||
TLocaleLoader.sendTo(getCallerPlugin(callerClass), path, sender, args);
|
||||
TLocaleLoader.sendTo(Ref.getCallerPlugin(callerClass), path, sender, args);
|
||||
}
|
||||
|
||||
public static void sendToConsole(String path, String... args) {
|
||||
@@ -45,7 +48,7 @@ public class TLocale {
|
||||
|
||||
public static String asString(String path, String... args) {
|
||||
try {
|
||||
return asString(path, Ref.getCallerClassNotOptional(3), args);
|
||||
return asString(path, Ref.getCallerClass(3).orElse(Main.class), args);
|
||||
} catch (Exception e) {
|
||||
TLib.getTLib().getLogger().error(Strings.replaceWithOrder(TLib.getInternalLanguage().getString("FETCH-LOCALE-ERROR"), path));
|
||||
TLib.getTLib().getLogger().error(Strings.replaceWithOrder(TLib.getInternalLanguage().getString("LOCALE-ERROR-REASON"), e.getMessage()));
|
||||
@@ -55,7 +58,7 @@ public class TLocale {
|
||||
|
||||
public static List<String> asStringList(String path, String... args) {
|
||||
try {
|
||||
return asStringList(path, Ref.getCallerClassNotOptional(3), args);
|
||||
return asStringList(path, Ref.getCallerClass(3).orElse(Main.class), args);
|
||||
} catch (Exception e) {
|
||||
TLib.getTLib().getLogger().error(Strings.replaceWithOrder(TLib.getInternalLanguage().getString("FETCH-LOCALE-ERROR"), path));
|
||||
TLib.getTLib().getLogger().error(Strings.replaceWithOrder(TLib.getInternalLanguage().getString("LOCALE-ERROR-REASON"), e.getMessage()));
|
||||
@@ -64,48 +67,64 @@ public class TLocale {
|
||||
}
|
||||
|
||||
public static void reload() {
|
||||
Ref.getCallerClass(3).ifPresent(clazz -> TLocaleLoader.load(getCallerPlugin(clazz), false));
|
||||
Ref.getCallerClass(3).ifPresent(clazz -> TLocaleLoader.load(Ref.getCallerPlugin(clazz), false));
|
||||
}
|
||||
|
||||
private static JavaPlugin getCallerPlugin(Class<?> callerClass) {
|
||||
try {
|
||||
Field pluginField = callerClass.getClassLoader().getClass().getDeclaredField("plugin");
|
||||
pluginField.setAccessible(true);
|
||||
return (JavaPlugin) pluginField.get(callerClass.getClassLoader());
|
||||
} catch (Exception ignored) {
|
||||
TLib.getTLib().getLogger().error("无效的语言文件发送形式: &4" + callerClass.getName());
|
||||
public static final class Translate extends TLocale {
|
||||
|
||||
public static boolean isPlaceholderUseDefault() {
|
||||
return Main.getInst().getConfig().getBoolean("LOCALE.USE_PAPI", false);
|
||||
}
|
||||
|
||||
public static boolean isPlaceholderPluginEnabled() {
|
||||
return Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null && Bukkit.getPluginManager().getPlugin("PlaceholderAPI").isEnabled();
|
||||
}
|
||||
|
||||
public static String setColored(String args) {
|
||||
return ChatColor.translateAlternateColorCodes('&', args);
|
||||
}
|
||||
|
||||
public static List<String> setColored(List<String> args) {
|
||||
return args.stream().map(var -> ChatColor.translateAlternateColorCodes('&', var)).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static String setPlaceholders(CommandSender sender, String args) {
|
||||
return isPlaceholderPluginEnabled() ? sender instanceof Player ? PlaceholderAPI.setPlaceholders((Player) sender, args) : args : args;
|
||||
}
|
||||
|
||||
public static List<String> setPlaceholders(CommandSender sender, List<String> args) {
|
||||
return isPlaceholderPluginEnabled() ? sender instanceof Player ? PlaceholderAPI.setPlaceholders((Player) sender, args) : args : args;
|
||||
}
|
||||
return (JavaPlugin) Main.getInst();
|
||||
}
|
||||
|
||||
public static final class Logger extends TLocale {
|
||||
|
||||
public static void info(String path, String... args) {
|
||||
Ref.getCallerClass(3).ifPresent(clazz -> TLoggerManager.getLogger(TLocale.getCallerPlugin(clazz)).info(asString(path, clazz, args)));
|
||||
Ref.getCallerClass(3).ifPresent(clazz -> asStringList(path, clazz, args).forEach(locale -> TLoggerManager.getLogger(Ref.getCallerPlugin(clazz)).info(locale)));
|
||||
}
|
||||
|
||||
public static void warn(String path, String... args) {
|
||||
Ref.getCallerClass(3).ifPresent(clazz -> TLoggerManager.getLogger(TLocale.getCallerPlugin(clazz)).warn(asString(path, clazz, args)));
|
||||
Ref.getCallerClass(3).ifPresent(clazz -> asStringList(path, clazz, args).forEach(locale -> TLoggerManager.getLogger(Ref.getCallerPlugin(clazz)).warn(locale)));
|
||||
}
|
||||
|
||||
public static void error(String path, String... args) {
|
||||
Ref.getCallerClass(3).ifPresent(clazz -> TLoggerManager.getLogger(TLocale.getCallerPlugin(clazz)).error(asString(path, clazz, args)));
|
||||
Ref.getCallerClass(3).ifPresent(clazz -> asStringList(path, clazz, args).forEach(locale -> TLoggerManager.getLogger(Ref.getCallerPlugin(clazz)).error(locale)));
|
||||
}
|
||||
|
||||
public static void fatal(String path, String... args) {
|
||||
Ref.getCallerClass(3).ifPresent(clazz -> TLoggerManager.getLogger(TLocale.getCallerPlugin(clazz)).fatal(asString(path, clazz, args)));
|
||||
Ref.getCallerClass(3).ifPresent(clazz -> asStringList(path, clazz, args).forEach(locale -> TLoggerManager.getLogger(Ref.getCallerPlugin(clazz)).fatal(locale)));
|
||||
}
|
||||
|
||||
public static void fine(String path, String... args) {
|
||||
Ref.getCallerClass(3).ifPresent(clazz -> TLoggerManager.getLogger(TLocale.getCallerPlugin(clazz)).fine(asString(path, clazz, args)));
|
||||
Ref.getCallerClass(3).ifPresent(clazz -> asStringList(path, clazz, args).forEach(locale -> TLoggerManager.getLogger(Ref.getCallerPlugin(clazz)).fine(locale)));
|
||||
}
|
||||
|
||||
public static void finest(String path, String... args) {
|
||||
Ref.getCallerClass(3).ifPresent(clazz -> TLoggerManager.getLogger(TLocale.getCallerPlugin(clazz)).finest(asString(path, clazz, args)));
|
||||
Ref.getCallerClass(3).ifPresent(clazz -> asStringList(path, clazz, args).forEach(locale -> TLoggerManager.getLogger(Ref.getCallerPlugin(clazz)).finest(locale)));
|
||||
}
|
||||
|
||||
public static void verbose(String path, String... args) {
|
||||
Ref.getCallerClass(3).ifPresent(clazz -> TLoggerManager.getLogger(TLocale.getCallerPlugin(clazz)).verbose(asString(path, clazz, args)));
|
||||
Ref.getCallerClass(3).ifPresent(clazz -> asStringList(path, clazz, args).forEach(locale -> TLoggerManager.getLogger(Ref.getCallerPlugin(clazz)).verbose(locale)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.google.common.collect.ImmutableList;
|
||||
import com.ilummc.tlib.TLib;
|
||||
import com.ilummc.tlib.resources.type.TLocaleText;
|
||||
import com.ilummc.tlib.util.Strings;
|
||||
import me.skymc.taboolib.TabooLib;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
@@ -11,19 +12,17 @@ import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ThreadSafe
|
||||
@SuppressWarnings("rawtypes")
|
||||
class TLocaleInstance {
|
||||
|
||||
private final Map<String, List<TLocaleSendable>> map = new HashMap<>();
|
||||
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;
|
||||
@@ -38,7 +37,7 @@ class TLocaleInstance {
|
||||
return map.size();
|
||||
}
|
||||
|
||||
public Map<String, List<TLocaleSendable>> getMap() {
|
||||
public Map<String, List<TLocaleSerialize>> getMap() {
|
||||
return map;
|
||||
}
|
||||
|
||||
@@ -46,58 +45,71 @@ class TLocaleInstance {
|
||||
return plugin;
|
||||
}
|
||||
|
||||
public AtomicInteger getLatestUpdateNodes() {
|
||||
return latestUpdateNodes;
|
||||
}
|
||||
|
||||
public void sendTo(String path, CommandSender sender, String... args) {
|
||||
try {
|
||||
map.getOrDefault(path, ImmutableList.of(TLocaleSendable.getEmpty(path))).forEach(sendable -> {
|
||||
map.getOrDefault(path, ImmutableList.of(TLocaleSerialize.getEmpty(path))).forEach(tSender -> {
|
||||
if (Bukkit.isPrimaryThread()) {
|
||||
sendable.sendTo(sender, args);
|
||||
tSender.sendTo(sender, args);
|
||||
} else {
|
||||
Bukkit.getScheduler().runTask(plugin, () -> sendable.sendTo(sender, args));
|
||||
Bukkit.getScheduler().runTask(plugin, () -> tSender.sendTo(sender, args));
|
||||
}
|
||||
});
|
||||
} catch (Exception | Error e) {
|
||||
TLib.getTLib().getLogger().error(Strings.replaceWithOrder(TLib.getInternalLanguage().getString("SEND-LOCALE-ERROR"), path));
|
||||
TLib.getTLib().getLogger().error(Strings.replaceWithOrder(TLib.getInternalLanguage().getString("LOCALE-ERROR-REASON"), e.toString()));
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public String asString(String path, String... args) {
|
||||
return map.getOrDefault(path, ImmutableList.of(TLocaleSendable.getEmpty(path))).get(0).asString(args);
|
||||
return map.getOrDefault(path, ImmutableList.of(TLocaleSerialize.getEmpty(path))).get(0).asString(args);
|
||||
}
|
||||
|
||||
public List<String> asStringList(String path, String... args) {
|
||||
return map.getOrDefault(path, ImmutableList.of(TLocaleSendable.getEmpty(path))).get(0).asStringList(args);
|
||||
return map.getOrDefault(path, ImmutableList.of(TLocaleSerialize.getEmpty(path))).get(0).asStringList(args);
|
||||
}
|
||||
|
||||
private static final Function<Object, TLocaleSendable> TO_SENDABLE = o -> {
|
||||
if (o instanceof TLocaleSendable) {
|
||||
return ((TLocaleSendable) o);
|
||||
} else if (o instanceof String || (o instanceof List && isListString(((List) o)))) {
|
||||
return TLocaleText.of(o);
|
||||
} else {
|
||||
return TLocaleText.of(String.valueOf(o));
|
||||
}
|
||||
};
|
||||
|
||||
private static boolean isListString(List list) {
|
||||
for (Object o : list) {
|
||||
if (!(o instanceof String)) return false;
|
||||
if (!(o instanceof String)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public void load(YamlConfiguration configuration) {
|
||||
configuration.getKeys(true).forEach(s -> {
|
||||
Object object = configuration.get(s);
|
||||
if (object instanceof TLocaleSendable) {
|
||||
map.put(s, Collections.singletonList((TLocaleSendable) object));
|
||||
} else if (object instanceof List && !((List) object).isEmpty()) {
|
||||
map.put(s, ((List<?>) object).stream().map(TO_SENDABLE).collect(Collectors.toList()));
|
||||
} else if (!(object instanceof ConfigurationSection)) {
|
||||
String str = String.valueOf(object);
|
||||
map.put(s, Collections.singletonList(str.length() == 0 ? TLocaleSendable.getEmpty() : TLocaleText.of(str)));
|
||||
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 {
|
||||
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 (!(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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,25 +1,22 @@
|
||||
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;
|
||||
import com.ilummc.tlib.resources.type.*;
|
||||
import com.ilummc.tlib.util.IO;
|
||||
import com.ilummc.tlib.util.Strings;
|
||||
import me.skymc.taboocode.TabooCodeLang;
|
||||
import me.skymc.taboolib.Main;
|
||||
import me.skymc.taboolib.TabooLib;
|
||||
import me.skymc.taboolib.fileutils.ConfigUtils;
|
||||
import me.skymc.taboolib.other.NumberUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
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.*;
|
||||
@@ -38,6 +35,7 @@ public class TLocaleLoader {
|
||||
}
|
||||
|
||||
public static void sendTo(Plugin plugin, String path, CommandSender sender, String... args) {
|
||||
TabooLib.debug(plugin, "TLocaleLoader.sendTo: " + plugin + ", path: " + path + ", sender: " + sender + ", args: " + Arrays.asList(args));
|
||||
if (Bukkit.isPrimaryThread()) {
|
||||
Optional.ofNullable(map.get(plugin.getName())).ifPresent(localeInstance -> localeInstance.sendTo(path, sender, args));
|
||||
} else {
|
||||
@@ -48,6 +46,7 @@ public class TLocaleLoader {
|
||||
}
|
||||
|
||||
public static String asString(Plugin plugin, String path, String... args) {
|
||||
TabooLib.debug(plugin, "TLocaleLoader.asString: " + plugin.getName() + ", path: " + path + ", args: " + Arrays.asList(args));
|
||||
TLocaleInstance tLocaleInstance = map.get(plugin.getName());
|
||||
if (tLocaleInstance != null) {
|
||||
return tLocaleInstance.asString(path, args);
|
||||
@@ -57,6 +56,7 @@ public class TLocaleLoader {
|
||||
}
|
||||
|
||||
public static List<String> asStringList(Plugin plugin, String path, String... args) {
|
||||
TabooLib.debug(plugin, "TLocaleLoader.asStringList: " + plugin + ", path: " + path + ", args: " + Arrays.asList(args));
|
||||
TLocaleInstance tLocaleInstance = map.get(plugin.getName());
|
||||
if (tLocaleInstance != null) {
|
||||
return tLocaleInstance.asStringList(path, args);
|
||||
@@ -76,20 +76,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) {
|
||||
@@ -97,6 +100,18 @@ public class TLocaleLoader {
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isLocaleLoaded(Plugin plugin) {
|
||||
return map.containsKey(plugin.getName());
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
public static List<String> getLocalePriority() {
|
||||
return Main.getInst().getConfig().contains("LOCALE.PRIORITY") ? Main.getInst().getConfig().getStringList("LOCALE.PRIORITY") : Collections.singletonList("zh_CN");
|
||||
}
|
||||
|
||||
private static boolean isLoadLocale(Plugin plugin, boolean isCover) {
|
||||
return (isCover || !isLocaleLoaded(plugin)) && (plugin.equals(Main.getInst()) || isDependWithTabooLib(plugin));
|
||||
}
|
||||
@@ -109,98 +124,43 @@ 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.contains("VERSION") && configuration.contains("VERSION")) && NumberUtils.getDouble(configurationAtSteam.get("VERSION").toString()) > NumberUtils.getDouble(configuration.get("VERSION").toString());
|
||||
}
|
||||
|
||||
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) {
|
||||
return map.containsKey(plugin.getName());
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
private static List<String> getLocalePriority() {
|
||||
return Main.getInst().getConfig().contains("LOCALE.PRIORITY") ? Main.getInst().getConfig().getStringList("LOCALE.PRIORITY") : Collections.singletonList("zh_CN");
|
||||
}
|
||||
|
||||
private static TLocaleInstance getLocaleInstance(Plugin plugin) {
|
||||
TLocaleInstance instance = new TLocaleInstance(plugin);
|
||||
map.put(plugin.getName(), instance);
|
||||
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);
|
||||
/*
|
||||
if (i > 0) {
|
||||
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()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,45 +0,0 @@
|
||||
package com.ilummc.tlib.resources;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public interface TLocaleSendable {
|
||||
|
||||
static TLocaleSendable getEmpty() {
|
||||
return (sender, args) -> {
|
||||
// Empty
|
||||
};
|
||||
}
|
||||
|
||||
static TLocaleSendable getEmpty(String path) {
|
||||
return new TLocaleSendable() {
|
||||
|
||||
@Override
|
||||
public void sendTo(CommandSender sender, String... args) {
|
||||
sender.sendMessage("§4<" + path + "§4>");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asString(String... args) {
|
||||
return "§4<" + path + "§4>";
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> asStringList(String... args) {
|
||||
return Collections.singletonList("§4<" + path + "§4>");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
void sendTo(CommandSender sender, String... args);
|
||||
|
||||
default String asString(String... args) {
|
||||
return "";
|
||||
}
|
||||
|
||||
default List<String> asStringList(String... args) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
37
src/main/java/com/ilummc/tlib/resources/TLocaleSender.java
Normal file
37
src/main/java/com/ilummc/tlib/resources/TLocaleSender.java
Normal file
@@ -0,0 +1,37 @@
|
||||
package com.ilummc.tlib.resources;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
* @Since 2018-05-12 13:58
|
||||
*/
|
||||
public interface TLocaleSender {
|
||||
|
||||
/**
|
||||
* 发送信息
|
||||
*
|
||||
* @param sender 发送目标
|
||||
* @param args 参数
|
||||
*/
|
||||
void sendTo(CommandSender sender, String... args);
|
||||
|
||||
/**
|
||||
* 获取文本
|
||||
*
|
||||
* @param args 参数
|
||||
* @return 文本
|
||||
*/
|
||||
String asString(String... args);
|
||||
|
||||
/**
|
||||
* 获取文本集合
|
||||
*
|
||||
* @param args 参数
|
||||
* @return 文本集合
|
||||
*/
|
||||
List<String> asStringList(String... args);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package com.ilummc.tlib.resources;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
* @Since 2018-05-12 14:01
|
||||
*/
|
||||
public abstract class TLocaleSerialize implements TLocaleSender, ConfigurationSerializable {
|
||||
|
||||
public static boolean isPlaceholderEnabled(Map<String, Object> map) {
|
||||
Object placeholderObject = map.getOrDefault("papi", TLocale.Translate.isPlaceholderUseDefault());
|
||||
return placeholderObject instanceof Boolean ? (boolean) placeholderObject : placeholderObject instanceof String && "true".equals(placeholderObject);
|
||||
}
|
||||
|
||||
public static String getStringOrDefault(Map<String, Object> map, String path, String def) {
|
||||
Object var = map.getOrDefault(path, def);
|
||||
return var instanceof String ? (String) var : def;
|
||||
}
|
||||
|
||||
public static Integer getIntegerOrDefault(Map<String, Object> map, String path, Integer def) {
|
||||
Object var = map.getOrDefault(path, def);
|
||||
return var instanceof Integer ? (Integer) var : def;
|
||||
}
|
||||
|
||||
public static Double getDoubleOrDefault(Map<String, Object> map, String path, Double def) {
|
||||
Object var = map.getOrDefault(path, def);
|
||||
return var instanceof Double ? (Double) var : def;
|
||||
}
|
||||
|
||||
static TLocaleSerialize getEmpty() {
|
||||
return new TLocaleSerialize() {
|
||||
|
||||
@Override
|
||||
public void sendTo(CommandSender sender, String... args) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> serialize() {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
static TLocaleSerialize getEmpty(String path) {
|
||||
return new TLocaleSerialize() {
|
||||
|
||||
@Override
|
||||
public Map<String, Object> serialize() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendTo(CommandSender sender, String... args) {
|
||||
sender.sendMessage("§8<" + path + "§8>");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asString(String... args) {
|
||||
return "§8<" + path + "§8>";
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> asStringList(String... args) {
|
||||
return Collections.singletonList("§4<" + path + "§4>");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asString(String... args) {
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> asStringList(String... args) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
@@ -3,12 +3,11 @@ package com.ilummc.tlib.resources.type;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.ilummc.tlib.compat.PlaceholderHook;
|
||||
import com.ilummc.tlib.nms.ActionBar;
|
||||
import com.ilummc.tlib.resources.TLocaleSendable;
|
||||
import com.ilummc.tlib.resources.TLocaleSerialize;
|
||||
import com.ilummc.tlib.util.Strings;
|
||||
import me.skymc.taboolib.Main;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
||||
import org.bukkit.configuration.serialization.SerializableAs;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
@@ -17,7 +16,7 @@ import java.util.Map;
|
||||
|
||||
@Immutable
|
||||
@SerializableAs("ACTION")
|
||||
public class TLocaleActionBar implements TLocaleSendable, ConfigurationSerializable {
|
||||
public class TLocaleActionBar extends TLocaleSerialize {
|
||||
|
||||
private final String text;
|
||||
private final boolean papi;
|
||||
@@ -29,8 +28,7 @@ public class TLocaleActionBar implements TLocaleSendable, ConfigurationSerializa
|
||||
|
||||
public static TLocaleActionBar valueOf(Map<String, Object> map) {
|
||||
String text = ChatColor.translateAlternateColorCodes('&', String.valueOf(map.getOrDefault("text", "Empty Action bar message.")));
|
||||
boolean papi = (boolean) map.getOrDefault("papi", Main.getInst().getConfig().getBoolean("LOCALE.USE_PAPI", false));
|
||||
return new TLocaleActionBar(text, papi);
|
||||
return new TLocaleActionBar(text, isPlaceholderEnabled(map));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -7,13 +7,11 @@ import com.ilummc.tlib.TLib;
|
||||
import com.ilummc.tlib.bungee.api.chat.*;
|
||||
import com.ilummc.tlib.bungee.chat.ComponentSerializer;
|
||||
import com.ilummc.tlib.compat.PlaceholderHook;
|
||||
import com.ilummc.tlib.resources.TLocaleSendable;
|
||||
import com.ilummc.tlib.resources.TLocale;
|
||||
import com.ilummc.tlib.resources.TLocaleSerialize;
|
||||
import com.ilummc.tlib.util.Strings;
|
||||
import me.skymc.taboolib.Main;
|
||||
import me.skymc.taboolib.jsonformatter.JSONFormatter;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
||||
import org.bukkit.configuration.serialization.SerializableAs;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
@@ -25,7 +23,7 @@ import java.util.stream.Collectors;
|
||||
|
||||
@ThreadSafe
|
||||
@SerializableAs("JSON")
|
||||
public class TLocaleJson implements TLocaleSendable, ConfigurationSerializable {
|
||||
public class TLocaleJson extends TLocaleSerialize {
|
||||
|
||||
private static final Pattern pattern = Pattern.compile("<([^<>]*)?@([^<>]*)>");
|
||||
private final List<BaseComponent[]> components;
|
||||
@@ -39,9 +37,6 @@ public class TLocaleJson implements TLocaleSendable, ConfigurationSerializable {
|
||||
}
|
||||
|
||||
public static TLocaleJson valueOf(Map<String, Object> map) {
|
||||
boolean papi = (boolean) map.getOrDefault("papi", Main.getInst().getConfig().getBoolean("LOCALE.USE_PAPI", false));
|
||||
|
||||
// text 里面的东西
|
||||
List<String> textList = getTextList(map.getOrDefault("text", "Empty Node"));
|
||||
|
||||
// 分析 args 并替换
|
||||
@@ -73,7 +68,7 @@ public class TLocaleJson implements TLocaleSendable, ConfigurationSerializable {
|
||||
// 如果 args 有这个 xxx
|
||||
if (section.containsKey(node)) {
|
||||
Map<String, Object> arg = (Map<String, Object>) section.get(node);
|
||||
text = ChatColor.translateAlternateColorCodes('&', String.valueOf(arg.getOrDefault("text", text)));
|
||||
text = TLocale.Translate.setColored(String.valueOf(arg.getOrDefault("text", text)));
|
||||
// 可能有很多个 BaseComponent,于是为每个 component 单独设置各种事件
|
||||
BaseComponent[] component = TextComponent.fromLegacyText(text);
|
||||
arg.forEach((key, value) -> {
|
||||
@@ -82,7 +77,7 @@ public class TLocaleJson implements TLocaleSendable, ConfigurationSerializable {
|
||||
} else if ("command".equalsIgnoreCase(key) || "commands".equalsIgnoreCase(key)) {
|
||||
Arrays.stream(component).forEach(baseComponent -> baseComponent.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, String.valueOf(value))));
|
||||
} else if ("hover".equalsIgnoreCase(key)) {
|
||||
Arrays.stream(component).forEach(baseComponent -> baseComponent.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(ChatColor.translateAlternateColorCodes('&', String.valueOf(value))).create())));
|
||||
Arrays.stream(component).forEach(baseComponent -> baseComponent.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(TLocale.Translate.setColored(String.valueOf(value))).create())));
|
||||
}
|
||||
});
|
||||
// 添加到原来的 list 里面
|
||||
@@ -99,16 +94,16 @@ public class TLocaleJson implements TLocaleSendable, ConfigurationSerializable {
|
||||
}
|
||||
return builder.toArray(new BaseComponent[0]);
|
||||
}).collect(Collectors.toList());
|
||||
return new TLocaleJson(collect, papi, map);
|
||||
return new TLocaleJson(collect, isPlaceholderEnabled(map), map);
|
||||
}
|
||||
return new TLocaleJson(textList.stream().map(TextComponent::fromLegacyText).collect(Collectors.toList()), papi, map);
|
||||
return new TLocaleJson(textList.stream().map(TextComponent::fromLegacyText).collect(Collectors.toList()), isPlaceholderEnabled(map), map);
|
||||
}
|
||||
|
||||
private static List<String> getTextList(Object textObj) {
|
||||
if (textObj instanceof List) {
|
||||
return ((List<?>) textObj).stream().map(Object::toString).map(s -> ChatColor.translateAlternateColorCodes('&', s)).collect(Collectors.toList());
|
||||
return ((List<?>) textObj).stream().map(Object::toString).map(s -> TLocale.Translate.setColored(s)).collect(Collectors.toList());
|
||||
} else if (textObj instanceof String) {
|
||||
return Lists.newArrayList(ChatColor.translateAlternateColorCodes('&', (String) textObj));
|
||||
return Lists.newArrayList(TLocale.Translate.setColored((String) textObj));
|
||||
} else {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
package com.ilummc.tlib.resources.type;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import com.ilummc.tlib.resources.TLocaleSendable;
|
||||
import com.ilummc.tlib.resources.TLocaleSerialize;
|
||||
import me.skymc.taboolib.sound.SoundPack;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
||||
import org.bukkit.configuration.serialization.SerializableAs;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
@@ -20,7 +19,7 @@ import java.util.stream.Collectors;
|
||||
*/
|
||||
@Immutable
|
||||
@SerializableAs("ACTION")
|
||||
public class TLocaleSound implements TLocaleSendable, ConfigurationSerializable {
|
||||
public class TLocaleSound extends TLocaleSerialize {
|
||||
|
||||
private final List<SoundPack> soundPacks;
|
||||
|
||||
|
||||
@@ -4,12 +4,11 @@ import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.ilummc.tlib.compat.PlaceholderHook;
|
||||
import com.ilummc.tlib.resources.TLocaleSendable;
|
||||
import com.ilummc.tlib.resources.TLocale;
|
||||
import com.ilummc.tlib.resources.TLocaleSerialize;
|
||||
import com.ilummc.tlib.util.Strings;
|
||||
import me.skymc.taboolib.Main;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
||||
import org.bukkit.configuration.serialization.SerializableAs;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
@@ -19,7 +18,7 @@ import java.util.stream.Collectors;
|
||||
@Immutable
|
||||
@SerializableAs("TEXT")
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
public class TLocaleText implements TLocaleSendable, ConfigurationSerializable {
|
||||
public class TLocaleText extends TLocaleSerialize {
|
||||
|
||||
private final Object text;
|
||||
private final boolean usePlaceholder;
|
||||
@@ -35,68 +34,46 @@ public class TLocaleText implements TLocaleSendable, ConfigurationSerializable {
|
||||
}
|
||||
}
|
||||
|
||||
private String replaceMsg(CommandSender sender, String s) {
|
||||
return usePlaceholder ? PlaceholderHook.replace(sender, s) : s;
|
||||
}
|
||||
|
||||
public static TLocaleText valueOf(Map<String, Object> map) {
|
||||
if (map.containsKey("text")) {
|
||||
Object object = map.get("text");
|
||||
Object objPapi = map.getOrDefault("papi", Main.getInst().getConfig().getBoolean("LOCALE.USE_PAPI", false));
|
||||
boolean papi = objPapi instanceof Boolean ? (boolean) objPapi : objPapi instanceof String && "true".equals(objPapi);
|
||||
if (object instanceof List) {
|
||||
return new TLocaleText(((List<String>) object).stream()
|
||||
.map(s -> ChatColor.translateAlternateColorCodes('&', s))
|
||||
.collect(Collectors.toList()), papi);
|
||||
} else if (object instanceof String[]) {
|
||||
return new TLocaleText(Arrays.stream(((String[]) object))
|
||||
.map(s -> ChatColor.translateAlternateColorCodes('&', s))
|
||||
.collect(Collectors.toList()), papi);
|
||||
} else {
|
||||
return new TLocaleText(ChatColor.translateAlternateColorCodes('&', Objects.toString(object)), papi);
|
||||
}
|
||||
}
|
||||
return new TLocaleText("§cError chat message loaded.", Main.getInst().getConfig().getBoolean("LOCALE.USE_PAPI", false));
|
||||
}
|
||||
|
||||
public static TLocaleText of(String s) {
|
||||
return new TLocaleText(ChatColor.translateAlternateColorCodes('&', s), Main.getInst().getConfig().getBoolean("LOCALE.USE_PAPI", false));
|
||||
return new TLocaleText(TLocale.Translate.setColored(s), TLocale.Translate.isPlaceholderUseDefault());
|
||||
}
|
||||
|
||||
public static TLocaleText of(Object o) {
|
||||
return o instanceof String ? of(((String) o)) : new TLocaleText(o, false);
|
||||
}
|
||||
|
||||
public static TLocaleText valueOf(Map<String, Object> map) {
|
||||
if (map.containsKey("text")) {
|
||||
Object object = map.get("text");
|
||||
if (object instanceof String[]) {
|
||||
return new TLocaleText(Arrays.stream(((String[]) object)).collect(Collectors.toList()), isPlaceholderEnabled(map));
|
||||
} else {
|
||||
return new TLocaleText(Objects.toString(object), isPlaceholderEnabled(map));
|
||||
}
|
||||
}
|
||||
return new TLocaleText("§cError chat message loaded.", TLocale.Translate.isPlaceholderUseDefault());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendTo(CommandSender sender, String... args) {
|
||||
if (text instanceof String) {
|
||||
sender.sendMessage(replaceMsg(sender, Strings.replaceWithOrder((String) text, args)));
|
||||
sender.sendMessage(replaceText(sender, Strings.replaceWithOrder((String) text, args)));
|
||||
} else if (text instanceof List) {
|
||||
((List) text).forEach(s -> sender.sendMessage(replaceMsg(sender, Strings.replaceWithOrder(String.valueOf(s), args))));
|
||||
((List) text).forEach(s -> sender.sendMessage(replaceText(sender, Strings.replaceWithOrder(String.valueOf(s), args))));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asString(String... args) {
|
||||
return Strings.replaceWithOrder(objectToString(text), args);
|
||||
return Strings.replaceWithOrder(TLocale.Translate.setColored(objectToString(text)), args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> asStringList(String... args) {
|
||||
if (text instanceof String) {
|
||||
return Collections.singletonList(((String) text));
|
||||
if (text instanceof List) {
|
||||
return ((List<String>) text).stream().map(x -> Strings.replaceWithOrder(TLocale.Translate.setColored(x), args)).collect(Collectors.toList());
|
||||
} else {
|
||||
return ((List<String>) text);
|
||||
}
|
||||
}
|
||||
|
||||
private String objectToString(Object text) {
|
||||
if (text instanceof String) {
|
||||
return ((String) text);
|
||||
} else {
|
||||
StringJoiner joiner = new StringJoiner("\n");
|
||||
((List<String>) text).forEach(joiner::add);
|
||||
return joiner.toString();
|
||||
return Collections.singletonList(asString(args));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -111,9 +88,20 @@ public class TLocaleText implements TLocaleSendable, ConfigurationSerializable {
|
||||
|
||||
@Override
|
||||
public Map<String, Object> serialize() {
|
||||
if (usePlaceholder) {
|
||||
return Maps.newHashMap(ImmutableMap.of("text", text, "papi", true));
|
||||
return usePlaceholder ? Maps.newHashMap(ImmutableMap.of("text", text, "papi", true)) : Maps.newHashMap(ImmutableMap.of("text", text));
|
||||
}
|
||||
|
||||
private String replaceText(CommandSender sender, String args) {
|
||||
return usePlaceholder ? TLocale.Translate.setPlaceholders(sender, args) : TLocale.Translate.setColored(args);
|
||||
}
|
||||
|
||||
private String objectToString(Object text) {
|
||||
if (text instanceof String) {
|
||||
return ((String) text);
|
||||
} else {
|
||||
StringJoiner joiner = new StringJoiner("\n");
|
||||
((List<String>) text).forEach(joiner::add);
|
||||
return joiner.toString();
|
||||
}
|
||||
return Maps.newHashMap(ImmutableMap.of("text", text));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,11 @@
|
||||
package com.ilummc.tlib.resources.type;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import com.ilummc.tlib.compat.PlaceholderHook;
|
||||
import com.ilummc.tlib.resources.TLocale;
|
||||
import com.ilummc.tlib.resources.TLocaleSendable;
|
||||
import com.ilummc.tlib.resources.TLocaleSerialize;
|
||||
import com.ilummc.tlib.util.Strings;
|
||||
import me.skymc.taboolib.Main;
|
||||
import me.skymc.taboolib.display.TitleUtils;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
||||
import org.bukkit.configuration.serialization.SerializableAs;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
@@ -24,7 +20,7 @@ import java.util.Map;
|
||||
|
||||
@Immutable
|
||||
@SerializableAs("TITLE")
|
||||
public class TLocaleTitle implements TLocaleSendable, ConfigurationSerializable {
|
||||
public class TLocaleTitle extends TLocaleSerialize {
|
||||
|
||||
private final String title;
|
||||
private final String subtitle;
|
||||
@@ -47,12 +43,12 @@ public class TLocaleTitle implements TLocaleSendable, ConfigurationSerializable
|
||||
TLocaleTitle title;
|
||||
try {
|
||||
title = new TLocaleTitle(
|
||||
(String) map.getOrDefault("title", ""),
|
||||
(String) map.getOrDefault("subtitle", ""),
|
||||
(int) map.getOrDefault("fadein", 10),
|
||||
(int) map.getOrDefault("fadeout", 10),
|
||||
(int) map.getOrDefault("stay", 20),
|
||||
(boolean) map.getOrDefault("papi", Main.getInst().getConfig().getBoolean("LOCALE.USE_PAPI", false)));
|
||||
getStringOrDefault(map, "title", ""),
|
||||
getStringOrDefault(map, "subtitle", ""),
|
||||
getIntegerOrDefault(map, "fadein", 10),
|
||||
getIntegerOrDefault(map, "fadeout", 10),
|
||||
getIntegerOrDefault(map, "stay", 10),
|
||||
isPlaceholderEnabled(map));
|
||||
} catch (Exception e) {
|
||||
title = new TLocaleTitle("Empty Title message.", e.getMessage(), 10, 20, 10, false);
|
||||
}
|
||||
@@ -70,7 +66,7 @@ public class TLocaleTitle implements TLocaleSendable, ConfigurationSerializable
|
||||
|
||||
@Override
|
||||
public String asString(String... args) {
|
||||
return Strings.replaceWithOrder("TITLE: [title: '" + title + "', subtitle: '" + subtitle + "', fadeIn: " + fadein + ", fadeOut: " + fadeout + "]", args);
|
||||
return Strings.replaceWithOrder(Strings.replaceWithOrder("TITLE: [title: ''{0}'', subtitle: ''{1}'', fadeIn: {2}, fadeOut: {3}]", title, subtitle, fadein, fadeout), args);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -90,7 +86,7 @@ public class TLocaleTitle implements TLocaleSendable, ConfigurationSerializable
|
||||
return map;
|
||||
}
|
||||
|
||||
private String replaceText(CommandSender sender, String s) {
|
||||
return ChatColor.translateAlternateColorCodes('&', usePlaceholder ? PlaceholderHook.replace(sender, s) : s);
|
||||
private String replaceText(CommandSender sender, String args) {
|
||||
return usePlaceholder ? TLocale.Translate.setPlaceholders(sender, args) : TLocale.Translate.setColored(args);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,10 @@ package com.ilummc.tlib.util;
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import com.ilummc.tlib.TLib;
|
||||
import com.ilummc.tlib.resources.TLocale;
|
||||
import com.ilummc.tlib.util.asm.AsmAnalyser;
|
||||
import me.skymc.taboolib.Main;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.objectweb.asm.ClassReader;
|
||||
import org.objectweb.asm.ClassWriter;
|
||||
import sun.reflect.Reflection;
|
||||
@@ -96,6 +99,17 @@ public class Ref {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
public static JavaPlugin getCallerPlugin(Class<?> callerClass) {
|
||||
try {
|
||||
Field pluginField = callerClass.getClassLoader().getClass().getDeclaredField("plugin");
|
||||
pluginField.setAccessible(true);
|
||||
return (JavaPlugin) pluginField.get(callerClass.getClassLoader());
|
||||
} catch (Exception ignored) {
|
||||
TLocale.Logger.error("LOCALE.CALLER-PLUGIN-NOT-FOUND", callerClass.getName());
|
||||
}
|
||||
return (JavaPlugin) Main.getInst();
|
||||
}
|
||||
|
||||
private static abstract class CallerClass {
|
||||
|
||||
private static CallerClass impl;
|
||||
|
||||
@@ -27,4 +27,16 @@ public class Strings {
|
||||
}
|
||||
return stringBuilder.toString();
|
||||
}
|
||||
|
||||
public static String replaceWithOrder(String template, String... args) {
|
||||
return replaceWithOrder(template, (Object[]) args);
|
||||
}
|
||||
|
||||
public static boolean isBlank(String var) {
|
||||
return var == null || var.trim().isEmpty();
|
||||
}
|
||||
|
||||
public static boolean isEmpty(CharSequence var) {
|
||||
return var == null || var.length() == 0;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user