Example: a properties config

The smallest complete Settings Saver integration, for a mod with one key=value config file, with the build script, plugin class and service file.

Last updated

On this page

Complete example

A mod with one key=value config file whose settings follow the player to every instance. Three files are all it takes: a build script line, a plugin class and a service file.

Files#

mymod/
├── build.gradle.kts
└── src/main/
    ├── java/com/example/mymod/compat/MyModSettingsSaver.java
    └── resources/META-INF/services/dev.xichil.settingssaver.api.v1.SettingsSaverPlugin

The mod's config file, written and read by the mod itself as before:

config/mymod.properties
renderDistance=12
showHud=true
hudScale=1.0
windowX=120
windowY=80
lastServer=play.example.com

The build script#

Add these to the build your mod already has:

build.gradle.kts
repositories {
    maven("https://maven.xichil.dev")
}

dependencies {
    // Settings Saver's own jar ships these classes. Never bundle, shade or jar-in-jar them.
    compileOnly("dev.xichil.settingssaver:settingssaver-api:1.0.0")
}

The plugin class#

src/main/java/com/example/mymod/compat/MyModSettingsSaver.java
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.SettingsSaverPlugin;
import dev.xichil.settingssaver.api.v1.SettingsSaverRegistrar;

/** Only Settings Saver loads this class. Nothing else in the mod may reference it. */
public final class MyModSettingsSaver implements SettingsSaverPlugin {
    @Override
    public void register(SettingsSaverRegistrar registrar) {
        registrar.register(ModSettings.builder("mymod", "My Mod", "2.1.0")
                .file("config/mymod.properties", SettingsFileFormats.PROPERTIES)
                .savedByDefault(true)
                .excludedByDefault("windowX", "windowY", "lastServer")
                .build());
    }
}

The service file#

src/main/resources/META-INF/services/dev.xichil.settingssaver.api.v1.SettingsSaverPlugin
com.example.mymod.compat.MyModSettingsSaver

How it works#

Line Effect
builder("mymod", "My Mod", "2.1.0") The mod id names the folder in each profile. The name is shown to players. The version is recorded with every save.
.file("config/mymod.properties", SettingsFileFormats.PROPERTIES) The file is read as key=value lines, so each setting syncs on its own.
.savedByDefault(true) Profiles save the file without the player turning the mod on. Leave it out if your settings usually belong to a modpack.
.excludedByDefault("windowX", "windowY", "lastServer") These keys stay with each instance: they are never saved to or loaded from a profile.

What the player gets#

After the player presses Save settings to profile once (or turns on auto sync), the profile holds:

<store>/profiles/default/mymod/config/mymod.properties
renderDistance=12
showHud=true
hudScale=1.0

windowX, windowY and lastServer are not there. A new instance that starts with the mod gets this file as its config/mymod.properties, and the mod fills in its own defaults for the three missing keys.

Variations#

You want Change
off until the player turns it on remove .savedByDefault(true)
keep the file exactly, comments included .file("config/mymod.properties") without a format
a second file another .file(...) call
every file in config/mymod/ .directory("config/mymod")