Read your config after the restore

Load your mod's config at a point where Settings Saver has already written the profile's copy, on Fabric, Quilt, Forge and NeoForge of every era.

Last updated

Steps

A new instance gets your saved settings file during its first launch. Your mod only benefits if it reads the file after that. This guide shows where to load your config on each loader so that is always true.

Prerequisites#

The rule#

Read your config in your mod's init or setup event. Never read it in a static initializer, a mod constructor or a coremod.

Reading the config in… Sees the restored file?
a static { } block or static field initializer not on every loader
the mod class constructor not on every loader
a coremod or early transformer not on every loader
the loader's init or setup event yes, everywhere

Why#

Settings Saver runs your plugin, and restores your file, at a point that depends on the loader:

Loader Your file is restored Before Minecraft reads its options?
Fabric, Quilt, Legacy Fabric at preLaunch yes
Forge 1.15.2 (31.2 and newer) to 26.3, NeoForge while the launch is set up yes
Forge 1.13.2 to 1.15.2 (31.1) when Settings Saver's mod is constructed no
Forge 1.7.10 to 1.12.2 when Settings Saver's mod is constructed, still before preInit no

On the last two rows, mods are constructed in an order you don't control. Your constructor may run before Settings Saver's. Wherever the plugin runs, a fresh instance has your file at the latest while mods are constructed, so the events that follow construction are safe on every loader.

The full timeline is in When plugins run.

Steps#

1. Move config loading out of static code and constructors#

Before: may read the file too early
public final class MyMod {
    // Runs when the class is loaded, possibly before the restore.
    static final MyConfig CONFIG = MyConfig.load(Paths.get("config", "mymod.properties"));
}
After: loaded from the init event
public final class MyMod {
    private static MyConfig config;

    /** Each loader's entry class calls this from its init or setup event. */
    public static void init() {
        config = MyConfig.load(Paths.get("config", "mymod.properties"));
    }

    public static MyConfig config() {
        return config;
    }
}

2. Call it from the right place on each loader#

Fabric, Quilt, Legacy Fabric

Load the config in your initializer. The restore already happened at preLaunch.

FabricEntry.java
package com.example.mymod.loader;

import com.example.mymod.MyMod;
import net.fabricmc.api.ClientModInitializer;

public final class FabricEntry implements ClientModInitializer {
    @Override
    public void onInitializeClient() {
        MyMod.init();
    }
}

onInitialize of a ModInitializer is just as safe.

Forge, NeoForge

Which event to use depends on the era:

  • Forge 1.7.10 to 1.12.2: load the config in preInit or later. The @Mod constructor is too early.
  • Forge 1.13.2 and newer, NeoForge: load the config in FMLCommonSetupEvent, or let the loader's own config system load it. The @Mod constructor is too early on Forge 1.13.2 to 1.15.2 (31.1), so don't use it on any version.

On Forge 1.15.2 (31.2 and newer) and on NeoForge the restore happens before any mod is constructed. Using the setup event everywhere keeps one code path for all versions.

3. Or load it lazily, with no loader code at all#

If your entry classes only have a constructor to call from (for example because they compile against annotation stubs and have no event API), load the config on first use instead:

src/main/java/com/example/mymod/MyMod.java
public final class MyMod {
    private static MyConfig config;

    /** Loads the config the first time something needs it. */
    public static synchronized MyConfig config() {
        if (config == null) {
            config = MyConfig.load(Paths.get("config", "mymod.properties"));
        }
        return config;
    }
}

This is safe on every loader as long as nothing calls config() while mods are being constructed: not your mod's constructor, and not a static initializer. Code that runs later (a menu opening, a key press, a tick) always sees the restored file.

4. Don't create the file before the restore#

A file is only filled in from the profile when the instance doesn't have it yet. If your mod writes its default config before Settings Saver ran, the instance has the file, and the profile's copy is not applied on that launch.

So the code that creates a missing config with defaults belongs in the same init or setup event as the code that reads it.

5. Reload after a Load, if your mod can#

When the player presses Load settings from profile, Settings Saver rewrites your file while the game runs. Settings Saver doesn't call your mod. Your mod sees the new values the next time it reads the file: at the next launch, or earlier if your mod watches its config file or re-reads it on its own.

When your mod writes its config#

Nothing changes: write the file whenever settings change, as before. With sync Auto, Settings Saver notices the changed file within a few seconds and saves the changed keys into the profile. A final check runs when the game closes.

Write the file atomically if you can (write a temporary file, then rename it), so a half-written file is never read.

Checklist#

Next steps#