LibJF/docs/libjf-config-v0.md

3.5 KiB

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:

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, @ClientOnly or @ServerOnly to hide them from the file as well them (-> libjf-base). 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 For example, to save a config for a mod titled yourmod:

// Directly using ConfigInstance
ConfigInstance.get("yourmod").write();
// Using ConfigHolder
ConfigHolder.getInstance().get("yourmod").write();

LibJF config is only intended for simple config screens, it does not support nested classes, multiple pages or controls like sliders. 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>

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:

@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:

@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.

"custom": {
  "libjf:config": {
    "referencedConfigs": ["libjf-web-v0"]
  }
}