Package dev.xichil.settingssaver.api.v1


package dev.xichil.settingssaver.api.v1
Settings Saver's public API for other mods, version 1.

Settings Saver keeps a player's settings in named profiles outside any one game directory, and syncs them into every instance (modpack, launcher profile, Minecraft version) that uses the same profile. Out of the box it handles Minecraft's own options.txt. This package lets any other mod have its own settings files saved, loaded and synced the same way, with Settings Saver as an optional dependency: when Settings Saver isn't installed, the mod runs exactly as it did before.

Getting started

  1. Compile against the API with compileOnly("dev.xichil.settingssaver:settingssaver-api:<version>"). Never bundle, shade or jar-in-jar it: Settings Saver's own jar ships these classes, and a second copy on the class path would split them in two.
  2. Implement SettingsSaverPlugin in a class of its own and describe your files with a ModSettings:
    public final class MyModSettingsSaver implements SettingsSaverPlugin {
        public void register(SettingsSaverRegistrar registrar) {
            registrar.register(ModSettings.builder("mymod", "My Mod", "2.1.0")
                    .file("config/mymod.properties", SettingsFileFormats.PROPERTIES)
                    .excludedByDefault("windowX", "windowY")
                    .build());
        }
    }
    
  3. List that class, by its fully qualified name, in META-INF/services/dev.xichil.settingssaver.api.v1.SettingsSaverPlugin inside your jar.

Settings Saver finds the plugin with ServiceLoader. When Settings Saver is absent, nobody reads that service file, so the plugin class, which should be the only class of your mod that references this package, is never loaded and can't fail with NoClassDefFoundError. If you'd rather register from your own init code, see SettingsSaverMods.

What Settings Saver does with a registration

  • Off unless chosen. A mod's settings are only saved into a profile when the player turns the mod on for that profile, or when the mod asked for saved by default and the player hasn't turned it off.
  • Key by key. Each file is read through a SettingsFileFormat into a flat key -> value map. Only settings the player actually changed are written into the profile, keys can be excluded one by one, and loading a profile merges its values over the instance's file instead of replacing it. With the default SettingsFileFormats.WHOLE_FILE the file is a single value and is copied as a whole.
  • Across versions. The profile records which mod version and which Minecraft DataVersion its copy was saved in. SettingsFixes convert the settings when a profile written by one version is used by another.
  • Storage. A profile keeps the mod's files under profiles/<profile>/<mod id>/<path>, next to a module.txt with the version record, written with the same format as the file in the game directory.

Threads and errors

Your plugin's register runs once, on the thread that launches the game. Your formats, fixes and mergers are called later, from whichever thread is syncing: Settings Saver's background watcher, the menu's Save and Load actions, or the shutdown hook. Keep them stateless (or thread-safe), quick, and free of side effects. Settings Saver never lets a mod's code break the game: a registration it refuses, a plugin that throws, and a fix or merger that throws are all logged and skipped, and the rest of the mod's settings, and every other mod's, still sync.

Compatibility

Nothing in this package references Minecraft, a mod loader or Settings Saver's internals; it is plain Java 8, so one integration works on every Minecraft version and loader Settings Saver supports. The package only ever changes compatibly: members may be added, none are removed or change meaning. A breaking change would come as a new v2 package next to this one, and Settings Saver would keep serving v1.

docs/API.md in Settings Saver's repository walks through the same steps with build snippets for each loader.

See Also: