Taboolib 5.0 fully refactored & Not a plugin now.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
package io.izzel.taboolib.module.command;
|
||||
|
||||
import io.izzel.taboolib.TabooLibAPI;
|
||||
import io.izzel.taboolib.locale.TLocale;
|
||||
import io.izzel.taboolib.module.locale.TLocale;
|
||||
import io.izzel.taboolib.module.command.base.BaseMainCommand;
|
||||
import io.izzel.taboolib.module.inject.TFunction;
|
||||
import io.izzel.taboolib.module.lite.SimpleReflection;
|
||||
|
||||
@@ -5,7 +5,7 @@ import com.google.common.collect.Lists;
|
||||
import io.izzel.taboolib.TabooLib;
|
||||
import io.izzel.taboolib.TabooLibAPI;
|
||||
import io.izzel.taboolib.Version;
|
||||
import io.izzel.taboolib.locale.TLocale;
|
||||
import io.izzel.taboolib.module.locale.TLocale;
|
||||
import io.izzel.taboolib.util.ArrayUtil;
|
||||
import io.izzel.taboolib.util.Strings;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package io.izzel.taboolib.module.command.base;
|
||||
|
||||
import io.izzel.taboolib.locale.TLocale;
|
||||
import io.izzel.taboolib.module.locale.TLocale;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package io.izzel.taboolib.module.command.base;
|
||||
|
||||
import io.izzel.taboolib.locale.TLocale;
|
||||
import io.izzel.taboolib.module.locale.TLocale;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ package io.izzel.taboolib.module.config;
|
||||
import com.google.common.collect.Maps;
|
||||
import io.izzel.taboolib.TabooLib;
|
||||
import io.izzel.taboolib.TabooLibAPI;
|
||||
import io.izzel.taboolib.locale.TLocale;
|
||||
import io.izzel.taboolib.module.locale.TLocale;
|
||||
import io.izzel.taboolib.module.logger.TLogger;
|
||||
import io.izzel.taboolib.util.Files;
|
||||
import io.izzel.taboolib.util.Ref;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package io.izzel.taboolib.module.dependency;
|
||||
|
||||
import io.izzel.taboolib.TabooLibAPI;
|
||||
import io.izzel.taboolib.locale.TLocale;
|
||||
import io.izzel.taboolib.module.locale.TLocale;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package io.izzel.taboolib.module.dependency;
|
||||
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
|
||||
public class TDependencyLoader {
|
||||
|
||||
public static synchronized void addToPath(Plugin plugin, URL url) {
|
||||
try {
|
||||
Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
|
||||
method.setAccessible(true);
|
||||
method.invoke(plugin.getClass().getClassLoader(), url);
|
||||
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static synchronized void addToPath(Plugin plugin, File file) {
|
||||
try {
|
||||
addToPath(plugin, file.toURI().toURL());
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package io.izzel.taboolib.module.item;
|
||||
|
||||
import io.izzel.taboolib.Version;
|
||||
import io.izzel.taboolib.locale.TLocale;
|
||||
import io.izzel.taboolib.module.locale.TLocale;
|
||||
import io.izzel.taboolib.util.ArrayUtil;
|
||||
import org.bukkit.Color;
|
||||
import org.bukkit.Material;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package io.izzel.taboolib.module.item;
|
||||
|
||||
import io.izzel.taboolib.Version;
|
||||
import io.izzel.taboolib.locale.TLocale;
|
||||
import io.izzel.taboolib.module.locale.TLocale;
|
||||
import io.izzel.taboolib.module.lite.SimpleI18n;
|
||||
import io.izzel.taboolib.module.nms.NMSHandler;
|
||||
import io.izzel.taboolib.module.nms.nbt.NBTBase;
|
||||
|
||||
@@ -2,7 +2,7 @@ package io.izzel.taboolib.module.lite;
|
||||
|
||||
import io.izzel.taboolib.TabooLib;
|
||||
import io.izzel.taboolib.Version;
|
||||
import io.izzel.taboolib.locale.TLocaleLoader;
|
||||
import io.izzel.taboolib.module.locale.TLocaleLoader;
|
||||
import io.izzel.taboolib.module.inject.TFunction;
|
||||
import io.izzel.taboolib.module.nms.NMSHandler;
|
||||
import io.izzel.taboolib.module.nms.nbt.NBTCompound;
|
||||
|
||||
172
src/main/scala/io/izzel/taboolib/module/locale/TLocale.java
Normal file
172
src/main/scala/io/izzel/taboolib/module/locale/TLocale.java
Normal file
@@ -0,0 +1,172 @@
|
||||
package io.izzel.taboolib.module.locale;
|
||||
|
||||
import io.izzel.taboolib.TabooLib;
|
||||
import io.izzel.taboolib.module.logger.TLoggerManager;
|
||||
import io.izzel.taboolib.module.nms.NMSHandler;
|
||||
import io.izzel.taboolib.module.tellraw.TellrawCreator;
|
||||
import io.izzel.taboolib.util.Ref;
|
||||
import io.izzel.taboolib.util.Strings;
|
||||
import io.izzel.taboolib.util.chat.ChatColor;
|
||||
import io.izzel.taboolib.util.chat.ComponentSerializer;
|
||||
import io.izzel.taboolib.util.chat.TextComponent;
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author IzzelAliz
|
||||
*/
|
||||
public class TLocale {
|
||||
|
||||
private TLocale() {
|
||||
throw new AssertionError();
|
||||
}
|
||||
|
||||
static String asString(String path, Class<?> callerClass, String... args) {
|
||||
return TLocaleLoader.asString(Ref.getCallerPlugin(callerClass), path, args);
|
||||
}
|
||||
|
||||
static List<String> asStringList(String path, Class<?> callerClass, String... args) {
|
||||
return TLocaleLoader.asStringList(Ref.getCallerPlugin(callerClass), path, args);
|
||||
}
|
||||
|
||||
private static void sendTo(String path, CommandSender sender, String[] args, Class<?> callerClass) {
|
||||
TLocaleLoader.sendTo(Ref.getCallerPlugin(callerClass), path, sender, args);
|
||||
}
|
||||
|
||||
public static void sendToConsole(String path, String... args) {
|
||||
Ref.getCallerClass(3).ifPresent(clazz -> sendTo(path, Bukkit.getConsoleSender(), args, clazz));
|
||||
}
|
||||
|
||||
public static void sendTo(CommandSender sender, String path, String... args) {
|
||||
Ref.getCallerClass(3).ifPresent(clazz -> sendTo(path, sender, args, clazz));
|
||||
}
|
||||
|
||||
public static void sendTo(String path, CommandSender sender, String... args) {
|
||||
Ref.getCallerClass(3).ifPresent(clazz -> sendTo(path, sender, args, clazz));
|
||||
}
|
||||
|
||||
public static void broadcast(String path, String... args) {
|
||||
Ref.getCallerClass(3).ifPresent(clazz -> Bukkit.getOnlinePlayers().forEach(player -> sendTo(path, player, args, clazz)));
|
||||
}
|
||||
|
||||
public static String asString(String path, String... args) {
|
||||
try {
|
||||
return asString(path, Ref.getCallerClass(3).orElse(TabooLib.class), args);
|
||||
} catch (Exception e) {
|
||||
TabooLib.getLogger().error(Strings.replaceWithOrder(TabooLib.getInst().getInternal().getString("FETCH-LOCALE-ERROR"), path));
|
||||
TabooLib.getLogger().error(Strings.replaceWithOrder(TabooLib.getInst().getInternal().getString("LOCALE-ERROR-REASON"), e.getMessage()));
|
||||
return "§4<" + path + "§4>";
|
||||
}
|
||||
}
|
||||
|
||||
public static List<String> asStringList(String path, String... args) {
|
||||
try {
|
||||
return asStringList(path, Ref.getCallerClass(3).orElse(TabooLib.class), args);
|
||||
} catch (Exception e) {
|
||||
TabooLib.getLogger().error(Strings.replaceWithOrder(TabooLib.getInst().getInternal().getString("LOCALE-ERROR-REASON"), e.getMessage()));
|
||||
return Collections.singletonList("§4<" + path + "§4>");
|
||||
}
|
||||
}
|
||||
|
||||
public static void reload() {
|
||||
Ref.getCallerClass(3).ifPresent(clazz -> TLocaleLoader.load(Ref.getCallerPlugin(clazz), false));
|
||||
}
|
||||
|
||||
public static final class Tellraw extends TLocale {
|
||||
|
||||
public static void send(CommandSender sender, String rawMessage) {
|
||||
if (sender instanceof Player) {
|
||||
TellrawCreator.getAbstractTellraw().sendRawMessage((Player) sender, rawMessage);
|
||||
} else {
|
||||
sender.sendMessage(TextComponent.toLegacyText(ComponentSerializer.parse(rawMessage)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static final class Display extends TLocale {
|
||||
|
||||
public static void sendTitle(Player player, String title, String subTitle) {
|
||||
sendTitle(player, title, subTitle, 10, 20, 10);
|
||||
}
|
||||
|
||||
public static void sendTitle(Player player, String title, String subTitle, int fadein, int stay, int fadeout) {
|
||||
NMSHandler.getHandler().sendTitle(player, title, fadein, stay, fadeout, subTitle, fadein, stay, fadeout);
|
||||
}
|
||||
|
||||
public static void sendActionBar(Player player, String text) {
|
||||
NMSHandler.getHandler().sendActionBar(player, text);
|
||||
}
|
||||
}
|
||||
|
||||
public static final class Translate extends TLocale {
|
||||
|
||||
public static boolean isPlaceholderUseDefault() {
|
||||
return TabooLib.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 setUncolored(String args) {
|
||||
return ChatColor.stripColor(args);
|
||||
}
|
||||
|
||||
public static List<String> setUncolored(List<String> args) {
|
||||
return args.stream().map(ChatColor::stripColor).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;
|
||||
}
|
||||
}
|
||||
|
||||
public static final class Logger extends TLocale {
|
||||
|
||||
public static void info(String path, String... 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 -> 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 -> 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 -> 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 -> 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 -> 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 -> asStringList(path, clazz, args).forEach(locale -> TLoggerManager.getLogger(Ref.getCallerPlugin(clazz)).verbose(locale)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
package io.izzel.taboolib.module.locale;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import io.izzel.taboolib.TabooLib;
|
||||
import io.izzel.taboolib.module.locale.type.TLocaleText;
|
||||
import io.izzel.taboolib.util.Strings;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
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.concurrent.atomic.AtomicInteger;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ThreadSafe
|
||||
@SuppressWarnings("rawtypes")
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return map.toString();
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return map.size();
|
||||
}
|
||||
|
||||
public Map<String, List<TLocaleSerialize>> getMap() {
|
||||
return map;
|
||||
}
|
||||
|
||||
public Plugin getPlugin() {
|
||||
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 -> {
|
||||
if (Bukkit.isPrimaryThread() || "true".equals(System.getProperty("tlib.forceAsync"))) {
|
||||
tSender.sendTo(sender, args);
|
||||
} else {
|
||||
Bukkit.getScheduler().runTask(plugin, () -> tSender.sendTo(sender, args));
|
||||
}
|
||||
});
|
||||
} catch (Exception | Error e) {
|
||||
TabooLib.getLogger().error(Strings.replaceWithOrder(TabooLib.getInst().getInternal().getString("SEND-LOCALE-ERROR"), path));
|
||||
TabooLib.getLogger().error(Strings.replaceWithOrder(TabooLib.getInst().getInternal().getString("LOCALE-ERROR-REASON"), e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
public String asString(String path, String... 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(TLocaleSerialize.getEmpty(path))).get(0).asStringList(args);
|
||||
}
|
||||
|
||||
private static boolean isListString(List list) {
|
||||
for (Object o : list) {
|
||||
if (!(o instanceof String)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public void load(YamlConfiguration configuration) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
package io.izzel.taboolib.module.locale;
|
||||
|
||||
import io.izzel.taboolib.TabooLib;
|
||||
import io.izzel.taboolib.TabooLibAPI;
|
||||
import io.izzel.taboolib.module.locale.type.*;
|
||||
import io.izzel.taboolib.module.config.TConfigWatcher;
|
||||
import io.izzel.taboolib.module.logger.TLogger;
|
||||
import io.izzel.taboolib.util.Files;
|
||||
import io.izzel.taboolib.util.IO;
|
||||
import io.izzel.taboolib.util.Strings;
|
||||
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 java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class TLocaleLoader {
|
||||
|
||||
private static final Map<String, TLocaleInstance> map = new ConcurrentHashMap<>();
|
||||
|
||||
static {
|
||||
ConfigurationSerialization.registerClass(TLocaleText.class, "TEXT");
|
||||
ConfigurationSerialization.registerClass(TLocaleJson.class, "JSON");
|
||||
ConfigurationSerialization.registerClass(TLocaleBook.class, "BOOK");
|
||||
ConfigurationSerialization.registerClass(TLocaleSound.class, "SOUND");
|
||||
ConfigurationSerialization.registerClass(TLocaleTitle.class, "TITLE");
|
||||
ConfigurationSerialization.registerClass(TLocaleBossBar.class, "BAR");
|
||||
ConfigurationSerialization.registerClass(TLocaleActionBar.class, "ACTION");
|
||||
}
|
||||
|
||||
public static void sendTo(Plugin plugin, String path, CommandSender sender, String... args) {
|
||||
TabooLibAPI.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 {
|
||||
synchronized (TLocaleLoader.class) {
|
||||
Optional.ofNullable(map.get(plugin.getName())).ifPresent(localeInstance -> localeInstance.sendTo(path, sender, args));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static String asString(Plugin plugin, String path, String... args) {
|
||||
TabooLibAPI.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);
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
public static List<String> asStringList(Plugin plugin, String path, String... args) {
|
||||
TabooLibAPI.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);
|
||||
} else {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
public static void load(Plugin plugin, boolean isCover) {
|
||||
try {
|
||||
if (isLoadLocale(plugin, isCover)) {
|
||||
// 获取文件
|
||||
File localeFile = getLocaleFile(plugin);
|
||||
if (localeFile == null) {
|
||||
return;
|
||||
}
|
||||
// 加载文件
|
||||
YamlConfiguration localeConfiguration = Files.loadYaml(plugin, localeFile);
|
||||
YamlConfiguration localeConfigurationAtStream = getLocaleAtStream(plugin, localeFile);
|
||||
// 载入配置
|
||||
loadPluginLocale(plugin, localeFile, localeConfiguration, localeConfigurationAtStream);
|
||||
// 注册监听
|
||||
TConfigWatcher.getInst().removeListener(localeFile);
|
||||
TConfigWatcher.getInst().addListener(localeFile, null, obj -> loadPluginLocale(plugin, localeFile, Files.loadYaml(plugin, localeFile), getLocaleAtStream(plugin, localeFile)));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
errorLogger("ERROR-LOADING-LANG", plugin.getName(), e.toString() + "\n" + e.getStackTrace()[0].toString());
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isLocaleLoaded(Plugin plugin) {
|
||||
return map.containsKey(plugin.getName());
|
||||
}
|
||||
|
||||
public static boolean isDependWithTabooLib(Plugin plugin) {
|
||||
return plugin.getClass().getSuperclass().getSimpleName().equals("TabooPlugin");
|
||||
}
|
||||
|
||||
public static List<String> getLocalePriority() {
|
||||
return TabooLib.getConfig().contains("LOCALE.PRIORITY") ? TabooLib.getConfig().getStringList("LOCALE.PRIORITY") : Collections.singletonList("zh_CN");
|
||||
}
|
||||
|
||||
private static boolean isLoadLocale(Plugin plugin, boolean isCover) {
|
||||
return isCover || !isLocaleLoaded(plugin);
|
||||
}
|
||||
|
||||
private static void infoLogger(String path, String... args) {
|
||||
TLogger.getGlobalLogger().info(Strings.replaceWithOrder(io.izzel.taboolib.TabooLib.getInst().getInternal().getString(path), args));
|
||||
}
|
||||
|
||||
private static void errorLogger(String path, String... args) {
|
||||
TLogger.getGlobalLogger().error(Strings.replaceWithOrder(io.izzel.taboolib.TabooLib.getInst().getInternal().getString(path), args));
|
||||
}
|
||||
|
||||
private static File getLocaleFile(Plugin plugin) {
|
||||
releaseLocales(plugin);
|
||||
return getLocalePriority().stream().map(localeName -> new File("plugins/" + plugin.getName() + "/lang/" + localeName + ".yml")).filter(File::exists).findFirst().orElse(null);
|
||||
}
|
||||
|
||||
private static void releaseLocales(Plugin plugin) {
|
||||
getLocalePriority().stream().filter(localeName -> !new File("plugins/" + plugin.getName() + "/lang/" + localeName + ".yml").exists() && plugin.getResource("lang/" + localeName + ".yml") != null).forEach(localeName -> plugin.saveResource("lang/" + localeName + ".yml", true));
|
||||
}
|
||||
|
||||
private static TLocaleInstance getLocaleInstance(Plugin plugin) {
|
||||
TLocaleInstance instance = new TLocaleInstance(plugin);
|
||||
map.put(plugin.getName(), instance);
|
||||
return instance;
|
||||
}
|
||||
|
||||
private static YamlConfiguration getLocaleAtStream(Plugin plugin, File localeFile) {
|
||||
InputStream localeInputSteam = Files.getResource(plugin, "lang/" + localeFile.getName());
|
||||
try {
|
||||
YamlConfiguration yaml = new YamlConfiguration();
|
||||
yaml.loadFromString(IO.readFully(localeInputSteam, Charset.forName("utf-8")));
|
||||
return yaml;
|
||||
} catch (Exception ignored) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static void loadPluginLocale(Plugin plugin, File localeFile, YamlConfiguration localeConfiguration, YamlConfiguration localeConfigurationAtStream) {
|
||||
TLocaleInstance localeInstance = getLocaleInstance(plugin);
|
||||
if (localeConfigurationAtStream != null) {
|
||||
localeInstance.load(localeConfigurationAtStream);
|
||||
}
|
||||
localeInstance.load(localeConfiguration);
|
||||
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(localeInstance.getLatestUpdateNodes().get()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package io.izzel.taboolib.module.locale;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
* @Since 2018-05-12 13:58
|
||||
*/
|
||||
public interface TLocaleSender {
|
||||
|
||||
void sendTo(CommandSender sender, String... args);
|
||||
|
||||
String asString(String... args);
|
||||
|
||||
List<String> asStringList(String... args);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package io.izzel.taboolib.module.locale;
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package io.izzel.taboolib.module.locale.type;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import io.izzel.taboolib.module.locale.TLocale;
|
||||
import io.izzel.taboolib.module.locale.TLocaleSerialize;
|
||||
import io.izzel.taboolib.module.compat.PlaceholderHook;
|
||||
import io.izzel.taboolib.util.Strings;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.configuration.serialization.SerializableAs;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
import java.util.Map;
|
||||
|
||||
@Immutable
|
||||
@SerializableAs("ACTION")
|
||||
public class TLocaleActionBar extends TLocaleSerialize {
|
||||
|
||||
private final String text;
|
||||
private final boolean papi;
|
||||
|
||||
private TLocaleActionBar(String text, boolean papi) {
|
||||
this.text = text;
|
||||
this.papi = papi;
|
||||
}
|
||||
|
||||
public static TLocaleActionBar valueOf(Map<String, Object> map) {
|
||||
String text = ChatColor.translateAlternateColorCodes('&', String.valueOf(map.getOrDefault("text", "Empty Action bar message.")));
|
||||
return new TLocaleActionBar(text, isPlaceholderEnabled(map));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendTo(CommandSender sender, String... args) {
|
||||
if (sender instanceof Player) {
|
||||
TLocale.Display.sendActionBar(((Player) sender), replace(sender, text, args));
|
||||
}
|
||||
}
|
||||
|
||||
private String replace(CommandSender sender, String text, String[] args) {
|
||||
String s = Strings.replaceWithOrder(text, args);
|
||||
return papi ? PlaceholderHook.replace(sender, s) : s;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asString(String... args) {
|
||||
return "ActionBar: [" + text + "]";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> serialize() {
|
||||
Map<String, Object> map = Maps.newHashMap();
|
||||
map.put("text", text);
|
||||
if (papi) {
|
||||
map.put("papi", true);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package io.izzel.taboolib.module.locale.type;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import io.izzel.taboolib.TabooLib;
|
||||
import io.izzel.taboolib.module.locale.TLocale;
|
||||
import io.izzel.taboolib.module.locale.TLocaleSerialize;
|
||||
import io.izzel.taboolib.module.tellraw.TellrawJson;
|
||||
import io.izzel.taboolib.origin.book.BookFormatter;
|
||||
import io.izzel.taboolib.origin.book.builder.BookBuilder;
|
||||
import io.izzel.taboolib.util.Strings;
|
||||
import io.izzel.taboolib.util.chat.ComponentSerializer;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.configuration.serialization.SerializableAs;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
* @Since 2018-05-27 0:05
|
||||
*/
|
||||
@ThreadSafe
|
||||
@SerializableAs("BOOK")
|
||||
public class TLocaleBook extends TLocaleSerialize {
|
||||
|
||||
/*
|
||||
BookTest:
|
||||
- ==: BOOK
|
||||
pages:
|
||||
0:
|
||||
- '第一页内容'
|
||||
- '[ <变量1@page-1> ]'
|
||||
1:
|
||||
- '第二页内容'
|
||||
- '[ <变量2@page-2> ]'
|
||||
args:
|
||||
page-1:
|
||||
hover: '展示内容1'
|
||||
command: '/say %player_name% NB1'
|
||||
page-2:
|
||||
hover: '展示内容2'
|
||||
suggest: '/say %player_name% NB2'
|
||||
*/
|
||||
|
||||
private final List<TellrawJson> pages;
|
||||
private final Map<String, Object> map;
|
||||
private final boolean papi;
|
||||
|
||||
public TLocaleBook(List<TellrawJson> pages, Map<String, Object> map, boolean papi) {
|
||||
this.pages = pages;
|
||||
this.map = map;
|
||||
this.papi = papi;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> serialize() {
|
||||
return Maps.newHashMap(map);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendTo(CommandSender sender, String... args) {
|
||||
if (!(sender instanceof Player)) {
|
||||
return;
|
||||
}
|
||||
new BukkitRunnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
BookBuilder bookBuilder = BookFormatter.writtenBook();
|
||||
pages.stream().map(jsonPage -> format(jsonPage, sender, args)).map(ComponentSerializer::parse).forEach(bookBuilder::addPages);
|
||||
new BukkitRunnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
BookFormatter.forceOpen((Player) sender, bookBuilder.build());
|
||||
}
|
||||
}.runTask(TabooLib.getPlugin());
|
||||
}
|
||||
}.runTaskAsynchronously(TabooLib.getPlugin());
|
||||
}
|
||||
|
||||
private String format(TellrawJson jsonPage, CommandSender sender, String[] args) {
|
||||
return papi ? TLocale.Translate.setPlaceholders(sender, Strings.replaceWithOrder(jsonPage.toRawMessage(), args)) : Strings.replaceWithOrder(jsonPage.toRawMessage(), args);
|
||||
}
|
||||
|
||||
public static TLocaleBook valueOf(Map<String, Object> map) {
|
||||
Map<String, Object> pages = map.containsKey("pages") ? (Map<String, Object>) map.get("pages") : new HashMap<>();
|
||||
Map<String, Object> section = map.containsKey("args") ? (Map<String, Object>) map.get("args") : new HashMap<>();
|
||||
List<TellrawJson> pageJsonList = pages.values().stream().map(page -> TLocaleJson.formatJson(section, page, TellrawJson.create())).collect(Collectors.toList());
|
||||
return new TLocaleBook(pageJsonList, map, isPlaceholderEnabled(map));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package io.izzel.taboolib.module.locale.type;
|
||||
|
||||
import io.izzel.taboolib.module.locale.TLocale;
|
||||
import io.izzel.taboolib.module.locale.TLocaleSerialize;
|
||||
import io.izzel.taboolib.util.Strings;
|
||||
import net.md_5.bungee.api.chat.TextComponent;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.configuration.serialization.SerializableAs;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.util.NumberConversions;
|
||||
import org.inventivetalent.bossbar.BossBar;
|
||||
import org.inventivetalent.bossbar.BossBarAPI;
|
||||
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
* @Since 2018-05-27 18:52
|
||||
*/
|
||||
@ThreadSafe
|
||||
@SerializableAs("BAR")
|
||||
public class TLocaleBossBar extends TLocaleSerialize {
|
||||
|
||||
/*
|
||||
BossBar:
|
||||
- ==: BAR
|
||||
text: 'BossBar 血条公告'
|
||||
color: BLUE
|
||||
style: NOTCHED_20
|
||||
progress: 1.0
|
||||
timeout: 20
|
||||
timeout-interval: 2
|
||||
*/
|
||||
|
||||
private final String text;
|
||||
private final BossBarAPI.Color color;
|
||||
private final BossBarAPI.Style style;
|
||||
private final float progress;
|
||||
private final int timeout;
|
||||
private final int timeoutInterval;
|
||||
private final boolean papi;
|
||||
|
||||
public TLocaleBossBar(String text, BossBarAPI.Color color, BossBarAPI.Style style, float progress, int timeout, int timeoutInterval, boolean papi) {
|
||||
this.text = text;
|
||||
this.color = color;
|
||||
this.style = style;
|
||||
this.progress = progress;
|
||||
this.timeout = timeout;
|
||||
this.timeoutInterval = timeoutInterval;
|
||||
this.papi = papi;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendTo(CommandSender sender, String... args) {
|
||||
if (Bukkit.getPluginManager().getPlugin("BossBarAPI") == null) {
|
||||
TLocale.Logger.error("LOCALE.BAR-PLUGIN-NOT-FOUND");
|
||||
return;
|
||||
}
|
||||
if (sender instanceof Player) {
|
||||
TextComponent textComponent = new TextComponent(papi ? TLocale.Translate.setPlaceholders(sender, Strings.replaceWithOrder(text, args)) : TLocale.Translate.setColored(Strings.replaceWithOrder(text, args)));
|
||||
BossBar bossBar = BossBarAPI.addBar((Player) sender, textComponent, color, style, progress, timeout, timeoutInterval);
|
||||
} else {
|
||||
sender.sendMessage(text);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> serialize() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static TLocaleBossBar valueOf(Map<String, Object> map) {
|
||||
return new TLocaleBossBar(
|
||||
map.getOrDefault("text", "§4* Invalid Text*").toString(),
|
||||
getColor(String.valueOf(map.get("color"))),
|
||||
getStyle(String.valueOf(map.get("style"))),
|
||||
NumberConversions.toFloat(map.getOrDefault("progress", 1)),
|
||||
NumberConversions.toInt(map.getOrDefault("timeout", 20)),
|
||||
NumberConversions.toInt(map.getOrDefault("timeout-interval", 2)),
|
||||
isPlaceholderEnabled(map));
|
||||
}
|
||||
|
||||
private static BossBarAPI.Color getColor(String color) {
|
||||
try {
|
||||
return BossBarAPI.Color.valueOf(color);
|
||||
} catch (Exception e) {
|
||||
TLocale.Logger.error("LOCALE.BAR-STYLE-IDENTIFICATION-FAILED", e.toString());
|
||||
return BossBarAPI.Color.WHITE;
|
||||
}
|
||||
}
|
||||
|
||||
private static BossBarAPI.Style getStyle(String style) {
|
||||
try {
|
||||
return BossBarAPI.Style.valueOf(style);
|
||||
} catch (Exception e) {
|
||||
TLocale.Logger.error("LOCALE.BAR-COLOR-IDENTIFICATION-FAILED", e.toString());
|
||||
return BossBarAPI.Style.NOTCHED_20;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,222 @@
|
||||
package io.izzel.taboolib.module.locale.type;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
import io.izzel.taboolib.TabooLib;
|
||||
import io.izzel.taboolib.module.locale.TLocale;
|
||||
import io.izzel.taboolib.module.locale.TLocaleSerialize;
|
||||
import io.izzel.taboolib.module.compat.PlaceholderHook;
|
||||
import io.izzel.taboolib.module.tellraw.TellrawJson;
|
||||
import io.izzel.taboolib.util.Strings;
|
||||
import io.izzel.taboolib.util.Variables;
|
||||
import io.izzel.taboolib.util.chat.*;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.configuration.serialization.SerializableAs;
|
||||
import org.bukkit.util.NumberConversions;
|
||||
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
import java.util.*;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
||||
@ThreadSafe
|
||||
@SerializableAs("JSON")
|
||||
public class TLocaleJson extends TLocaleSerialize {
|
||||
|
||||
private static final Pattern pattern = Pattern.compile("<([^<>]*)?@([^<>]*)>");
|
||||
private final List<BaseComponent[]> components;
|
||||
private final boolean papi;
|
||||
private final Map<String, Object> map;
|
||||
|
||||
private TLocaleJson(List<BaseComponent[]> components, boolean papi, Map<String, Object> map) {
|
||||
this.components = ImmutableList.copyOf(components);
|
||||
this.papi = papi;
|
||||
this.map = map;
|
||||
}
|
||||
|
||||
public static TLocaleJson valueOf(Map<String, Object> map) {
|
||||
List<String> textList = getTextList(map.getOrDefault("text", "Empty Node"));
|
||||
|
||||
// 分析 args 并替换
|
||||
Object argsObj = map.get("args");
|
||||
if (argsObj instanceof Map) {
|
||||
Map<String, Object> section = new HashMap<>(((Map<?, ?>) argsObj).size());
|
||||
|
||||
// valueOf(k) 是因为这个键可能加载为一个 Integer 导致 contains(String) 返回 false
|
||||
((Map<?, ?>) argsObj).forEach((k, v) -> section.put(String.valueOf(k), v));
|
||||
List<BaseComponent[]> collect = textList.stream().map(s -> {
|
||||
int index = 0;
|
||||
String[] template = pattern.split(s);
|
||||
Matcher matcher = pattern.matcher(s);
|
||||
// 有可能开头和结尾是替换文本,所以做个特判
|
||||
List<BaseComponent> builder = template.length > index ? new ArrayList<>(Arrays.asList(TextComponent.fromLegacyText(template[index++]))) : new ArrayList<>();
|
||||
while (matcher.find()) {
|
||||
String replace = matcher.group();
|
||||
// 假的 <@>
|
||||
if (replace.length() <= 2) {
|
||||
continue;
|
||||
}
|
||||
// 真的 <@xxx>
|
||||
replace = replace.substring(1, replace.length() - 1);
|
||||
String[] split = replace.split("@");
|
||||
// @ 前面的字符串
|
||||
String text = split.length > 1 ? split[0] : "";
|
||||
// @ 后面的节点名
|
||||
String node = split.length > 1 ? split[1] : split[0];
|
||||
// 如果 args 有这个 xxx
|
||||
if (section.containsKey(node)) {
|
||||
Map<String, Object> arg = (Map<String, Object>) section.get(node);
|
||||
text = TLocale.Translate.setColored(String.valueOf(arg.getOrDefault("text", text)));
|
||||
// 可能有很多个 BaseComponent,于是为每个 component 单独设置各种事件
|
||||
BaseComponent[] component = TextComponent.fromLegacyText(text);
|
||||
arg.forEach((key, value) -> {
|
||||
if (key.equalsIgnoreCase("suggest")) {
|
||||
Arrays.stream(component).forEach(baseComponent -> baseComponent.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, String.valueOf(value))));
|
||||
} else if (key.equalsIgnoreCase("command") || "commands".equalsIgnoreCase(key)) {
|
||||
Arrays.stream(component).forEach(baseComponent -> baseComponent.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, String.valueOf(value))));
|
||||
} else if (key.equalsIgnoreCase("hover")) {
|
||||
Arrays.stream(component).forEach(baseComponent -> baseComponent.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(TLocale.Translate.setColored(String.valueOf(value))).create())));
|
||||
}
|
||||
});
|
||||
// 添加到原来的 list 里面
|
||||
builder.addAll(Arrays.asList(component));
|
||||
} else {
|
||||
// 这个参数节点并没有找到,于是随便放点字符串进去
|
||||
builder.addAll(Arrays.asList(TextComponent.fromLegacyText(text)));
|
||||
TabooLib.getLogger().warn(Strings.replaceWithOrder(TabooLib.getInst().getInternal().getString("MISSING-ARGUMENT"), node));
|
||||
}
|
||||
// 有可能一开头就是 <@xxx>,然后 split 出来就少了一些,于是直接加上
|
||||
if (index < template.length) {
|
||||
builder.addAll(Arrays.asList(TextComponent.fromLegacyText(template[index++])));
|
||||
}
|
||||
}
|
||||
return builder.toArray(new BaseComponent[0]);
|
||||
}).collect(Collectors.toList());
|
||||
return new TLocaleJson(collect, isPlaceholderEnabled(map), 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(TLocale.Translate::setColored).collect(Collectors.toList());
|
||||
} else if (textObj instanceof String) {
|
||||
return Lists.newArrayList(TLocale.Translate.setColored((String) textObj));
|
||||
} else {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendTo(CommandSender sender, String... args) {
|
||||
components.forEach(comp -> sendRawMessage(sender, replace(comp, sender, args)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asString(String... args) {
|
||||
return components.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> serialize() {
|
||||
return Maps.newHashMap(map);
|
||||
}
|
||||
|
||||
private void sendRawMessage(CommandSender sender, BaseComponent[] components) {
|
||||
TLocale.Tellraw.send(sender, ComponentSerializer.toString(components));
|
||||
}
|
||||
|
||||
private List<BaseComponent> replace(List<BaseComponent> component, CommandSender sender, String... args) {
|
||||
return component.stream().map(c -> replace(c, sender, args)).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private BaseComponent[] replace(BaseComponent[] component, CommandSender sender, String... args) {
|
||||
BaseComponent[] components = new BaseComponent[component.length];
|
||||
for (int i = 0; i < components.length; i++) {
|
||||
components[i] = replace(component[i].duplicate(), sender, args);
|
||||
}
|
||||
return components;
|
||||
}
|
||||
|
||||
private BaseComponent replace(BaseComponent component, CommandSender sender, String... args) {
|
||||
if (component.getClickEvent() != null) {
|
||||
ClickEvent clickEvent = new ClickEvent(component.getClickEvent().getAction(), replace(sender, component.getClickEvent().getValue(), args));
|
||||
component.setClickEvent(clickEvent);
|
||||
}
|
||||
if (component.getHoverEvent() != null) {
|
||||
HoverEvent hoverEvent = new HoverEvent(component.getHoverEvent().getAction(), replace(component.getHoverEvent().getValue(), sender, args));
|
||||
component.setHoverEvent(hoverEvent);
|
||||
}
|
||||
if (component.getExtra() != null) {
|
||||
component.setExtra(replace(component.getExtra(), sender, args));
|
||||
}
|
||||
if (component instanceof TextComponent) {
|
||||
((TextComponent) component).setText(replace(sender, ((TextComponent) component).getText(), args));
|
||||
}
|
||||
return component;
|
||||
}
|
||||
|
||||
private String replace(CommandSender sender, String text, String[] args) {
|
||||
String s = Strings.replaceWithOrder(text, args);
|
||||
return papi ? PlaceholderHook.replace(sender, s) : s;
|
||||
}
|
||||
|
||||
public static TellrawJson formatJson(Map<String, Object> section, Object textObject, TellrawJson pageJson) {
|
||||
List<String> textList = textObject instanceof List ? (List<String>) textObject : Collections.singletonList(String.valueOf(textObject));
|
||||
// 遍历本页文本
|
||||
for (int i = 0; i < textList.size(); i++) {
|
||||
// 捕捉变量
|
||||
for (Variables.Variable variable : new Variables(TLocale.Translate.setColored(textList.get(i)), pattern).find().getVariableList()) {
|
||||
// 如果是变量
|
||||
if (variable.isVariable()) {
|
||||
String[] split = variable.getText().split("@");
|
||||
// @ 前面的字符串
|
||||
String text = split.length > 1 ? split[0] : "§4* Invalid Text *";
|
||||
// @ 后面的节点名
|
||||
String node = split.length > 1 ? split[1] : null;
|
||||
// 处理变量
|
||||
formatNode(section, pageJson, text, node);
|
||||
} else {
|
||||
pageJson.append(variable.getText());
|
||||
}
|
||||
}
|
||||
if (i + 1 < textList.size()) {
|
||||
pageJson.newLine();
|
||||
}
|
||||
}
|
||||
return pageJson;
|
||||
}
|
||||
|
||||
private static void formatNode(Map<String, Object> section, TellrawJson pageJson, String text, String node) {
|
||||
if (section.containsKey(node)) {
|
||||
try {
|
||||
Map<String, Object> args = (Map<String, Object>) section.get(node);
|
||||
// 文本
|
||||
pageJson.append(args.getOrDefault("text", text).toString());
|
||||
// 功能
|
||||
if (args.containsKey("hover")) {
|
||||
pageJson.hoverText(args.get("hover").toString());
|
||||
}
|
||||
if (args.containsKey("suggest")) {
|
||||
pageJson.clickSuggest(args.get("suggest").toString());
|
||||
}
|
||||
if (args.containsKey("command")) {
|
||||
pageJson.clickCommand(args.get("command").toString());
|
||||
}
|
||||
if (args.containsKey("page")) {
|
||||
pageJson.clickChangePage(NumberConversions.toInt(args.get("page").toString()));
|
||||
}
|
||||
if (args.containsKey("url")) {
|
||||
pageJson.clickOpenURL(args.get("url").toString());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
TLocale.Logger.error("LOCALE.BOOK-ARGUMENTS-IDENTIFICATION-FAILED", e.toString());
|
||||
}
|
||||
} else {
|
||||
pageJson.append("§4* Invalid Argument *");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package io.izzel.taboolib.module.locale.type;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import io.izzel.taboolib.module.locale.TLocaleSerialize;
|
||||
import io.izzel.taboolib.origin.lite.Sounds;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.configuration.serialization.SerializableAs;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
* @Since 2018-05-06 14:35
|
||||
*/
|
||||
@Immutable
|
||||
@SerializableAs("ACTION")
|
||||
public class TLocaleSound extends TLocaleSerialize {
|
||||
|
||||
private final List<Sounds> soundPacks;
|
||||
|
||||
public TLocaleSound(List<Sounds> soundPacks) {
|
||||
this.soundPacks = soundPacks;
|
||||
}
|
||||
|
||||
public static TLocaleSound valueOf(Map<String, Object> map) {
|
||||
List<Sounds> soundPacks = new ArrayList<>();
|
||||
Object sounds = map.containsKey("sounds") ? map.get("sounds") : map.getOrDefault("sound", "");
|
||||
if (sounds instanceof List) {
|
||||
soundPacks = ((List<String>) sounds).stream().map(Sounds::new).collect(Collectors.toList());
|
||||
} else {
|
||||
soundPacks.add(new Sounds(sounds.toString()));
|
||||
}
|
||||
return new TLocaleSound(soundPacks);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendTo(CommandSender sender, String... args) {
|
||||
if (sender instanceof Player) {
|
||||
soundPacks.forEach(x -> x.play((Player) sender));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asString(String... args) {
|
||||
return toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "soundPacks=" + "TLocaleSound{" + soundPacks + '}';
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> serialize() {
|
||||
Map<String, Object> map = Maps.newHashMap();
|
||||
if (soundPacks.size() == 1) {
|
||||
map.put("sounds", soundPacks.get(0).toString());
|
||||
} else if (soundPacks.size() > 1) {
|
||||
map.put("sounds", soundPacks.stream().map(Sounds::toString).collect(Collectors.toList()));
|
||||
}
|
||||
return map;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
package io.izzel.taboolib.module.locale.type;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.Maps;
|
||||
import io.izzel.taboolib.module.locale.TLocale;
|
||||
import io.izzel.taboolib.module.locale.TLocaleSerialize;
|
||||
import io.izzel.taboolib.util.Strings;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.configuration.serialization.SerializableAs;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Immutable
|
||||
@SerializableAs("TEXT")
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
public class TLocaleText extends TLocaleSerialize {
|
||||
|
||||
private final Object text;
|
||||
private final boolean usePlaceholder;
|
||||
|
||||
private TLocaleText(Object text, boolean usePlaceholder) {
|
||||
this.usePlaceholder = usePlaceholder;
|
||||
if (text instanceof String) {
|
||||
this.text = text;
|
||||
} else if (text instanceof List) {
|
||||
this.text = ImmutableList.copyOf(((List) text));
|
||||
} else {
|
||||
throw new IllegalArgumentException("Param 'text' can only be an instance of String or List<String>");
|
||||
}
|
||||
}
|
||||
|
||||
public static TLocaleText of(String s) {
|
||||
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(replaceText(sender, Strings.replaceWithOrder((String) text, args)));
|
||||
} else if (text instanceof List) {
|
||||
((List) text).forEach(s -> sender.sendMessage(replaceText(sender, Strings.replaceWithOrder(String.valueOf(s), args))));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asString(String... args) {
|
||||
return Strings.replaceWithOrder(TLocale.Translate.setColored(objectToString(text)), args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> asStringList(String... args) {
|
||||
if (text instanceof List) {
|
||||
return ((List<String>) text).stream().map(x -> Strings.replaceWithOrder(TLocale.Translate.setColored(x), args)).collect(Collectors.toList());
|
||||
} else {
|
||||
return Collections.singletonList(asString(args));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
if (text instanceof String[]) {
|
||||
return Arrays.toString((String[]) text);
|
||||
} else {
|
||||
return text.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> serialize() {
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package io.izzel.taboolib.module.locale.type;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import io.izzel.taboolib.module.locale.TLocale;
|
||||
import io.izzel.taboolib.module.locale.TLocaleSerialize;
|
||||
import io.izzel.taboolib.util.Strings;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.configuration.serialization.SerializableAs;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Bkm016
|
||||
* @since 2018-04-22
|
||||
*/
|
||||
|
||||
@Immutable
|
||||
@SerializableAs("TITLE")
|
||||
public class TLocaleTitle extends TLocaleSerialize {
|
||||
|
||||
private final String title;
|
||||
private final String subtitle;
|
||||
private final int fadein;
|
||||
private final int fadeout;
|
||||
private final int stay;
|
||||
|
||||
private boolean usePlaceholder;
|
||||
|
||||
private TLocaleTitle(String title, String subString, int fadein, int fadeout, int stay, boolean usePlaceholder) {
|
||||
this.title = title;
|
||||
this.subtitle = subString;
|
||||
this.fadein = fadein;
|
||||
this.fadeout = fadeout;
|
||||
this.stay = stay;
|
||||
this.usePlaceholder = usePlaceholder;
|
||||
}
|
||||
|
||||
public static TLocaleTitle valueOf(Map<String, Object> map) {
|
||||
TLocaleTitle title;
|
||||
try {
|
||||
title = new TLocaleTitle(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);
|
||||
}
|
||||
return title;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendTo(CommandSender sender, String... args) {
|
||||
if (sender instanceof Player) {
|
||||
TLocale.Display.sendTitle((Player) sender, replaceText(sender, Strings.replaceWithOrder(title, args)), replaceText(sender, Strings.replaceWithOrder(subtitle, args)), fadein, stay, fadeout);
|
||||
} else {
|
||||
TLocale.Logger.error("LOCALE.TITLE-SEND-TO-NON-PLAYER", asString(args));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String asString(String... args) {
|
||||
return Strings.replaceWithOrder(Strings.replaceWithOrder("TITLE: [title: ''{0}'', subtitle: ''{1}'', fadeIn: {2}, fadeOut: {3}]", title, subtitle, fadein, fadeout), args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return asString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> serialize() {
|
||||
HashMap<String, Object> map = Maps.newHashMap();
|
||||
map.put("papi", usePlaceholder);
|
||||
map.put("title", title);
|
||||
map.put("subtitle", subtitle);
|
||||
map.put("fadein", fadein);
|
||||
map.put("fadeout", fadeout);
|
||||
map.put("stay", stay);
|
||||
return map;
|
||||
}
|
||||
|
||||
private String replaceText(CommandSender sender, String text, String... args) {
|
||||
return usePlaceholder ? TLocale.Translate.setPlaceholders(sender, text) : TLocale.Translate.setColored(text);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package io.izzel.taboolib.module.logger;
|
||||
|
||||
import io.izzel.taboolib.TabooLibAPI;
|
||||
import io.izzel.taboolib.locale.TLocale;
|
||||
import io.izzel.taboolib.module.locale.TLocale;
|
||||
import io.izzel.taboolib.util.Strings;
|
||||
import net.md_5.bungee.BungeeCord;
|
||||
import net.md_5.bungee.api.chat.TextComponent;
|
||||
|
||||
@@ -2,7 +2,7 @@ package io.izzel.taboolib.module.mysql.hikari;
|
||||
|
||||
import com.zaxxer.hikari.HikariConfig;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import io.izzel.taboolib.locale.TLocale;
|
||||
import io.izzel.taboolib.module.locale.TLocale;
|
||||
import io.izzel.taboolib.module.config.TConfig;
|
||||
import io.izzel.taboolib.module.inject.TInject;
|
||||
import io.izzel.taboolib.module.mysql.IHost;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package io.izzel.taboolib.module.tellraw;
|
||||
|
||||
import io.izzel.taboolib.locale.TLocale;
|
||||
import io.izzel.taboolib.module.locale.TLocale;
|
||||
import io.izzel.taboolib.util.ArrayUtil;
|
||||
import io.izzel.taboolib.util.Strings;
|
||||
import io.izzel.taboolib.util.chat.*;
|
||||
|
||||
Reference in New Issue
Block a user