From ee3bb22c2c877c3a145f0a61d227058bd80cf96b Mon Sep 17 00:00:00 2001 From: Luck Date: Thu, 13 Aug 2020 10:31:20 +0100 Subject: [PATCH] Improve Bukkit identifyClassLoader (#2444) --- .../me/lucko/luckperms/bukkit/LPBukkitBootstrap.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitBootstrap.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitBootstrap.java index f2c248954..98cf5bf6c 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitBootstrap.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/LPBukkitBootstrap.java @@ -42,6 +42,7 @@ import org.bukkit.plugin.java.JavaPlugin; import org.checkerframework.checker.nullness.qual.Nullable; import java.io.InputStream; +import java.lang.reflect.Method; import java.nio.file.Path; import java.time.Instant; import java.util.ArrayList; @@ -282,9 +283,14 @@ public class LPBukkitBootstrap extends JavaPlugin implements LuckPermsBootstrap } @Override - public @Nullable String identifyClassLoader(ClassLoader classLoader) { - if (classLoader instanceof org.bukkit.plugin.java.PluginClassLoader) { - return ((org.bukkit.plugin.java.PluginClassLoader) classLoader).getPlugin().getName(); + public @Nullable String identifyClassLoader(ClassLoader classLoader) throws ReflectiveOperationException { + Class pluginClassLoaderClass = Class.forName("org.bukkit.plugin.java.PluginClassLoader"); + if (pluginClassLoaderClass.isInstance(classLoader)) { + Method getPluginMethod = pluginClassLoaderClass.getDeclaredMethod("getPlugin"); + getPluginMethod.setAccessible(true); + + JavaPlugin plugin = (JavaPlugin) getPluginMethod.invoke(classLoader); + return plugin.getName(); } return null; }