night-config icon indicating copy to clipboard operation
night-config copied to clipboard

[Forge Related] Reloading config skips validators

Open TimeConqueror opened this issue 4 years ago • 2 comments

I added string config value as represented below:

ForgeConfigSpec.ConfigValue<String> DEBUG_LEVEL = new ForgeConfigSpec.Builder()
         .comment("Level of debug, if it is enabled in enable_debug. Available variants: debug, trace",
                    "Debug: additionally prints debug messages.",
                    "Trace: additionally prints debug and trace messages.")
         .defineInList("debug_level", "debug", Lists.newArrayList("debug", "trace"));

As you can see, I allowed debug_level to contain only debug and trace. At first time, when I placed smth like test in debug_level value, config was automatically corrected to debug during config initialization, and that's right. But after changing this value in config file again with already launched Forge, it just skips validators and sets the value without validating, so it can contain any other string.

TimeConqueror avatar Mar 12 '20 16:03 TimeConqueror

Hi! Thanks for reporting this issue. I'm not sure whether it's a problem with NightConfig of with MinecraftForge itself, I'll have to do some testing.

TheElectronWill avatar Mar 13 '20 22:03 TheElectronWill

It's easily fixable in Forge (by adding this line), making me believe that it's a Forge bug. I'm not 100% sure that Forge should be doing this validation though, so please still do some investigation/testing.

Cadiboo avatar Oct 11 '20 08:10 Cadiboo

Since #152, it is now possible to add a "load filter" on a FileConfig, which allows to cancel loading operations based on an arbitrary condition (the new content of the configuration is available to make this decision). Usage:

FileConfig config = FileConfig.builder(file)
    .onLoadFilter(newConfig -> {
        return mySpec.isCorrect(newConfig); // reject incorrect configurations: the reload will not occur
    })
    .build();

It is also possible to be notified of (successfull) loading operations, which could be used to trigger the correction:

FileConfig config = FileConfig.builder(file)
    .onLoad(() -> {
        mySpec.correct(config);
    })
    .build();

TheElectronWill avatar Jan 28 '24 15:01 TheElectronWill