Skip to content

Container screens and keys

These types add a mod’s controls to any container screen, vanilla’s or another mod’s, without a mixin of its own, and give the mod key mappings that work while a screen is open.

Type Summary Status Since Side Thread
ContainerClickCallback A mouse button pressed on a container screen, before vanilla handles it. Experimental 2.10.0 client render
ContainerKeyCallback A key pressed on a container screen, before vanilla handles it. Experimental 2.10.0 client render
ContainerScreenInitCallback Lays out a mod’s widgets on a container screen. Experimental 2.10.0 client render
ContainerScreens An open container screen’s menu image and hovered slot, and what its key events mean. Experimental 2.10.0 client render
KeyContext Where a key mapping applies. Experimental 2.10.0 client any
KeyDefault A key mapping’s default binding. Experimental 2.10.0 client any
KeyMappings Registers a mod’s key mappings. Experimental 2.10.0 client setup
MappedKey A key mapping from KeyMappings#register. Experimental 2.10.0 client render
ScreenWidgets Adds widgets to the screen a ContainerScreenInitCallback listener lays out. Experimental 2.10.0 client render
SlotOverlayCallback Draws over a container screen’s slots. Experimental 2.10.0 client render

With SORT_KEY a MappedKey and Sorter.sort the mod’s own sorting, in client initialization:

Sort the container under the mouse with a key
SORT_KEY = KeyMappings.register(id("sort"), id("main"), KeyContext.IN_SCREEN,
KeyDefault.key(GLFW.GLFW_KEY_R));
ContainerKeyCallback.EVENT.register((screen, hovered, keyCode, scanCode, modifiers) -> {
if (hovered == null || !SORT_KEY.matchesKey(keyCode, scanCode)) return false;
// R is a letter: while the player types a name or a search, it is theirs.
if (ContainerScreens.isTyping(screen)) return false;
Sorter.sort(screen.getMenu(), hovered);
return true;
});

ContainerScreenInitCallback and ScreenWidgets

Section titled “ContainerScreenInitCallback and ScreenWidgets”

ContainerScreenInitCallback: Experimental · Since 2.10.0 · Side: client · Thread: render

ContainerScreens: Experimental · Since 2.10.0 · Side: client · Thread: render

ScreenWidgets: Experimental · Since 2.10.0 · Side: client · Thread: render

ContainerScreenInitCallback lays out a mod’s widgets on every container screen, using ContainerScreens to place them beside the menu image. With SortButton the mod’s own button:

Put a sort button beside every container screen
ContainerScreenInitCallback.EVENT.register((screen, widgets) -> widgets.add(new SortButton(
ContainerScreens.left(screen) + ContainerScreens.imageWidth(screen) + 2,
ContainerScreens.top(screen), screen)));

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

A listener bound to a letter key takes that letter from an anvil’s name or a recipe viewer’s search box unless it lets typing through, and a toggle bound to any key flips again at each repeat unless it skips them. With LOCK_KEY a MappedKey and Locks.toggle the mod’s own, in client initialization:

Toggle a slot's lock once per press, and never while typing
ContainerKeyCallback.EVENT.register((screen, hovered, keyCode, scanCode, modifiers) -> {
if (hovered == null || !LOCK_KEY.matchesKey(keyCode, scanCode)) return false;
if (ContainerScreens.isTyping(screen)) return false;
// A held key repeats: take the repeats too, so the screen does not act on them.
if (!ContainerScreens.isKeyRepeat()) Locks.toggle(hovered);
return true;
});
  • ContainerScreens#isKeyRepeat answers for the event the listener is being called for, so call it inside the listener, not from a tick or a draw.
  • A listener that returns false for a repeat lets the screen and the other listeners act on it: return true for the repeats of a key the listener owns.
  • A viewer’s own shortcut on the same key (JEI, EMI, and REI all use R for a recipe) still runs when the listener returns false for ContainerScreens#isTyping.
  • Two of a mod’s listeners whose keys a player binds to one key, one of them with a modifier (R and Ctrl + R on Forge), both match Ctrl + R: the listener for the plain key checks the event’s modifiers.

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

SlotOverlayCallback draws over each slot as the screen draws it. With LOCKED an IntSet of the mod’s locked slot indexes, and MARK_Z a depth in front of the item and its count:

Mark locked slots in front of their items
SlotOverlayCallback.EVENT.register((screen, graphics, slot) -> {
if (LOCKED.contains(slot.index)) {
graphics.fill(slot.x, slot.y, slot.x + 16, slot.y + 16, MARK_Z, 0x40FF0000);
}
});

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

MappedKey: Experimental · Since 2.10.0 · Side: client · Thread: render

KeyMappings registers a mod’s key, which players rebind in Controls; the MappedKey it returns reads it in game and on screens. In game, poll it from a ClientTickCallback listener, as that section’s example does. For the quick start’s id("sort") in id("main"), the mod’s lang file holds:

assets/mymod/lang/en_us.json
{
"key.mymod.sort": "Sort container",
"key.categories.mymod.main": "My Mod"
}
  • Slot regions: SlotRegions, which finds the slots a sort button acts on.
  • Menus: MenuScreenBindings, a mod’s own menu screens.