From 91840d6a6ba2e2923ff4fa16c13cad1ded0b1731 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=9D=8F=E9=BB=91?= Date: Mon, 8 Oct 2018 19:44:36 +0800 Subject: [PATCH] + update --- pom.xml | 2 +- src/main/java/me/skymc/taboolib/Main.java | 4 +- .../me/skymc/taboolib/TabooLibLoader.java | 162 ++++++++++-------- .../common/function/TFunctionLoader.java | 89 +++------- .../skymc/taboolib/common/inject/TInject.java | 18 ++ .../taboolib/common/inject/TInjectLoader.java | 67 ++++++++ .../taboolib/common/inject/TInjectTask.java | 15 ++ .../PlayerContainerLoader.java | 85 ++++----- .../skymc/taboolib/fileutils/FileUtils.java | 3 +- .../taboolib/listener/TListenerHandler.java | 32 ++-- 10 files changed, 261 insertions(+), 216 deletions(-) create mode 100644 src/main/java/me/skymc/taboolib/common/inject/TInject.java create mode 100644 src/main/java/me/skymc/taboolib/common/inject/TInjectLoader.java create mode 100644 src/main/java/me/skymc/taboolib/common/inject/TInjectTask.java diff --git a/pom.xml b/pom.xml index dbd5fc0..57eb648 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ me.skymc TabooLib - 4.51 + 4.52 UTF-8 diff --git a/src/main/java/me/skymc/taboolib/Main.java b/src/main/java/me/skymc/taboolib/Main.java index 7913663..e977c0e 100644 --- a/src/main/java/me/skymc/taboolib/Main.java +++ b/src/main/java/me/skymc/taboolib/Main.java @@ -186,6 +186,8 @@ public class Main extends JavaPlugin { TLocale.Logger.error("NOTIFY.FAIL-DISABLE"); return; } + // 注销插件 + TabooLibLoader.unregister(); // 保存数据 Bukkit.getOnlinePlayers().forEach(x -> DataUtils.saveOnline(x.getName())); // 结束线程 @@ -204,8 +206,6 @@ public class Main extends JavaPlugin { HikariHandler.closeDataSourceForce(); // 注销监听器 TListenerHandler.cancelListeners(); - // 注销子模块 - TFunctionLoader.unloadFunction(); // 结束数据库储存方法 if (getStorageType() == StorageType.SQL) { GlobalDataManager.SQLMethod.cancelSQLMethod(); diff --git a/src/main/java/me/skymc/taboolib/TabooLibLoader.java b/src/main/java/me/skymc/taboolib/TabooLibLoader.java index 5eeae1a..05b0198 100644 --- a/src/main/java/me/skymc/taboolib/TabooLibLoader.java +++ b/src/main/java/me/skymc/taboolib/TabooLibLoader.java @@ -1,5 +1,7 @@ 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.annotations.Dependency; 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.listener.TListener; import me.skymc.taboolib.listener.TListenerHandler; +import me.skymc.taboolib.methods.ReflectionUtils; import me.skymc.taboolib.playerdata.DataUtils; import org.bukkit.Bukkit; import org.bukkit.event.EventHandler; @@ -20,10 +23,7 @@ import org.bukkit.plugin.Plugin; import java.io.File; import java.io.InputStream; import java.net.InetAddress; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Optional; +import java.util.*; /** * @Author sky @@ -32,7 +32,8 @@ import java.util.Optional; @TListener public class TabooLibLoader implements Listener { - static HashMap> pluginClasses = new HashMap<>(); + static Map> pluginClasses = Maps.newHashMap(); + static List loaders = Lists.newArrayList(); static void setup() { testInternet(); @@ -43,84 +44,69 @@ public class TabooLibLoader implements Listener { static void register() { setupClasses(); + loadClasses(); registerListener(); registerMetrics(); } - /** - * 获取插件所有被读取到的类 - * - * @param plugin 插件 - * @return List - */ + static void unregister() { + unloadClasses(); + } + public static Optional> getPluginClasses(Plugin plugin) { return Optional.ofNullable(pluginClasses.get(plugin.getName())); } - /** - * 初始化插件文件夹 - */ + public static List getPluginClassSafely(Plugin plugin) { + List 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() { Main.setPlayerDataFolder(FileUtils.folder(Main.getInst().getConfig().getString("DATAURL.PLAYER-DATA"))); Main.setServerDataFolder(FileUtils.folder(Main.getInst().getConfig().getString("DATAURL.SERVER-DATA"))); } - /** - * 载入插件数据库 - */ static void setupDatabase() { DataUtils.addPluginData("TabooLibrary", null); - // 检查是否启用数据库 Main.setStorageType(Main.getInst().getConfig().getBoolean("MYSQL.ENABLE") ? Main.StorageType.SQL : Main.StorageType.LOCAL); - // 初始化数据库 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 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() { - if (!Main.isOfflineVersion()) { - return; - } - for (Dependency dependency : TDependencyInjector.getDependencies(TLib.getTLib())) { - if (dependency.type() == Dependency.Type.LIBRARY && dependency.maven().matches(".*:.*:.*")) { - String fileName = String.join("-", dependency.maven().split(":")) + ".jar"; + if (Main.isOfflineVersion()) { + 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 -> { File targetFile = FileUtils.file(TLib.getTLib().getLibsFolder(), fileName); InputStream inputStream = FileUtils.getResource("libs/" + fileName); if (!targetFile.exists() && inputStream != null) { FileUtils.inputStreamToFile(inputStream, FileUtils.file(TLib.getTLib().getLibsFolder(), fileName)); } - } + }); } } - /** - * 检查网络连接状态 - */ static void testInternet() { try { 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()) { TLocale.Logger.error("TLIB.LOAD-FAIL-OFFLINE", Main.getInst().getDescription().getVersion()); + for (; ; ) { + // 停止主线程 + } + } + } + + static void setupClasses(Plugin plugin) { + if (TabooLib.isTabooLib(plugin) || TabooLib.isDependTabooLib(plugin)) { try { - while (true) { - Thread.sleep(1000); - } + long time = System.currentTimeMillis(); + List 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 registerListener() { - // 载入所有 TListener 监听器 - TListenerHandler.setupListeners(); - // 注册所有 TListener 监听器 - Bukkit.getScheduler().runTask(TabooLib.instance(), TListenerHandler::registerListeners); + static void setupClasses() { + Arrays.stream(Bukkit.getPluginManager().getPlugins()).forEach(TabooLibLoader::setupClasses); + pluginClasses.get("TabooLib").stream().filter(TabooLibLoader::isLoader).forEach(pluginClass -> { + try { + loaders.add((Loader) ReflectionUtils.instantiateObject(pluginClass)); + } catch (Exception e) { + e.printStackTrace(); + } + }); } - /** - * 注册插件统计 - */ - 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 loadClass(Plugin plugin, Class loadClass) { + loaders.forEach(loader -> { + try { + loader.load(plugin, loadClass); + } catch (Throwable ignored) { + } + }); + } + + static void unloadClass(Plugin plugin, Class loadClass) { + loaders.forEach(loader -> { + try { + loader.unload(plugin, loadClass); + } catch (Throwable ignored) { + } + }); } @EventHandler(priority = EventPriority.LOWEST) public void onEnable(PluginEnableEvent e) { setupClasses(e.getPlugin()); + Optional.ofNullable(pluginClasses.get(e.getPlugin().getName())).ifPresent(classes -> classes.forEach(pluginClass -> loadClass(e.getPlugin(), pluginClass))); } @EventHandler(priority = EventPriority.MONITOR) 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) { + } } } diff --git a/src/main/java/me/skymc/taboolib/common/function/TFunctionLoader.java b/src/main/java/me/skymc/taboolib/common/function/TFunctionLoader.java index 038e3f2..4da0219 100644 --- a/src/main/java/me/skymc/taboolib/common/function/TFunctionLoader.java +++ b/src/main/java/me/skymc/taboolib/common/function/TFunctionLoader.java @@ -1,82 +1,39 @@ package me.skymc.taboolib.common.function; -import me.skymc.taboolib.TabooLib; 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 java.lang.reflect.Method; -import java.util.*; /** * @Author sky * @Since 2018-09-08 14:00 */ -@TListener -public class TFunctionLoader implements Listener { +public class TFunctionLoader implements TabooLibLoader.Loader { - private static HashMap> pluginFunction = new HashMap<>(); - - 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; + @Override + public void load(Plugin plugin, Class pluginClass) { + if (pluginClass.isAnnotationPresent(TFunction.class)) { + TFunction function = pluginClass.getAnnotation(TFunction.class); + try { + Method method = pluginClass.getDeclaredMethod(function.enable()); + method.setAccessible(true); + method.invoke(pluginClass.newInstance()); + } catch (Exception ignored) { + } } - TabooLibLoader.getPluginClasses(plugin).ifPresent(classes -> { - for (Class pluginClass : classes) { - if (pluginClass.isAnnotationPresent(TFunction.class)) { - TFunction function = (TFunction) pluginClass.getAnnotation(TFunction.class); - try { - Method method = pluginClass.getDeclaredMethod(function.enable()); - method.setAccessible(true); - method.invoke(pluginClass.newInstance()); - pluginFunction.computeIfAbsent(plugin.getName(), name -> new ArrayList<>()).add(pluginClass); - } catch (Exception ignored) { - } - } + } + + @Override + public void unload(Plugin plugin, Class pluginClass) { + if (pluginClass.isAnnotationPresent(TFunction.class)) { + TFunction function = pluginClass.getAnnotation(TFunction.class); + try { + Method method = pluginClass.getDeclaredMethod(function.disable()); + method.setAccessible(true); + method.invoke(pluginClass.newInstance()); + } catch (Exception ignored) { } - }); - } - - public static void unloadFunction() { - Arrays.stream(Bukkit.getPluginManager().getPlugins()).forEach(TFunctionLoader::unloadFunction); - } - - public static void unloadFunction(Plugin plugin) { - Optional.ofNullable(pluginFunction.remove(plugin.getName())).ifPresent(classes -> { - for (Class pluginClass : classes) { - if (pluginClass.isAnnotationPresent(TFunction.class)) { - TFunction function = (TFunction) pluginClass.getAnnotation(TFunction.class); - try { - Method method = pluginClass.getDeclaredMethod(function.disable()); - method.setAccessible(true); - method.invoke(pluginClass.newInstance()); - } catch (Exception ignored) { - } - } - } - }); - } - - @EventHandler - public void onEnable(PluginEnableEvent e) { - loadFunction(e.getPlugin()); - } - - @EventHandler - public void onDisable(PluginDisableEvent e) { - unloadFunction(e.getPlugin()); + } } } diff --git a/src/main/java/me/skymc/taboolib/common/inject/TInject.java b/src/main/java/me/skymc/taboolib/common/inject/TInject.java new file mode 100644 index 0000000..c1d9e5d --- /dev/null +++ b/src/main/java/me/skymc/taboolib/common/inject/TInject.java @@ -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 {}; + +} \ No newline at end of file diff --git a/src/main/java/me/skymc/taboolib/common/inject/TInjectLoader.java b/src/main/java/me/skymc/taboolib/common/inject/TInjectLoader.java new file mode 100644 index 0000000..5b8800c --- /dev/null +++ b/src/main/java/me/skymc/taboolib/common/inject/TInjectLoader.java @@ -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, 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()); + } + } + } +} diff --git a/src/main/java/me/skymc/taboolib/common/inject/TInjectTask.java b/src/main/java/me/skymc/taboolib/common/inject/TInjectTask.java new file mode 100644 index 0000000..bdd06da --- /dev/null +++ b/src/main/java/me/skymc/taboolib/common/inject/TInjectTask.java @@ -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); + +} diff --git a/src/main/java/me/skymc/taboolib/common/playercontainer/PlayerContainerLoader.java b/src/main/java/me/skymc/taboolib/common/playercontainer/PlayerContainerLoader.java index 14d4a87..16af0e5 100644 --- a/src/main/java/me/skymc/taboolib/common/playercontainer/PlayerContainerLoader.java +++ b/src/main/java/me/skymc/taboolib/common/playercontainer/PlayerContainerLoader.java @@ -3,89 +3,62 @@ package me.skymc.taboolib.common.playercontainer; import com.ilummc.tlib.logger.TLogger; import me.skymc.taboolib.TabooLib; import me.skymc.taboolib.TabooLibLoader; -import me.skymc.taboolib.listener.TListener; import org.bukkit.Bukkit; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerQuitEvent; -import org.bukkit.event.server.PluginDisableEvent; -import org.bukkit.event.server.PluginEnableEvent; import org.bukkit.plugin.Plugin; 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; /** * @Author sky * @Since 2018-09-14 23:45 */ -@TListener -public class PlayerContainerLoader implements Listener { +public class PlayerContainerLoader implements Listener, TabooLibLoader.Loader { - private static Map> pluginContainer = new ConcurrentHashMap<>(); + Map> pluginContainer = new ConcurrentHashMap<>(); PlayerContainerLoader() { - load(); + Bukkit.getPluginManager().registerEvents(this, TabooLib.instance()); } - public static void load() { - Arrays.stream(Bukkit.getPluginManager().getPlugins()).forEach(PlayerContainerLoader::load); - } - - 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()) { - PlayerContainer annotation = field.getAnnotation(PlayerContainer.class); - if (annotation == null) { - continue; - } - field.setAccessible(true); - try { - pluginContainer.computeIfAbsent(plugin.getName(), name -> new ArrayList<>()).add(new Container(field.get(pluginClass), annotation.uniqueId())); - } catch (IllegalAccessException ignored) { - } - } + @Override + public void load(Plugin plugin, Class pluginClass) { + for (Field field : pluginClass.getDeclaredFields()) { + PlayerContainer annotation = field.getAnnotation(PlayerContainer.class); + if (annotation == null) { + continue; } - }); + field.setAccessible(true); + try { + pluginContainer.computeIfAbsent(plugin.getName(), name -> new ArrayList<>()).add(new Container(field.get(pluginClass), annotation.uniqueId())); + } catch (IllegalAccessException ignored) { + } + } } - public static void unload() { - Arrays.stream(Bukkit.getPluginManager().getPlugins()).forEach(PlayerContainerLoader::unload); - } - - public static void unload(Plugin plugin) { + @Override + public void unload(Plugin plugin, Class cancelClass) { 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) public void onQuit(PlayerQuitEvent e) { - for (List containers : pluginContainer.values()) { - for (Container container : containers) { - if (container.getContainer() instanceof Map) { - ((Map) container.getContainer()).remove(container.isUniqueId() ? e.getPlayer().getUniqueId() : e.getPlayer().getName()); - } else if (container.getContainer() instanceof Collection) { - ((Collection) container.getContainer()).remove(container.isUniqueId() ? e.getPlayer().getUniqueId() : e.getPlayer().getName()); - } else { - TLogger.getGlobalLogger().error("Invalid Container: " + container.getContainer().getClass().getSimpleName()); - } + pluginContainer.values().stream().flatMap(Collection::stream).forEach(container -> { + if (container.getContainer() instanceof Map) { + ((Map) container.getContainer()).remove(container.isUniqueId() ? e.getPlayer().getUniqueId() : e.getPlayer().getName()); + } else if (container.getContainer() instanceof Collection) { + ((Collection) container.getContainer()).remove(container.isUniqueId() ? e.getPlayer().getUniqueId() : e.getPlayer().getName()); + } else { + TLogger.getGlobalLogger().error("Invalid Container: " + container.getContainer().getClass().getSimpleName()); } - } + }); } - } diff --git a/src/main/java/me/skymc/taboolib/fileutils/FileUtils.java b/src/main/java/me/skymc/taboolib/fileutils/FileUtils.java index 861223c..f11b99f 100644 --- a/src/main/java/me/skymc/taboolib/fileutils/FileUtils.java +++ b/src/main/java/me/skymc/taboolib/fileutils/FileUtils.java @@ -18,6 +18,7 @@ import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; import java.util.Objects; +import java.util.concurrent.CopyOnWriteArrayList; import java.util.jar.JarFile; /** @@ -94,7 +95,7 @@ public class FileUtils { * @return {@link List} */ public static List getClasses(Plugin plugin) { - List classes = new ArrayList<>(); + List classes = new CopyOnWriteArrayList<>(); URL url = plugin.getClass().getProtectionDomain().getCodeSource().getLocation(); try { File src; diff --git a/src/main/java/me/skymc/taboolib/listener/TListenerHandler.java b/src/main/java/me/skymc/taboolib/listener/TListenerHandler.java index 927419e..cc5d97b 100644 --- a/src/main/java/me/skymc/taboolib/listener/TListenerHandler.java +++ b/src/main/java/me/skymc/taboolib/listener/TListenerHandler.java @@ -44,27 +44,25 @@ public class TListenerHandler implements Listener { * @param plugin 插件 */ public static void setupListener(Plugin plugin) { - TabooLibLoader.getPluginClasses(plugin).ifPresent(classes -> { - for (Class pluginClass : classes) { - if (org.bukkit.event.Listener.class.isAssignableFrom(pluginClass) && pluginClass.isAnnotationPresent(TListener.class)) { - try { - TListener tListener = pluginClass.getAnnotation(TListener.class); - // 检查注册条件 - if (tListener.depend().length > 0 && !Strings.isBlank(tListener.depend()[0])) { - if (Arrays.stream(tListener.depend()).anyMatch(depend -> Bukkit.getPluginManager().getPlugin(depend) == null)) { - continue; - } + for (Class pluginClass : TabooLibLoader.getPluginClassSafely(plugin)) { + if (org.bukkit.event.Listener.class.isAssignableFrom(pluginClass) && pluginClass.isAnnotationPresent(TListener.class)) { + try { + TListener tListener = pluginClass.getAnnotation(TListener.class); + // 检查注册条件 + if (tListener.depend().length > 0 && !Strings.isBlank(tListener.depend()[0])) { + if (Arrays.stream(tListener.depend()).anyMatch(depend -> Bukkit.getPluginManager().getPlugin(depend) == null)) { + continue; } - // 实例化监听器 - Listener listener = plugin.getClass().equals(pluginClass) ? (Listener) plugin : (Listener) ReflectionUtils.instantiateObject(pluginClass); - listeners.computeIfAbsent(plugin.getName(), name -> new ArrayList<>()).add(listener); - TabooLib.debug("Listener " + listener.getClass().getSimpleName() + " setup successfully. (" + plugin.getName() + ")"); - } catch (Exception e) { - TabooLib.debug("Listener setup failed: " + e.toString()); } + // 实例化监听器 + Listener listener = plugin.getClass().equals(pluginClass) ? (Listener) plugin : (Listener) ReflectionUtils.instantiateObject(pluginClass); + listeners.computeIfAbsent(plugin.getName(), name -> new ArrayList<>()).add(listener); + TabooLib.debug("Listener " + listener.getClass().getSimpleName() + " setup successfully. (" + plugin.getName() + ")"); + } catch (Exception e) { + TabooLib.debug("Listener setup failed: " + e.toString()); } } - }); + } } /**