Darkere / ConfigSwapper

0 stars 1 forks source link

Configuration changes aren't applied until client/server restarts #5

Closed endorh closed 1 year ago

endorh commented 1 year ago

Hi, I'm the developer of Simple Config. Recently, someone in my Discord server brought to me an issue around supporting multiple config files to support Zombie Awareness's config setup.

They mentioned they'd tried using your mod to bind a command to some configuration changes, but those changes weren't applying until the next server restart.

In order to help them, I tried quickly checking your mod to see if I could help them get it to work, but I found instead that your mod wasn't reloading the configs properly. Since I already faced this problem when making my own mod, I thought I'd as well let you know how I did it, and why your current implementation doesn't work.


The /reload command you use in ChangeModeCommand:80 is a vanilla command that reloads datapacks. Unfortunately, it does nothing to reload Forge's config files.

What you need to reload Forge's config files is triggering a ModConfigEvent.Reloading event for the mod config in question. Unfortunately, this event has a private constructor in Forge (at least it was back in 1.16), so you need to use reflection to trigger it.

Feel free to use the approach I used in SimpleConfigNetworkHandler:203. The key parts are the tryFireEvent and the newReloading methods. I prepare all the reflection objects necessary in a static initializer, but there are many other ways to do it.

Darkere commented 1 year ago

Config reloading is done in the ApplyMode function here: https://github.com/Darkere/ConfigSwapper/blob/c16a6479011660169dc4f4b32612b9c61352061a/src/main/java/com/darkere/configswapper/ModeConfig.java#L198

endorh commented 1 year ago

Oh, my bad, it seems I didn't look deep enough.

Apparently, the error was in the other mod in the end, sorry for the inconvenience 😅

Darkere commented 1 year ago

Hey @endorh I got a report that showed the error. I was trying to reflect into the wrong class. At least in 1.19.2 reflection seems to be entirely unnecessary now. See here how you can fire the event: https://github.com/Darkere/ConfigSwapper/blob/4dd28a6ccdd666f14a38f520f3adfa2f6a5838c6/src/main/java/com/darkere/configswapper/ModeConfig.java#L189

endorh commented 1 year ago

I'm glad you could fix it. Thanks for letting me know about the reflection not being necessary anymore, I'll keep it in mind for my next refactor. ^^