Skip to content

Commit

Permalink
Update reflection for 1.20
Browse files Browse the repository at this point in the history
  • Loading branch information
kernitus committed Jan 6, 2024
1 parent 8f85735 commit c13385d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ private void attach(Player player) throws Exception {
return;
}

Object manager = Reflector.getDeclaredFieldValueByType(playerConnection, "NetworkManager");
Object manager = Reflector.getFieldValueByType(playerConnection, "NetworkManager");

channel = (Channel) Reflector.getDeclaredFieldValueByType(manager, "Channel");
channel = (Channel) Reflector.getFieldValueByType(manager, "Channel");

// remove old listener, if it wasn't properly cleared up
if (channel.pipeline().get("ocm_handler") != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,15 +205,20 @@ public static Field getMapFieldWithTypes(Class<?> clazz, Class<?> keyType, Class
" and value type " + valueType.getSimpleName() + " not found");
}

public static Object getFieldValueByType(Object object, String simpleClassName) throws Exception {
Stream<Field> publicFields = Stream.of(object.getClass().getFields());
Stream<Field> declaredFields = Stream.of(object.getClass().getDeclaredFields());
Stream<Field> allFields = Stream.concat(publicFields, declaredFields);

// Find the first field that matches the type name
Field matchingField = allFields
.filter(declaredField -> declaredField.getType().getSimpleName().equals(simpleClassName))
.findFirst()
.orElseThrow(() -> new NoSuchFieldException("Couldn't find field with type " + simpleClassName + " in " + object.getClass()));

public static Object getDeclaredFieldValueByType(Object object, String simpleClassName) throws Exception {
for (Field declaredField : object.getClass().getDeclaredFields()) {
if (declaredField.getType().getSimpleName().equals(simpleClassName)) {
declaredField.setAccessible(true);
return declaredField.get(object);
}
}
throw new NoSuchFieldException("Couldn't find field with type " + simpleClassName + " in " + object.getClass());
// Make the field accessible and return its value
matchingField.setAccessible(true);
return matchingField.get(object);
}

public static Object getFieldValue(Field field, Object handle) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@ public void onPlayerChangedWorld(PlayerChangedWorldEvent event) {
private static void updateModeset(Player player, UUID worldId, String modesetFromName) {
final UUID playerId = player.getUniqueId();
final PlayerData playerData = PlayerStorage.getPlayerData(playerId);
Messenger.debug("PLAYER DATA: " + playerData);
final String originalModeset = playerData.getModesetForWorld(worldId);
String modesetName = playerData.getModesetForWorld(worldId);
Messenger.debug("modeset name: " + originalModeset);

// Get modesets allowed in to world
Set<String> allowedModesets = Config.getWorlds().get(worldId);
Expand Down

0 comments on commit c13385d

Please sign in to comment.