diff --git a/src/main/scala/io/izzel/taboolib/module/dependency/TDependencyLoader.java b/src/main/scala/io/izzel/taboolib/module/dependency/TDependencyLoader.java index 0dd9f9f..fc638e7 100644 --- a/src/main/scala/io/izzel/taboolib/module/dependency/TDependencyLoader.java +++ b/src/main/scala/io/izzel/taboolib/module/dependency/TDependencyLoader.java @@ -1,10 +1,12 @@ package io.izzel.taboolib.module.dependency; import io.izzel.taboolib.common.plugin.InternalPlugin; +import io.izzel.taboolib.util.Ref; import org.bukkit.Bukkit; import org.bukkit.plugin.Plugin; import java.io.File; +import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.MalformedURLException; @@ -13,11 +15,22 @@ import java.net.URLClassLoader; public class TDependencyLoader { + private static final long ucpOffset; + + static { + try { + Field ucp = Bukkit.class.getClassLoader().getClass().getDeclaredField("ucp"); + ucpOffset = Ref.UNSAFE.objectFieldOffset(ucp); + } catch (Exception e) { + throw new IllegalStateException(); + } + } + public static synchronized void addToPath(Plugin plugin, URL url) { try { - Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class); - method.setAccessible(true); - method.invoke(plugin instanceof InternalPlugin ? Bukkit.class.getClassLoader() : plugin.getClass().getClassLoader(), url); + Object ucp = Ref.UNSAFE.getObject(plugin instanceof InternalPlugin ? Bukkit.class.getClassLoader() : plugin.getClass().getClassLoader(), ucpOffset); + Method addURL = ucp.getClass().getMethod("addURL", URL.class); + addURL.invoke(ucp, url); } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); } diff --git a/src/main/scala/io/izzel/taboolib/util/Ref.java b/src/main/scala/io/izzel/taboolib/util/Ref.java index 20a06d6..eec8d1b 100644 --- a/src/main/scala/io/izzel/taboolib/util/Ref.java +++ b/src/main/scala/io/izzel/taboolib/util/Ref.java @@ -34,9 +34,8 @@ public class Ref { try { return (Unsafe) io.izzel.taboolib.util.Reflection.getValue(null, Unsafe.class, true, "theUnsafe"); } catch (Throwable t) { - t.printStackTrace(); + throw new IllegalStateException("Unsafe not found"); } - return null; } public static List getDeclaredFields(Class clazz) {