Convert settings between Minecraft versions

Store a setting differently on older and newer Minecraft versions and keep one profile working on all of them, with minecraftFix, upgrade and downgrade.

Last updated

Steps

A mod that runs on several Minecraft versions sometimes has to store a setting differently per version. The classic case is key bindings: numeric LWJGL 2 key codes before Minecraft 1.13, key names from 1.13 on. A Minecraft fix lets one profile serve instances on both sides.

Prerequisites#

How it works#

The profile keeps your settings in the format of the newest Minecraft version Settings Saver knows. Your fixes convert on the way in and on the way out:

instance on 1.12.2  --upgrade-->  profile (newest format)  --downgrade-->  instance on 1.8.9
instance on 26.3    ----------->  profile (newest format)  ------------->  instance on 1.21.11

So, unlike mod version fixes, both directions are used all the time. Implement downgrade whenever an older game would misread the new values.

Steps#

1. Write the fix in both directions#

/** From Minecraft 1.13 on, the zoom key is stored by name instead of by LWJGL 2 key code. */
final class ZoomKeyFix implements SettingsFix {
    @Override
    public void upgrade(String file, Map<String, String> settings) {
        if ("46".equals(settings.get("zoomKey"))) {
            settings.put("zoomKey", "key.keyboard.c");
        }
    }

    @Override
    public void downgrade(String file, Map<String, String> settings) {
        if ("key.keyboard.c".equals(settings.get("zoomKey"))) {
            settings.put("zoomKey", "46");
        }
    }
}

A real fix would map every key your mod supports, with a lookup table in each direction. The shape stays the same: look at the value, convert it if it is in the other format, leave it alone otherwise.

2. Register it at the first release that uses the new format#

ModSettings.builder("mymod", "My Mod", "2.1.0")
        .file("config/mymod.properties", SettingsFileFormats.PROPERTIES)
        .minecraftFix("1.13", new ZoomKeyFix())
        .build();

The release id is a release such as "1.13", "1.20.5" or "26.1". It must be one Settings Saver knows. If it isn't, that fix is skipped when you register, with a warning in the log, and the rest of your registration is kept:

[Settings Saver] WARN mymod: unknown Minecraft release '1.99' for a fix; skipping that fix (register it by DataVersion).

3. Use a DataVersion for snapshots and very new releases#

A release that came out after the player's Settings Saver build is not in its list. For those, and for snapshots, register by DataVersion, the number Minecraft uses to tell its versions apart. It is the number in the version: line of options.txt, and the Minecraft Wiki lists it for every version. 1.13 is 1519:

.minecraftFix(1519, new ZoomKeyFix())      // the same as .minecraftFix("1.13", ...)

The DataVersion must be positive, or the builder throws IllegalArgumentException.

Use When
.minecraftFix("1.13", fix) a release Settings Saver has known since before your mod's release
.minecraftFix(1519, fix) snapshots, or releases newer than the Settings Saver build players may have

When in doubt, the DataVersion always works.

4. Decide what a downgrade may lose#

A downgrade may lose information the older format can't hold. A value the old version doesn't have can become its nearest equivalent. It may also do nothing, if the old format simply ignores the new keys.

This is safe: Settings Saver only writes the downgraded settings into the older game's file. The profile keeps the full values, and an older instance never replaces a profile value it could only approximate.

Order#

Do I need a Minecraft fix?#

Situation Fix needed?
Your settings file looks the same on every Minecraft version no
A value means something else on older versions (key codes, ids, a renamed enum) yes
A setting only exists from some Minecraft version on, and older versions ignore the key no
A setting only exists from some Minecraft version on, and older versions fail on the key yes: remove it in downgrade
Your mod's own version changed the format no, that is a mod version fix

Checklist#

Next steps#