From 35962542affd5cafeded486c56b40be815714f9b Mon Sep 17 00:00:00 2001 From: AnimeGitB Date: Thu, 24 Nov 2022 01:36:58 +1030 Subject: [PATCH] Fix oversight on EnumTypeAdapterFactory --- .../emu/grasscutter/utils/JsonAdapters.java | 51 +++++++++---------- 1 file changed, 23 insertions(+), 28 deletions(-) diff --git a/src/main/java/emu/grasscutter/utils/JsonAdapters.java b/src/main/java/emu/grasscutter/utils/JsonAdapters.java index c9c1a330a..20d2b243a 100644 --- a/src/main/java/emu/grasscutter/utils/JsonAdapters.java +++ b/src/main/java/emu/grasscutter/utils/JsonAdapters.java @@ -71,37 +71,32 @@ public class JsonAdapters { static class EnumTypeAdapterFactory implements TypeAdapterFactory { @SuppressWarnings("unchecked") public TypeAdapter create(Gson gson, TypeToken type) { - Class rawType = (Class) type.getRawType(); - if (!rawType.isEnum()) return null; - - Field id = null; - // System.out.println("Looking for enum value field"); - for (Field f : rawType.getDeclaredFields()) { - id = switch (f.getName()) { - case "value", "id" -> f; - default -> null; - }; - if (id != null) break; - } - if (id == null) { - // System.out.println("Not found"); - return null; - } - // System.out.println("Enum value field found - " + id.getName()); - + Class enumClass = (Class) type.getRawType(); + if (!enumClass.isEnum()) return null; + + // Make mappings of (string) names to enum constants val map = new HashMap(); - boolean acc = id.isAccessible(); - id.setAccessible(true); - try { - for (T constant : rawType.getEnumConstants()) { - map.put(constant.toString(), constant); - map.put(String.valueOf(id.getInt(constant)), constant); + val enumConstants = enumClass.getEnumConstants(); + for (val constant : enumConstants) + map.put(constant.toString(), constant); + + // If the enum also has a numeric value, map those to the constants too + // System.out.println("Looking for enum value field"); + for (Field f : enumClass.getDeclaredFields()) { + if (switch (f.getName()) {case "value", "id" -> true; default -> false;}) { + // System.out.println("Enum value field found - " + f.getName()); + boolean acc = f.isAccessible(); + f.setAccessible(true); + try { + for (val constant : enumConstants) + map.put(String.valueOf(f.getInt(constant)), constant); + } catch (IllegalAccessException e) { + // System.out.println("Failed to access enum id field."); + } + f.setAccessible(acc); + break; } - } catch (IllegalAccessException e) { - // System.out.println("Failed to access enum id field."); - return null; } - id.setAccessible(acc); return new TypeAdapter() { public T read(JsonReader reader) throws IOException {