An optional dependency
Why a mod that integrates with Settings Saver still runs without it, how the service file keeps the API classes unloaded, and what would break that.
On this page
Your mod compiles against the Settings Saver API but doesn't need Settings Saver at run time. This works because of how Java loads classes, and it keeps working as long as one rule is followed: only the plugin class references the API.
The mechanism#
Java loads a class the first time running code needs it. A class that nothing ever uses is never loaded, even if it is in the jar.
| Settings Saver installed | Settings Saver missing | |
|---|---|---|
| The service file in your jar | read by Settings Saver through java.util.ServiceLoader |
never read by anyone |
| Your plugin class | loaded and called once | never loaded |
| The API classes | provided by Settings Saver's jar | absent, and never asked for |
| Your mod | settings are synced | runs exactly as before |
The service file is only a text file. It names your class, but naming a class doesn't load it. Only
Settings Saver asks ServiceLoader for implementations of SettingsSaverPlugin, and when it isn't
there, nobody asks.
The one rule#
The plugin class is the only class in your mod that references the API package.
| Allowed | Not allowed |
|---|---|
The plugin class, and classes only it uses (your formats, fixes and merger), import dev.xichil.settingssaver.api.v1 |
Any other class imports the API |
| Nothing references the plugin class | Your mod class calls MyModSettingsSaver.something() |
| The plugin uses compile-time constants of your other classes | The plugin calls methods that load Minecraft or loader classes |
If a class that your mod loads on its own references the API, the JVM throws NoClassDefFoundError
for players without Settings Saver.
Your formats, fixes and mergers implement API interfaces, so they count as "classes only the plugin uses". Keep them in the same package as the plugin, away from the rest of your code.
Why the API must not be bundled#
The API jar is a compileOnly dependency: it is on the compile class path and in no jar you ship.
Settings Saver's own jar contains the same classes.
If your jar also contained them, there could be two copies of each API class at run time. Your plugin
would implement one copy of SettingsSaverPlugin while Settings Saver looks for the other, and your
registration would not reach it. So never bundle, shade, relocate or jar-in-jar the API.
No metadata, no load order#
Because discovery goes through the JDK, you don't declare Settings Saver in any mod metadata file, and there is nothing to order. Settings Saver decides when to look for plugins (When plugins run).
API stability#
The package name carries the API version: dev.xichil.settingssaver.api.v1.
- Within
v1, changes are compatible only. Members may be added. None are removed or change meaning. - A breaking change would come as a new
v2package next tov1, and Settings Saver would keep servingv1.
A mod built against settingssaver-api:1.0.0 therefore keeps working with later Settings Saver
releases.
Registering from code instead#
The same class-loading rule makes registering from your own code safe: the call sits in a class that is only loaded after a check that Settings Saver is installed.