Implement load dependency in LaunchClassLoader

This commit is contained in:
Luohuayu 2020-02-16 19:15:55 +08:00 committed by GitHub
parent 7967c1dbe2
commit 555d694381

View File

@ -20,11 +20,16 @@ public class TDependencyLoader {
public static synchronized void addToPath(Plugin plugin, URL url) {
try {
ClassLoader loader = plugin instanceof InternalPlugin ? Bukkit.class.getClassLoader() : plugin.getClass().getClassLoader();
if ("LaunchClassLoader".equals(loader.getClass().getSimpleName())) {
MethodHandle methodHandle = Ref.lookup().findVirtual(loader.getClass(), "addURL", MethodType.methodType(void.class, java.net.URL.class));
methodHandle.invoke(loader, url);
} else {
Field ucpField = loader.getClass().getDeclaredField("ucp");
long ucpOffset = Ref.getUnsafe().objectFieldOffset(ucpField);
Object ucp = Ref.getUnsafe().getObject(loader, ucpOffset);
MethodHandle methodHandle = Ref.lookup().findVirtual(ucp.getClass(), "addURL", MethodType.methodType(void.class, java.net.URL.class));
methodHandle.invoke(ucp, url);
}
} catch (Throwable e) {
e.printStackTrace();
}