1 Config Data
NorbiPeti edited this page 2020-10-30 00:38:43 +01:00

The plugin comes with a system that automatically manages plugin configuration and data storage (although the two are the same at the moment). A plugin can extend ButtonPlugin instead of JavaPlugin to get easy access to this feature. Custom components can also easily access it.

/**
 * Some description of the config option
 */
private final ConfigData<String> someConfigOption = getIConfig().getData("someConfigOption", "someDefaultValue");

Plugins need to use getIConfig() while components need to use getConfig(). This will automatically generate that config option when the plugin is loaded and insert the comment above it (for this you need to have annotation processing enabled). Comments are only detected in plugin and component classes, and classes annotated with @HasConfig. Components themselves can also have comments.

Then you can use someConfigOption.get() and someConfigOption.set("someValue"); and the changes will be automatically saved to file after a couple seconds. Plugin configuration is saved to the global section of config.yml while component configuration is saved to the component's section.

Complex types

The system can save primitive types (and String) without issue but to use other types you need to specify a getter and a setter. This can easily be done like so:

public final ConfigData<CustomClass> somethingCustom = getConfig().getData("somethingCustom", new CustomClass("default"),
                str -> new CustomClass(str), cc -> cc.ToString());

The underlying type (String in this case) can be any primitive or String, the actual type depends on the value stored in the config file.

Further customization

There is also ReadOnlyConfigData, ListConfigData, and a primitive value (which gets stored in the yml) can also be specified as a default value (this is used to save a default channel ID on Discord for example instead of obtaining a channel object and setting that as default). These can be obtained by the different getData() methods or the getConfig("path") config builder.