diff --git a/inventoryaccess/inventory-access/src/main/java/xyz/xenondevs/inventoryaccess/util/ReflectionRegistry.java b/inventoryaccess/inventory-access/src/main/java/xyz/xenondevs/inventoryaccess/util/ReflectionRegistry.java index 2d9dfb5..2f6a347 100644 --- a/inventoryaccess/inventory-access/src/main/java/xyz/xenondevs/inventoryaccess/util/ReflectionRegistry.java +++ b/inventoryaccess/inventory-access/src/main/java/xyz/xenondevs/inventoryaccess/util/ReflectionRegistry.java @@ -13,10 +13,9 @@ public class ReflectionRegistry { public static final int VERSION = getVersionNumber(); public static final String CRAFT_BUKKIT_PACKAGE_PATH = getCB(); - public static final String BUKKIT_PACKAGE_PATH = "org.bukkit."; // Classes - public static final Class PLUGIN_CLASS_LOADER_CLASS = getBukkitClass("plugin.java.PluginClassLoader"); + public static final Class PLUGIN_CLASS_LOADER_CLASS = ReflectionUtils.getClass("org.bukkit.plugin.java.PluginClassLoader"); public static final Class PAPER_PLUGIN_CLASS_LOADER_CLASS = getClassOrNull("io.papermc.paper.plugin.entrypoint.classloader.PaperPluginClassLoader"); public static final Class CB_CRAFT_META_SKULL_CLASS = getCBClass("inventory.CraftMetaSkull"); public static final Class CB_CRAFT_META_ITEM_CLASS = getCBClass("inventory.CraftMetaItem"); diff --git a/inventoryaccess/inventory-access/src/main/java/xyz/xenondevs/inventoryaccess/util/ReflectionUtils.java b/inventoryaccess/inventory-access/src/main/java/xyz/xenondevs/inventoryaccess/util/ReflectionUtils.java index 6034376..ad07b80 100644 --- a/inventoryaccess/inventory-access/src/main/java/xyz/xenondevs/inventoryaccess/util/ReflectionUtils.java +++ b/inventoryaccess/inventory-access/src/main/java/xyz/xenondevs/inventoryaccess/util/ReflectionUtils.java @@ -1,6 +1,8 @@ package xyz.xenondevs.inventoryaccess.util; import org.bukkit.Bukkit; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import xyz.xenondevs.inventoryaccess.version.InventoryAccessRevision; import java.lang.reflect.Constructor; @@ -10,7 +12,7 @@ import java.lang.reflect.Method; @SuppressWarnings({"unchecked", "unused"}) public class ReflectionUtils { - protected static String getCB() { + protected static @NotNull String getCB() { String path = Bukkit.getServer().getClass().getPackage().getName(); String version = path.substring(path.lastIndexOf(".") + 1); return "org.bukkit.craftbukkit." + version + "."; @@ -22,7 +24,7 @@ public class ReflectionUtils { return Integer.parseInt(version.split("\\.")[1]); } - public static Class getImplClass(String path) { + public static @NotNull Class getImplClass(@NotNull String path) { try { return (Class) Class.forName("xyz.xenondevs.inventoryaccess." + InventoryAccessRevision.REQUIRED_REVISION.getPackageName() + "." + path); } catch (Throwable t) { @@ -30,15 +32,11 @@ public class ReflectionUtils { } } - public static Class getBukkitClass(String path) { - return getClass(ReflectionRegistry.BUKKIT_PACKAGE_PATH + path); - } - - public static Class getCBClass(String path) { + public static @NotNull Class getCBClass(@NotNull String path) { return getClass(ReflectionRegistry.CRAFT_BUKKIT_PACKAGE_PATH + path); } - public static Class getClass(String path) { + public static @NotNull Class getClass(@NotNull String path) { try { return (Class) Class.forName(path); } catch (Throwable t) { @@ -46,7 +44,7 @@ public class ReflectionUtils { } } - public static Class getClassOrNull(String path) { + public static @Nullable Class getClassOrNull(@NotNull String path) { try { return (Class) Class.forName(path); } catch (Throwable t) { @@ -54,7 +52,7 @@ public class ReflectionUtils { } } - public static Field getField(Class clazz, boolean declared, String name) { + public static @NotNull Field getField(@NotNull Class clazz, boolean declared, @NotNull String name) { try { Field field = declared ? clazz.getDeclaredField(name) : clazz.getField(name); if (declared) field.setAccessible(true); @@ -64,7 +62,7 @@ public class ReflectionUtils { } } - public static Constructor getConstructor(Class clazz, boolean declared, Class... parameterTypes) { + public static @NotNull Constructor getConstructor(@NotNull Class clazz, boolean declared, @NotNull Class @NotNull... parameterTypes) { try { Constructor constructor = declared ? clazz.getDeclaredConstructor(parameterTypes) : clazz.getConstructor(parameterTypes); if (declared) constructor.setAccessible(true); @@ -74,7 +72,7 @@ public class ReflectionUtils { } } - public static T constructEmpty(Class clazz) { + public static @NotNull T constructEmpty(@NotNull Class clazz) { try { return (T) getConstructor(clazz, true).newInstance(); } catch (Throwable t) { @@ -82,7 +80,7 @@ public class ReflectionUtils { } } - public static T construct(Constructor constructor, Object... args) { + public static @NotNull T construct(@NotNull Constructor constructor, @Nullable Object @Nullable... args) { try { return constructor.newInstance(args); } catch (Throwable t) { @@ -90,7 +88,7 @@ public class ReflectionUtils { } } - public static Method getMethod(Class clazz, boolean declared, String name, Class... parameterTypes) { + public static @NotNull Method getMethod(@NotNull Class clazz, boolean declared, @NotNull String name, @NotNull Class@NotNull ... parameterTypes) { try { Method method = declared ? clazz.getDeclaredMethod(name, parameterTypes) : clazz.getMethod(name, parameterTypes); if (declared) method.setAccessible(true); @@ -100,7 +98,7 @@ public class ReflectionUtils { } } - public static Method getMethodOrNull(Class clazz, boolean declared, String name, Class... parameterTypes) { + public static @Nullable Method getMethodOrNull(@NotNull Class clazz, boolean declared, @NotNull String name, @NotNull Class @NotNull... parameterTypes) { try { Method method = declared ? clazz.getDeclaredMethod(name, parameterTypes) : clazz.getMethod(name, parameterTypes); if (declared) method.setAccessible(true); @@ -110,7 +108,7 @@ public class ReflectionUtils { } } - public static T invokeMethod(Method method, Object obj, Object... args) { + public static T invokeMethod(@NotNull Method method, @Nullable Object obj, @Nullable Object @Nullable... args) { try { return (T) method.invoke(obj, args); } catch (Throwable t) { @@ -118,7 +116,7 @@ public class ReflectionUtils { } } - public static void setFieldValue(Field field, Object obj, Object value) { + public static void setFieldValue(@NotNull Field field, @Nullable Object obj, @Nullable Object value) { try { field.set(obj, value); } catch (Throwable t) { @@ -127,7 +125,7 @@ public class ReflectionUtils { } @SuppressWarnings("unchecked") - public static T getFieldValue(Field field, Object obj) { + public static @Nullable T getFieldValue(@NotNull Field field, @Nullable Object obj) { try { return (T) field.get(obj); } catch (Throwable t) { diff --git a/invui-core/src/main/java/xyz/xenondevs/invui/InvUI.java b/invui-core/src/main/java/xyz/xenondevs/invui/InvUI.java index 8f579d3..60f0fbf 100644 --- a/invui-core/src/main/java/xyz/xenondevs/invui/InvUI.java +++ b/invui-core/src/main/java/xyz/xenondevs/invui/InvUI.java @@ -7,15 +7,13 @@ import org.bukkit.event.server.PluginDisableEvent; import org.bukkit.plugin.Plugin; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import xyz.xenondevs.inventoryaccess.util.ReflectionRegistry; import xyz.xenondevs.inventoryaccess.util.ReflectionUtils; import java.util.ArrayList; import java.util.List; import java.util.logging.Logger; -import static xyz.xenondevs.inventoryaccess.util.ReflectionRegistry.PAPER_PLUGIN_CLASS_LOADER_GET_LOADED_JAVA_PLUGIN_METHOD; -import static xyz.xenondevs.inventoryaccess.util.ReflectionRegistry.PLUGIN_CLASS_LOADER_PLUGIN_FIELD; +import static xyz.xenondevs.inventoryaccess.util.ReflectionRegistry.*; public class InvUI implements Listener { @@ -45,10 +43,14 @@ public class InvUI implements Listener { private @Nullable Plugin tryFindPlugin() { ClassLoader loader = getClass().getClassLoader(); - if (ReflectionRegistry.PLUGIN_CLASS_LOADER_CLASS.isInstance(loader)) { - return ReflectionUtils.getFieldValue(PLUGIN_CLASS_LOADER_PLUGIN_FIELD, loader); - } else if (ReflectionRegistry.PAPER_PLUGIN_CLASS_LOADER_CLASS.isInstance(loader)) { - return ReflectionUtils.invokeMethod(PAPER_PLUGIN_CLASS_LOADER_GET_LOADED_JAVA_PLUGIN_METHOD, loader); + try { + if (PLUGIN_CLASS_LOADER_CLASS.isInstance(loader)) { + return ReflectionUtils.getFieldValue(PLUGIN_CLASS_LOADER_PLUGIN_FIELD, loader); + } else if (PAPER_PLUGIN_CLASS_LOADER_CLASS != null && PAPER_PLUGIN_CLASS_LOADER_CLASS.isInstance(loader)) { + return ReflectionUtils.invokeMethod(PAPER_PLUGIN_CLASS_LOADER_GET_LOADED_JAVA_PLUGIN_METHOD, loader); + } + } catch (Throwable t) { + t.printStackTrace(); } return null;