shedaniel / cloth-config

Client-Sided API for Minecraft 1.14
Other
185 stars 70 forks source link

ConfigEntry.Gui.CollapsibleObject does not include fields in super classes #275

Open rikka0w0 opened 1 month ago

rikka0w0 commented 1 month ago

As the title suggests. It is inconvenient when handling a large set of similar options and requires a lot of duplication and hard coding.

I found a solution. In DefaultGuiProviders.java:

    private static List<AbstractConfigListEntry> getChildren(String i18n, Class<?> fieldType, Object iConfig, Object iDefaults, GuiRegistryAccess guiProvider) {
-        return Arrays.stream(fieldType.getDeclaredFields())
+       return traverseFields(fieldType, true).stream()

where traverseFields is a new helper function:

    private static List<Field> traverseFields(Class<?> fieldType, boolean includeSuper) {
        List<Field> result = new LinkedList<>();
        Class<?> cls = fieldType;
        while (cls != null && !cls.equals(Object.class)) {
            result.addAll(Arrays.asList(cls.getDeclaredFields()));

            if (!includeSuper) {
                break;
            }

            cls = cls.getSuperclass();
        }
        return result;
    }

We could introduce another option in the CollapsibleObject annotation to specify whether we include the fields in its super class or not.