Mod screens
Implement ModScreen to build a screen with labels, buttons, and input fields.
ShowcaseScreen.java
final class ShowcaseScreen implements ModScreen {
private TextInput name;
@Override
public String title() {
return "My Menu";
}
@Override
public void init(ScreenBuilder screen) {
int centre = screen.width() / 2;
screen.label("Hello", centre - screen.textWidth("Hello") / 2, 40, 0xFFFFFFFF);
name = screen.textInput(centre - 100, 70, 200, "Steve");
screen.button("OK", centre - 50, 100, 100, 20, screen::close);
}
@Override
public void render(HudCanvas canvas) {
// Draw freely on top of the widgets (optional)
}
@Override
public void onClosed() {
LOGGER.info("name was {}", name.value());
}
}
Open and close
Open a screen from a callback that has a ClientView (for example, ClientEvents.TICK).
mod.events().register(ClientEvents.TICK, view -> {
if (open.consumeClick()) {
view.openScreen(new ShowcaseScreen());
}
});
Behavior
- A screen runs on top of the game's
Screen, and the buttons and input fields are game widgets. ScreenBuilder:width(),height(),textWidth,label,button(label, x, y, width, height, onPress),textInput(x, y, width, initialValue),close()- You can draw freely with
render(HudCanvas). - Override
pausesGame()to decide whether the game pauses in single player. onClosedis called on Esc, oncloseScreen, and when the screen changes to another screen.
A block entity's inventory menu is not a screen but a menu that the server opens. See Block entities and menus.