Try to remove reflective access warning on Java 11

This commit is contained in:
fullwall 2021-04-08 18:47:38 +08:00
parent 011e58c739
commit eb4262a80f

View File

@ -291,6 +291,18 @@ public class NMS {
return BRIDGE.getYaw(entity); return BRIDGE.getYaw(entity);
} }
public static void giveReflectiveAccess(Class<?> clazz, Class<?> to) {
try {
if (GET_MODULE == null) {
Class<?> module = Class.forName("java.lang.Module");
GET_MODULE = Class.class.getMethod("getModule");
ADD_OPENS = module.getMethod("addOpens", String.class, module);
}
ADD_OPENS.invoke(GET_MODULE.invoke(clazz), to.getPackage().getName(), GET_MODULE.invoke(to));
} catch (Exception e) {
}
}
public static boolean isOnGround(org.bukkit.entity.Entity entity) { public static boolean isOnGround(org.bukkit.entity.Entity entity) {
return BRIDGE.isOnGround(entity); return BRIDGE.isOnGround(entity);
} }
@ -304,6 +316,7 @@ public class NMS {
} }
public static void loadBridge(String rev) throws Exception { public static void loadBridge(String rev) throws Exception {
giveReflectiveAccess(Class.forName("net.minecraft.server.v" + rev + ".Entity"), NMS.class);
BRIDGE = (NMSBridge) Class.forName("net.citizensnpcs.nms.v" + rev + ".util.NMSImpl").getConstructor() BRIDGE = (NMSBridge) Class.forName("net.citizensnpcs.nms.v" + rev + ".util.NMSImpl").getConstructor()
.newInstance(); .newInstance();
} }
@ -492,7 +505,9 @@ public class NMS {
BRIDGE.updatePathfindingRange(npc, pathfindingRange); BRIDGE.updatePathfindingRange(npc, pathfindingRange);
} }
private static Method ADD_OPENS;
private static NMSBridge BRIDGE; private static NMSBridge BRIDGE;
private static Method GET_MODULE;
private static MethodHandles.Lookup LOOKUP = MethodHandles.lookup(); private static MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
private static Field MODIFIERS_FIELD = NMS.getField(Field.class, "modifiers", false); private static Field MODIFIERS_FIELD = NMS.getField(Field.class, "modifiers", false);
private static Object UNSAFE; private static Object UNSAFE;