diff --git a/pom.xml b/pom.xml index 0bbd3eb..d486151 100644 --- a/pom.xml +++ b/pom.xml @@ -92,6 +92,11 @@ Vault 1.5.6 + + javax.mail + mail + 1.4.7 + org.spigotmc spigot1102 diff --git a/src/main/java/pw/yumc/YumCore/bukkit/Log.java b/src/main/java/pw/yumc/YumCore/bukkit/Log.java index c5aa2c4..e27aa25 100644 --- a/src/main/java/pw/yumc/YumCore/bukkit/Log.java +++ b/src/main/java/pw/yumc/YumCore/bukkit/Log.java @@ -40,18 +40,6 @@ public class Log { logger.addHandler(handler); } - /** - * Sends console a message - * - * @param message - * Message to be displayed - */ - public static void console(final String... msg) { - for (final String str : msg) { - console(str); - } - } - /** * Sends console a message * @@ -74,6 +62,18 @@ public class Log { console.sendMessage(prefix + String.format(message, object)); } + /** + * Sends console a message + * + * @param message + * Message to be displayed + */ + public static void console(final String[] msg) { + for (final String str : msg) { + console(str); + } + } + /** * 调试消息 * diff --git a/src/main/java/pw/yumc/YumCore/utils/ReflectUtil.java b/src/main/java/pw/yumc/YumCore/utils/ReflectUtil.java new file mode 100644 index 0000000..848cff9 --- /dev/null +++ b/src/main/java/pw/yumc/YumCore/utils/ReflectUtil.java @@ -0,0 +1,208 @@ +package pw.yumc.YumCore.utils; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; + +/** + * Minecraft反射类 + * + * @since 2015年12月14日 下午1:35:11 + * @author 许凯 + */ +@SuppressWarnings("all") +public class ReflectUtil { + + public static Field getDeclaredFieldByName(final Class source, final String name) { + try { + final Field field = source.getDeclaredField(name); + field.setAccessible(true); + return field; + } catch (final Exception e) { + e.printStackTrace(); + } + return null; + } + + public static List getDeclaredFieldByType(final Class source, final Class type) { + final List list = new ArrayList<>(); + for (final Field field : source.getDeclaredFields()) { + if (field.getType() == type) { + field.setAccessible(true); + list.add(field); + } + } + return list; + } + + public static Method getDeclaredMethod(final Class clzz, final String methodName, final Class... args) { + try { + return clzz.getDeclaredMethod(methodName, args); + } catch (final Exception e) { + e.printStackTrace(); + } + return null; + } + + public static Method getDeclaredMethodByNameAndParams(final Class source, final String name, final Class... args) { + for (final Method method : findMethodByParams(source.getDeclaredMethods(), args)) { + if (method.getName().equals(name)) { + return method; + } + } + return null; + } + + public static List getDeclaredMethodByNameAndType(final Class source, final String name, final Class returnType) { + final List methods = new ArrayList<>(); + for (final Method method : source.getDeclaredMethods()) { + if (method.getName().equals(name) && method.getReturnType().equals(returnType)) { + methods.add(method); + } + } + return methods; + } + + public static List getDeclaredMethodByParams(final Class source, final Class... args) { + return findMethodByParams(source.getDeclaredMethods(), args); + } + + public static List getDeclaredMethodByParamsAndType(final Class source, final Class returnType, final Class... args) { + final List methods = new ArrayList<>(); + for (final Method method : findMethodByParams(source.getDeclaredMethods(), args)) { + if (method.getReturnType().equals(returnType)) { + methods.add(method); + } + } + return methods; + } + + public static List getDeclaredMethodByType(final Class source, final Class returnType) { + final List methods = new ArrayList<>(); + for (final Method method : source.getDeclaredMethods()) { + if (method.getReturnType().equals(returnType)) { + methods.add(method); + } + } + return methods; + } + + public static Field getFieldByName(final Class source, final String name) { + try { + final Field field = source.getField(name); + field.setAccessible(true); + return field; + } catch (final Exception e) { + e.printStackTrace(); + } + return null; + } + + public static List getFieldByType(final Class source, final Class type) { + final List list = new ArrayList<>(); + for (final Field field : source.getFields()) { + if (field.getType() == type) { + field.setAccessible(true); + list.add(field); + } + } + return list; + } + + public static Object getHandle(final Object bukkitObj) { + try { + return bukkitObj.getClass().getMethod("getHandle").invoke(bukkitObj); + } catch (final Exception e) { + } + return null; + } + + public static Method getMethodByNameAndParams(final Class source, final String name, final Class... args) { + for (final Method method : findMethodByParams(source.getMethods(), args)) { + if (method.getName().equals(name)) { + return method; + } + } + return null; + } + + public static List getMethodByNameAndType(final Class source, final String name, final Class returnType) { + final List methods = new ArrayList<>(); + for (final Method method : source.getMethods()) { + if (method.getName().equals(name) && method.getReturnType().equals(returnType)) { + methods.add(method); + } + } + return methods; + } + + public static List getMethodByParams(final Class source, final Class... args) { + return findMethodByParams(source.getMethods(), args); + } + + public static List getMethodByParamsAndType(final Class source, final Class returnType, final Class... args) { + final List methods = new ArrayList<>(); + for (final Method method : findMethodByParams(source.getMethods(), args)) { + if (method.getReturnType().equals(returnType)) { + methods.add(method); + } + } + return methods; + } + + public static List getMethodByType(final Class source, final Class returnType) { + final List methods = new ArrayList<>(); + for (final Method method : source.getMethods()) { + if (method.getReturnType().equals(returnType)) { + methods.add(method); + } + } + return methods; + } + + public static void invokeMethod(final Object object, final String methodName, final Class arg, final Object value) { + try { + final Method m = object.getClass().getDeclaredMethod(methodName, arg); + m.invoke(object, value); + } catch (final Exception e) { + e.printStackTrace(); + } + } + + public static void invokeMethod(final Object object, final String methodName, final Class[] args, final Object[] value) { + try { + final Method m = object.getClass().getDeclaredMethod(methodName, args); + m.invoke(object, value); + } catch (final Exception e) { + e.printStackTrace(); + } + } + + public static void invokeMethod(final Object object, final String methodName, final Object value) { + try { + final Method m = object.getClass().getDeclaredMethod(methodName, value.getClass()); + m.invoke(object, value); + } catch (final Exception e) { + e.printStackTrace(); + } + } + + private static List findMethodByParams(final Method[] methods, final Class... args) { + final List list = new ArrayList<>(); + start: + for (final Method method : methods) { + if (method.getParameterTypes().length == args.length) { + final Class[] array = method.getParameterTypes(); + for (int i = 0; i < args.length; i++) { + if (!array[i].equals(args[i])) { + continue start; + } + } + method.setAccessible(true); + list.add(method); + } + } + return list; + } +}