TabooLib v4.6-R2

+ Fixed some bugs for TDependencyInjector in DISABLE-INJECT mode.
+ Fixed some bugs for TInject in HOT-RELOAD mode.
+ Add TPluginLoadEvent.
+ Add TPluginEnableEvent.
This commit is contained in:
坏黑 2018-11-02 18:12:56 +08:00
parent 289de88b69
commit a4c35a3028
7 changed files with 116 additions and 27 deletions

View File

@ -127,17 +127,12 @@ public class TLib {
return libsFolder; return libsFolder;
} }
// ********************************* public boolean isInjectEnabled() {
//
// Private Methods
//
// *********************************
private boolean isInjectEnabled() {
return Main.getInst().getConfig().getBoolean("PLUGIN-INJECTOR.ENABLE", true); 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); return Main.getInst().getConfig().getStringList("PLUGIN-INJECTOR.DISABLE-ON-PLUGIN-EXISTS").stream().anyMatch(PluginUtils::isPluginExists);
} }
} }

View File

@ -24,19 +24,6 @@ public class TDependencyInjector {
private static List<String> injected = new ArrayList<>(); private static List<String> 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) { static void injectOnEnable(Plugin plugin) {
inject(plugin, plugin); inject(plugin, plugin);
} }
@ -45,6 +32,10 @@ public class TDependencyInjector {
eject(plugin, plugin); eject(plugin, plugin);
} }
public static boolean injected(Plugin plugin) {
return injected.contains(plugin.getName());
}
public static void inject(Plugin plugin, Object o) { public static void inject(Plugin plugin, Object o) {
if (!injected.contains(plugin.getName())) { if (!injected.contains(plugin.getName())) {
injected.add(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) { private static void ejectConfig(Plugin plugin, Object o) {
for (Field field : Ref.getDeclaredFields(o.getClass())) { for (Field field : Ref.getDeclaredFields(o.getClass())) {
TConfig config; TConfig config;

View File

@ -9,6 +9,8 @@ import com.ilummc.tlib.inject.TDependencyInjector;
import com.ilummc.tlib.resources.TLocale; import com.ilummc.tlib.resources.TLocale;
import me.skymc.taboolib.bstats.Metrics; import me.skymc.taboolib.bstats.Metrics;
import me.skymc.taboolib.deprecated.TabooLibDeprecated; 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.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;
@ -19,7 +21,6 @@ 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.server.PluginDisableEvent; import org.bukkit.event.server.PluginDisableEvent;
import org.bukkit.event.server.PluginEnableEvent;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import java.io.File; import java.io.File;
@ -183,7 +184,7 @@ public class TabooLibLoader implements Listener {
} }
@EventHandler(priority = EventPriority.LOWEST) @EventHandler(priority = EventPriority.LOWEST)
public void onEnable(PluginEnableEvent e) { public void onEnable(TPluginEnableEvent e) {
setupClasses(e.getPlugin()); setupClasses(e.getPlugin());
Optional.ofNullable(pluginClasses.get(e.getPlugin().getName())).ifPresent(classes -> classes.forEach(pluginClass -> loadClass(e.getPlugin(), pluginClass))); Optional.ofNullable(pluginClasses.get(e.getPlugin().getName())).ifPresent(classes -> classes.forEach(pluginClass -> loadClass(e.getPlugin(), pluginClass)));
} }

View File

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

View File

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

View File

@ -2,10 +2,13 @@ package me.skymc.taboolib.listener;
import com.ilummc.tlib.TLib; import com.ilummc.tlib.TLib;
import com.ilummc.tlib.inject.TConfigWatcher; import com.ilummc.tlib.inject.TConfigWatcher;
import com.ilummc.tlib.inject.TDependencyInjector;
import com.ilummc.tlib.resources.TLocale; import com.ilummc.tlib.resources.TLocale;
import me.skymc.taboolib.Main; import me.skymc.taboolib.Main;
import me.skymc.taboolib.TabooLib; import me.skymc.taboolib.TabooLib;
import me.skymc.taboolib.common.configuration.TConfiguration; 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.MysqlUtils;
import me.skymc.taboolib.mysql.hikari.HikariHandler; import me.skymc.taboolib.mysql.hikari.HikariHandler;
import me.skymc.taboolib.mysql.protect.MySQLConnection; import me.skymc.taboolib.mysql.protect.MySQLConnection;
@ -25,11 +28,26 @@ import java.util.Optional;
* @author sky * @author sky
*/ */
@TListener @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 @EventHandler
public void disable(PluginDisableEvent e) { public void disable(PluginDisableEvent e) {
TabooLib.debug("Plugin \"" + e.getPlugin().getName() + "\" was disabled."); 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()); TimeCycleManager.cancel(e.getPlugin());
// 注销插件配置 // 注销插件配置

View File

@ -2,6 +2,8 @@ package me.skymc.taboolib.plugin;
import com.google.common.base.Joiner; import com.google.common.base.Joiner;
import me.skymc.taboolib.Main; import me.skymc.taboolib.Main;
import me.skymc.taboolib.events.TPluginEnableEvent;
import me.skymc.taboolib.events.TPluginLoadEvent;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;
@ -10,11 +12,11 @@ import org.bukkit.command.SimpleCommandMap;
import org.bukkit.event.Event; import org.bukkit.event.Event;
import org.bukkit.plugin.*; import org.bukkit.plugin.*;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import java.lang.reflect.Method;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URLClassLoader; import java.net.URLClassLoader;
import java.util.*; import java.util.*;
import java.util.Map.Entry; import java.util.Map.Entry;
@ -207,7 +209,6 @@ public class PluginUtils {
} }
} }
} }
try { try {
target = Bukkit.getPluginManager().loadPlugin(pluginFile); target = Bukkit.getPluginManager().loadPlugin(pluginFile);
} catch (InvalidDescriptionException e) { } catch (InvalidDescriptionException e) {
@ -215,8 +216,22 @@ public class PluginUtils {
} catch (InvalidPluginException e) { } catch (InvalidPluginException e) {
return new PluginLoadState(PluginLoadStateType.INVALID_PLUGIN, e.toString()); 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(); target.onLoad();
try {
Bukkit.getPluginManager().callEvent(new TPluginEnableEvent(target));
} catch (Exception e) {
e.printStackTrace();
}
Bukkit.getPluginManager().enablePlugin(target); Bukkit.getPluginManager().enablePlugin(target);
return new PluginLoadState(PluginLoadStateType.LOADED, "null"); return new PluginLoadState(PluginLoadStateType.LOADED, "null");
} }