Controllers

In-depth review of Controllers.

Built-in controllers

This is an incomplete list of controllers!
There are many different built-in controllers to get you set up quickly.
BooleanController
A toggleable controller that displays a different Text based on the state of the option.
.controller(BooleanController::new)
  • valueFormatter parameter is a function to return Text based on the state of the option. (optional)
  • coloured parameter is a boolean that colours the returned text red or green based on the state. (optional)
To pass extra parameters, you need to construct a BooleanController like so.
.controller(opt -> new BooleanController(opt, valueFormatter, coloured))
TickBoxController
A toggleable controller that displays a tick box.
.controller(TickBoxController::new)
There are no optional parameters for this controller.
<Number>SliderController
Replace <Number> with either Double, Float, Integer and Long.
Slider controllers take a minimum, a maximum and an interval for the slider in their respected number types.
.controller(opt -> new <Number>SliderController(opt, min, max, interval))
Sliders can also take a valueFormatter to return a Text based on the slider value.
.controller(opt -> new <Number>SliderController(opt, min, max, interval, valueFormatter))
EnumController
A controller that allows you to cycle through Enum constants.
.controller(EnumController::new)
Enums can implement the NameableEnum interface to automatically name each constant without a value formatter function.
public interface Alphabet implements NameableEnum {
A,
B,
C;
@Override
public Component getDisplayName() {
return Component.translatable("mymod.alphabet." + name().toLowerCase());
}
}
Or alternatively, just pass a valueFormatter to the controller as usual;
.controller(opt -> new EnumController(opt, v ->
Component.translatable("mymod.alphabet." + v.name().toLowerCase())
)
StringController
An input box that allows users to input text as a String. This allows for highlighting text with the keyboard and strings that are longer than the option itself, similarly to Minecraft's EditBox
.controller(StringController::new)
This controller has no arguments, though its functionality can be extended by creating your own controller, implementing IStringController, more on this here.
ColorController
A controller that allows users to input colors as RGB hex format, with a preview.
.controller(ColorController::new)
  • allowAlpha parameter adds an alpha channel to the hex format as RGBA.
.controller(opt -> new ColorController(opt, true)
<Number>FieldController
Replace <Number> with either Double, Float, Long or Integer.
Similar to StringController, but forces a number format, doubles and floats allow decimals, but longs and integers do not.
  • min and max parameters specify how small and big the number is allowed to be. This is applied when the user unfocuses from the field or clicks apply changes.
  • valueFormatter parameter is a function to convert the current value into a Component to be displayed when the user is not focused on the option.
.controller(<Number>SliderController::new)

Creating a custom Controller