Skip to content

Commit

Permalink
Add getFirst<Getter|Setter> API
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Jun 13, 2021
1 parent bd19c9e commit b199013
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions main/src/main/java/net/citizensnpcs/util/NMS.java
Expand Up @@ -164,6 +164,32 @@ public static MethodHandle getFinalSetter(Class<?> clazz, String field, boolean
return null;
}

private static Field getFirstFieldMatchingType(Class<?> clazz, Class<?> type) {
Field found = null;
for (Field field : clazz.getDeclaredFields()) {
if (field.getType() == type) {
found = field;
break;
}
}
if (found != null) {
found.setAccessible(true);
}
return found;
}

public static MethodHandle getFirstGetter(Class<?> clazz, Class<?> type) {
try {
Field found = getFirstFieldMatchingType(clazz, type);
if (found == null)
return null;
return LOOKUP.unreflectGetter(found);
} catch (Exception e) {
Messaging.logTr(Messages.ERROR_GETTING_FIELD, type, e.getLocalizedMessage());
}
return null;
}

public static MethodHandle getFirstMethodHandle(Class<?> clazz, boolean log, Class<?>... params) {
if (clazz == null)
return null;
Expand Down Expand Up @@ -195,6 +221,18 @@ public static MethodHandle getFirstMethodHandle(Class<?> clazz, boolean log, Cla
return null;
}

public static MethodHandle getFirstSetter(Class<?> clazz, Class<?> type) {
try {
Field found = getFirstFieldMatchingType(clazz, type);
if (found == null)
return null;
return LOOKUP.unreflectSetter(found);
} catch (Exception e) {
Messaging.logTr(Messages.ERROR_GETTING_FIELD, type, e.getLocalizedMessage());
}
return null;
}

public static GameProfileRepository getGameProfileRepository() {
return BRIDGE.getGameProfileRepository();
}
Expand Down

0 comments on commit b199013

Please sign in to comment.