StringListEditor

StringListEditor edits a list of unique strings with a list, an input field and Add and Remove buttons - server addresses, item ids, player names.

Last updated

On this page

Edits a list of unique strings, such as server addresses, item ids or ignored player names: a list of the items, a text field for a new item, and Add and Remove buttons.

StringListEditor ignored = StringListEditor.builder("ignored")
        .items(() -> config.ignoredPlayers)               // re-read on every refresh
        .onAdd(name -> config.ignoredPlayers.add(name))
        .onRemove(name -> config.ignoredPlayers.remove(name))
        .inputLabel(Text.translatable("mymod.ignored.add", "Player name"))
        .validator(s -> s.contains(" ") ? Text.translatable("mymod.ignored.space", "No spaces") : null)
        .maxLength(16)
        .build();

MenuScreen.builder("main").title(title).add(ignored).build();   // or Section.Builder.add(ignored)

Builder#

Method Required Default Notes
items(Supplier<List<String>>) yes the current items in display order; re-read on every refresh
onAdd(Consumer<String>) yes adds an item to your list when Add is pressed; gets only non-empty, valid text that isn't in the list yet
onRemove(Consumer<String>) yes removes an item from your list when Remove is pressed
inputLabel(Text) yes the label above the text field, e.g. "Server address"
validator(Function<String, Text>) no none an extra check for a new item; empty input and duplicates are handled already
addLabel(Text) no UiTexts.ADD ("Add")
removeLabel(Text) no UiTexts.REMOVE ("Remove")
maxLength(int) no 256 the most characters of a new item

It has no enabled, tooltip or visible of its own: it isn't a single entry.

What it is made of#

The editor expands into four ordinary entries, so every backend can draw it:

Entry Id Enabled
a SelectList of the items, as literal text <id>.list always
a TextInput for a new item <id>.input always
the Add Button <id>.add while the field holds non-empty, valid text not in the list ("Already in the list" otherwise)
the Remove Button <id>.remove while a row is selected

Pressing Add passes the text to onAdd and empties the field. Pressing Remove passes the selected item to onRemove and clears the selection. Keep the four ids free on your screen.

Behaviour#

Test it#

host.type("ignored.input", "Steve").click("ignored.add");
host.select("ignored.list", "Steve").click("ignored.remove");

See also#