Fix FieldFetch not getting fields from superclasses

This commit is contained in:
KingRainbow44 2023-06-01 14:18:12 -04:00
parent 27be6c31e6
commit 3c0e834348
No known key found for this signature in database
GPG Key ID: FC2CB64B00D257BE
2 changed files with 32 additions and 10 deletions

View File

@ -1,8 +1,5 @@
package emu.grasscutter.utils; package emu.grasscutter.utils;
import static emu.grasscutter.utils.FileUtils.getResourcePath;
import static emu.grasscutter.utils.lang.Language.translate;
import emu.grasscutter.Grasscutter; import emu.grasscutter.Grasscutter;
import emu.grasscutter.config.ConfigContainer; import emu.grasscutter.config.ConfigContainer;
import emu.grasscutter.data.DataLoader; import emu.grasscutter.data.DataLoader;
@ -11,15 +8,20 @@ import emu.grasscutter.utils.objects.Returnable;
import io.javalin.http.Context; import io.javalin.http.Context;
import io.netty.buffer.*; import io.netty.buffer.*;
import it.unimi.dsi.fastutil.ints.*; import it.unimi.dsi.fastutil.ints.*;
import org.slf4j.Logger;
import javax.annotation.Nullable;
import java.io.*; import java.io.*;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.*; import java.nio.file.*;
import java.time.*; import java.time.*;
import java.time.temporal.TemporalAdjusters; import java.time.temporal.TemporalAdjusters;
import java.util.*; import java.util.*;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
import javax.annotation.Nullable;
import org.slf4j.Logger; import static emu.grasscutter.utils.FileUtils.getResourcePath;
import static emu.grasscutter.utils.lang.Language.translate;
@SuppressWarnings({"UnusedReturnValue", "BooleanMethodIsAlwaysInverted"}) @SuppressWarnings({"UnusedReturnValue", "BooleanMethodIsAlwaysInverted"})
public final class Utils { public final class Utils {
@ -484,6 +486,7 @@ public final class Utils {
* *
* @param runnable The task to run. * @param runnable The task to run.
*/ */
@SuppressWarnings("BusyWait")
public static void waitFor(Returnable<Boolean> runnable) { public static void waitFor(Returnable<Boolean> runnable) {
while (!runnable.invoke()) { while (!runnable.invoke()) {
try { try {
@ -493,4 +496,23 @@ public final class Utils {
} }
} }
} }
/**
* Recursively finds all fields in a class.
*
* @param type The class to find fields in.
* @return A list of all fields in the class.
*/
public static List<Field> getAllFields(Class<?> type) {
var fields = new LinkedList<>(
Arrays.asList(type.getDeclaredFields())
);
// Check for superclasses.
if (type.getSuperclass() != null) {
fields.addAll(getAllFields(type.getSuperclass()));
}
return fields;
}
} }

View File

@ -1,11 +1,11 @@
package emu.grasscutter.utils.objects; package emu.grasscutter.utils.objects;
import com.google.gson.JsonNull; import com.google.gson.*;
import com.google.gson.JsonObject;
import emu.grasscutter.server.dispatch.IDispatcher; import emu.grasscutter.server.dispatch.IDispatcher;
import emu.grasscutter.utils.Utils;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.Arrays; import java.util.*;
import java.util.HashMap;
public interface FieldFetch { public interface FieldFetch {
/** /**
@ -18,7 +18,7 @@ public interface FieldFetch {
// Prepare field properties. // Prepare field properties.
var fieldValues = new JsonObject(); var fieldValues = new JsonObject();
var fieldMap = new HashMap<String, Field>(); var fieldMap = new HashMap<String, Field>();
Arrays.stream(this.getClass().getDeclaredFields()) Utils.getAllFields(this.getClass())
.forEach(field -> fieldMap.put(field.getName(), field)); .forEach(field -> fieldMap.put(field.getName(), field));
// Find the values of all requested fields. // Find the values of all requested fields.