LibJF/docs/libjf-config-v0.md

92 lines
3.4 KiB
Markdown
Raw Normal View History

2022-01-05 11:54:03 +01:00
# libjf-config-v0
LibJF config provides config screens and is partially based on TinyConfig and MidnightLib
It depends on libjf-unsafe-v0 to ensure configs are loaded before you use them and libjf-base
To add a config create a class using only static fields with default values like this:
```java
import io.gitlab.jfronny.libjf.config.api.JfConfig;
import io.gitlab.jfronny.libjf.config.api.Entry;
import io.gitlab.jfronny.libjf.gson.GsonHidden;
public class TestConfig implements JfConfig {
@Entry public static boolean disablePacks = false;
@Entry public static Boolean disablePacks2 = false;
@Entry public static int intTest = 20;
@Entry(min = -6) public static float floatTest = -5;
@Entry(max = 21) public static double doubleTest = 20;
@Entry public static String dieStr = "lolz";
@Entry @GsonHidden public static String guiOnlyStr = "lolz";
public static String gsonOnlyStr = "lolz";
@Entry public static Test enumTest = Test.Test;
public enum Test {
Test, ER
}
}
```
You MUST annotate any field configurable through the UI as @Entry and the class MUST extend JfConfig.
You MAY annotate fields as @GsonHidden to not serialize them (-> [libjf-base](libjf-base.md)).
Numeric values MAY have a min and max value specified in their @Entry.
To register a config, add a `libjf:config` entrypoint pointing to its class to your fabric.mod.json.
To manually register a config or save changes, use `io.gitlab.jfronny.libjf.config.api.ConfigInstance`
2022-01-05 11:54:03 +01:00
For example, to save a config for a mod titled `yourmod`:
```java
// Directly using ConfigInstance
ConfigInstance.get("yourmod").write();
// Using ConfigHolder
2022-01-08 20:02:15 +01:00
ConfigHolder.getInstance().get("yourmod").write();
2022-01-05 11:54:03 +01:00
```
LibJF config is only intended for simple config screens, it does not support nested classes, multiple pages or controls like sliders.
2022-01-08 20:02:15 +01:00
Use something else for those
## Translations
Config keys are translated as `<mod id>.jfconfig.<field name>`.
You may add a tooltip as follows: `<mod id>.jfconfig.<field name>.tooltip`.
Enum keys are translated as follows: `<mod id>.jfconfig.enum.<enum class name>.<entry name>`
2022-01-08 20:02:15 +01:00
## Presets
libjf-config-v0 provides a preset system to automatically fill in certain values based on a function.
To add a snippet, add a public static method to your config class and annotate it with @Preset.
If your preset is selected, the method will be executed.
You may assign a name by using your language file, the format for names is `<mod id>.jfconfig.<method name>`
Example:
```java
@Preset
public static void moskau() {
disablePacks = true;
disablePacks2 = true;
intTest = -5;
floatTest = -6;
doubleTest = 4;
dieStr = "Moskau";
}
```
## Verifiers
If you need to manually validate config values outside of minimums or maximums, you may add a public static method
and annotate it with @Verifier. This method will be executed whenever your config changes, which might happen often.
Be careful to write performant code here!
Example:
```java
@Verifier
public static void setIntTestIfDisable() {
if (disablePacks) intTest = 0;
}
```
## References
Sometimes, your mod interacts with other mods (such as libjf-web-v0), and you may wish to display their config screens as well.
If that other mod utilizes libjf-config-v0, you can simply add a json block as seen below to your fabric.mod.json
and it will be mentioned in the GUI.
```json
"custom": {
"libjf:config": {
"referencedConfigs": ["libjf-web-v0"]
}
}
```