+ optimized TInject

+ fixed PluginUtils
This commit is contained in:
坏黑 2018-11-25 16:48:12 +08:00
parent 5e4a9a8c98
commit b37ec49a80
8 changed files with 109 additions and 38 deletions

View File

@ -10,7 +10,6 @@ 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.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;
@ -35,6 +34,18 @@ import java.util.*;
@TListener @TListener
public class TabooLibLoader implements Listener { public class TabooLibLoader implements Listener {
/*
关于 TabooLib 各项自动化接口的执行顺序
[ENABLING]
第一阶段运行 @TInjectInstance Inject
第二阶段先后实例化 @TListener -> 实例化 @Instantiable
第三阶段并行运行 @TFunction & 运行 @TInject
[ENABLED]
第三阶段注册 @TListener
*/
static TabooLibDeprecated tabooLibDeprecated; static TabooLibDeprecated tabooLibDeprecated;
static Map<String, List<Class>> pluginClasses = Maps.newHashMap(); static Map<String, List<Class>> pluginClasses = Maps.newHashMap();
static List<Loader> loaders = Lists.newArrayList(); static List<Loader> loaders = Lists.newArrayList();
@ -50,9 +61,10 @@ public class TabooLibLoader implements Listener {
static void register() { static void register() {
setupClasses(); setupClasses();
preLoadClasses();
registerListener(); registerListener();
registerMetrics(); registerMetrics();
loadClasses(); postLoadClasses();
try { try {
tabooLibDeprecated = new TabooLibDeprecated(); tabooLibDeprecated = new TabooLibDeprecated();
} catch (Exception e) { } catch (Exception e) {
@ -103,8 +115,12 @@ public class TabooLibLoader implements Listener {
return !Loader.class.equals(pluginClass) && Loader.class.isAssignableFrom(pluginClass); return !Loader.class.equals(pluginClass) && Loader.class.isAssignableFrom(pluginClass);
} }
static void loadClasses() { static void preLoadClasses() {
pluginClasses.forEach((key, classes) -> classes.forEach(pluginClass -> loadClass(Bukkit.getPluginManager().getPlugin(key), pluginClass))); pluginClasses.forEach((key, classes) -> classes.forEach(pluginClass -> preLoadClass(Bukkit.getPluginManager().getPlugin(key), pluginClass)));
}
static void postLoadClasses() {
pluginClasses.forEach((key, classes) -> classes.forEach(pluginClass -> postLoadClass(Bukkit.getPluginManager().getPlugin(key), pluginClass)));
} }
static void unloadClasses() { static void unloadClasses() {
@ -189,10 +205,19 @@ public class TabooLibLoader implements Listener {
}); });
} }
static void loadClass(Plugin plugin, Class<?> loadClass) { static void preLoadClass(Plugin plugin, Class<?> loadClass) {
loaders.forEach(loader -> { loaders.forEach(loader -> {
try { try {
loader.load(plugin, loadClass); loader.preLoad(plugin, loadClass);
} catch (Throwable ignored) {
}
});
}
static void postLoadClass(Plugin plugin, Class<?> loadClass) {
loaders.forEach(loader -> {
try {
loader.postLoad(plugin, loadClass);
} catch (Throwable ignored) { } catch (Throwable ignored) {
} }
}); });
@ -208,9 +233,14 @@ public class TabooLibLoader implements Listener {
} }
@EventHandler(priority = EventPriority.LOWEST) @EventHandler(priority = EventPriority.LOWEST)
public void onEnable(TPluginEnableEvent e) { public void onEnablePre(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 -> preLoadClass(e.getPlugin(), pluginClass)));
}
@EventHandler
public void onEnablePost(TPluginEnableEvent e) {
Optional.ofNullable(pluginClasses.get(e.getPlugin().getName())).ifPresent(classes -> classes.forEach(pluginClass -> postLoadClass(e.getPlugin(), pluginClass)));
} }
@EventHandler(priority = EventPriority.MONITOR) @EventHandler(priority = EventPriority.MONITOR)
@ -220,7 +250,10 @@ public class TabooLibLoader implements Listener {
public interface Loader { public interface Loader {
default void load(Plugin plugin, Class<?> loadClass) { default void preLoad(Plugin plugin, Class<?> loadClass) {
}
default void postLoad(Plugin plugin, Class<?> loadClass) {
} }
default void unload(Plugin plugin, Class<?> cancelClass) { default void unload(Plugin plugin, Class<?> cancelClass) {

View File

@ -5,8 +5,6 @@ import me.skymc.taboolib.commands.builder.type.CompleterCommand;
import me.skymc.taboolib.commands.builder.type.CompleterTab; import me.skymc.taboolib.commands.builder.type.CompleterTab;
import me.skymc.taboolib.commands.internal.TCommandHandler; import me.skymc.taboolib.commands.internal.TCommandHandler;
import me.skymc.taboolib.string.ArrayUtils; import me.skymc.taboolib.string.ArrayUtils;
import org.bukkit.Bukkit;
import org.bukkit.command.*;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import java.util.ArrayList; import java.util.ArrayList;
@ -33,14 +31,18 @@ public class SimpleCommandBuilder {
private CompleterCommand completerCommand = EMPTY_COMPLETER_COMMAND; private CompleterCommand completerCommand = EMPTY_COMPLETER_COMMAND;
private boolean silence; private boolean silence;
private boolean forceRegister; private boolean forceRegister;
private boolean build;
SimpleCommandBuilder(String command, Plugin plugin) { SimpleCommandBuilder(String command, Plugin plugin) {
Preconditions.checkNotNull(command, "command cannot not be null");
Preconditions.checkNotNull(plugin, "plugin cannot not be null");
this.command = command; this.command = command;
this.plugin = plugin; this.plugin = plugin;
this.description = ""; this.description = "";
this.usage = "/" + command; this.usage = "/" + command;
this.aliases = new ArrayList<>(); this.aliases = new ArrayList<>();
this.silence = false; this.silence = false;
this.build = false;
} }
public static SimpleCommandBuilder create(String command, Plugin plugin) { public static SimpleCommandBuilder create(String command, Plugin plugin) {
@ -92,6 +94,10 @@ public class SimpleCommandBuilder {
return this; return this;
} }
public boolean isBuild() {
return build;
}
public SimpleCommandBuilder build() { public SimpleCommandBuilder build() {
Preconditions.checkNotNull(completerCommand, "缺少 \"CompleterCommand\" 部分"); Preconditions.checkNotNull(completerCommand, "缺少 \"CompleterCommand\" 部分");
Preconditions.checkNotNull(completerTab, "缺少 \"CompleterTab\" 部分"); Preconditions.checkNotNull(completerTab, "缺少 \"CompleterTab\" 部分");
@ -109,6 +115,7 @@ public class SimpleCommandBuilder {
(sender, command, s, args) -> completerCommand.execute(sender, args), (sender, command, s, args) -> completerCommand.execute(sender, args),
(sender, command, s, args) -> completerTab.execute(sender, args), (sender, command, s, args) -> completerTab.execute(sender, args),
silence); silence);
build = true;
return this; return this;
} }
} }

View File

@ -15,7 +15,7 @@ import java.lang.reflect.Modifier;
public class TFunctionLoader implements TabooLibLoader.Loader { public class TFunctionLoader implements TabooLibLoader.Loader {
@Override @Override
public void load(Plugin plugin, Class<?> pluginClass) { public void postLoad(Plugin plugin, Class<?> pluginClass) {
if (pluginClass.isAnnotationPresent(TFunction.class)) { if (pluginClass.isAnnotationPresent(TFunction.class)) {
TFunction function = pluginClass.getAnnotation(TFunction.class); TFunction function = pluginClass.getAnnotation(TFunction.class);
try { try {

View File

@ -22,11 +22,19 @@ public class TInjectLoader implements TabooLibLoader.Loader {
private static Map<Class<?>, TInjectTask> injectTypes = Maps.newHashMap(); private static Map<Class<?>, TInjectTask> injectTypes = Maps.newHashMap();
static { static {
// Instance Inject
injectTypes.put(Plugin.class, (plugin, field, args) -> {
try {
field.set(null, plugin);
} catch (Exception e) {
e.printStackTrace();
}
});
// TLogger Inject // TLogger Inject
injectTypes.put(TLogger.class, (plugin, field, args) -> { injectTypes.put(TLogger.class, (plugin, field, args) -> {
try { try {
field.set(null, TLogger.getUnformatted(plugin)); field.set(null, args.length == 0 ? TLogger.getUnformatted(plugin) : TLogger.getUnformatted(args[0]));
} catch (IllegalAccessException e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
}); });
@ -40,47 +48,74 @@ public class TInjectLoader implements TabooLibLoader.Loader {
}); });
// TConfiguration Inject // TConfiguration Inject
injectTypes.put(TConfiguration.class, (plugin, field, args) -> { injectTypes.put(TConfiguration.class, (plugin, field, args) -> {
if (args.length == 0) {
TLogger.getGlobalLogger().error("Invalid inject arguments: " + field.getName());
return;
}
try { try {
field.set(null, TConfiguration.createInResource(plugin, args[0])); if (args.length == 0) {
} catch (IllegalAccessException e) { TLogger.getGlobalLogger().error("Invalid inject arguments: " + field.getName() + " (" + field.getType().getName() + ")");
} else {
field.set(null, TConfiguration.createInResource(plugin, args[0]));
}
} catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
}); });
// SimpleCommandBuilder Inject // SimpleCommandBuilder Inject
injectTypes.put(SimpleCommandBuilder.class, (plugin, field, args) -> { injectTypes.put(SimpleCommandBuilder.class, (plugin, field, args) -> {
try { try {
((SimpleCommandBuilder) field.get(null)).build(); SimpleCommandBuilder builder = (SimpleCommandBuilder) field.get(null);
} catch (IllegalAccessException e) { if (builder.isBuild()) {
TLogger.getGlobalLogger().error("Command was registered. (" + field.getType().getName() + ")");
} else {
builder.build();
}
} catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
}); });
} }
@Override @Override
public void load(Plugin plugin, Class<?> pluginClass) { public void preLoad(Plugin plugin, Class<?> pluginClass) {
for (Field declaredField : pluginClass.getDeclaredFields()) { for (Field declaredField : pluginClass.getDeclaredFields()) {
TInject annotation = declaredField.getAnnotation(TInject.class); TInject annotation = declaredField.getAnnotation(TInject.class);
if (annotation == null) { if (annotation == null) {
continue; continue;
} }
if (!Modifier.isStatic(declaredField.getModifiers())) { if (!Modifier.isStatic(declaredField.getModifiers())) {
TLogger.getGlobalLogger().error(declaredField.getName() + " is not a static field."); TLogger.getGlobalLogger().error(declaredField.getName() + " is not a static field. (" + declaredField.getType().getName() + ")");
continue;
}
if (declaredField.getType().equals(plugin.getClass())) {
try {
declaredField.setAccessible(true);
injectTypes.get(Plugin.class).run(plugin, declaredField, annotation.value());
} catch (Exception e) {
TLogger.getGlobalLogger().error(declaredField.getName() + " inject failed: " + e.getMessage() + " (" + declaredField.getType().getName() + ")");
}
}
}
}
@Override
public void postLoad(Plugin plugin, Class<?> pluginClass) {
for (Field declaredField : pluginClass.getDeclaredFields()) {
TInject annotation = declaredField.getAnnotation(TInject.class);
if (annotation == null || declaredField.getType().equals(plugin.getClass())) {
continue;
}
if (!Modifier.isStatic(declaredField.getModifiers())) {
TLogger.getGlobalLogger().error(declaredField.getName() + " is not a static field. (" + declaredField.getType().getName() + ")");
continue; continue;
} }
TInjectTask tInjectTask = injectTypes.get(declaredField.getType()); TInjectTask tInjectTask = injectTypes.get(declaredField.getType());
if (tInjectTask == null) { if (tInjectTask == null) {
TLogger.getGlobalLogger().error(declaredField.getName() + " cannot inject."); TLogger.getGlobalLogger().error(declaredField.getName() + " is an invalid inject type. (" + declaredField.getType().getName() + ")");
continue; continue;
} }
try { try {
declaredField.setAccessible(true); declaredField.setAccessible(true);
tInjectTask.run(plugin, declaredField, annotation.value()); tInjectTask.run(plugin, declaredField, annotation.value());
} catch (Exception e) { } catch (Exception e) {
TLogger.getGlobalLogger().error(declaredField.getName() + " inject failed: " + e.getMessage()); TLogger.getGlobalLogger().error(declaredField.getName() + " inject failed: " + e.getMessage() + " (" + declaredField.getType().getName() + ")");
} }
} }
} }

View File

@ -30,7 +30,7 @@ public class PlayerContainerLoader implements Listener, TabooLibLoader.Loader {
} }
@Override @Override
public void load(Plugin plugin, Class<?> pluginClass) { public void postLoad(Plugin plugin, Class<?> pluginClass) {
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) {

View File

@ -8,12 +8,12 @@ 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.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;
import me.skymc.taboolib.timecycle.TimeCycleManager; import me.skymc.taboolib.timecycle.TimeCycleManager;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
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.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
@ -30,7 +30,7 @@ import java.util.Optional;
@TListener @TListener
public class ListenerPlugin implements Listener { public class ListenerPlugin implements Listener {
@EventHandler @EventHandler (priority = EventPriority.LOWEST)
public void enable(TPluginEnableEvent e) { public void enable(TPluginEnableEvent e) {
if (!TLib.getTLib().isInjectEnabled() || !TLib.getTLib().isBlackListPluginExists()) { if (!TLib.getTLib().isInjectEnabled() || !TLib.getTLib().isBlackListPluginExists()) {
try { try {

View File

@ -4,13 +4,14 @@ import com.ilummc.tlib.logger.TLogger;
import com.ilummc.tlib.util.Strings; import com.ilummc.tlib.util.Strings;
import me.skymc.taboolib.TabooLib; import me.skymc.taboolib.TabooLib;
import me.skymc.taboolib.TabooLibLoader; import me.skymc.taboolib.TabooLibLoader;
import me.skymc.taboolib.events.TPluginEnableEvent;
import me.skymc.taboolib.methods.ReflectionUtils; import me.skymc.taboolib.methods.ReflectionUtils;
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.HandlerList; import org.bukkit.event.HandlerList;
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.lang.reflect.Method; import java.lang.reflect.Method;
@ -164,11 +165,11 @@ public class TListenerHandler implements Listener {
return listeners; return listeners;
} }
@EventHandler @EventHandler(priority = EventPriority.LOW)
public void onPluginEnable(PluginEnableEvent e) { public void onPluginEnable(TPluginEnableEvent e) {
try { try {
setupListener(e.getPlugin()); setupListener(e.getPlugin());
registerListener(e.getPlugin()); Bukkit.getScheduler().runTask(TabooLib.instance(), () -> registerListener(e.getPlugin()));
} catch (Exception ignored) { } catch (Exception ignored) {
} }
} }

View File

@ -225,11 +225,6 @@ public class PluginUtils {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
try {
Bukkit.getPluginManager().callEvent(new TPluginLoadEvent(target));
} catch (Exception e) {
e.printStackTrace();
}
target.onLoad(); target.onLoad();
try { try {
Bukkit.getPluginManager().callEvent(new TPluginEnableEvent(target)); Bukkit.getPluginManager().callEvent(new TPluginEnableEvent(target));