Options

Options provide a way to access and modify your config's values. They can be added with a option() method in categories or in the base command.

Configuration

Options implement printFunc(), saveFunc() and helpFunc() in the same way as the base command node.

valueAccess()

For a simple option access requires getter and setter functions, which return response text.

listener()

Listeners are invoked when option setter is called. When called listener is provided with changed option's name and new value. You can call this method multiple times to add more listeners.

Example

SimpleOptions class provides a collection of option types for basic data types like boolean, int, long, float, double and String.

configBuilder.option((source) -> {
    // OptionBuilder<FabricClientCommandSource>
    var optionBuilder = SimpleOptions.string("string-option", source);
    optionBuilder.helpFunc(() -> Text.of("This is a string option!"));
    optionBuilder.valueAccess(
            (context) -> Text.of("String option value is: " + ExampleConfig.getInstance().strOpt),
            (context, newStr) -> {
                ExampleConfig.getInstance().strOpt = newStr;
                return Text.of("String option set to: " + newStr);
            });
    
    return optionBuilder;
});

Setting up value access can be tedious for multiple functions. Defaults class provides simpler interfaces for common cases.

optionBuilder.valueAccess(
        Defaults.defaultValueAccess(() -> ExampleConfig.getInstance().strOpt,
                (newStr) -> ExampleConfig.getInstance().strOpt = newStr)
);

Result

/example-mod string-option -> get value
                           \> [String] -> set value

Advanced options

Following sections go into detail of option types beyond basic data types.

Last updated