improve Reflections.isSignWaxed

This commit is contained in:
Brokkonaut
2023-06-12 06:26:45 +02:00
parent f743347e66
commit 547aa81063

View File

@@ -12,24 +12,49 @@ public class Reflections {
public static boolean isSignWaxed(Sign sign) { public static boolean isSignWaxed(Sign sign) {
try { try {
if (FIELD_CraftBlockEntityState_snapshot == null) { if (FIELD_CraftBlockEntityState_snapshot == null) {
FIELD_CraftBlockEntityState_snapshot = sign.getClass().getSuperclass().getDeclaredField("snapshot"); Class<?> superClass = sign.getClass().getSuperclass();
while (superClass != null) {
try {
FIELD_CraftBlockEntityState_snapshot = superClass.getDeclaredField("snapshot");
FIELD_CraftBlockEntityState_snapshot.setAccessible(true); FIELD_CraftBlockEntityState_snapshot.setAccessible(true);
break;
} catch (NoSuchFieldException ignored) {
}
superClass = superClass.getSuperclass();
}
}
if (FIELD_CraftBlockEntityState_snapshot == null) {
LogBlock.getInstance().getLogger().log(Level.SEVERE, "Reflections: Sign field 'snapshot' not found");
return false;
} }
Object snapshot = FIELD_CraftBlockEntityState_snapshot.get(sign); Object snapshot = FIELD_CraftBlockEntityState_snapshot.get(sign);
if (snapshot == null) { if (snapshot == null) {
LogBlock.getInstance().getLogger().log(Level.SEVERE, "Reflections: Sign snapshot is null?");
return false; return false;
} }
if (FIELD_SignBlockEntity_isWaxed == null) { if (FIELD_SignBlockEntity_isWaxed == null) {
for (Field f : snapshot.getClass().getDeclaredFields()) { Class<?> snapshotClass = snapshot.getClass();
while (snapshotClass != null) {
for (Field f : snapshotClass.getDeclaredFields()) {
if (f.getType() == boolean.class) { if (f.getType() == boolean.class) {
FIELD_SignBlockEntity_isWaxed = f; FIELD_SignBlockEntity_isWaxed = f;
FIELD_SignBlockEntity_isWaxed.setAccessible(true); FIELD_SignBlockEntity_isWaxed.setAccessible(true);
break;
} }
} }
if (FIELD_SignBlockEntity_isWaxed != null) {
break;
} }
return FIELD_SignBlockEntity_isWaxed != null && FIELD_SignBlockEntity_isWaxed.getBoolean(snapshot); snapshotClass = snapshotClass.getSuperclass();
} catch (SecurityException | NoSuchFieldException | IllegalArgumentException | IllegalAccessException e) { }
LogBlock.getInstance().getLogger().log(Level.SEVERE, "Sign.isWaxed reflection failed", e); }
if (FIELD_SignBlockEntity_isWaxed == null) {
LogBlock.getInstance().getLogger().log(Level.SEVERE, "Reflections: Sign field 'isWaxed' not found");
return false;
}
return FIELD_SignBlockEntity_isWaxed.getBoolean(snapshot);
} catch (SecurityException | IllegalArgumentException | IllegalAccessException e) {
LogBlock.getInstance().getLogger().log(Level.SEVERE, "Reflections: Sign.isWaxed reflection failed", e);
} }
return false; return false;
} }