Plugin Configuration
Plugins can declare a settings interface in preference_config.json. The player renders the controls and saves user settings. At runtime, use ConfigManager and ConfigHelper to read, update, and observe settings.
Enable the Settings Interface
Set PluginHasConfig in the module's spmod {} block.
spmod {
PluginHasConfig = true
}Put the UI definition in src/main/resources/preference_config.json so it is packaged at the root of the plugin's classpath. This file describes the settings interface. The user's values are stored in the plugin's data directory.
Definition Structure
The root object contains a configs array. Each element describes one group of settings.
{
"configs": [
{
"title": "General",
"config": "config.json",
"preferences": [
{
"type": "switch",
"key": "feature.enabled",
"title": "Enable feature",
"summary": "Turn the main plugin feature on or off",
"default_value": true
}
]
}
]
}| Group field | Meaning |
|---|---|
title | Display name for the group. Use a distinct name for each group. |
config | Path to the user settings file relative to the plugin's data directory, such as config.json. |
preferences | Array of settings in the group. |
User settings are stored under workshop/data/<plugin ID>/ in the player's application data directory. Use the API to resolve files and paths on Windows and Linux.
Write valid JSON without comments. Use a unique key for each setting in a file, and use that same key in your code. Dot-separated keys such as feature.enabled represent nested values.
Setting Types
Every setting needs a type and a display title. Each setting with a stored value also needs a key and a default_value matching its type. summary is optional helper text. Sliders currently omit it.
Switch
A switch stores a Boolean.
{
"type": "switch",
"key": "feature.enabled",
"title": "Enable feature",
"summary": "Turn the main plugin feature on or off",
"default_value": true
}List
A list lets the user choose one option and stores its string value.
{
"type": "list",
"key": "display.mode",
"title": "Display mode",
"summary": "Choose how content is displayed",
"entries": ["Compact", "Detailed"],
"entry_values": ["compact", "detailed"],
"default_value": "compact"
}entries contains the labels and entry_values contains the stored values. Provide corresponding entries in the same order, and choose a default_value from entry_values.
Button
A button runs an action and has no stored value, so it does not need a key or default_value.
{
"type": "button",
"title": "Show current settings",
"summary": "Show a message in the player",
"arrow_type": "none",
"on_click": "com.example.myplugin.ConfigActions.showSettings"
}arrow_type can be none, link, or arrow. Omit it to hide the arrow. on_click identifies the action by its fully qualified class name and method name. Use a public static method that takes no arguments.
package com.example.myplugin
import com.xuncorp.spw.workshop.api.UnstableSpwWorkshopApi
import com.xuncorp.spw.workshop.api.WorkshopApi
class ConfigActions {
companion object {
@JvmStatic
@OptIn(UnstableSpwWorkshopApi::class)
fun showSettings() {
val config = WorkshopApi.manager.createConfigManager().getConfig()
val enabled = config.get("feature.enabled", true)
WorkshopApi.ui.toast("Feature enabled: $enabled", WorkshopApi.Ui.ToastType.Success)
}
}
}package com.example.myplugin;
import com.xuncorp.spw.workshop.api.WorkshopApi;
import com.xuncorp.spw.workshop.api.config.ConfigHelper;
public class ConfigActions {
public static void showSettings() {
ConfigHelper config = WorkshopApi.manager().createConfigManager().getConfig();
boolean enabled = config.get("feature.enabled", true);
WorkshopApi.ui().toast("Feature enabled: " + enabled, WorkshopApi.Ui.ToastType.Success);
}
}In a Kotlin companion object, use @JvmStatic to expose a static entry point. If you use @JvmName, reference the resulting JVM name in on_click.
Slider
A seekbar stores a floating-point value between min and max.
{
"type": "seekbar",
"key": "display.opacity",
"title": "Opacity",
"default_value": 80.0,
"min": 0.0,
"max": 100.0
}Use valid numbers for both bounds and choose a default within that range. In Kotlin, read the value using the corresponding floating-point type, such as config.get("display.opacity", 80f).
Text Input
An edittext opens a text input dialog and stores a string.
{
"type": "edittext",
"key": "display.label",
"title": "Display name",
"summary": "Set the text displayed by the plugin",
"default_value": "My Plugin"
}Read and Write Settings
Call WorkshopApi.manager.createConfigManager() from your plugin. The host uses the caller to identify the plugin. These configuration APIs are marked UnstableSpwWorkshopApi, so Kotlin callers must opt in explicitly.
Place the following code in a runtime method such as start().
val manager = WorkshopApi.manager.createConfigManager()
val config = manager.getConfig()
val enabled = config.get("feature.enabled", true)
val mode = config.get("display.mode", "compact")
config.set("display.label", "New display name")
if (!config.save()) {
println("Could not save settings")
}ConfigManager manager = WorkshopApi.manager().createConfigManager();
ConfigHelper config = manager.getConfig();
boolean enabled = config.get("feature.enabled", true);
String mode = config.get("display.mode", "compact");
config.set("display.label", "New display name");
if (!config.save()) {
System.out.println("Could not save settings");
}ConfigManager and ConfigHelper are in com.xuncorp.spw.workshop.api.config.
| Method | Behavior |
|---|---|
manager.getConfig() | Get the default config.json. |
manager.getConfig("display.json") | Get a named config file. The manager caches the helper for that file. |
config.get(key, defaultValue) | Read a value, or return the supplied default if it is missing or cannot be read as the requested type. |
config.set(key, value) | Update the in-memory value. Call save() to persist it. |
config.save() | Write settings to disk and return whether the write succeeded. |
config.reload() | Reload from disk, replacing unsaved in-memory changes, and return whether the reload succeeded. |
config.getConfigPath() | Get the path to the actual configuration file. |
The settings interface saves the user's edits automatically. Call save() when the plugin writes settings. Defaults in preference_config.json are used for display. Supply the same defaults when reading a setting, since users may never have edited it.
Listen for Changes
Use addConfigChangeListener to observe changes to a settings file. The callback receives that file's ConfigHelper. Keep a reference to the listener and pass it to removeConfigChangeListener when the plugin stops to avoid duplicate registrations.
This example shows a complete Kotlin plugin class.
package com.example.myplugin
import com.xuncorp.spw.workshop.api.PluginContext
import com.xuncorp.spw.workshop.api.SpwPlugin
import com.xuncorp.spw.workshop.api.UnstableSpwWorkshopApi
import com.xuncorp.spw.workshop.api.WorkshopApi
import com.xuncorp.spw.workshop.api.config.ConfigHelper
import com.xuncorp.spw.workshop.api.config.ConfigManager
import java.util.function.Consumer
@OptIn(UnstableSpwWorkshopApi::class)
class MainPlugin(pluginContext: PluginContext) : SpwPlugin(pluginContext) {
private val manager: ConfigManager by lazy {
WorkshopApi.manager.createConfigManager()
}
private val listener = Consumer<ConfigHelper> { config ->
val enabled = config.get("feature.enabled", true)
println("Feature setting updated: $enabled")
}
override fun start() {
manager.addConfigChangeListener(listener)
}
override fun stop() {
manager.removeConfigChangeListener(listener)
}
}Without a filename, the listener observes config.json. To observe another file, use manager.addConfigChangeListener("display.json", listener). Use the exact filename from the configuration group's config field. Wildcard subscriptions are unsupported.
Configuration callbacks can run off the UI thread. Switch to the UI framework's required thread before updating the interface. Saving the same file unconditionally in a callback can trigger repeated notifications.
See also Developer Quickstart, Plugin Permissions, and Publish Mods.
