Skip to content

Commit ce1c004

Browse files
committed
Recursively look for field on super classes
1 parent 93aa63a commit ce1c004

1 file changed

Lines changed: 23 additions & 3 deletions

File tree

src/main/java/net/countercraft/movecraft/combat/features/BlockBehaviorOverride.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,17 @@ protected Object getBlockClass(Material m)
353353
protected static <T> void writeField(@NotNull Object block, @NotNull Consumer<T> whatToDoWithField,
354354
String fieldName) throws IllegalAccessException, NoSuchFieldException, ClassCastException,
355355
InaccessibleObjectException, SecurityException {
356-
Field field = block.getClass().getField(fieldName);
356+
Class<?> clazz = block.getClass();
357+
Field field = null;
358+
while (clazz != null) {
359+
try {
360+
field = clazz.getDeclaredField(fieldName);
361+
break;
362+
} catch (NoSuchFieldException e) {
363+
clazz = clazz.getSuperclass();
364+
}
365+
}
366+
if (field == null) throw new NoSuchFieldException(fieldName);
357367
field.setAccessible(true);
358368
T obj = (T) field.get(block);
359369
whatToDoWithField.accept(obj);
@@ -362,7 +372,17 @@ protected static <T> void writeField(@NotNull Object block, @NotNull Consumer<T>
362372
protected static <T> void writeField(@NotNull Object block, T value, String fieldName)
363373
throws IllegalAccessException, NoSuchFieldException, ClassCastException, InaccessibleObjectException,
364374
SecurityException {
365-
Field field = block.getClass().getField(fieldName);
375+
Class<?> clazz = block.getClass();
376+
Field field = null;
377+
while (clazz != null) {
378+
try {
379+
field = clazz.getDeclaredField(fieldName);
380+
break;
381+
} catch (NoSuchFieldException e) {
382+
clazz = clazz.getSuperclass();
383+
}
384+
}
385+
if (field == null) throw new NoSuchFieldException(fieldName);
366386
field.setAccessible(true);
367387
field.set(block, value);
368388
}
@@ -378,7 +398,7 @@ protected static <T> Optional<T> getFieldValueSafe(@NotNull Object instance, Str
378398
protected static <T> T getFieldValue(@NotNull Object instance, String fieldName)
379399
throws IllegalAccessException, NoSuchFieldException, ClassCastException, InaccessibleObjectException,
380400
SecurityException {
381-
Field field = instance.getClass().getField(fieldName);
401+
Field field = instance.getClass().getDeclaredField(fieldName);
382402
field.setAccessible(true);
383403
T obj = (T) field.get(instance);
384404
return obj;

0 commit comments

Comments
 (0)