Example: settings across versions

A complete integration whose profile works across three versions of a mod and both sides of Minecraft 1.13, using mod version fixes and a Minecraft fix.

Last updated

On this page

Complete example

My Mod 3.1.0 runs on Minecraft 1.7.10 to 26.3. Its config changed twice over its own versions, and it stores key bindings differently before and after Minecraft 1.13. Three fixes let one profile serve every combination.

Change Since Kind of fix
renderDist was renamed to renderDistance My Mod 2.0.0 mod version fix
zoom changed from a factor (1.5) to a percentage (150) My Mod 3.0.0 mod version fix
keys are stored by name (key.keyboard.c), not by LWJGL 2 code (46) Minecraft 1.13 Minecraft fix

Files#

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

The plugin class#

src/main/java/com/example/mymod/compat/MyModSettingsSaver.java
package com.example.mymod.compat;

import com.example.mymod.MyMod;
import dev.xichil.settingssaver.api.v1.ModSettings;
import dev.xichil.settingssaver.api.v1.SettingsFileFormats;
import dev.xichil.settingssaver.api.v1.SettingsFix;
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(describe());
    }

    /** Separate from register() so a unit test can check it. */
    static ModSettings describe() {
        return ModSettings.builder(MyMod.MOD_ID, "My Mod", MyMod.VERSION)
                .file("config/mymod.properties", SettingsFileFormats.PROPERTIES)
                .savedByDefault(true)
                .excludedByDefault("windowX")
                // 2.0.0 renamed a key.
                .modVersionFix("2.0.0", SettingsFix.renameKey("renderDist", "renderDistance"))
                // 3.0.0 changed zoom from a factor to a percentage.
                .modVersionFix("3.0.0", new ZoomPercentFix())
                // Keys are stored by name from Minecraft 1.13 on, by LWJGL 2 code before.
                .minecraftFix("1.13", new KeyNameFix())
                .build();
    }
}

MyMod.MOD_ID and MyMod.VERSION are public static final String constants with literal values. The compiler copies them into this class, so the plugin doesn't load MyMod.

The mod version fix#

src/main/java/com/example/mymod/compat/ZoomPercentFix.java
package com.example.mymod.compat;

import dev.xichil.settingssaver.api.v1.SettingsFix;

import java.util.Map;

/** 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");
        }
    }
}

The Minecraft fix#

src/main/java/com/example/mymod/compat/KeyNameFix.java
package com.example.mymod.compat;

import dev.xichil.settingssaver.api.v1.SettingsFix;

import java.util.HashMap;
import java.util.Map;

/**
 * From Minecraft 1.13 on, My Mod stores keys by name (key.keyboard.c); before, by LWJGL 2 key code (46).
 * Stateless: the tables are filled once and never change.
 */
final class KeyNameFix implements SettingsFix {
    /** The settings of config/mymod.properties that hold a key. */
    private static final String[] KEY_SETTINGS = {"zoomKey", "menuKey"};

    private static final Map<String, String> CODE_TO_NAME = new HashMap<String, String>();
    private static final Map<String, String> NAME_TO_CODE = new HashMap<String, String>();

    static {
        key("0", "key.keyboard.unknown");        // not bound
        key("19", "key.keyboard.r");
        key("33", "key.keyboard.f");
        key("34", "key.keyboard.g");
        key("42", "key.keyboard.left.shift");
        key("44", "key.keyboard.z");
        key("46", "key.keyboard.c");
        key("47", "key.keyboard.v");
        // ... one line per key My Mod lets players bind
    }

    private static void key(String code, String name) {
        CODE_TO_NAME.put(code, name);
        NAME_TO_CODE.put(name, code);
    }

    @Override
    public void upgrade(String file, Map<String, String> settings) {
        convert(file, settings, CODE_TO_NAME);
    }

    @Override
    public void downgrade(String file, Map<String, String> settings) {
        convert(file, settings, NAME_TO_CODE);
    }

    private static void convert(String file, Map<String, String> settings, Map<String, String> table) {
        if (!file.equals("config/mymod.properties")) {
            return;                              // another file of the mod
        }
        for (String setting : KEY_SETTINGS) {
            String converted = table.get(settings.get(setting));
            if (converted != null) {
                settings.put(setting, converted);
            }
            // Not in the table: already converted, or a key this table doesn't know. Leave it.
        }
    }
}

The service file#

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

How it works#

One profile, four instances#

Instance Its config/mymod.properties
My Mod 1.4.0, Minecraft 1.12.2 renderDist=12, zoom=1.5, zoomKey=46
My Mod 3.1.0, Minecraft 1.8.9 renderDistance=12, zoom=150, zoomKey=46
My Mod 3.1.0, Minecraft 1.21.11 renderDistance=12, zoom=150, zoomKey=key.keyboard.c
My Mod 3.1.0, Minecraft 26.3 renderDistance=12, zoom=150, zoomKey=key.keyboard.c

The profile's copy, as My Mod 3.1.0 keeps it, is always in the newest form:

<store>/profiles/default/mymod/config/mymod.properties
renderDistance=12
zoom=150
zoomKey=key.keyboard.c

What runs when#

Event Fixes that run
3.1.0 reads a profile that 1.4.0 saved renameKey upgrade, then ZoomPercentFix upgrade
3.1.0 on Minecraft 1.8.9 saves KeyNameFix upgrade (46 becomes key.keyboard.c)
3.1.0 on Minecraft 1.8.9 gets the profile KeyNameFix downgrade (key.keyboard.c becomes 46)
3.1.0 on Minecraft 26.3 saves or loads none

Why each fix is safe to run twice#

This matters because settings can pass through a fix more than once. See Newer and older versions together.

Details worth copying#