Convert settings between mod versions

Keep profiles saved by older versions of your mod usable after you rename a key or change a value, with modVersionFix and SettingsFix.

Last updated

Steps

A profile outlives the version of your mod that saved it. When a new version of your mod renames a key or stores a value differently, register a fix so settings saved by older versions are converted. This guide covers changes that come from your mod's versions. For changes that follow Minecraft's version, see Convert settings between Minecraft versions.

Prerequisites#

How it works#

Every save records the version of your mod that wrote it (the third argument of ModSettings.builder). When a profile that an older version saved is read, Settings Saver runs the upgrade of every fix whose version is after the saved version, up to the running version, oldest first.

profile saved by 1.4.0, running 3.1.0, fixes at 2.0.0 and 3.0.0:

  saved settings --upgrade 2.0.0--> --upgrade 3.0.0--> settings in 3.1.0's format

Steps#

1. Rename a key#

Register the fix at the first version that uses the new format:

ModSettings.builder("mymod", "My Mod", "2.1.0")
        .file("config/mymod.properties", SettingsFileFormats.PROPERTIES)
        // My Mod 2.0 renamed renderDist to renderDistance.
        .modVersionFix("2.0.0", SettingsFix.renameKey("renderDist", "renderDistance"))
        .build();

SettingsFix.renameKey(from, to) moves the value of from to to on upgrade, and back on downgrade. The value is kept. The key moves to the end of the file. A map without the old key is left alone. If both keys exist, the old key's value replaces the other.

2. Change a value#

For anything else, implement SettingsFix. Both methods get the file's path and its settings as a mutable map, and change the map in place:

/** My Mod 3.0 stores zoom as a percentage (150) instead of a factor (1.5). */
final class ZoomPercentFix implements SettingsFix {
    @Override
    public void upgrade(String file, Map<String, String> settings) {
        String zoom = settings.get("zoom");
        if (zoom == null || !zoom.contains(".")) {
            return;                                    // not there, or already a percentage
        }
        try {
            settings.put("zoom", String.valueOf(Math.round(Double.parseDouble(zoom) * 100)));
        } catch (NumberFormatException e) {
            settings.remove("zoom");                   // unreadable: My Mod falls back to its default
        }
    }

    @Override
    public void downgrade(String file, Map<String, String> settings) {
        String zoom = settings.get("zoom");
        if (zoom == null || zoom.contains(".")) {
            return;
        }
        try {
            settings.put("zoom", String.valueOf(Integer.parseInt(zoom) / 100.0));
        } catch (NumberFormatException e) {
            settings.remove("zoom");
        }
    }
}
.modVersionFix("3.0.0", new ZoomPercentFix())

downgrade has an empty default implementation. That is right when the older format simply ignores what upgrade produced.

3. Tell files apart#

The file argument is the path as registered, with / separators, such as config/mymod.properties. For a registered directory it is the path of the file inside it. A fix runs for every file of your mod, so check the path when two files share key names:

@Override
public void upgrade(String file, Map<String, String> settings) {
    if (!file.equals("config/mymod.properties")) {
        return;
    }
    // ...
}

4. Keep every old fix, forever#

Register all your fixes in every later version:

ModSettings.builder("mymod", "My Mod", "3.1.0")
        .file("config/mymod.properties", SettingsFileFormats.PROPERTIES)
        .modVersionFix("2.0.0", SettingsFix.renameKey("renderDist", "renderDistance"))
        .modVersionFix("3.0.0", new ZoomPercentFix())
        .build();

A profile saved long ago by 1.x still needs the 2.0.0 fix when 3.1.0 reads it.

5. Keep the version argument current#

The fixes that run depend on the mod version you pass to ModSettings.builder. Read it from your build (a constant your build fills in, or a properties file) so it can't fall behind. A stale version means profiles are recorded as older than they are, and fixes run on settings that are already converted.

Rules for a fix#

Rule Why
Change the map in place It is the only output.
Leave a map without your keys unchanged Settings Saver also runs fixes over partial maps: only the keys a profile has saved, or a file with one setting removed.
Tolerate anything: malformed, hand-edited or already converted values See Newer and older versions together. Leave such values, or remove them so your mod uses its default. Don't throw.
No side effects: no I/O, no state, no Minecraft classes A fix may run many times per sync, from several threads. The same input must always give the same output.

A fix that throws a RuntimeException is logged and skipped for that conversion. The other fixes still run.

How versions are compared#

Comparison Result
1.10 and 1.9 1.10 is newer: number parts compare as numbers
2.0 and 2.0.0 equal: missing parts count as 0
2.0.0-beta.1 and 2.0.0 the pre-release is older than its release
2.0.0+build.5 and 2.0.0 equal: build metadata after + is ignored
v2.0.0 and 2.0.0 equal: a leading v is ignored

Parts that aren't numbers compare as text. Fixes that share a version run in the order they were added.

Newer and older versions together#

Players run different versions of your mod in different instances, on one profile.

Checklist#

Next steps#