Register from your own code
Register your mod's settings with SettingsSaverMods.register instead of a service file, guarded by a check that Settings Saver is installed.
Steps
The service file is the preferred way to register. This guide covers the alternative: calling SettingsSaverMods from your mod's init code.
Plugin or code?#
Service file (SettingsSaverPlugin) |
From code (SettingsSaverMods.register) |
|
|---|---|---|
| Loader-specific code | none | an "is Settings Saver installed" check per loader |
| Runs | as early as Settings Saver itself | whenever your init code runs |
| Fresh instance gets your file before your mod reads it | yes, if you read it in the init event | only if you register before you read it |
| Risk when Settings Saver is missing | none | NoClassDefFoundError if the check is wrong |
Use the service file unless you have a reason not to, for example a build that can't ship
META-INF/services files.
Steps#
1. Keep the API in one class#
The API classes exist at run time only when Settings Saver is installed. Put every reference to them in a class that nothing else loads:
package com.example.mymod.compat;
import dev.xichil.settingssaver.api.v1.ModSettings;
import dev.xichil.settingssaver.api.v1.SettingsFileFormats;
import dev.xichil.settingssaver.api.v1.SettingsSaverMods;
/** Only loaded after the check in step 2 passed. */
public final class MyModSettingsSaver {
private MyModSettingsSaver() {
}
public static void registerNow() {
SettingsSaverMods.register(ModSettings.builder("mymod", "My Mod", "2.1.0")
.file("config/mymod.properties", SettingsFileFormats.PROPERTIES)
.build());
}
}
2. Check that Settings Saver is installed, then call it#
Settings Saver's mod id is settingssaver on every loader.
Fabric, Quilt, Legacy Fabric
if (FabricLoader.getInstance().isModLoaded("settingssaver")) {
MyModSettingsSaver.registerNow();
}
Forge, NeoForge
Forge 1.13 and newer, and NeoForge:
if (ModList.get().isLoaded("settingssaver")) {
MyModSettingsSaver.registerNow();
}
Forge 1.7.10 to 1.12.2:
if (Loader.isModLoaded("settingssaver")) {
MyModSettingsSaver.registerNow();
}
The JVM loads MyModSettingsSaver only when the if body runs, so without Settings Saver the API is
never touched.
SettingsSaverMods.MOD_ID holds the same id. It is a compile-time constant, so the compiler copies its
value into your class and using it in the check doesn't load the API either.
3. Register before you read your config#
Call the check at the start of your init or setup event, before your config is loaded:
public static void init() {
if (FabricLoader.getInstance().isModLoaded("settingssaver")) {
MyModSettingsSaver.registerNow(); // a fresh instance gets the saved file here
}
config = MyConfig.load(Paths.get("config", "mymod.properties"));
}
What a late registration gets#
- The file syncs from the moment you register.
- If this launch applied the profile (a new instance's first launch, or the sync modes Auto and Auto + always apply), your file gets the same restore when you register.
- Code that read the file before the registration saw the instance's own version of it.
- A registration made before Settings Saver has started is kept and handed over when it starts, in the order the registrations were made.
SettingsSaverMods.register is thread-safe and never throws for a refused registration: it is logged
and ignored, exactly as with a plugin. See Fix an integration that doesn't sync.
Don't do both#
Register a mod id once. If a plugin class and your init code both register mymod, the first
registration stays and the second is ignored with a warning.
Next steps#
- An optional dependency: why class loading makes the check work.
- When plugins run