+ update inject api
This commit is contained in:
parent
e3daaf0162
commit
f04fcaa2b3
@ -9,16 +9,24 @@ import io.izzel.taboolib.util.Reflection;
|
|||||||
*/
|
*/
|
||||||
public class CompatKotlin {
|
public class CompatKotlin {
|
||||||
|
|
||||||
public static boolean isCompanion(Class clazz) {
|
public static boolean isCompanion(Class<?> pluginClass) {
|
||||||
return clazz.getName().endsWith("$Companion");
|
return pluginClass.getName().endsWith("$Companion");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Object getCompanion(Class clazz) {
|
public static Object getCompanion(Class<?> pluginClass) {
|
||||||
try {
|
try {
|
||||||
return Reflection.getValue(null, TabooLibAPI.getPluginBridge().getClass(clazz.getName().substring(0, clazz.getName().indexOf("$Companion"))), true, "Companion");
|
return Reflection.getValue(null, TabooLibAPI.getPluginBridge().getClass(pluginClass.getName().substring(0, pluginClass.getName().indexOf("$Companion"))), true, "Companion");
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
t.printStackTrace();
|
t.printStackTrace();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Object getInstance(Class<?> pluginClass) {
|
||||||
|
try {
|
||||||
|
return Reflection.getValue(null, pluginClass, true, "INSTANCE");
|
||||||
|
} catch (Throwable ignored) {
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,11 @@
|
|||||||
package io.izzel.taboolib.module.db.local;
|
package io.izzel.taboolib.module.db.local;
|
||||||
|
|
||||||
import io.izzel.taboolib.TabooLibLoader;
|
import io.izzel.taboolib.TabooLibLoader;
|
||||||
import io.izzel.taboolib.module.locale.logger.TLogger;
|
import io.izzel.taboolib.module.inject.TInjectHelper;
|
||||||
import io.izzel.taboolib.util.Ref;
|
import io.izzel.taboolib.util.Ref;
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.Modifier;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Author 坏黑
|
* @Author 坏黑
|
||||||
@ -21,21 +20,13 @@ public class LocalLoader implements TabooLibLoader.Loader {
|
|||||||
if (annotation == null) {
|
if (annotation == null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
Object instance = null;
|
|
||||||
// 如果是非静态类型
|
|
||||||
if (!Modifier.isStatic(field.getModifiers())) {
|
|
||||||
// 是否为主类
|
|
||||||
if (pluginClass.equals(plugin.getClass())) {
|
|
||||||
instance = plugin;
|
|
||||||
} else {
|
|
||||||
TLogger.getGlobalLogger().error(field.getName() + " is not a static field. (" + pluginClass.getName() + ")");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ref.forcedAccess(field);
|
Ref.forcedAccess(field);
|
||||||
try {
|
for (Object instance : TInjectHelper.getInstance(field, pluginClass, plugin)) {
|
||||||
field.set(instance, Local.get(plugin.getName()).get(annotation.value()));
|
try {
|
||||||
} catch (IllegalAccessException ignored) {
|
field.set(instance, Local.get(plugin.getName()).get(annotation.value()));
|
||||||
|
} catch (Throwable t) {
|
||||||
|
t.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -32,9 +32,12 @@ public class PlayerContainerLoader implements Listener, TabooLibLoader.Loader {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
field.setAccessible(true);
|
field.setAccessible(true);
|
||||||
try {
|
for (Object instance : TInjectHelper.getInstance(field, pluginClass, plugin)) {
|
||||||
pluginContainer.computeIfAbsent(plugin.getName(), name -> new ArrayList<>()).add(new Container(field.get(pluginClass), annotation.uniqueId()));
|
try {
|
||||||
} catch (IllegalAccessException ignored) {
|
pluginContainer.computeIfAbsent(plugin.getName(), name -> new ArrayList<>()).add(new Container(field.get(instance), annotation.uniqueId()));
|
||||||
|
} catch (Throwable t) {
|
||||||
|
t.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,10 @@
|
|||||||
package io.izzel.taboolib.module.inject;
|
package io.izzel.taboolib.module.inject;
|
||||||
|
|
||||||
import io.izzel.taboolib.TabooLibLoader;
|
import io.izzel.taboolib.TabooLibLoader;
|
||||||
import io.izzel.taboolib.compat.kotlin.CompatKotlin;
|
|
||||||
import io.izzel.taboolib.module.locale.logger.TLogger;
|
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
|
|
||||||
import java.lang.annotation.Annotation;
|
import java.lang.annotation.Annotation;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.lang.reflect.Modifier;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Author sky
|
* @Author sky
|
||||||
@ -17,65 +14,49 @@ public class TFunctionLoader implements TabooLibLoader.Loader {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void preLoad(Plugin plugin, Class<?> pluginClass) {
|
public void preLoad(Plugin plugin, Class<?> pluginClass) {
|
||||||
invokeMethods(pluginClass, TFunction.Load.class);
|
invokeMethods(plugin, pluginClass, TFunction.Load.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void postLoad(Plugin plugin, Class<?> pluginClass) {
|
public void postLoad(Plugin plugin, Class<?> pluginClass) {
|
||||||
invokeMethods(pluginClass, true);
|
invokeMethods(plugin, pluginClass, true);
|
||||||
invokeMethods(pluginClass, TFunction.Init.class);
|
invokeMethods(plugin, pluginClass, TFunction.Init.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void unload(Plugin plugin, Class<?> pluginClass) {
|
public void unload(Plugin plugin, Class<?> pluginClass) {
|
||||||
invokeMethods(pluginClass, false);
|
invokeMethods(plugin, pluginClass, false);
|
||||||
invokeMethods(pluginClass, TFunction.Cancel.class);
|
invokeMethods(plugin, pluginClass, TFunction.Cancel.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void invokeMethods(Class<?> pluginClass, boolean enable) {
|
public void invokeMethods(Plugin plugin, Class<?> pluginClass, boolean enable) {
|
||||||
if (pluginClass.isAnnotationPresent(TFunction.class)) {
|
if (pluginClass.isAnnotationPresent(TFunction.class)) {
|
||||||
TFunction function = pluginClass.getAnnotation(TFunction.class);
|
TFunction function = pluginClass.getAnnotation(TFunction.class);
|
||||||
try {
|
try {
|
||||||
Object instance = null;
|
|
||||||
Method method = pluginClass.getDeclaredMethod(enable ? function.enable() : function.disable());
|
Method method = pluginClass.getDeclaredMethod(enable ? function.enable() : function.disable());
|
||||||
if (CompatKotlin.isCompanion(pluginClass)) {
|
|
||||||
instance = CompatKotlin.getCompanion(pluginClass);
|
|
||||||
if (instance == null) {
|
|
||||||
TLogger.getGlobalLogger().error(method.getName() + " required @JvmStatic.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} else if (!Modifier.isStatic(method.getModifiers())) {
|
|
||||||
TLogger.getGlobalLogger().error(method.getName() + " is not a static method.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
method.setAccessible(true);
|
method.setAccessible(true);
|
||||||
method.invoke(instance);
|
for (Object instance : TInjectHelper.getInstance(method, pluginClass, plugin)) {
|
||||||
|
try {
|
||||||
|
method.invoke(instance);
|
||||||
|
} catch (Throwable t) {
|
||||||
|
t.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
} catch (NoSuchMethodException ignore) {
|
} catch (NoSuchMethodException ignore) {
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void invokeMethods(Class<?> pluginClass, Class<? extends Annotation> a) {
|
public void invokeMethods(Plugin plugin, Class<?> pluginClass, Class<? extends Annotation> a) {
|
||||||
for (Method declaredMethod : pluginClass.getDeclaredMethods()) {
|
for (Method declaredMethod : pluginClass.getDeclaredMethods()) {
|
||||||
if (declaredMethod.isAnnotationPresent(a)) {
|
if (declaredMethod.isAnnotationPresent(a)) {
|
||||||
try {
|
declaredMethod.setAccessible(true);
|
||||||
Object instance = null;
|
for (Object instance : TInjectHelper.getInstance(declaredMethod, pluginClass, plugin)) {
|
||||||
if (CompatKotlin.isCompanion(pluginClass)) {
|
try {
|
||||||
instance = CompatKotlin.getCompanion(pluginClass);
|
declaredMethod.invoke(instance);
|
||||||
if (instance == null) {
|
} catch (Throwable t) {
|
||||||
TLogger.getGlobalLogger().error(declaredMethod.getName() + " required @JvmStatic.");
|
t.printStackTrace();
|
||||||
return;
|
|
||||||
}
|
|
||||||
} else if (!Modifier.isStatic(declaredMethod.getModifiers())) {
|
|
||||||
TLogger.getGlobalLogger().error(declaredMethod.getName() + " is not a static method.");
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
declaredMethod.setAccessible(true);
|
|
||||||
declaredMethod.invoke(instance);
|
|
||||||
} catch (Throwable t) {
|
|
||||||
t.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,14 +2,12 @@ package io.izzel.taboolib.module.inject;
|
|||||||
|
|
||||||
import io.izzel.taboolib.TabooLibAPI;
|
import io.izzel.taboolib.TabooLibAPI;
|
||||||
import io.izzel.taboolib.TabooLibLoader;
|
import io.izzel.taboolib.TabooLibLoader;
|
||||||
import io.izzel.taboolib.module.locale.logger.TLogger;
|
|
||||||
import io.izzel.taboolib.util.Ref;
|
import io.izzel.taboolib.util.Ref;
|
||||||
import io.izzel.taboolib.util.Strings;
|
import io.izzel.taboolib.util.Strings;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.Modifier;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Author sky
|
* @Author sky
|
||||||
@ -29,22 +27,13 @@ public class THookLoader implements TabooLibLoader.Loader {
|
|||||||
if (Plugin.class.isAssignableFrom(declaredField.getType()) && declaredField.isAnnotationPresent(THook.class)) {
|
if (Plugin.class.isAssignableFrom(declaredField.getType()) && declaredField.isAnnotationPresent(THook.class)) {
|
||||||
THook hook = declaredField.getAnnotation(THook.class);
|
THook hook = declaredField.getAnnotation(THook.class);
|
||||||
if (Strings.nonEmpty(hook.plugin())) {
|
if (Strings.nonEmpty(hook.plugin())) {
|
||||||
Object instance = null;
|
|
||||||
// 如果是非静态类型
|
|
||||||
if (!Modifier.isStatic(declaredField.getModifiers())) {
|
|
||||||
// 是否为主类
|
|
||||||
if (pluginClass.equals(plugin.getClass())) {
|
|
||||||
instance = plugin;
|
|
||||||
} else {
|
|
||||||
TLogger.getGlobalLogger().error(declaredField.getName() + " is not a static field. (" + pluginClass.getName() + ")");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ref.forcedAccess(declaredField);
|
Ref.forcedAccess(declaredField);
|
||||||
try {
|
for (Object instance : TInjectHelper.getInstance(declaredField, pluginClass, plugin)) {
|
||||||
declaredField.set(instance, Bukkit.getPluginManager().getPlugin(hook.plugin()));
|
try {
|
||||||
} catch (Throwable t) {
|
declaredField.set(instance, Bukkit.getPluginManager().getPlugin(hook.plugin()));
|
||||||
t.printStackTrace();
|
} catch (Throwable t) {
|
||||||
|
t.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,12 +2,10 @@ package io.izzel.taboolib.module.inject;
|
|||||||
|
|
||||||
import io.izzel.taboolib.TabooLibLoader;
|
import io.izzel.taboolib.TabooLibLoader;
|
||||||
import io.izzel.taboolib.module.lite.SimpleVersionControl;
|
import io.izzel.taboolib.module.lite.SimpleVersionControl;
|
||||||
import io.izzel.taboolib.module.locale.logger.TLogger;
|
|
||||||
import io.izzel.taboolib.util.Ref;
|
import io.izzel.taboolib.util.Ref;
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.Modifier;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Author sky
|
* @Author sky
|
||||||
@ -22,22 +20,13 @@ public class TInjectAsm implements TabooLibLoader.Loader {
|
|||||||
if (annotation == null || annotation.asm().isEmpty()) {
|
if (annotation == null || annotation.asm().isEmpty()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
Object instance = null;
|
|
||||||
// 如果是非静态类型
|
|
||||||
if (!Modifier.isStatic(declaredField.getModifiers())) {
|
|
||||||
// 是否为主类
|
|
||||||
if (pluginClass.equals(plugin.getClass())) {
|
|
||||||
instance = plugin;
|
|
||||||
} else {
|
|
||||||
TLogger.getGlobalLogger().error(declaredField.getName() + " is not a static field. (" + pluginClass.getName() + ")");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ref.forcedAccess(declaredField);
|
Ref.forcedAccess(declaredField);
|
||||||
try {
|
for (Object instance : TInjectHelper.getInstance(declaredField, pluginClass, plugin)) {
|
||||||
declaredField.set(instance, SimpleVersionControl.createNMS(annotation.asm()).useCache().translate(plugin).newInstance());
|
try {
|
||||||
} catch (Throwable t) {
|
declaredField.set(instance, SimpleVersionControl.createNMS(annotation.asm()).useCache().translate(plugin).newInstance());
|
||||||
TLogger.getGlobalLogger().warn("Cannot translate class \"" + declaredField.getType().getName() + "\": " + t.getMessage());
|
} catch (Throwable t) {
|
||||||
|
t.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@ import io.izzel.taboolib.util.Reflection;
|
|||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.Modifier;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
@ -54,27 +54,15 @@ public class TInjectCreator implements TabooLibLoader.Loader {
|
|||||||
if (annotation == null || annotation.state() != state) {
|
if (annotation == null || annotation.state() != state) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
ClassData classData = new ClassData(loadClass, declaredField.getType());
|
List<Object> instance = TInjectHelper.getInstance(declaredField, loadClass, plugin);
|
||||||
Object instance = null;
|
if (instance.isEmpty()) {
|
||||||
// 非静态类型
|
continue;
|
||||||
if (!Modifier.isStatic(declaredField.getModifiers())) {
|
|
||||||
// 在插件主类
|
|
||||||
if (loadClass.equals(plugin.getClass())) {
|
|
||||||
instance = plugin;
|
|
||||||
}
|
|
||||||
// 判断 pluginCLass 是否为 TInject 创建
|
|
||||||
else if (instanceMap.containsKey(classData)) {
|
|
||||||
instance = instanceMap.get(classData).getInstance();
|
|
||||||
} else {
|
|
||||||
TLogger.getGlobalLogger().error(declaredField.getName() + " is not a static field. (" + loadClass.getName() + ")");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Ref.forcedAccess(declaredField);
|
Ref.forcedAccess(declaredField);
|
||||||
try {
|
try {
|
||||||
InstanceData instanceData = new InstanceData(declaredField.getType().newInstance(), annotation);
|
InstanceData instanceData = new InstanceData(declaredField.getType().newInstance(), annotation);
|
||||||
declaredField.set(instance, instanceData.getInstance());
|
declaredField.set(instance, instanceData.getInstance());
|
||||||
instanceMap.put(classData, instanceData);
|
instanceMap.put(new ClassData(loadClass, declaredField.getType()), instanceData);
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
TLogger.getGlobalLogger().error(declaredField.getName() + " instantiation failed: " + t.getMessage());
|
TLogger.getGlobalLogger().error(declaredField.getName() + " instantiation failed: " + t.getMessage());
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,15 @@
|
|||||||
package io.izzel.taboolib.module.inject;
|
package io.izzel.taboolib.module.inject;
|
||||||
|
|
||||||
|
import com.google.common.collect.Lists;
|
||||||
|
import io.izzel.taboolib.compat.kotlin.CompatKotlin;
|
||||||
|
import io.izzel.taboolib.module.locale.logger.TLogger;
|
||||||
|
import org.bukkit.plugin.Plugin;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.lang.reflect.Modifier;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Author sky
|
* @Author sky
|
||||||
* @Since 2019-08-17 23:22
|
* @Since 2019-08-17 23:22
|
||||||
@ -23,4 +33,60 @@ public class TInjectHelper {
|
|||||||
return inject.cancel();
|
return inject.cancel();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static List<Object> getInstance(Field field, Class<?> pluginClass, Plugin plugin) {
|
||||||
|
List<Object> instance = Lists.newArrayList();
|
||||||
|
// Static
|
||||||
|
if (Modifier.isStatic(field.getModifiers())) {
|
||||||
|
instance.add(null);
|
||||||
|
}
|
||||||
|
// No Static
|
||||||
|
else if (!Modifier.isStatic(field.getModifiers())) {
|
||||||
|
// Main
|
||||||
|
if (pluginClass.equals(plugin.getClass())) {
|
||||||
|
instance.add(plugin);
|
||||||
|
}
|
||||||
|
// TInject
|
||||||
|
else if (TInjectCreator.getInstanceMap().entrySet().stream().anyMatch(e -> e.getKey().getType().equals(pluginClass))) {
|
||||||
|
TInjectCreator.getInstanceMap().entrySet().stream().filter(e -> e.getKey().getType().equals(pluginClass)).forEach(i -> instance.add(i.getValue().getInstance()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Nothing
|
||||||
|
else if (instance.isEmpty()) {
|
||||||
|
TLogger.getGlobalLogger().error(field.getName() + " is not a static field. (" + pluginClass.getName() + ")");
|
||||||
|
}
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<Object> getInstance(Method method, Class<?> pluginClass, Plugin plugin) {
|
||||||
|
List<Object> instance = Lists.newArrayList();
|
||||||
|
// Object
|
||||||
|
if (CompatKotlin.getInstance(pluginClass) != null) {
|
||||||
|
instance.add(CompatKotlin.getInstance(pluginClass));
|
||||||
|
}
|
||||||
|
// Companion Object
|
||||||
|
else if (CompatKotlin.isCompanion(pluginClass)) {
|
||||||
|
instance.add(CompatKotlin.getCompanion(pluginClass));
|
||||||
|
}
|
||||||
|
// Static
|
||||||
|
else if (Modifier.isStatic(method.getModifiers())) {
|
||||||
|
instance.add(null);
|
||||||
|
}
|
||||||
|
// No Static
|
||||||
|
else if (!Modifier.isStatic(method.getModifiers())) {
|
||||||
|
// Main
|
||||||
|
if (pluginClass.equals(plugin.getClass())) {
|
||||||
|
instance.add(plugin);
|
||||||
|
}
|
||||||
|
// TInject
|
||||||
|
else if (TInjectCreator.getInstanceMap().entrySet().stream().anyMatch(e -> e.getKey().getType().equals(pluginClass))) {
|
||||||
|
TInjectCreator.getInstanceMap().entrySet().stream().filter(e -> e.getKey().getType().equals(pluginClass)).forEach(i -> instance.add(i.getValue().getInstance()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Nothing
|
||||||
|
else if (instance.isEmpty()) {
|
||||||
|
TLogger.getGlobalLogger().error(method.getName() + " is not a static method. (" + pluginClass.getName() + ")");
|
||||||
|
}
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,6 @@ import org.bukkit.plugin.java.JavaPlugin;
|
|||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.lang.reflect.Modifier;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -135,22 +134,11 @@ public class TInjectLoader implements TabooLibLoader.Loader {
|
|||||||
public void preLoad(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 || !declaredField.getType().equals(plugin.getClass())) {
|
if (annotation == null || !declaredField.getType().equals(plugin.getClass())) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
Object instance = null;
|
Ref.forcedAccess(declaredField);
|
||||||
// 如果是非静态类型
|
TInjectHelper.getInstance(declaredField, pluginClass, plugin).forEach(instance -> inject(plugin, declaredField, instance, annotation, injectTypes.get(Plugin.class), pluginClass));
|
||||||
if (!Modifier.isStatic(declaredField.getModifiers())) {
|
|
||||||
// 是否为主类
|
|
||||||
if (pluginClass.equals(plugin.getClass())) {
|
|
||||||
instance = plugin;
|
|
||||||
} else {
|
|
||||||
TLogger.getGlobalLogger().error(declaredField.getName() + " is not a static field. (" + pluginClass.getName() + ")");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
inject(plugin, declaredField, instance, annotation, injectTypes.get(Plugin.class), pluginClass);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -161,26 +149,15 @@ public class TInjectLoader implements TabooLibLoader.Loader {
|
|||||||
if (annotation == null || declaredField.getType().equals(plugin.getClass())) {
|
if (annotation == null || declaredField.getType().equals(plugin.getClass())) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
Object instance = null;
|
Ref.forcedAccess(declaredField);
|
||||||
// 如果是非静态类型
|
|
||||||
if (!Modifier.isStatic(declaredField.getModifiers())) {
|
|
||||||
// 是否为主类
|
|
||||||
if (pluginClass.equals(plugin.getClass())) {
|
|
||||||
instance = plugin;
|
|
||||||
} else {
|
|
||||||
TLogger.getGlobalLogger().error(declaredField.getName() + " is not a static field. (" + pluginClass.getName() + ")");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
TInjectTask tInjectTask = injectTypes.get(declaredField.getType());
|
TInjectTask tInjectTask = injectTypes.get(declaredField.getType());
|
||||||
if (tInjectTask != null) {
|
if (tInjectTask != null) {
|
||||||
inject(plugin, declaredField, instance, annotation, tInjectTask, pluginClass);
|
TInjectHelper.getInstance(declaredField, pluginClass, plugin).forEach(instance -> inject(plugin, declaredField, instance, annotation, tInjectTask, pluginClass));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void inject(Plugin plugin, Field field, Object instance, TInject annotation, TInjectTask injectTask, Class pluginClass) {
|
public void inject(Plugin plugin, Field field, Object instance, TInject annotation, TInjectTask injectTask, Class pluginClass) {
|
||||||
Ref.forcedAccess(field);
|
|
||||||
try {
|
try {
|
||||||
injectTask.run(plugin, field, annotation, pluginClass, instance);
|
injectTask.run(plugin, field, annotation, pluginClass, instance);
|
||||||
TabooLibAPI.debug(field.getName() + " injected. (" + field.getType().getName() + ")");
|
TabooLibAPI.debug(field.getName() + " injected. (" + field.getType().getName() + ")");
|
||||||
|
@ -4,13 +4,10 @@ import com.google.common.collect.Lists;
|
|||||||
import com.google.common.collect.Maps;
|
import com.google.common.collect.Maps;
|
||||||
import io.izzel.taboolib.TabooLib;
|
import io.izzel.taboolib.TabooLib;
|
||||||
import io.izzel.taboolib.TabooLibLoader;
|
import io.izzel.taboolib.TabooLibLoader;
|
||||||
import io.izzel.taboolib.compat.kotlin.CompatKotlin;
|
|
||||||
import io.izzel.taboolib.module.locale.logger.TLogger;
|
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
import org.bukkit.scheduler.BukkitRunnable;
|
import org.bukkit.scheduler.BukkitRunnable;
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.lang.reflect.Modifier;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@ -44,27 +41,18 @@ public class TScheduleLoader implements TabooLibLoader.Loader {
|
|||||||
if (annotation == null) {
|
if (annotation == null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
Object[] instance = new Object[] {pluginClass.equals(plugin.getClass()) ? plugin : null};
|
method.setAccessible(true);
|
||||||
if (CompatKotlin.isCompanion(pluginClass)) {
|
|
||||||
instance[0] = CompatKotlin.getCompanion(pluginClass);
|
|
||||||
if (instance[0] == null) {
|
|
||||||
TLogger.getGlobalLogger().error(method.getName() + " required @JvmStatic.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} else if (!Modifier.isStatic(method.getModifiers()) && instance[0] == null) {
|
|
||||||
TLogger.getGlobalLogger().error(method.getName() + " is not a static method.");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (plugin.equals(TabooLib.getPlugin())) {
|
if (plugin.equals(TabooLib.getPlugin())) {
|
||||||
run(plugin, new BukkitRunnable() {
|
run(plugin, new BukkitRunnable() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
for (Object i : TInjectHelper.getInstance(method, pluginClass, plugin)) {
|
||||||
method.setAccessible(true);
|
try {
|
||||||
method.invoke(instance[0]);
|
method.invoke(i);
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
t.printStackTrace();
|
t.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, annotation.delay(), annotation.period(), annotation.async());
|
}, annotation.delay(), annotation.period(), annotation.async());
|
||||||
@ -73,11 +61,12 @@ public class TScheduleLoader implements TabooLibLoader.Loader {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
for (Object i : TInjectHelper.getInstance(method, pluginClass, plugin)) {
|
||||||
method.setAccessible(true);
|
try {
|
||||||
method.invoke(instance[0]);
|
method.invoke(i);
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
t.printStackTrace();
|
t.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
@ -1,13 +1,14 @@
|
|||||||
package io.izzel.taboolib.module.packet;
|
package io.izzel.taboolib.module.packet;
|
||||||
|
|
||||||
import io.izzel.taboolib.TabooLibLoader;
|
import io.izzel.taboolib.TabooLibLoader;
|
||||||
|
import io.izzel.taboolib.module.inject.TInjectHelper;
|
||||||
import io.izzel.taboolib.module.locale.logger.TLogger;
|
import io.izzel.taboolib.module.locale.logger.TLogger;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.lang.reflect.Modifier;
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Author sky
|
* @Author sky
|
||||||
@ -19,49 +20,44 @@ public class TPacketLoader implements TabooLibLoader.Loader {
|
|||||||
public void activeLoad(Plugin plugin, Class<?> pluginClass) {
|
public void activeLoad(Plugin plugin, Class<?> pluginClass) {
|
||||||
for (Method method : pluginClass.getDeclaredMethods()) {
|
for (Method method : pluginClass.getDeclaredMethods()) {
|
||||||
if (method.isAnnotationPresent(TPacket.class)) {
|
if (method.isAnnotationPresent(TPacket.class)) {
|
||||||
Object instance = null;
|
List<Object> instance = TInjectHelper.getInstance(method, pluginClass, plugin);
|
||||||
// 如果是非静态类型
|
if (instance.isEmpty()) {
|
||||||
if (!Modifier.isStatic(method.getModifiers())) {
|
continue;
|
||||||
// 是否为主类
|
|
||||||
if (pluginClass.equals(plugin.getClass())) {
|
|
||||||
instance = plugin;
|
|
||||||
} else {
|
|
||||||
TLogger.getGlobalLogger().error(method.getName() + " is not a static method. (" + pluginClass.getName() + ")");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
method.setAccessible(true);
|
method.setAccessible(true);
|
||||||
TPacket packet = method.getAnnotation(TPacket.class);
|
TPacket packet = method.getAnnotation(TPacket.class);
|
||||||
boolean packetType = method.getReturnType().equals(Boolean.TYPE) || method.getReturnType().equals(Boolean.class);
|
boolean packetType = method.getReturnType().equals(Boolean.TYPE) || method.getReturnType().equals(Boolean.class);
|
||||||
// object type
|
// object type
|
||||||
if (Arrays.equals(method.getParameterTypes(), new Class[] {Player.class, Object.class})) {
|
if (Arrays.equals(method.getParameterTypes(), new Class[] {Player.class, Object.class})) {
|
||||||
Object finalInstance = instance;
|
for (Object i : instance) {
|
||||||
TPacketHandler.addListener(plugin, new TPacketListener() {
|
TPacketHandler.addListener(plugin, new TPacketListener() {
|
||||||
@Override
|
@Override
|
||||||
public boolean onSend(Player player, Object p) {
|
public boolean onSend(Player player, Object p) {
|
||||||
return eval(finalInstance, packet, TPacket.Type.SEND, packetType, method, player, p);
|
return eval(i, packet, TPacket.Type.SEND, packetType, method, player, p);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onReceive(Player player, Object p) {
|
public boolean onReceive(Player player, Object p) {
|
||||||
return eval(finalInstance, packet, TPacket.Type.RECEIVE, packetType, method, player, p);
|
return eval(i, packet, TPacket.Type.RECEIVE, packetType, method, player, p);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// packet type
|
// packet type
|
||||||
else if (Arrays.equals(method.getParameterTypes(), new Class[] {Player.class, Packet.class})) {
|
else if (Arrays.equals(method.getParameterTypes(), new Class[] {Player.class, Packet.class})) {
|
||||||
Object finalInstance1 = instance;
|
for (Object i : instance) {
|
||||||
TPacketHandler.addListener(plugin, new TPacketListener() {
|
TPacketHandler.addListener(plugin, new TPacketListener() {
|
||||||
@Override
|
@Override
|
||||||
public boolean onSend(Player player, Packet p) {
|
public boolean onSend(Player player, Packet p) {
|
||||||
return eval(finalInstance1, packet, TPacket.Type.SEND, packetType, method, player, p);
|
return eval(i, packet, TPacket.Type.SEND, packetType, method, player, p);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onReceive(Player player, Packet p) {
|
public boolean onReceive(Player player, Packet p) {
|
||||||
return eval(finalInstance1, packet, TPacket.Type.RECEIVE, packetType, method, player, p);
|
return eval(i, packet, TPacket.Type.RECEIVE, packetType, method, player, p);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
TLogger.getGlobalLogger().error(method.getName() + " is an invalid packet listener. (" + pluginClass.getName() + ")");
|
TLogger.getGlobalLogger().error(method.getName() + " is an invalid packet listener. (" + pluginClass.getName() + ")");
|
||||||
TLogger.getGlobalLogger().error("Usage: boolean fun(Player player, Object packet) { ... }");
|
TLogger.getGlobalLogger().error("Usage: boolean fun(Player player, Object packet) { ... }");
|
||||||
|
Loading…
Reference in New Issue
Block a user