Toggle
Toggle is an on/off setting drawn as a vanilla button that reads ON or OFF after its label, like the toggles on the game's options screens.
An on/off setting, drawn as a vanilla button reading "label: ON" or "label: OFF", like the toggles on vanilla's options screens. A click flips it.
Toggle.builder("particles")
.label(Text.translatable("mymod.particles", "Particles"))
.value(() -> config.particles)
.onChange(on -> config.particles = on)
.build();
Builder#
| Method | Required | Default | Notes |
|---|---|---|---|
label(Supplier<Text>) / label(Text) |
yes | the text before ": ON/OFF" | |
value(BooleanSupplier) |
yes | reads your current value; re-read on every refresh | |
onChange(Consumer<Boolean>) |
yes | stores the new value; main thread | |
enabled, tooltip, visible |
no | options every widget has |
Behaviour#
- A click calls
onChangewith the opposite ofvalue. The toggle shows the new value only ifvaluereturns it afterwards (State and refresh). - "ON" and "OFF" are vanilla's
options.onandoptions.off, so they're translated in every language. - For a list of independent choices, use a Checkbox: the same value, drawn as a check box.
- Footer: yes.
Test it#
host.click("particles"); // flips it
Rendered as [toggle particles] Particles: ON.
See also#
- Javadoc: Toggle