Skip to content

Menus

These types give a mod’s block a menu of its own, such as a machine’s, opened with the data the client needs to draw it and the numbers it shows. They replace Fabric’s and Forge’s own extended menu types, opening calls, and screen registration, which differ between the loaders, and vanilla’s data slots for values past their 16 bits.

Type Summary Status Since Side Thread
IntValue A synced whole-number value, from MenuValues#addInt. Experimental 2.10.0 both any
LongValue A synced long value, from MenuValues#addLong. Experimental 2.10.0 both any
MenuFactory Builds a menu type’s client menu from the extra data the server sent. Experimental 2.10.0 client render
Menus Opens menus whose type came from us.someguy.stope.api.registry.Registrar#menuType. Experimental 2.10.0 server server
MenuScreenBindings The client screens of a mod’s menu types. Experimental 2.10.0 client setup
MenuValues A menu’s synced values. Experimental 2.10.0 both any

MachineMenu is the mod’s menu, with a constructor that takes the type, the container ID, and the machine’s position. With REGISTRAR, the mod’s Registrar, in the mod’s ModMenus class, which its initializer loads:

Register a menu type that reads the machine's position
static final Registered<MenuType<MachineMenu>> MACHINE = REGISTRAR.menuType("machine",
(containerId, inventory, extra) ->
new MachineMenu(ModMenus.MACHINE.get(), containerId, extra.readBlockPos()));

Experimental · Since 2.10.0 · Side: server · Thread: server

Menus opens a menu for a player and sends the client what its screen needs. In the block’s use method, with level, pos, and player from its arguments:

Open the machine's menu when a player uses its block
if (!level.isClientSide() && player instanceof ServerPlayer serverPlayer) {
Menus.open(serverPlayer, new SimpleMenuProvider(
(containerId, inventory, p) -> new MachineMenu(ModMenus.MACHINE.get(), containerId, pos),
Component.translatable("container.mymod.machine")),
extra -> extra.writeBlockPos(pos));
}
return InteractionResult.sidedSuccess(level.isClientSide());
  • The server’s menu comes from the MenuProvider and the client’s from the type’s MenuFactory, so both build the same slots in the same order, or the client’s clicks reach the wrong slots.

Experimental · Since 2.10.0 · Side: both · Thread: any

MenuValues sends a menu’s numbers to its screen whole. In MachineMenu’s constructor, with machine, the machine’s block entity (null in the client’s menu, which has none), and energy, a LongValue field the screen reads:

Show a machine's stored energy, a long, on its screen
this.energy = MenuValues.of(this).addLong(machine == null ? () -> 0 : machine::energy);

Experimental · Since 2.10.0 · Side: client · Thread: setup

MenuScreenBindings tells the client which screen a menu type opens. With MachineScreen, the mod’s screen, whose constructor takes the menu, the inventory, and the title, in client initialization:

Show the machine's screen for its menus
MenuScreenBindings.bind(ModMenus.MACHINE, MachineScreen::new);
  • On Forge the screen is bound in client setup, after the mod’s constructor has called bind, so a menu opened before then has no screen; no menu opens that early in a normal game.