Data and events
Plugin state
Your settings live in your own config file, and that keeps working. What it cannot do on its own is travel: when a player saves a preset, copies a share code or lets a server config load, only Eymistaken's HUD config moves, and your module's settings stay behind.
Opting in
Override saveState() and loadState(). Whatever you return is stored under your getName() and carried through all four persistence paths: simplecps.json, named presets, EYMHUD1- share codes and per-server configs.
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
// Inside your MyCustomModule class:
@Override
public JsonElement saveState() {
JsonObject state = new JsonObject();
state.addProperty("enabled", MyAddonConfig.enabled);
state.addProperty("anchor", MyAddonConfig.anchor.name());
state.addProperty("xOffset", MyAddonConfig.xOffset);
state.addProperty("yOffset", MyAddonConfig.yOffset);
state.addProperty("scale", MyAddonConfig.scale);
state.addProperty("textColor", MyAddonConfig.textColor);
return state;
}
@Override
public void loadState(JsonElement state) {
if (!state.isJsonObject()) return;
JsonObject obj = state.getAsJsonObject();
// Check has() on every key: the blob may come from an older or newer
// build of your addon, or from a player who never touched this setting.
if (obj.has("enabled")) MyAddonConfig.enabled = obj.get("enabled").getAsBoolean();
if (obj.has("xOffset")) MyAddonConfig.xOffset = obj.get("xOffset").getAsInt();
if (obj.has("yOffset")) MyAddonConfig.yOffset = obj.get("yOffset").getAsInt();
if (obj.has("scale")) MyAddonConfig.scale = obj.get("scale").getAsInt();
if (obj.has("textColor")) MyAddonConfig.textColor = obj.get("textColor").getAsInt();
MyAddonConfig.save();
}Both default to a no-op, so plugins written before this existed keep compiling and running unchanged.
Rules of the round trip
- Return
nullfromsaveState()to store nothing. That is what every built-in module does, since their settings are already fields onSimpleCPSConfig. loadState()is never called withnull. When no blob is stored for your module the call is skipped, so a config written by someone without your addon leaves your settings untouched instead of resetting them.- It runs once at registration and again on every preset, share code or server config that is applied, so it can fire at any point after startup, not just during init.
- Do not put JSON nulls anywhere in the tree you return. The serializer drops them at every depth, so they will not survive a round trip; omit the key instead.
saveState()runs on every config save, so keep it cheap.
Your blob survives players who do not have your addon. Export a preset with your module in it, hand it to a player who has not installed your addon, and the state rides along inert until the day they do. The reverse holds too: importing a config from a player without your module does not wipe your settings.
Failures are contained, not handled. Both methods are wrapped in a try and catch by HudModuleManager. A throw is logged and skipped, and your module keeps its current settings rather than taking down the save or the import. Do not rely on that as error handling, but you do not have to defend against every malformed value either.