+ optimized TInject
+ fixed PluginUtils
This commit is contained in:
parent
5e4a9a8c98
commit
b37ec49a80
@ -10,7 +10,6 @@ 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;
|
||||
@ -35,6 +34,18 @@ import java.util.*;
|
||||
@TListener
|
||||
public class TabooLibLoader implements Listener {
|
||||
|
||||
/*
|
||||
关于 TabooLib 各项自动化接口的执行顺序
|
||||
|
||||
[ENABLING]
|
||||
第一阶段:运行 @TInject(Instance Inject)
|
||||
第二阶段(先后):实例化 @TListener -> 实例化 @Instantiable
|
||||
第三阶段(并行):运行 @TFunction & 运行 @TInject
|
||||
|
||||
[ENABLED]
|
||||
第三阶段:注册 @TListener
|
||||
*/
|
||||
|
||||
static TabooLibDeprecated tabooLibDeprecated;
|
||||
static Map<String, List<Class>> pluginClasses = Maps.newHashMap();
|
||||
static List<Loader> loaders = Lists.newArrayList();
|
||||
@ -50,9 +61,10 @@ public class TabooLibLoader implements Listener {
|
||||
|
||||
static void register() {
|
||||
setupClasses();
|
||||
preLoadClasses();
|
||||
registerListener();
|
||||
registerMetrics();
|
||||
loadClasses();
|
||||
postLoadClasses();
|
||||
try {
|
||||
tabooLibDeprecated = new TabooLibDeprecated();
|
||||
} catch (Exception e) {
|
||||
@ -103,8 +115,12 @@ public class TabooLibLoader implements Listener {
|
||||
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 preLoadClasses() {
|
||||
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() {
|
||||
@ -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 -> {
|
||||
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) {
|
||||
}
|
||||
});
|
||||
@ -208,9 +233,14 @@ public class TabooLibLoader implements Listener {
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.LOWEST)
|
||||
public void onEnable(TPluginEnableEvent e) {
|
||||
public void onEnablePre(TPluginEnableEvent e) {
|
||||
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)
|
||||
@ -220,7 +250,10 @@ public class TabooLibLoader implements Listener {
|
||||
|
||||
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) {
|
||||
|
@ -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.internal.TCommandHandler;
|
||||
import me.skymc.taboolib.string.ArrayUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.*;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@ -33,14 +31,18 @@ public class SimpleCommandBuilder {
|
||||
private CompleterCommand completerCommand = EMPTY_COMPLETER_COMMAND;
|
||||
private boolean silence;
|
||||
private boolean forceRegister;
|
||||
private boolean build;
|
||||
|
||||
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.plugin = plugin;
|
||||
this.description = "";
|
||||
this.usage = "/" + command;
|
||||
this.aliases = new ArrayList<>();
|
||||
this.silence = false;
|
||||
this.build = false;
|
||||
}
|
||||
|
||||
public static SimpleCommandBuilder create(String command, Plugin plugin) {
|
||||
@ -92,6 +94,10 @@ public class SimpleCommandBuilder {
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean isBuild() {
|
||||
return build;
|
||||
}
|
||||
|
||||
public SimpleCommandBuilder build() {
|
||||
Preconditions.checkNotNull(completerCommand, "缺少 \"CompleterCommand\" 部分");
|
||||
Preconditions.checkNotNull(completerTab, "缺少 \"CompleterTab\" 部分");
|
||||
@ -109,6 +115,7 @@ public class SimpleCommandBuilder {
|
||||
(sender, command, s, args) -> completerCommand.execute(sender, args),
|
||||
(sender, command, s, args) -> completerTab.execute(sender, args),
|
||||
silence);
|
||||
build = true;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ import java.lang.reflect.Modifier;
|
||||
public class TFunctionLoader implements TabooLibLoader.Loader {
|
||||
|
||||
@Override
|
||||
public void load(Plugin plugin, Class<?> pluginClass) {
|
||||
public void postLoad(Plugin plugin, Class<?> pluginClass) {
|
||||
if (pluginClass.isAnnotationPresent(TFunction.class)) {
|
||||
TFunction function = pluginClass.getAnnotation(TFunction.class);
|
||||
try {
|
||||
|
@ -22,11 +22,19 @@ public class TInjectLoader implements TabooLibLoader.Loader {
|
||||
private static Map<Class<?>, TInjectTask> injectTypes = Maps.newHashMap();
|
||||
|
||||
static {
|
||||
// Instance Inject
|
||||
injectTypes.put(Plugin.class, (plugin, field, args) -> {
|
||||
try {
|
||||
field.set(null, plugin);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
// TLogger Inject
|
||||
injectTypes.put(TLogger.class, (plugin, field, args) -> {
|
||||
try {
|
||||
field.set(null, TLogger.getUnformatted(plugin));
|
||||
} catch (IllegalAccessException e) {
|
||||
field.set(null, args.length == 0 ? TLogger.getUnformatted(plugin) : TLogger.getUnformatted(args[0]));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
@ -40,47 +48,74 @@ public class TInjectLoader implements TabooLibLoader.Loader {
|
||||
});
|
||||
// TConfiguration Inject
|
||||
injectTypes.put(TConfiguration.class, (plugin, field, args) -> {
|
||||
if (args.length == 0) {
|
||||
TLogger.getGlobalLogger().error("Invalid inject arguments: " + field.getName());
|
||||
return;
|
||||
}
|
||||
try {
|
||||
if (args.length == 0) {
|
||||
TLogger.getGlobalLogger().error("Invalid inject arguments: " + field.getName() + " (" + field.getType().getName() + ")");
|
||||
} else {
|
||||
field.set(null, TConfiguration.createInResource(plugin, args[0]));
|
||||
} catch (IllegalAccessException e) {
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
// SimpleCommandBuilder Inject
|
||||
injectTypes.put(SimpleCommandBuilder.class, (plugin, field, args) -> {
|
||||
try {
|
||||
((SimpleCommandBuilder) field.get(null)).build();
|
||||
} catch (IllegalAccessException e) {
|
||||
SimpleCommandBuilder builder = (SimpleCommandBuilder) field.get(null);
|
||||
if (builder.isBuild()) {
|
||||
TLogger.getGlobalLogger().error("Command was registered. (" + field.getType().getName() + ")");
|
||||
} else {
|
||||
builder.build();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load(Plugin plugin, Class<?> pluginClass) {
|
||||
public void preLoad(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.");
|
||||
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;
|
||||
}
|
||||
TInjectTask tInjectTask = injectTypes.get(declaredField.getType());
|
||||
if (tInjectTask == null) {
|
||||
TLogger.getGlobalLogger().error(declaredField.getName() + " cannot inject.");
|
||||
TLogger.getGlobalLogger().error(declaredField.getName() + " is an invalid inject type. (" + declaredField.getType().getName() + ")");
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
declaredField.setAccessible(true);
|
||||
tInjectTask.run(plugin, declaredField, annotation.value());
|
||||
} catch (Exception e) {
|
||||
TLogger.getGlobalLogger().error(declaredField.getName() + " inject failed: " + e.getMessage());
|
||||
TLogger.getGlobalLogger().error(declaredField.getName() + " inject failed: " + e.getMessage() + " (" + declaredField.getType().getName() + ")");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -30,7 +30,7 @@ public class PlayerContainerLoader implements Listener, TabooLibLoader.Loader {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load(Plugin plugin, Class<?> pluginClass) {
|
||||
public void postLoad(Plugin plugin, Class<?> pluginClass) {
|
||||
for (Field field : pluginClass.getDeclaredFields()) {
|
||||
PlayerContainer annotation = field.getAnnotation(PlayerContainer.class);
|
||||
if (annotation == null) {
|
||||
|
@ -8,12 +8,12 @@ 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;
|
||||
import me.skymc.taboolib.timecycle.TimeCycleManager;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.server.PluginDisableEvent;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
@ -30,7 +30,7 @@ import java.util.Optional;
|
||||
@TListener
|
||||
public class ListenerPlugin implements Listener {
|
||||
|
||||
@EventHandler
|
||||
@EventHandler (priority = EventPriority.LOWEST)
|
||||
public void enable(TPluginEnableEvent e) {
|
||||
if (!TLib.getTLib().isInjectEnabled() || !TLib.getTLib().isBlackListPluginExists()) {
|
||||
try {
|
||||
|
@ -4,13 +4,14 @@ import com.ilummc.tlib.logger.TLogger;
|
||||
import com.ilummc.tlib.util.Strings;
|
||||
import me.skymc.taboolib.TabooLib;
|
||||
import me.skymc.taboolib.TabooLibLoader;
|
||||
import me.skymc.taboolib.events.TPluginEnableEvent;
|
||||
import me.skymc.taboolib.methods.ReflectionUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.HandlerList;
|
||||
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;
|
||||
@ -164,11 +165,11 @@ public class TListenerHandler implements Listener {
|
||||
return listeners;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPluginEnable(PluginEnableEvent e) {
|
||||
@EventHandler(priority = EventPriority.LOW)
|
||||
public void onPluginEnable(TPluginEnableEvent e) {
|
||||
try {
|
||||
setupListener(e.getPlugin());
|
||||
registerListener(e.getPlugin());
|
||||
Bukkit.getScheduler().runTask(TabooLib.instance(), () -> registerListener(e.getPlugin()));
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
|
@ -225,11 +225,6 @@ public class PluginUtils {
|
||||
} 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));
|
||||
|
Loading…
Reference in New Issue
Block a user