From ad21b56683fed07392387febf7f2afcf0608e6ed Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Fri, 30 Oct 2020 00:38:43 +0100 Subject: [PATCH] Created Config/Data (markdown) --- Config-Data.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Config-Data.md diff --git a/Config-Data.md b/Config-Data.md new file mode 100644 index 0000000..4652ac1 --- /dev/null +++ b/Config-Data.md @@ -0,0 +1,26 @@ +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 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 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.