Merge dependent settings
Replace the default changed-keys-win merge with a SettingsMerger when your mod's settings depend on each other, such as presets or numbered lists.
Steps
When the player changes settings in an instance, Settings Saver writes only the changed keys into the profile. That is right for independent settings. This guide shows how to take over the merge when a change to one setting must also update or remove others.
Prerequisites#
- A file registered with a key-by-key format.
The default merge#
Without a merger, every changed key replaces the profile's value and every other key in the profile
stays as it was. In code: stored.putAll(changed).
That lets two instances that changed different settings both keep their change. It goes wrong when keys belong together:
| Case | What the default merge leaves behind |
|---|---|
A preset key and the preset.* values it sets |
the old preset's values, next to the new preset name |
A list split over numbered keys (slot.0, slot.1, …) that got shorter |
the entries past the new end, which the player deleted |
Steps#
1. Implement the merger#
SettingsMerger has one method:
/** Picking a new preset drops the values the old preset set. */
final class PresetMerger implements SettingsMerger {
@Override
public void merge(String file, Map<String, String> stored, Map<String, String> changed) {
if (changed.containsKey("preset")) {
for (Iterator<String> it = stored.keySet().iterator(); it.hasNext(); ) {
if (it.next().startsWith("preset.")) {
it.remove();
}
}
}
stored.putAll(changed);
}
}
| Parameter | What it holds | May you change it? |
|---|---|---|
file |
the file's path as registered, with / separators; for a directory, the path of the file inside it |
|
stored |
the profile's current settings for this file, in order; empty the first time the file is saved | yes, in place |
changed |
the keys the player changed since the last sync, plus keys the profile doesn't have yet, each with its new value; excluded keys are already left out; never empty | no |
After merge returns, Settings Saver writes stored into the profile as it is. Keys you remove from it
are removed from the profile.
Both maps are in the profile's form: already converted to the running version of your mod and to the newest Minecraft format (see How settings are converted).
2. Register it#
ModSettings.builder("mymod", "My Mod", "2.1.0")
.file("config/mymod.properties", SettingsFileFormats.PROPERTIES)
.merger(new PresetMerger())
.build();
One merger serves every file of your mod. Use the file argument to tell them apart. Calling merger
again replaces the previous one, and merger(null) goes back to the default.
3. End with the default for everything else#
Almost every merger should finish with stored.putAll(changed). If you leave it out, changes to the keys
you didn't handle are not saved.
Example: a numbered list#
The mod stores a list as slot.count plus one key per entry. When the list gets shorter, the entries
past the new end must leave the profile:
/** slot.count=3 with slot.0, slot.1, slot.2. A shorter list drops the entries past its end. */
final class SlotListMerger implements SettingsMerger {
@Override
public void merge(String file, Map<String, String> stored, Map<String, String> changed) {
String count = changed.get("slot.count");
if (count != null) {
try {
int size = Integer.parseInt(count.trim());
for (Iterator<String> it = stored.keySet().iterator(); it.hasNext(); ) {
String key = it.next();
if (key.startsWith("slot.") && !key.equals("slot.count") && index(key) >= size) {
it.remove();
}
}
} catch (NumberFormatException e) {
// An unreadable count: keep what is stored and fall through to the default merge.
}
}
stored.putAll(changed);
}
/** The N of slot.N, or -1 when it isn't a number. */
private static int index(String key) {
try {
return Integer.parseInt(key.substring("slot.".length()));
} catch (NumberFormatException e) {
return -1;
}
}
}
changed holds only the keys whose value changed, so the merger can't see the whole new list. It works
from slot.count, which changes whenever the length does. Entries that kept their value stay in stored
untouched.
Rules#
- Thread-safe and stateless. One merger may be called for several files and from several threads.
- Quick, no I/O. The call is made while the profile is locked.
- A merger that throws a
RuntimeExceptionis logged, and Settings Saver uses the default merge for that call, so no change is lost.
When a merger is not the answer#
| Goal | Use instead |
|---|---|
| Keep a key out of profiles | excludedByDefault |
| Convert a key between versions | a fix |
| Sync a file that can't be split into keys | WHOLE_FILE |
Next steps#
- How syncing works: where
changedcomes from. - Test your integration without Minecraft: a merger is a plain function over two maps.