Register files and directories
Tell Settings Saver which of your mod's settings files and folders to sync, with the path rules and what happens to files that appear later.
Steps
This guide shows how to register one file, several files and whole directories, and which paths are accepted.
Prerequisites#
- Your first integration: you have a plugin class.
The four methods#
All four are on ModSettings.Builder. Call them as often as you need, at least once in total.
| Call | Syncs | How |
|---|---|---|
.file(path) |
one file | as a whole (WHOLE_FILE) |
.file(path, format) |
one file | key by key, read with format |
.directory(path) |
every file in a folder, at any depth | each as a whole |
.directory(path, format) |
every file in a folder, at any depth | each key by key, read with format |
"As a whole" and "key by key" are explained in Choose a file format.
Steps#
1. Register single files#
ModSettings.builder("mymod", "My Mod", "2.1.0")
.file("config/mymod.properties", SettingsFileFormats.PROPERTIES) // key by key
.file("config/mymod-notes.txt") // as a whole
.build();
A file doesn't have to exist yet. Until it does, there is nothing to save. An instance without the file gets the profile's copy when it starts.
2. Register a directory for one-file-per-thing settings#
Use a directory when your mod keeps one file per server, per waypoint set or per preset:
ModSettings.builder("mymod", "My Mod", "2.1.0")
.directory("config/mymod/waypoints") // each file as a whole
.directory("config/mymod/presets", SettingsFileFormats.PROPERTIES) // each file key by key
.build();
What a directory does:
- The files synced are the ones in the instance's folder plus the ones in the profile's copy of it. A file created in one instance appears in every other instance that uses the profile.
- With a format, every file in the directory must be in that format.
- Files that appear while the game runs are picked up at the next launch, or by the next manual Save or Load.
- Files ending in
.tmpare ignored. - A directory with more than 256 files is cut off there, with a warning in the log. Register only directories that hold settings, never caches or worlds.
3. Follow the path rules#
Paths are relative to the game directory, the instance folder that holds options.txt.
| Path you pass | Result |
|---|---|
"config/mymod.toml" |
accepted |
".\\config\\mymod.toml" (the Java string for .\config\mymod.toml) |
accepted, stored as config/mymod.toml |
"config//mymod.toml" |
accepted, stored as config/mymod.toml |
"config/mymod/" (trailing slash, for a directory) |
accepted, stored as config/mymod |
"/home/alex/mymod.toml" |
rejected: absolute |
"C:/mymod.toml" |
rejected: contains : |
"../shared/mymod.toml" |
rejected: .. leaves the game directory |
"" |
rejected: empty |
| the same path twice for one mod | rejected |
A rejected path throws IllegalArgumentException from the builder call, in your code, with a message
that says what is wrong:
Invalid path '../x': it must be relative to the game directory and stay inside it
build() throws the same exception when no file or directory was added:
mymod: register at least one file or directory
4. Avoid paths another mod already syncs#
These checks happen when you register, because they depend on the other mods installed. They don't throw. Settings Saver logs a warning and ignores your whole registration when:
- one of your paths is, contains, or lies inside a path Settings Saver already syncs. That is
options.txtand every path another mod registered. Registeringconfigwould contain every other mod's files, so register your own folder, such asconfig/mymod. - a mod with your mod id is already registered. The first registration stays.
- one of your paths is
module.txt, the name profiles use for their version record.
The warning names the path and who owns it. See Fix an integration that doesn't sync.
Where the files go in a profile#
Each profile stores your files under your mod id, at the same relative path:
| In the game directory | In the store |
|---|---|
config/mymod.properties |
profiles/<profile>/mymod/config/mymod.properties |
config/mymod/waypoints/home.json |
profiles/<profile>/mymod/config/mymod/waypoints/home.json |
profiles/<profile>/mymod/module.txt (written by Settings Saver: the versions the settings were saved in) |
The profile's copy is written with the same format as the file in the game directory.
Limits#
- Text only. Files are read and written as UTF-8 text. Don't register binary files.
- Client only. Nothing is synced on a dedicated server.
- One mod id per registration. A jar that bundles several mods may register each one under its own
id, with one
registrar.register(...)call each.
Next steps#
- Choose a file format: pick how each file is read.
- Set what is saved by default: keep per-instance keys out of profiles.