diff --git a/src/main/java/com/ilummc/tlib/TLib.java b/src/main/java/com/ilummc/tlib/TLib.java index 06c5863..7ca4864 100644 --- a/src/main/java/com/ilummc/tlib/TLib.java +++ b/src/main/java/com/ilummc/tlib/TLib.java @@ -127,17 +127,12 @@ public class TLib { return libsFolder; } - // ********************************* - // - // Private Methods - // - // ********************************* - - private boolean isInjectEnabled() { + public boolean isInjectEnabled() { return Main.getInst().getConfig().getBoolean("PLUGIN-INJECTOR.ENABLE", true); } - private boolean isBlackListPluginExists() { + public boolean isBlackListPluginExists() { return Main.getInst().getConfig().getStringList("PLUGIN-INJECTOR.DISABLE-ON-PLUGIN-EXISTS").stream().anyMatch(PluginUtils::isPluginExists); } + } diff --git a/src/main/java/com/ilummc/tlib/inject/TDependencyInjector.java b/src/main/java/com/ilummc/tlib/inject/TDependencyInjector.java index 73b2202..b8910e9 100644 --- a/src/main/java/com/ilummc/tlib/inject/TDependencyInjector.java +++ b/src/main/java/com/ilummc/tlib/inject/TDependencyInjector.java @@ -24,19 +24,6 @@ public class TDependencyInjector { private static List injected = new ArrayList<>(); - public static Dependency[] getDependencies(Object o) { - Dependency[] dependencies = new Dependency[0]; - Dependencies d = o.getClass().getAnnotation(Dependencies.class); - if (d != null) { - dependencies = d.value(); - } - Dependency d2 = o.getClass().getAnnotation(Dependency.class); - if (d2 != null) { - dependencies = new Dependency[]{d2}; - } - return dependencies; - } - static void injectOnEnable(Plugin plugin) { inject(plugin, plugin); } @@ -45,6 +32,10 @@ public class TDependencyInjector { eject(plugin, plugin); } + public static boolean injected(Plugin plugin) { + return injected.contains(plugin.getName()); + } + public static void inject(Plugin plugin, Object o) { if (!injected.contains(plugin.getName())) { injected.add(plugin.getName()); @@ -64,6 +55,19 @@ public class TDependencyInjector { } } + public static Dependency[] getDependencies(Object o) { + Dependency[] dependencies = new Dependency[0]; + Dependencies d = o.getClass().getAnnotation(Dependencies.class); + if (d != null) { + dependencies = d.value(); + } + Dependency d2 = o.getClass().getAnnotation(Dependency.class); + if (d2 != null) { + dependencies = new Dependency[]{d2}; + } + return dependencies; + } + private static void ejectConfig(Plugin plugin, Object o) { for (Field field : Ref.getDeclaredFields(o.getClass())) { TConfig config; diff --git a/src/main/java/me/skymc/taboolib/TabooLibLoader.java b/src/main/java/me/skymc/taboolib/TabooLibLoader.java index 8e0a08d..681c2c4 100644 --- a/src/main/java/me/skymc/taboolib/TabooLibLoader.java +++ b/src/main/java/me/skymc/taboolib/TabooLibLoader.java @@ -9,6 +9,8 @@ import com.ilummc.tlib.inject.TDependencyInjector; import com.ilummc.tlib.resources.TLocale; import me.skymc.taboolib.bstats.Metrics; import me.skymc.taboolib.deprecated.TabooLibDeprecated; +import me.skymc.taboolib.events.TPluginEnableEvent; +import me.skymc.taboolib.events.TPluginLoadEvent; import me.skymc.taboolib.fileutils.FileUtils; import me.skymc.taboolib.listener.TListener; import me.skymc.taboolib.listener.TListenerHandler; @@ -19,7 +21,6 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.server.PluginDisableEvent; -import org.bukkit.event.server.PluginEnableEvent; import org.bukkit.plugin.Plugin; import java.io.File; @@ -183,7 +184,7 @@ public class TabooLibLoader implements Listener { } @EventHandler(priority = EventPriority.LOWEST) - public void onEnable(PluginEnableEvent e) { + public void onEnable(TPluginEnableEvent e) { setupClasses(e.getPlugin()); Optional.ofNullable(pluginClasses.get(e.getPlugin().getName())).ifPresent(classes -> classes.forEach(pluginClass -> loadClass(e.getPlugin(), pluginClass))); } diff --git a/src/main/java/me/skymc/taboolib/events/TPluginEnableEvent.java b/src/main/java/me/skymc/taboolib/events/TPluginEnableEvent.java new file mode 100644 index 0000000..3efff37 --- /dev/null +++ b/src/main/java/me/skymc/taboolib/events/TPluginEnableEvent.java @@ -0,0 +1,28 @@ +package me.skymc.taboolib.events; + +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.bukkit.plugin.Plugin; + +public class TPluginEnableEvent extends Event { + + private static final HandlerList handlers = new HandlerList(); + private Plugin plugin; + + public TPluginEnableEvent(Plugin plugin) { + this.plugin = plugin; + } + + public static HandlerList getHandlerList() { + return handlers; + } + + public Plugin getPlugin() { + return this.plugin; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } +} diff --git a/src/main/java/me/skymc/taboolib/events/TPluginLoadEvent.java b/src/main/java/me/skymc/taboolib/events/TPluginLoadEvent.java new file mode 100644 index 0000000..8e2a07e --- /dev/null +++ b/src/main/java/me/skymc/taboolib/events/TPluginLoadEvent.java @@ -0,0 +1,28 @@ +package me.skymc.taboolib.events; + +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.bukkit.plugin.Plugin; + +public class TPluginLoadEvent extends Event { + + private static final HandlerList handlers = new HandlerList(); + private Plugin plugin; + + public TPluginLoadEvent(Plugin plugin) { + this.plugin = plugin; + } + + public static HandlerList getHandlerList() { + return handlers; + } + + public Plugin getPlugin() { + return this.plugin; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } +} diff --git a/src/main/java/me/skymc/taboolib/listener/ListenerPluginDisable.java b/src/main/java/me/skymc/taboolib/listener/ListenerPlugin.java similarity index 78% rename from src/main/java/me/skymc/taboolib/listener/ListenerPluginDisable.java rename to src/main/java/me/skymc/taboolib/listener/ListenerPlugin.java index b4083e1..bc14b11 100644 --- a/src/main/java/me/skymc/taboolib/listener/ListenerPluginDisable.java +++ b/src/main/java/me/skymc/taboolib/listener/ListenerPlugin.java @@ -2,10 +2,13 @@ package me.skymc.taboolib.listener; import com.ilummc.tlib.TLib; import com.ilummc.tlib.inject.TConfigWatcher; +import com.ilummc.tlib.inject.TDependencyInjector; import com.ilummc.tlib.resources.TLocale; import me.skymc.taboolib.Main; import me.skymc.taboolib.TabooLib; import me.skymc.taboolib.common.configuration.TConfiguration; +import me.skymc.taboolib.events.TPluginEnableEvent; +import me.skymc.taboolib.events.TPluginLoadEvent; import me.skymc.taboolib.mysql.MysqlUtils; import me.skymc.taboolib.mysql.hikari.HikariHandler; import me.skymc.taboolib.mysql.protect.MySQLConnection; @@ -25,11 +28,26 @@ import java.util.Optional; * @author sky */ @TListener -public class ListenerPluginDisable implements Listener { +public class ListenerPlugin implements Listener { + + @EventHandler + public void enable(TPluginEnableEvent e) { + if (!TLib.getTLib().isInjectEnabled() || !TLib.getTLib().isBlackListPluginExists()) { + try { + TDependencyInjector.inject(e.getPlugin(), e.getPlugin()); + } catch (Exception err) { + err.printStackTrace(); + } + } + } @EventHandler public void disable(PluginDisableEvent e) { TabooLib.debug("Plugin \"" + e.getPlugin().getName() + "\" was disabled."); + // 注销插件注入 + if (!TLib.getTLib().isInjectEnabled() || !TLib.getTLib().isBlackListPluginExists()) { + TDependencyInjector.eject(e.getPlugin(), e.getPlugin()); + } // 注销时间周期 TimeCycleManager.cancel(e.getPlugin()); // 注销插件配置 diff --git a/src/main/java/me/skymc/taboolib/plugin/PluginUtils.java b/src/main/java/me/skymc/taboolib/plugin/PluginUtils.java index 11d8144..0c6fc72 100644 --- a/src/main/java/me/skymc/taboolib/plugin/PluginUtils.java +++ b/src/main/java/me/skymc/taboolib/plugin/PluginUtils.java @@ -2,6 +2,8 @@ package me.skymc.taboolib.plugin; import com.google.common.base.Joiner; import me.skymc.taboolib.Main; +import me.skymc.taboolib.events.TPluginEnableEvent; +import me.skymc.taboolib.events.TPluginLoadEvent; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.command.Command; @@ -10,11 +12,11 @@ import org.bukkit.command.SimpleCommandMap; import org.bukkit.event.Event; import org.bukkit.plugin.*; import org.bukkit.plugin.java.JavaPlugin; -import java.lang.reflect.Method; import java.io.File; import java.io.IOException; import java.lang.reflect.Field; +import java.lang.reflect.Method; import java.net.URLClassLoader; import java.util.*; import java.util.Map.Entry; @@ -207,7 +209,6 @@ public class PluginUtils { } } } - try { target = Bukkit.getPluginManager().loadPlugin(pluginFile); } catch (InvalidDescriptionException e) { @@ -215,8 +216,22 @@ public class PluginUtils { } catch (InvalidPluginException e) { return new PluginLoadState(PluginLoadStateType.INVALID_PLUGIN, e.toString()); } - + try { + Bukkit.getPluginManager().callEvent(new TPluginLoadEvent(target)); + } catch (Exception e) { + e.printStackTrace(); + } + try { + Bukkit.getPluginManager().callEvent(new TPluginLoadEvent(target)); + } catch (Exception e) { + e.printStackTrace(); + } target.onLoad(); + try { + Bukkit.getPluginManager().callEvent(new TPluginEnableEvent(target)); + } catch (Exception e) { + e.printStackTrace(); + } Bukkit.getPluginManager().enablePlugin(target); return new PluginLoadState(PluginLoadStateType.LOADED, "null"); }