Taboolib 5.0 fully refactored & Not a plugin now.

This commit is contained in:
坏黑 2019-07-06 00:25:35 +08:00
parent ab3ed6bebe
commit 9af34b9940
40 changed files with 237 additions and 53 deletions

View File

@ -2,7 +2,7 @@ package io.izzel.taboolib;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import io.izzel.taboolib.locale.TLocaleLoader;
import io.izzel.taboolib.module.locale.TLocaleLoader;
import io.izzel.taboolib.module.command.TCommandHandler;
import io.izzel.taboolib.module.config.TConfig;
import io.izzel.taboolib.module.config.TConfigWatcher;

View File

@ -1,6 +1,6 @@
package io.izzel.taboolib;
import io.izzel.taboolib.locale.TLocaleLoader;
import io.izzel.taboolib.module.locale.TLocaleLoader;
import io.izzel.taboolib.module.config.TConfig;
import io.izzel.taboolib.module.config.TConfigWatcher;
import io.izzel.taboolib.module.dependency.Dependency;

View File

@ -1,7 +1,7 @@
package io.izzel.taboolib.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.command.base.BaseSubCommand;
import io.izzel.taboolib.module.command.TCommand;

View File

@ -1,7 +1,7 @@
package io.izzel.taboolib.command;
import io.izzel.taboolib.locale.TLocale;
import io.izzel.taboolib.locale.TLocaleLoader;
import io.izzel.taboolib.module.locale.TLocale;
import io.izzel.taboolib.module.locale.TLocaleLoader;
import io.izzel.taboolib.module.command.base.BaseMainCommand;
import io.izzel.taboolib.module.command.base.BaseSubCommand;
import io.izzel.taboolib.module.command.TCommand;

View File

@ -2,7 +2,7 @@ package io.izzel.taboolib.command;
import com.google.common.base.Joiner;
import io.izzel.taboolib.TabooLibAPI;
import io.izzel.taboolib.locale.TLocale;
import io.izzel.taboolib.module.locale.TLocale;
import io.izzel.taboolib.module.command.TCommand;
import io.izzel.taboolib.module.command.base.BaseMainCommand;
import io.izzel.taboolib.module.command.base.BaseSubCommand;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;
/**

View File

@ -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();
}
}
}

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -1,4 +1,4 @@
package io.izzel.taboolib.locale;
package io.izzel.taboolib.module.locale;
import io.izzel.taboolib.TabooLib;
import io.izzel.taboolib.module.logger.TLoggerManager;

View File

@ -1,8 +1,8 @@
package io.izzel.taboolib.locale;
package io.izzel.taboolib.module.locale;
import com.google.common.collect.ImmutableList;
import io.izzel.taboolib.TabooLib;
import io.izzel.taboolib.locale.type.TLocaleText;
import io.izzel.taboolib.module.locale.type.TLocaleText;
import io.izzel.taboolib.util.Strings;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;

View File

@ -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()));
}
}
}

View File

@ -1,4 +1,4 @@
package io.izzel.taboolib.locale;
package io.izzel.taboolib.module.locale;
import org.bukkit.command.CommandSender;

View File

@ -1,4 +1,4 @@
package io.izzel.taboolib.locale;
package io.izzel.taboolib.module.locale;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.serialization.ConfigurationSerializable;

View File

@ -1,8 +1,8 @@
package io.izzel.taboolib.locale.type;
package io.izzel.taboolib.module.locale.type;
import com.google.common.collect.Maps;
import io.izzel.taboolib.locale.TLocale;
import io.izzel.taboolib.locale.TLocaleSerialize;
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;

View File

@ -1,9 +1,9 @@
package io.izzel.taboolib.locale.type;
package io.izzel.taboolib.module.locale.type;
import com.google.common.collect.Maps;
import io.izzel.taboolib.TabooLib;
import io.izzel.taboolib.locale.TLocale;
import io.izzel.taboolib.locale.TLocaleSerialize;
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;

View File

@ -1,7 +1,7 @@
package io.izzel.taboolib.locale.type;
package io.izzel.taboolib.module.locale.type;
import io.izzel.taboolib.locale.TLocale;
import io.izzel.taboolib.locale.TLocaleSerialize;
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;

View File

@ -1,11 +1,11 @@
package io.izzel.taboolib.locale.type;
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.locale.TLocale;
import io.izzel.taboolib.locale.TLocaleSerialize;
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;

View File

@ -1,7 +1,7 @@
package io.izzel.taboolib.locale.type;
package io.izzel.taboolib.module.locale.type;
import com.google.common.collect.Maps;
import io.izzel.taboolib.locale.TLocaleSerialize;
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;

View File

@ -1,10 +1,10 @@
package io.izzel.taboolib.locale.type;
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.locale.TLocale;
import io.izzel.taboolib.locale.TLocaleSerialize;
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;

View File

@ -1,8 +1,8 @@
package io.izzel.taboolib.locale.type;
package io.izzel.taboolib.module.locale.type;
import com.google.common.collect.Maps;
import io.izzel.taboolib.locale.TLocale;
import io.izzel.taboolib.locale.TLocaleSerialize;
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;

View File

@ -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;

View File

@ -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;

View File

@ -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.*;

View File

@ -1,7 +1,7 @@
package io.izzel.taboolib.origin.client;
import io.izzel.taboolib.TabooLib;
import io.izzel.taboolib.locale.TLocale;
import io.izzel.taboolib.module.locale.TLocale;
import io.izzel.taboolib.module.command.lite.SimpleCommandBuilder;
import io.izzel.taboolib.origin.client.packet.Packet;
import io.izzel.taboolib.origin.client.packet.PacketSerializer;

View File

@ -1,6 +1,6 @@
package io.izzel.taboolib.origin.client.packet.impl;
import io.izzel.taboolib.locale.TLocale;
import io.izzel.taboolib.module.locale.TLocale;
import io.izzel.taboolib.origin.client.TabooLibServer;
import io.izzel.taboolib.origin.client.packet.Packet;
import io.izzel.taboolib.origin.client.packet.PacketType;

View File

@ -1,6 +1,6 @@
package io.izzel.taboolib.origin.client.packet.impl;
import io.izzel.taboolib.locale.TLocale;
import io.izzel.taboolib.module.locale.TLocale;
import io.izzel.taboolib.origin.client.TabooLibServer;
import io.izzel.taboolib.origin.client.packet.Packet;
import io.izzel.taboolib.origin.client.packet.PacketType;

View File

@ -1,6 +1,6 @@
package io.izzel.taboolib.origin.client.packet.impl;
import io.izzel.taboolib.locale.TLocale;
import io.izzel.taboolib.module.locale.TLocale;
import io.izzel.taboolib.origin.client.TabooLibServer;
import io.izzel.taboolib.origin.client.packet.Packet;
import io.izzel.taboolib.origin.client.packet.PacketType;

View File

@ -1,7 +1,7 @@
package io.izzel.taboolib.origin.cronus.util;
import io.izzel.taboolib.module.logger.TLogger;
import io.izzel.taboolib.locale.TLocale;
import io.izzel.taboolib.module.locale.TLocale;
import io.izzel.taboolib.module.inject.TInject;
import java.util.regex.Matcher;

View File

@ -1,7 +1,7 @@
package io.izzel.taboolib.origin.database;
import io.izzel.taboolib.TabooLib;
import io.izzel.taboolib.locale.TLocale;
import io.izzel.taboolib.module.locale.TLocale;
import io.izzel.taboolib.util.Files;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;

View File

@ -1,7 +1,7 @@
package io.izzel.taboolib.origin.lite;
import io.izzel.taboolib.TabooLib;
import io.izzel.taboolib.locale.TLocale;
import io.izzel.taboolib.module.locale.TLocale;
import io.izzel.taboolib.module.command.TCommand;
import io.izzel.taboolib.module.command.base.BaseMainCommand;
import io.izzel.taboolib.module.command.base.BaseSubCommand;

View File

@ -1,7 +1,7 @@
package io.izzel.taboolib.util;
import io.izzel.taboolib.TabooLib;
import io.izzel.taboolib.locale.TLocale;
import io.izzel.taboolib.module.locale.TLocale;
import io.izzel.taboolib.plugin.InternalPlugin;
import io.izzel.taboolib.util.eagletdl.EagletTask;
import io.izzel.taboolib.util.eagletdl.ProgressEvent;

View File

@ -1,6 +1,6 @@
package io.lzzel.tlibscala
import io.izzel.taboolib.locale.TLocale.Logger
import io.izzel.taboolib.module.locale.TLocale.Logger
import org.bukkit.Bukkit
import org.bukkit.event.{Event, EventException, EventPriority, Listener}
import org.bukkit.plugin.{EventExecutor, Plugin}

View File

@ -1,6 +1,6 @@
package io.lzzel.tlibscala.runtime
import io.izzel.taboolib.locale.TLocale
import io.izzel.taboolib.module.locale.TLocale
import io.izzel.taboolib.module.compat.PermissionHook
import io.izzel.taboolib.origin.lite.Boards
import org.bukkit.entity.Player