Generic option

Generic options can be configured for any value type that has corresponding command argument type. In fact, all options listed in SimpleOptions class are wrappers around this one.

Configuration

Generic options have to be provided with a value type T and ArgumentType<T>. See OptionBuilder.generic() for implementation details.

Example

Generic option type being used to access an ItemStack option.

ClientCommandRegistrationCallback.EVENT.register(
        (dispatcher, access) -> {
            var configBuilder = CommandConfigBuilder.client("example-mod");
            configBuilder.option((source) -> {
                var optionBuilder = OptionBuilder.generic("item-option",
                        ItemStackArgumentType.itemStack(access),
                        ItemStackArgument.class,
                        source);
                optionBuilder.valueAccess(
                        (context) -> Text.of("Item: " + ExampleConfig.getInstance().stack),
                        (context, newStack) -> {
                            try {
                                ExampleConfig.getInstance().stack = newStack.createStack(1, false);
                            } catch (CommandSyntaxException e) {
                            }
                            return Text.of("New item: " + ExampleConfig.getInstance().stack);
                        }
                );
                return optionBuilder;
            });

            dispatcher.register(configBuilder.build());
        });

Last updated