LibJF/docs/libjf-config-v0.md

98 lines
3.8 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;
2022-04-29 15:48:27 +02:00
import io.gitlab.jfronny.commons.serialize.gson.GsonIgnore;
2022-01-05 11:54:03 +01:00
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";
2022-04-29 15:48:27 +02:00
@Entry @GsonIgnore public static String guiOnlyStr = "lolz";
2022-01-05 11:54:03 +01:00
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, @ClientOnly or @ServerOnly to hide them from the file as well them (-> [libjf-base](libjf-base.md)).
2022-01-05 11:54:03 +01:00
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
```
2022-04-04 20:03:10 +02:00
LibJF config is intentionally designed to be easy to implement, pleasant for the user and somewhat lightweight.
It is not intended to be used as a general purpose solution for everything, other libraries are better suited for that.
2022-01-08 20:02:15 +01:00
## 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-04-04 20:03:10 +02:00
## Categories
Categories can be added by creating public static subclasses in your config class and annotating them with @Category.
Entries will be read as before, however the translation prefix will be `jfconfig.<category>.` instead of `jfconfig.`
2022-04-04 20:03:10 +02:00
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"]
}
}
}
```