Fix reloading event firing during server shutdown and add explicit unloading event instead (#9016)

This commit is contained in:
cpw authored and GitHub committed 2023-02-06 13:30:16 -08:00
1 parent 57fd0ecb2e
commit 50f325c8fe
5 files changed
+42 -4

No files matched your search

@@ -66,8 +66,12 @@ public class ConfigTracker {
private void closeConfig(final ModConfig config, final Path configBasePath) {
if (config.getConfigData() != null) {
LOGGER.trace(CONFIG, "Closing config file type {} at {} for {}", config.getType(), config.getFileName(), config.getModId());
config.save();
// stop the filewatcher before we save the file and close it, so reload doesn't fire
config.getHandler().unload(configBasePath, config);
var unloading = IConfigEvent.unloading(config);
if (unloading != null)
config.fireEvent(unloading);
config.save();
config.setConfigData(null);
}
}
@@ -10,8 +10,17 @@ import net.minecraftforge.fml.Bindings;
import java.util.function.Function;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;
public interface IConfigEvent {
record ConfigConfig(Function<ModConfig, IConfigEvent> loading, Function<ModConfig, IConfigEvent> reloading) {}
record ConfigConfig(Function<ModConfig, IConfigEvent> loading, Function<ModConfig, IConfigEvent> reloading, @Nullable Function<ModConfig, IConfigEvent> unloading) {
@Deprecated(since = "1.19.3", forRemoval = true)
@ApiStatus.Internal
ConfigConfig(Function<ModConfig, IConfigEvent> loading, Function<ModConfig, IConfigEvent> reloading) {
this(loading, reloading, null);
}
}
ConfigConfig CONFIGCONFIG = Bindings.getConfigConfiguration().get();
static IConfigEvent reloading(ModConfig modConfig) {
@@ -20,6 +29,9 @@ public interface IConfigEvent {
static IConfigEvent loading(ModConfig modConfig) {
return CONFIGCONFIG.loading().apply(modConfig);
}
@Nullable static IConfigEvent unloading(ModConfig modConfig) {
return CONFIGCONFIG.unloading() == null ? null : CONFIGCONFIG.unloading().apply(modConfig);
}
ModConfig getConfig();
@SuppressWarnings("unchecked")
@@ -42,6 +42,6 @@ public class FMLOnlyBindings implements IBindingsProvider {
@Override
public Supplier<IConfigEvent.ConfigConfig> getConfigConfiguration() {
return ()->new IConfigEvent.ConfigConfig(ModConfigEvent.Loading::new, ModConfigEvent.Reloading::new);
return ()->new IConfigEvent.ConfigConfig(ModConfigEvent.Loading::new, ModConfigEvent.Reloading::new, ModConfigEvent.Unloading::new);
}
}
@@ -22,15 +22,37 @@ public class ModConfigEvent extends Event implements IModBusEvent, IConfigEvent
return config;
}
/**
* Fired during mod and server loading, depending on {@link ModConfig.Type} of config file.
* Any Config objects associated with this will be valid and can be queried directly.
*/
public static class Loading extends ModConfigEvent {
public Loading(final ModConfig config) {
super(config);
}
}
/**
* Fired when the configuration is changed. This can be caused by a change to the config
* from a UI or from editing the file itself. IMPORTANT: this can fire at any time
* and may not even be on the server or client threads. Ensure you properly synchronize
* any resultant changes.
*/
public static class Reloading extends ModConfigEvent {
public Reloading(final ModConfig config) {
super(config);
}
}
/**
* Fired when a config is unloaded. This only happens when the server closes, which is
* probably only really relevant on the client, to reset internal mod state when the
* server goes away, though it will fire on the dedicated server as well.
* The config file will be saved after this event has fired.
*/
public static class Unloading extends ModConfigEvent {
public Unloading(final ModConfig config) {
super(config);
}
}
}
@@ -38,6 +38,6 @@ public class ForgeBindings implements IBindingsProvider {
@Override
public Supplier<IConfigEvent.ConfigConfig> getConfigConfiguration() {
return ()->new IConfigEvent.ConfigConfig(ModConfigEvent.Loading::new, ModConfigEvent.Reloading::new);
return ()->new IConfigEvent.ConfigConfig(ModConfigEvent.Loading::new, ModConfigEvent.Reloading::new, ModConfigEvent.Unloading::new);
}
}