+ update
This commit is contained in:
parent
3b3212dcdc
commit
91840d6a6b
2
pom.xml
2
pom.xml
@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
<groupId>me.skymc</groupId>
|
<groupId>me.skymc</groupId>
|
||||||
<artifactId>TabooLib</artifactId>
|
<artifactId>TabooLib</artifactId>
|
||||||
<version>4.51</version>
|
<version>4.52</version>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
@ -186,6 +186,8 @@ public class Main extends JavaPlugin {
|
|||||||
TLocale.Logger.error("NOTIFY.FAIL-DISABLE");
|
TLocale.Logger.error("NOTIFY.FAIL-DISABLE");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
// 注销插件
|
||||||
|
TabooLibLoader.unregister();
|
||||||
// 保存数据
|
// 保存数据
|
||||||
Bukkit.getOnlinePlayers().forEach(x -> DataUtils.saveOnline(x.getName()));
|
Bukkit.getOnlinePlayers().forEach(x -> DataUtils.saveOnline(x.getName()));
|
||||||
// 结束线程
|
// 结束线程
|
||||||
@ -204,8 +206,6 @@ public class Main extends JavaPlugin {
|
|||||||
HikariHandler.closeDataSourceForce();
|
HikariHandler.closeDataSourceForce();
|
||||||
// 注销监听器
|
// 注销监听器
|
||||||
TListenerHandler.cancelListeners();
|
TListenerHandler.cancelListeners();
|
||||||
// 注销子模块
|
|
||||||
TFunctionLoader.unloadFunction();
|
|
||||||
// 结束数据库储存方法
|
// 结束数据库储存方法
|
||||||
if (getStorageType() == StorageType.SQL) {
|
if (getStorageType() == StorageType.SQL) {
|
||||||
GlobalDataManager.SQLMethod.cancelSQLMethod();
|
GlobalDataManager.SQLMethod.cancelSQLMethod();
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package me.skymc.taboolib;
|
package me.skymc.taboolib;
|
||||||
|
|
||||||
|
import com.google.common.collect.Lists;
|
||||||
|
import com.google.common.collect.Maps;
|
||||||
import com.ilummc.tlib.TLib;
|
import com.ilummc.tlib.TLib;
|
||||||
import com.ilummc.tlib.annotations.Dependency;
|
import com.ilummc.tlib.annotations.Dependency;
|
||||||
import com.ilummc.tlib.inject.TDependencyInjector;
|
import com.ilummc.tlib.inject.TDependencyInjector;
|
||||||
@ -8,6 +10,7 @@ import me.skymc.taboolib.bstats.Metrics;
|
|||||||
import me.skymc.taboolib.fileutils.FileUtils;
|
import me.skymc.taboolib.fileutils.FileUtils;
|
||||||
import me.skymc.taboolib.listener.TListener;
|
import me.skymc.taboolib.listener.TListener;
|
||||||
import me.skymc.taboolib.listener.TListenerHandler;
|
import me.skymc.taboolib.listener.TListenerHandler;
|
||||||
|
import me.skymc.taboolib.methods.ReflectionUtils;
|
||||||
import me.skymc.taboolib.playerdata.DataUtils;
|
import me.skymc.taboolib.playerdata.DataUtils;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
@ -20,10 +23,7 @@ import org.bukkit.plugin.Plugin;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.util.Arrays;
|
import java.util.*;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Author sky
|
* @Author sky
|
||||||
@ -32,7 +32,8 @@ import java.util.Optional;
|
|||||||
@TListener
|
@TListener
|
||||||
public class TabooLibLoader implements Listener {
|
public class TabooLibLoader implements Listener {
|
||||||
|
|
||||||
static HashMap<String, List<Class>> pluginClasses = new HashMap<>();
|
static Map<String, List<Class>> pluginClasses = Maps.newHashMap();
|
||||||
|
static List<Loader> loaders = Lists.newArrayList();
|
||||||
|
|
||||||
static void setup() {
|
static void setup() {
|
||||||
testInternet();
|
testInternet();
|
||||||
@ -43,84 +44,69 @@ public class TabooLibLoader implements Listener {
|
|||||||
|
|
||||||
static void register() {
|
static void register() {
|
||||||
setupClasses();
|
setupClasses();
|
||||||
|
loadClasses();
|
||||||
registerListener();
|
registerListener();
|
||||||
registerMetrics();
|
registerMetrics();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
static void unregister() {
|
||||||
* 获取插件所有被读取到的类
|
unloadClasses();
|
||||||
*
|
}
|
||||||
* @param plugin 插件
|
|
||||||
* @return List
|
|
||||||
*/
|
|
||||||
public static Optional<List<Class>> getPluginClasses(Plugin plugin) {
|
public static Optional<List<Class>> getPluginClasses(Plugin plugin) {
|
||||||
return Optional.ofNullable(pluginClasses.get(plugin.getName()));
|
return Optional.ofNullable(pluginClasses.get(plugin.getName()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public static List<Class> getPluginClassSafely(Plugin plugin) {
|
||||||
* 初始化插件文件夹
|
List<Class> classes = pluginClasses.get(plugin.getName());
|
||||||
*/
|
return classes == null ? new ArrayList<>() : new ArrayList<>(classes);
|
||||||
|
}
|
||||||
|
|
||||||
|
static boolean isLoader(Class pluginClass) {
|
||||||
|
return !Loader.class.equals(pluginClass) && Loader.class.isAssignableFrom(pluginClass);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void loadClasses() {
|
||||||
|
pluginClasses.forEach((key, classes) -> classes.forEach(pluginClass -> loadClass(Bukkit.getPluginManager().getPlugin(key), pluginClass)));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void unloadClasses() {
|
||||||
|
pluginClasses.forEach((key, classes) -> classes.forEach(pluginClass -> unloadClass(Bukkit.getPluginManager().getPlugin(key), pluginClass)));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void registerListener() {
|
||||||
|
TListenerHandler.setupListeners();
|
||||||
|
Bukkit.getScheduler().runTask(TabooLib.instance(), TListenerHandler::registerListeners);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void registerMetrics() {
|
||||||
|
Metrics metrics = new Metrics(TabooLib.instance());
|
||||||
|
metrics.addCustomChart(new Metrics.SingleLineChart("plugins_using_taboolib", () -> Math.toIntExact(Arrays.stream(Bukkit.getPluginManager().getPlugins()).filter(plugin -> plugin.getDescription().getDepend().contains("TabooLib")).count())));
|
||||||
|
}
|
||||||
|
|
||||||
static void setupDataFolder() {
|
static void setupDataFolder() {
|
||||||
Main.setPlayerDataFolder(FileUtils.folder(Main.getInst().getConfig().getString("DATAURL.PLAYER-DATA")));
|
Main.setPlayerDataFolder(FileUtils.folder(Main.getInst().getConfig().getString("DATAURL.PLAYER-DATA")));
|
||||||
Main.setServerDataFolder(FileUtils.folder(Main.getInst().getConfig().getString("DATAURL.SERVER-DATA")));
|
Main.setServerDataFolder(FileUtils.folder(Main.getInst().getConfig().getString("DATAURL.SERVER-DATA")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 载入插件数据库
|
|
||||||
*/
|
|
||||||
static void setupDatabase() {
|
static void setupDatabase() {
|
||||||
DataUtils.addPluginData("TabooLibrary", null);
|
DataUtils.addPluginData("TabooLibrary", null);
|
||||||
// 检查是否启用数据库
|
|
||||||
Main.setStorageType(Main.getInst().getConfig().getBoolean("MYSQL.ENABLE") ? Main.StorageType.SQL : Main.StorageType.LOCAL);
|
Main.setStorageType(Main.getInst().getConfig().getBoolean("MYSQL.ENABLE") ? Main.StorageType.SQL : Main.StorageType.LOCAL);
|
||||||
// 初始化数据库
|
|
||||||
TabooLibDatabase.init();
|
TabooLibDatabase.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 读取插件类
|
|
||||||
*/
|
|
||||||
static void setupClasses() {
|
|
||||||
Arrays.stream(Bukkit.getPluginManager().getPlugins()).forEach(TabooLibLoader::setupClasses);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 读取插件类
|
|
||||||
*/
|
|
||||||
static void setupClasses(Plugin plugin) {
|
|
||||||
if (!(TabooLib.isTabooLib(plugin) || TabooLib.isDependTabooLib(plugin))) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
long time = System.currentTimeMillis();
|
|
||||||
List<Class> classes = FileUtils.getClasses(plugin);
|
|
||||||
TLocale.Logger.info("DEPENDENCY.LOAD-CLASSES", plugin.getName(), String.valueOf(classes.size()), String.valueOf(System.currentTimeMillis() - time));
|
|
||||||
pluginClasses.put(plugin.getName(), classes);
|
|
||||||
} catch (Exception ignored) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 初始化插件依赖库
|
|
||||||
*/
|
|
||||||
static void setupLibraries() {
|
static void setupLibraries() {
|
||||||
if (!Main.isOfflineVersion()) {
|
if (Main.isOfflineVersion()) {
|
||||||
return;
|
Arrays.stream(TDependencyInjector.getDependencies(TLib.getTLib())).filter(dependency -> dependency.type() == Dependency.Type.LIBRARY && dependency.maven().matches(".*:.*:.*")).map(dependency -> String.join("-", dependency.maven().split(":")) + ".jar").forEach(fileName -> {
|
||||||
}
|
|
||||||
for (Dependency dependency : TDependencyInjector.getDependencies(TLib.getTLib())) {
|
|
||||||
if (dependency.type() == Dependency.Type.LIBRARY && dependency.maven().matches(".*:.*:.*")) {
|
|
||||||
String fileName = String.join("-", dependency.maven().split(":")) + ".jar";
|
|
||||||
File targetFile = FileUtils.file(TLib.getTLib().getLibsFolder(), fileName);
|
File targetFile = FileUtils.file(TLib.getTLib().getLibsFolder(), fileName);
|
||||||
InputStream inputStream = FileUtils.getResource("libs/" + fileName);
|
InputStream inputStream = FileUtils.getResource("libs/" + fileName);
|
||||||
if (!targetFile.exists() && inputStream != null) {
|
if (!targetFile.exists() && inputStream != null) {
|
||||||
FileUtils.inputStreamToFile(inputStream, FileUtils.file(TLib.getTLib().getLibsFolder(), fileName));
|
FileUtils.inputStreamToFile(inputStream, FileUtils.file(TLib.getTLib().getLibsFolder(), fileName));
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 检查网络连接状态
|
|
||||||
*/
|
|
||||||
static void testInternet() {
|
static void testInternet() {
|
||||||
try {
|
try {
|
||||||
InetAddress inetAddress = InetAddress.getByName(Main.getInst().getConfig().getString("TEST-URL", "aliyun.com"));
|
InetAddress inetAddress = InetAddress.getByName(Main.getInst().getConfig().getString("TEST-URL", "aliyun.com"));
|
||||||
@ -129,40 +115,70 @@ public class TabooLibLoader implements Listener {
|
|||||||
}
|
}
|
||||||
if (!Main.isInternetOnline() && !Main.isOfflineVersion() && !Main.isLibrariesExists()) {
|
if (!Main.isInternetOnline() && !Main.isOfflineVersion() && !Main.isLibrariesExists()) {
|
||||||
TLocale.Logger.error("TLIB.LOAD-FAIL-OFFLINE", Main.getInst().getDescription().getVersion());
|
TLocale.Logger.error("TLIB.LOAD-FAIL-OFFLINE", Main.getInst().getDescription().getVersion());
|
||||||
try {
|
for (; ; ) {
|
||||||
while (true) {
|
// 停止主线程
|
||||||
Thread.sleep(1000);
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void setupClasses(Plugin plugin) {
|
||||||
|
if (TabooLib.isTabooLib(plugin) || TabooLib.isDependTabooLib(plugin)) {
|
||||||
|
try {
|
||||||
|
long time = System.currentTimeMillis();
|
||||||
|
List<Class> classes = FileUtils.getClasses(plugin);
|
||||||
|
TLocale.Logger.info("DEPENDENCY.LOAD-CLASSES", plugin.getName(), String.valueOf(classes.size()), String.valueOf(System.currentTimeMillis() - time));
|
||||||
|
pluginClasses.put(plugin.getName(), classes);
|
||||||
} catch (Exception ignored) {
|
} catch (Exception ignored) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
static void setupClasses() {
|
||||||
* 载入插件监听
|
Arrays.stream(Bukkit.getPluginManager().getPlugins()).forEach(TabooLibLoader::setupClasses);
|
||||||
*/
|
pluginClasses.get("TabooLib").stream().filter(TabooLibLoader::isLoader).forEach(pluginClass -> {
|
||||||
static void registerListener() {
|
try {
|
||||||
// 载入所有 TListener 监听器
|
loaders.add((Loader) ReflectionUtils.instantiateObject(pluginClass));
|
||||||
TListenerHandler.setupListeners();
|
} catch (Exception e) {
|
||||||
// 注册所有 TListener 监听器
|
e.printStackTrace();
|
||||||
Bukkit.getScheduler().runTask(TabooLib.instance(), TListenerHandler::registerListeners);
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
static void loadClass(Plugin plugin, Class<?> loadClass) {
|
||||||
* 注册插件统计
|
loaders.forEach(loader -> {
|
||||||
*/
|
try {
|
||||||
static void registerMetrics() {
|
loader.load(plugin, loadClass);
|
||||||
Metrics metrics = new Metrics(TabooLib.instance());
|
} catch (Throwable ignored) {
|
||||||
metrics.addCustomChart(new Metrics.SingleLineChart("plugins_using_taboolib", () -> Math.toIntExact(Arrays.stream(Bukkit.getPluginManager().getPlugins()).filter(plugin -> plugin.getDescription().getDepend().contains("TabooLib")).count())));
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
static void unloadClass(Plugin plugin, Class<?> loadClass) {
|
||||||
|
loaders.forEach(loader -> {
|
||||||
|
try {
|
||||||
|
loader.unload(plugin, loadClass);
|
||||||
|
} catch (Throwable ignored) {
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler(priority = EventPriority.LOWEST)
|
@EventHandler(priority = EventPriority.LOWEST)
|
||||||
public void onEnable(PluginEnableEvent e) {
|
public void onEnable(PluginEnableEvent e) {
|
||||||
setupClasses(e.getPlugin());
|
setupClasses(e.getPlugin());
|
||||||
|
Optional.ofNullable(pluginClasses.get(e.getPlugin().getName())).ifPresent(classes -> classes.forEach(pluginClass -> loadClass(e.getPlugin(), pluginClass)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler(priority = EventPriority.MONITOR)
|
@EventHandler(priority = EventPriority.MONITOR)
|
||||||
public void onDisable(PluginDisableEvent e) {
|
public void onDisable(PluginDisableEvent e) {
|
||||||
pluginClasses.remove(e.getPlugin().getName());
|
Optional.ofNullable(pluginClasses.remove(e.getPlugin().getName())).ifPresent(classes -> classes.forEach(pluginClass -> unloadClass(e.getPlugin(), pluginClass)));
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface Loader {
|
||||||
|
|
||||||
|
default void load(Plugin plugin, Class<?> loadClass) {
|
||||||
|
}
|
||||||
|
|
||||||
|
default void unload(Plugin plugin, Class<?> cancelClass) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,64 +1,33 @@
|
|||||||
package me.skymc.taboolib.common.function;
|
package me.skymc.taboolib.common.function;
|
||||||
|
|
||||||
import me.skymc.taboolib.TabooLib;
|
|
||||||
import me.skymc.taboolib.TabooLibLoader;
|
import me.skymc.taboolib.TabooLibLoader;
|
||||||
import me.skymc.taboolib.listener.TListener;
|
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.event.EventHandler;
|
|
||||||
import org.bukkit.event.Listener;
|
|
||||||
import org.bukkit.event.server.PluginDisableEvent;
|
|
||||||
import org.bukkit.event.server.PluginEnableEvent;
|
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Author sky
|
* @Author sky
|
||||||
* @Since 2018-09-08 14:00
|
* @Since 2018-09-08 14:00
|
||||||
*/
|
*/
|
||||||
@TListener
|
public class TFunctionLoader implements TabooLibLoader.Loader {
|
||||||
public class TFunctionLoader implements Listener {
|
|
||||||
|
|
||||||
private static HashMap<String, List<Class>> pluginFunction = new HashMap<>();
|
@Override
|
||||||
|
public void load(Plugin plugin, Class<?> pluginClass) {
|
||||||
TFunctionLoader() {
|
|
||||||
loadFunction();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void loadFunction() {
|
|
||||||
Arrays.stream(Bukkit.getPluginManager().getPlugins()).forEach(TFunctionLoader::loadFunction);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void loadFunction(Plugin plugin) {
|
|
||||||
if (!(TabooLib.isTabooLib(plugin) || TabooLib.isDependTabooLib(plugin))) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
TabooLibLoader.getPluginClasses(plugin).ifPresent(classes -> {
|
|
||||||
for (Class pluginClass : classes) {
|
|
||||||
if (pluginClass.isAnnotationPresent(TFunction.class)) {
|
if (pluginClass.isAnnotationPresent(TFunction.class)) {
|
||||||
TFunction function = (TFunction) pluginClass.getAnnotation(TFunction.class);
|
TFunction function = pluginClass.getAnnotation(TFunction.class);
|
||||||
try {
|
try {
|
||||||
Method method = pluginClass.getDeclaredMethod(function.enable());
|
Method method = pluginClass.getDeclaredMethod(function.enable());
|
||||||
method.setAccessible(true);
|
method.setAccessible(true);
|
||||||
method.invoke(pluginClass.newInstance());
|
method.invoke(pluginClass.newInstance());
|
||||||
pluginFunction.computeIfAbsent(plugin.getName(), name -> new ArrayList<>()).add(pluginClass);
|
|
||||||
} catch (Exception ignored) {
|
} catch (Exception ignored) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void unloadFunction() {
|
@Override
|
||||||
Arrays.stream(Bukkit.getPluginManager().getPlugins()).forEach(TFunctionLoader::unloadFunction);
|
public void unload(Plugin plugin, Class<?> pluginClass) {
|
||||||
}
|
|
||||||
|
|
||||||
public static void unloadFunction(Plugin plugin) {
|
|
||||||
Optional.ofNullable(pluginFunction.remove(plugin.getName())).ifPresent(classes -> {
|
|
||||||
for (Class pluginClass : classes) {
|
|
||||||
if (pluginClass.isAnnotationPresent(TFunction.class)) {
|
if (pluginClass.isAnnotationPresent(TFunction.class)) {
|
||||||
TFunction function = (TFunction) pluginClass.getAnnotation(TFunction.class);
|
TFunction function = pluginClass.getAnnotation(TFunction.class);
|
||||||
try {
|
try {
|
||||||
Method method = pluginClass.getDeclaredMethod(function.disable());
|
Method method = pluginClass.getDeclaredMethod(function.disable());
|
||||||
method.setAccessible(true);
|
method.setAccessible(true);
|
||||||
@ -67,16 +36,4 @@ public class TFunctionLoader implements Listener {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@EventHandler
|
|
||||||
public void onEnable(PluginEnableEvent e) {
|
|
||||||
loadFunction(e.getPlugin());
|
|
||||||
}
|
|
||||||
|
|
||||||
@EventHandler
|
|
||||||
public void onDisable(PluginDisableEvent e) {
|
|
||||||
unloadFunction(e.getPlugin());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
18
src/main/java/me/skymc/taboolib/common/inject/TInject.java
Normal file
18
src/main/java/me/skymc/taboolib/common/inject/TInject.java
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package me.skymc.taboolib.common.inject;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author sky
|
||||||
|
* @Since 2018-10-05 12:11
|
||||||
|
*/
|
||||||
|
@Target(ElementType.FIELD)
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
public @interface TInject {
|
||||||
|
|
||||||
|
String[] value() default {};
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,67 @@
|
|||||||
|
package me.skymc.taboolib.common.inject;
|
||||||
|
|
||||||
|
import com.google.common.collect.Maps;
|
||||||
|
import com.ilummc.tlib.logger.TLogger;
|
||||||
|
import me.skymc.taboolib.TabooLibLoader;
|
||||||
|
import me.skymc.taboolib.common.configuration.TConfiguration;
|
||||||
|
import org.bukkit.plugin.Plugin;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.lang.reflect.Modifier;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author sky
|
||||||
|
* @Since 2018-10-05 13:40
|
||||||
|
*/
|
||||||
|
public class TInjectLoader implements TabooLibLoader.Loader {
|
||||||
|
|
||||||
|
private static Map<Class<?>, TInjectTask> injectTypes = Maps.newHashMap();
|
||||||
|
|
||||||
|
static {
|
||||||
|
// TLogger Inject
|
||||||
|
injectTypes.put(TLogger.class, (plugin, field, args) -> {
|
||||||
|
try {
|
||||||
|
field.set(null, TLogger.getUnformatted(plugin));
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// TConfiguration Inject
|
||||||
|
injectTypes.put(TConfiguration.class, (plugin, field, args) -> {
|
||||||
|
if (args.length == 0) {
|
||||||
|
TLogger.getGlobalLogger().error("Invalid inject arguments: " + field.getName());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
field.set(null, TConfiguration.createInResource(plugin, args[0]));
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void load(Plugin plugin, Class<?> pluginClass) {
|
||||||
|
for (Field declaredField : pluginClass.getDeclaredFields()) {
|
||||||
|
TInject annotation = declaredField.getAnnotation(TInject.class);
|
||||||
|
if (annotation == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!Modifier.isStatic(declaredField.getModifiers())) {
|
||||||
|
TLogger.getGlobalLogger().error(declaredField.getName() + " is not a static field.");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
TInjectTask tInjectTask = injectTypes.get(declaredField.getType());
|
||||||
|
if (tInjectTask == null) {
|
||||||
|
TLogger.getGlobalLogger().error(declaredField.getName() + " cannot inject.");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
tInjectTask.run(plugin, declaredField, annotation.value());
|
||||||
|
} catch (Exception e) {
|
||||||
|
TLogger.getGlobalLogger().error(declaredField.getName() + " inject failed: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
package me.skymc.taboolib.common.inject;
|
||||||
|
|
||||||
|
import org.bukkit.plugin.Plugin;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author sky
|
||||||
|
* @Since 2018-10-05 13:41
|
||||||
|
*/
|
||||||
|
public interface TInjectTask {
|
||||||
|
|
||||||
|
void run(Plugin plugin, Field field, String[] args);
|
||||||
|
|
||||||
|
}
|
@ -3,43 +3,34 @@ package me.skymc.taboolib.common.playercontainer;
|
|||||||
import com.ilummc.tlib.logger.TLogger;
|
import com.ilummc.tlib.logger.TLogger;
|
||||||
import me.skymc.taboolib.TabooLib;
|
import me.skymc.taboolib.TabooLib;
|
||||||
import me.skymc.taboolib.TabooLibLoader;
|
import me.skymc.taboolib.TabooLibLoader;
|
||||||
import me.skymc.taboolib.listener.TListener;
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.EventPriority;
|
import org.bukkit.event.EventPriority;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.event.player.PlayerQuitEvent;
|
import org.bukkit.event.player.PlayerQuitEvent;
|
||||||
import org.bukkit.event.server.PluginDisableEvent;
|
|
||||||
import org.bukkit.event.server.PluginEnableEvent;
|
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.util.*;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Author sky
|
* @Author sky
|
||||||
* @Since 2018-09-14 23:45
|
* @Since 2018-09-14 23:45
|
||||||
*/
|
*/
|
||||||
@TListener
|
public class PlayerContainerLoader implements Listener, TabooLibLoader.Loader {
|
||||||
public class PlayerContainerLoader implements Listener {
|
|
||||||
|
|
||||||
private static Map<String, List<Container>> pluginContainer = new ConcurrentHashMap<>();
|
Map<String, List<Container>> pluginContainer = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
PlayerContainerLoader() {
|
PlayerContainerLoader() {
|
||||||
load();
|
Bukkit.getPluginManager().registerEvents(this, TabooLib.instance());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void load() {
|
@Override
|
||||||
Arrays.stream(Bukkit.getPluginManager().getPlugins()).forEach(PlayerContainerLoader::load);
|
public void load(Plugin plugin, Class<?> pluginClass) {
|
||||||
}
|
|
||||||
|
|
||||||
public static void load(Plugin plugin) {
|
|
||||||
if (!(TabooLib.isTabooLib(plugin) || TabooLib.isDependTabooLib(plugin))) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
TabooLibLoader.getPluginClasses(plugin).ifPresent(classes -> {
|
|
||||||
for (Class pluginClass : classes) {
|
|
||||||
for (Field field : pluginClass.getDeclaredFields()) {
|
for (Field field : pluginClass.getDeclaredFields()) {
|
||||||
PlayerContainer annotation = field.getAnnotation(PlayerContainer.class);
|
PlayerContainer annotation = field.getAnnotation(PlayerContainer.class);
|
||||||
if (annotation == null) {
|
if (annotation == null) {
|
||||||
@ -52,31 +43,15 @@ public class PlayerContainerLoader implements Listener {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void unload() {
|
@Override
|
||||||
Arrays.stream(Bukkit.getPluginManager().getPlugins()).forEach(PlayerContainerLoader::unload);
|
public void unload(Plugin plugin, Class<?> cancelClass) {
|
||||||
}
|
|
||||||
|
|
||||||
public static void unload(Plugin plugin) {
|
|
||||||
pluginContainer.remove(plugin.getName());
|
pluginContainer.remove(plugin.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler
|
|
||||||
public void onEnable(PluginEnableEvent e) {
|
|
||||||
load(e.getPlugin());
|
|
||||||
}
|
|
||||||
|
|
||||||
@EventHandler
|
|
||||||
public void onDisable(PluginDisableEvent e) {
|
|
||||||
unload(e.getPlugin());
|
|
||||||
}
|
|
||||||
|
|
||||||
@EventHandler(priority = EventPriority.MONITOR)
|
@EventHandler(priority = EventPriority.MONITOR)
|
||||||
public void onQuit(PlayerQuitEvent e) {
|
public void onQuit(PlayerQuitEvent e) {
|
||||||
for (List<Container> containers : pluginContainer.values()) {
|
pluginContainer.values().stream().flatMap(Collection::stream).forEach(container -> {
|
||||||
for (Container container : containers) {
|
|
||||||
if (container.getContainer() instanceof Map) {
|
if (container.getContainer() instanceof Map) {
|
||||||
((Map) container.getContainer()).remove(container.isUniqueId() ? e.getPlayer().getUniqueId() : e.getPlayer().getName());
|
((Map) container.getContainer()).remove(container.isUniqueId() ? e.getPlayer().getUniqueId() : e.getPlayer().getName());
|
||||||
} else if (container.getContainer() instanceof Collection) {
|
} else if (container.getContainer() instanceof Collection) {
|
||||||
@ -84,8 +59,6 @@ public class PlayerContainerLoader implements Listener {
|
|||||||
} else {
|
} else {
|
||||||
TLogger.getGlobalLogger().error("Invalid Container: " + container.getContainer().getClass().getSimpleName());
|
TLogger.getGlobalLogger().error("Invalid Container: " + container.getContainer().getClass().getSimpleName());
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
@ -18,6 +18,7 @@ import java.nio.charset.Charset;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
import java.util.jar.JarFile;
|
import java.util.jar.JarFile;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -94,7 +95,7 @@ public class FileUtils {
|
|||||||
* @return {@link List<Class>}
|
* @return {@link List<Class>}
|
||||||
*/
|
*/
|
||||||
public static List<Class> getClasses(Plugin plugin) {
|
public static List<Class> getClasses(Plugin plugin) {
|
||||||
List<Class> classes = new ArrayList<>();
|
List<Class> classes = new CopyOnWriteArrayList<>();
|
||||||
URL url = plugin.getClass().getProtectionDomain().getCodeSource().getLocation();
|
URL url = plugin.getClass().getProtectionDomain().getCodeSource().getLocation();
|
||||||
try {
|
try {
|
||||||
File src;
|
File src;
|
||||||
|
@ -44,8 +44,7 @@ public class TListenerHandler implements Listener {
|
|||||||
* @param plugin 插件
|
* @param plugin 插件
|
||||||
*/
|
*/
|
||||||
public static void setupListener(Plugin plugin) {
|
public static void setupListener(Plugin plugin) {
|
||||||
TabooLibLoader.getPluginClasses(plugin).ifPresent(classes -> {
|
for (Class<?> pluginClass : TabooLibLoader.getPluginClassSafely(plugin)) {
|
||||||
for (Class<?> pluginClass : classes) {
|
|
||||||
if (org.bukkit.event.Listener.class.isAssignableFrom(pluginClass) && pluginClass.isAnnotationPresent(TListener.class)) {
|
if (org.bukkit.event.Listener.class.isAssignableFrom(pluginClass) && pluginClass.isAnnotationPresent(TListener.class)) {
|
||||||
try {
|
try {
|
||||||
TListener tListener = pluginClass.getAnnotation(TListener.class);
|
TListener tListener = pluginClass.getAnnotation(TListener.class);
|
||||||
@ -64,7 +63,6 @@ public class TListenerHandler implements Listener {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user