Final config

The whole

private void createConfig() {
    var configBuilder = CommandConfigBuilder.client("example-mod");
    configBuilder.helpFunc(() -> Text.of("This is a config for ExampleMod!"));
    configBuilder.saveFunc(ExampleConfig.getInstance()::save);

    configBuilder.category((source) -> {
        var categoryBuilder = CategoryBuilder.create("category", source);
        categoryBuilder.helpFunc(() -> Text.of("This is a category!"));
        categoryBuilder.option((unused) -> {
            var optionBuilder = SimpleOptions.integer("int-option", source);
            optionBuilder.helpFunc(() -> Text.of("This is an integer option!"));
            optionBuilder.valueAccess(
                    Defaults.defaultValueAccess(() -> ExampleConfig.getInstance().intOpt,
                            (newInt) -> ExampleConfig.getInstance().intOpt = newInt)
            );
            return optionBuilder;
        });
        return categoryBuilder;
    });

    configBuilder.option((source) -> {
        var optionBuilder = SimpleOptions.string("string-option", source);
        optionBuilder.helpFunc(() -> Text.of("This is a string option!"));
        optionBuilder.valueAccess(
                Defaults.defaultValueAccess(() -> ExampleConfig.getInstance().strOpt,
                        (newStr) -> ExampleConfig.getInstance().strOpt = newStr)
        );
        return optionBuilder;
    });

    configBuilder.node((command) -> {
        LiteralArgumentBuilder<FabricClientCommandSource> node = literal("reset");
        node.executes(context -> {
            ExampleConfig.reset();
            return configBuilder.print(context, Text.of("Resetting config!"));
        });
        command.then(node);
    });


    ClientCommandRegistrationCallback.EVENT.register(
            (dispatcher, access) -> {
                dispatcher.register(configBuilder.build());
            });
}

Compressing

The config is done! But its quite big... However, its entirely builder-based, which means you can make it using long method chains, as shown in the following example. Previous sections of the wiki don't use that approach for better readability.

private void createConfig() {
    ClientCommandRegistrationCallback.EVENT.register((dispatcher, access) -> dispatcher.register(
            CommandConfigBuilder.client("example-mod")
                    .helpFunc(() -> Text.of("This is a config for ExampleMod!"))
                    .saveFunc(ExampleConfig.getInstance()::save)
                    .category((source) -> CategoryBuilder.create("category", source)
                            .helpFunc(() -> Text.of("This is a category!"))
                            .option((unused) -> SimpleOptions.integer("int-option", source)
                                    .helpFunc(() -> Text.of("This is an integer option!"))
                                    .valueAccess(Defaults.defaultValueAccess(
                                            () -> ExampleConfig.getInstance().intOpt,
                                            (newInt) -> ExampleConfig.getInstance().intOpt = newInt))))
                    .option((source) -> SimpleOptions.string("string-option", source)
                            .helpFunc(() -> Text.of("This is a string option!"))
                            .valueAccess(Defaults.defaultValueAccess(
                                    () -> ExampleConfig.getInstance().strOpt,
                                    (newStr) -> ExampleConfig.getInstance().strOpt = newStr)))
                    .node((command) -> {
                        // Command builders don't work well with method chains
                        LiteralArgumentBuilder<FabricClientCommandSource> reset = literal("reset");
                        command.then(reset.executes(context -> {
                            ExampleConfig.reset();
                            return Defaults.clientPrintFunc().apply(context,
                                    Text.of("Resetting config!"));
                        }));
                    })
                    .build()));
}

Result

/example-mod category int-option -> get intOpt
           |                     \> [Integer] -> set intOpt
           - string-option -> get strOpt
           |               \> [String] -> set strOpt
           - reset -> reset ExampleConfig
           \ help -> "This is a config for ExampleMod!"
                  - category -> "This is a category!"
                  |          \- int-option -> "This is an integer option!"
                  \ string-option -> "This is a string option!"

Last updated