update 5.04
This commit is contained in:
@@ -24,12 +24,12 @@ import java.util.stream.Collectors;
|
||||
* @Author sky
|
||||
* @Since 2018-05-23 2:43
|
||||
*/
|
||||
@TFunction(enable = "init")
|
||||
public class TCommandHandler {
|
||||
|
||||
private static SimpleCommandMap commandMap;
|
||||
private static Map<String, Command> knownCommands;
|
||||
|
||||
@TFunction.Init
|
||||
static void init() {
|
||||
SimpleReflection.saveField(SimplePluginManager.class, "commandMap");
|
||||
SimpleReflection.saveField(SimpleCommandMap.class, "knownCommands");
|
||||
|
||||
@@ -14,6 +14,24 @@ import java.lang.annotation.Target;
|
||||
public @interface TFunction {
|
||||
|
||||
String enable() default "onEnable";
|
||||
|
||||
String disable() default "onDisable";
|
||||
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@interface Load {
|
||||
|
||||
}
|
||||
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@interface Init {
|
||||
|
||||
}
|
||||
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@interface Cancel {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
package io.izzel.taboolib.module.inject;
|
||||
|
||||
import io.izzel.taboolib.TabooLibAPI;
|
||||
import io.izzel.taboolib.TabooLibLoader;
|
||||
import io.izzel.taboolib.module.locale.logger.TLogger;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
|
||||
@@ -14,44 +14,54 @@ import java.lang.reflect.Modifier;
|
||||
*/
|
||||
public class TFunctionLoader implements TabooLibLoader.Loader {
|
||||
|
||||
@Override
|
||||
public void preLoad(Plugin plugin, Class<?> pluginClass) {
|
||||
invokeMethods(pluginClass, TFunction.Load.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postLoad(Plugin plugin, Class<?> pluginClass) {
|
||||
invokeMethods(pluginClass, true);
|
||||
invokeMethods(pluginClass, TFunction.Init.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unload(Plugin plugin, Class<?> pluginClass) {
|
||||
invokeMethods(pluginClass, false);
|
||||
invokeMethods(pluginClass, TFunction.Cancel.class);
|
||||
}
|
||||
|
||||
public void invokeMethods(Class<?> pluginClass, boolean enable) {
|
||||
if (pluginClass.isAnnotationPresent(TFunction.class)) {
|
||||
TFunction function = pluginClass.getAnnotation(TFunction.class);
|
||||
try {
|
||||
Method method = pluginClass.getDeclaredMethod(function.enable());
|
||||
Method method = pluginClass.getDeclaredMethod(enable ? function.enable() : function.disable());
|
||||
if (!Modifier.isStatic(method.getModifiers())) {
|
||||
TLogger.getGlobalLogger().error(method.getName() + " is not a static method.");
|
||||
return;
|
||||
}
|
||||
method.setAccessible(true);
|
||||
method.invoke(null);
|
||||
TabooLibAPI.debug("Function " + pluginClass.getSimpleName() + " loaded. (" + plugin.getName() + ")");
|
||||
} catch (NoSuchMethodException ignore) {
|
||||
} catch (Exception e) {
|
||||
TLogger.getGlobalLogger().warn("TFunction load Failed: " + pluginClass.getName());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@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());
|
||||
if (!Modifier.isStatic(method.getModifiers())) {
|
||||
TLogger.getGlobalLogger().error(method.getName() + " is not a static method.");
|
||||
return;
|
||||
public void invokeMethods(Class<?> pluginClass, Class<? extends Annotation> a) {
|
||||
for (Method declaredMethod : pluginClass.getDeclaredMethods()) {
|
||||
if (declaredMethod.isAnnotationPresent(a)) {
|
||||
try {
|
||||
if (!Modifier.isStatic(declaredMethod.getModifiers())) {
|
||||
TLogger.getGlobalLogger().error(declaredMethod.getName() + " is not a static method.");
|
||||
return;
|
||||
}
|
||||
declaredMethod.setAccessible(true);
|
||||
declaredMethod.invoke(null);
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
method.setAccessible(true);
|
||||
method.invoke(null);
|
||||
TabooLibAPI.debug("Function " + pluginClass.getSimpleName() + " unloaded. (" + plugin.getName() + ")");
|
||||
} catch (NoSuchMethodException ignore) {
|
||||
} catch (Exception e) {
|
||||
TLogger.getGlobalLogger().warn("TFunction unload Failed: " + pluginClass.getName());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,9 @@ import io.izzel.taboolib.module.packet.TPacketHandler;
|
||||
import io.izzel.taboolib.module.packet.TPacketListener;
|
||||
import io.izzel.taboolib.util.lite.cooldown.Cooldown;
|
||||
import io.izzel.taboolib.util.lite.cooldown.Cooldowns;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
@@ -66,6 +68,7 @@ public class TInjectLoader implements TabooLibLoader.Loader {
|
||||
}
|
||||
});
|
||||
TabooLibLoader.runTask(config::runListener);
|
||||
} catch (NoSuchMethodException ignore) {
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
@@ -97,6 +100,26 @@ public class TInjectLoader implements TabooLibLoader.Loader {
|
||||
t.printStackTrace();
|
||||
}
|
||||
});
|
||||
// PluginExists Inject
|
||||
injectTypes.put(Boolean.TYPE, (plugin, field, args, pluginClass, instance) -> {
|
||||
try {
|
||||
if (args.value().length > 0) {
|
||||
field.set(instance, Bukkit.getPluginManager().getPlugin(args.value()[0]) != null);
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
});
|
||||
// PluginHook Inject
|
||||
injectTypes.put(JavaPlugin.class, (plugin, field, args, pluginClass, instance) -> {
|
||||
try {
|
||||
if (args.value().length > 0) {
|
||||
field.set(instance, Bukkit.getPluginManager().getPlugin(args.value()[0]));
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -23,12 +23,12 @@ import java.util.Optional;
|
||||
* @Author 坏黑
|
||||
* @Since 2019-05-22 1:16
|
||||
*/
|
||||
@TFunction(enable = "init")
|
||||
public class SimpleI18n {
|
||||
|
||||
private static FileConfiguration lang;
|
||||
private static boolean released;
|
||||
|
||||
@TFunction.Init
|
||||
static void init() {
|
||||
File localeFile = getLocaleFile(TabooLib.getPlugin());
|
||||
if (localeFile == null) {
|
||||
|
||||
Reference in New Issue
Block a user