Skip to content

Commit

Permalink
Fix reflective access warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Apr 14, 2021
1 parent ca421b9 commit 3a9e538
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public void reload(CommandContext args, CommandSender sender, NPC npc) throws Co
}
Messaging.sendTr(sender, Messages.CITIZENS_RELOADING);
try {

plugin.reload();
Messaging.sendTr(sender, Messages.CITIZENS_RELOADED);
} catch (NPCLoadException ex) {
Expand Down
15 changes: 11 additions & 4 deletions main/src/main/java/net/citizensnpcs/util/NMS.java
Original file line number Diff line number Diff line change
Expand Up @@ -291,15 +291,16 @@ public static float getYaw(Entity entity) {
return BRIDGE.getYaw(entity);
}

public static void giveReflectiveAccess(Class<?> clazz, Class<?> to) {
public static void giveReflectiveAccess(Class<?> from, 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));
ADD_OPENS.invoke(GET_MODULE.invoke(from), from.getPackage().getName(), GET_MODULE.invoke(to));
} catch (Exception e) {
e.printStackTrace();
}
}

Expand All @@ -316,7 +317,8 @@ public static void load(CommandManager commands) {
}

public static void loadBridge(String rev) throws Exception {
giveReflectiveAccess(Class.forName("net.minecraft.server.v" + rev + ".Entity"), NMS.class);
Class<?> entity = Class.forName("net.minecraft.server.v" + rev + ".Entity");
giveReflectiveAccess(entity, NMS.class);
BRIDGE = (NMSBridge) Class.forName("net.citizensnpcs.nms.v" + rev + ".util.NMSImpl").getConstructor()
.newInstance();
}
Expand Down Expand Up @@ -509,9 +511,14 @@ public static void updatePathfindingRange(NPC npc, float pathfindingRange) {
private static NMSBridge BRIDGE;
private static Method GET_MODULE;
private static MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
private static Field MODIFIERS_FIELD = NMS.getField(Field.class, "modifiers", false);
private static Field MODIFIERS_FIELD;
private static Object UNSAFE;
private static MethodHandle UNSAFE_FIELD_OFFSET;
private static MethodHandle UNSAFE_PUT_OBJECT;
private static MethodHandle UNSAFE_STATIC_FIELD_OFFSET;

static {
giveReflectiveAccess(Field.class, NMS.class);
MODIFIERS_FIELD = NMS.getField(Field.class, "modifiers", false);
}
}

0 comments on commit 3a9e538

Please sign in to comment.