JSON Configuration
SSGL provides a JSON configuration system for saving selected GUI settings.
It uses GuiValue, so the same values can be used by your GUI and your configuration file.
Creating a config
Create a config using SSGL.config():
GuiConfig config = SSGL.config("mymod/settings.json");
The path is relative to Minecraft's config directory.
For example:
.minecraft/
└── config/
└── mymod/
└── settings.json
You can create multiple config files for different groups of settings.
Registering values
Register each setting by providing a key and a default value.
Boolean
GuiValue<Boolean> enabled = config.bool("enabled", true);
Integer
GuiValue<Integer> distance = config.intValue("distance", 8);
Double
GuiValue<Double> volume = config.doubleValue("volume", 0.65);
String
GuiValue<String> username = config.string("username", "Steve");
Enum
public enum RenderMode {
FAST,
BALANCED,
QUALITY
}
GuiValue<RenderMode> mode =
config.enumValue(
"render_mode",
RenderMode.class,
RenderMode.BALANCED
);
Enum values are saved using their constant names.
Using values in a GUI
The returned values are normal GuiValue instances.
gui.add(GuiElement.toggle("Enabled", enabled));
gui.add(GuiElement.slider(
"Volume",
0.0,
1.0,
volume
));
gui.add(GuiElement.stateButton("Render Mode", mode));
You do not need to register the same setting again for the GUI.
Saving
Saving is manual. Call save() when you want to write the registered values to disk.
if (config.save()) {
LOGGER.info("Settings saved.");
} else {
LOGGER.error("Could not save settings.");
}
A common place to save is an Apply or Save button.
GuiElement.button("Save", () -> config.save());
save() returns true when the operation succeeds and false if it fails.
Loading and default values
When a config is created, SSGL reads the existing JSON file.
When a value is registered:
- An existing valid value is used
- A missing key uses the supplied default
- An invalid value uses the default and is logged
For example:
GuiValue<Integer> distance = config.intValue("distance", 8);
If the file does not contain a valid integer for distance, the value starts at 8.
The file is not automatically rewritten when a default is used. Call save() to write the current registered values.
Selecting what gets saved
Only values registered with a GuiConfig are managed by that config.
GuiConfig config = SSGL.config("mymod/settings.json");
GuiValue<Boolean> enabled = config.bool("enabled", true);
GuiValue<Double> temporaryPreview = GuiValue.of(0.5);
Only enabled is registered and saved by this config. temporaryPreview is not included.
Multiple config files
You can keep settings in separate files.
GuiConfig generalConfig = SSGL.config("mymod/general.json");
GuiConfig cameraConfig = SSGL.config("mymod/camera.json");
Each config manages its own registered values.
Preserving other properties
When saving, SSGL preserves existing JSON properties that were not registered through that config instance.
This lets your mod keep other data in the same file without SSGL replacing the entire JSON object.
Getting the file path
Use file() to retrieve the resolved path:
Path path = config.file();
Notes
- Configuration keys must be unique within a
GuiConfig. - Config paths must be relative to Minecraft's config directory.
- Values cannot be null.
- Enum constants are stored by name. Renaming a constant may cause an old saved value to fall back to its default.
- Changes are not saved automatically. Call
save()when needed.