Skip to content

API reference Obsidian.API

MenuContainer class

A plugin's menu (see MenuDefinition), opened with IPlayer.OpenInventoryAsync, like a NeoForge mod's AbstractContainerMenu: its slots, then the player's main inventory and hotbar, numbered in that order. Players move items in and out of it like a chest's; ContainerClickEventArgs lets the plugin cancel or handle their clicks.

public class MenuContainer : BaseContainer

In Obsidian.API.

Remarks

By default the menu keeps its slots' items itself, without rules. MenuContainer.Slots keeps them elsewhere instead, in containers or behind item access, each slot with its own rules. Subclass it to decide where shift-clicks move stacks (MenuContainer.QuickMove), to handle the buttons of a mod's screen (MenuContainer.ClickButton), to keep it open only while something holds (MenuContainer.StillValid), to compute its MenuContainer.DataSlots (MenuContainer.GetData), to send modded clients extra data as it opens (MenuContainer.WriteOpenData) and to hear that it closed (MenuContainer.Closed). These run inside calls into the plugin that registered the menu type, never once it's unloading, except MenuContainer.Closed, which its unloading waits for.

Several players can have the same container open: their clicks take turns, and each sees the others' changes. It closes for its viewers, with ContainerClosedEventArgs, when the plugin that registered its menu type unloads; from then until a plugin registers the type again, opening one of its menus does nothing. A menu with a MenuContainer.BlockEntity closes when the block entity is removed or the player moves out of its reach.

Constructors

MenuContainer(MenuDefinition, ChatMessage)

public MenuContainer(MenuDefinition menu, ChatMessage title);

Creates a container of menu's type, which keeps its slots' items itself and starts empty.

Parameters

menu
The menu type.
title
The title the menu's screen shows.

Properties

BlockEntity

public BlockEntity BlockEntity { get; init; }

The block entity the menu belongs to, like a machine's: the menu closes when it's removed (its block broken or replaced, its chunk unloaded) and, like vanilla's stillValid, when the player is no longer within their block reach plus 4 blocks of its block. Null for a menu of nothing in the world.

DataSlots

public int DataSlots { get; init; }

How many data slots the menu has, like vanilla's ContainerData: short values, like a furnace's progress, that the server sends the players who have it open as they change (see MenuContainer.GetData).

public MenuDefinition Menu { get; }

The menu type.

public override long Revision { get; }

Slots

public IReadOnlyList<MenuSlot> Slots { get; init; }

Where each slot's item is kept and the rules players' clicks follow in it, one for each of the type's slots, in order. By default the menu keeps them all itself, without rules.

Methods

ClickButton(IPlayer, int)

protected internal virtual bool ClickButton(IPlayer player, int button);

Handles a button of the menu's screen that the player clicked, like vanilla's clickMenuButton: the screen sends the button's number, which means whatever the menu says.

Parameters

player
The player clicking.
button
The button's number, which the client chose.

Returns

Whether it changed the menu's slots, so that its viewers are sent them again.

Remarks

Called with the window's lock held, like a click, so that it may change the slots but must not wait on anything; only while the menu is still valid for the player and they aren't a spectator.

Closed(IPlayer)

protected internal virtual void Closed(IPlayer player);

Runs once each time the menu stops being open for player, like vanilla's removed: for instance to give back what its crafting slots held, or to stop a machine's preview. Every way a menu closes counts: the player closes it, another window opens over it, it's no longer valid (see MenuContainer.StillValid and MenuContainer.BlockEntity), the player dies, changes level or leaves, or the plugin that registered its menu type unloads. Nothing by default.

Remarks

It runs after the window closed (after ContainerClosedEventArgs, when the close raises one), without the server's locks, inside a call into the plugin the menu was made by, even while that plugin unloads, which waits for it. A menu of a plugin that finished unloading first, which only a leaving player's can be, isn't called. One that throws is logged.

GetData(int)

public virtual short GetData(int index);

A data slot's value: by default the last one MenuContainer.SetData set, or 0. Override it to read the value from what it shows, like a machine's progress; the server reads it once a tick for each player who has the menu open and sends it when it changed.

Parameters

index
The data slot, from 0 to MenuContainer.DataSlots - 1.
public override ItemStack GetItem(int slot);

QuickMove(IPlayer, int, ItemStack)

protected internal virtual IReadOnlyList<MenuSlotRange> QuickMove(IPlayer player, int slot, ItemStack stack);

Where a shift-click moves stack from slot, like vanilla's quickMoveStack: ranges of the window's slots, tried in turn until one takes some of it. Each tops up stacks of the same item first, then fills its first empty slot that takes the item, following the slots' rules. By default, like a chest: the menu's stacks go to the player's slots, filling from the hotbar's last slot, and the player's go to the menu's slots, filling from the first.

Parameters

player
The player shift-clicking.
slot
The clicked slot, as the window numbers it: the menu's slots, then the player's (see MenuContainer.PlayerSlotCount).
stack
What the slot holds. Don't change it.

Remarks

Called with the window's lock held, so that it must not wait on anything. The click repeats it while the stack moves and the slot still holds the same item. A range that includes slot skips it.

QuickMoveIn(IPlayer, ItemStack)

protected internal virtual int QuickMoveIn(IPlayer player, ItemStack stack);

Takes a stack the player shift-clicks in their inventory somewhere other than the menu's slots, before MenuContainer.QuickMove's ranges are tried: like an AE2 terminal's quickMoveStack, which stores it in its network. Nothing by default.

Parameters

player
The player shift-clicking.
stack
What the clicked slot holds. Don't change it.

Returns

How many of the stack it took, which leave the player's slot; 0 to leave it to the ranges.

Remarks

Called with the window's lock held, like MenuContainer.QuickMove, and only for the player's own slots. It may take a container's lock whose holders take no other, as a block entity's inventory lock is held, but must not wait on anything else.

public override void Resize(int newSize);

SetData(int, short)

public void SetData(int index, short value);

Sets a data slot's value, which the players who have the menu open are sent on their next tick.

Parameters

index
The data slot, from 0 to MenuContainer.DataSlots - 1.
value
The value.

SetItem(int, ItemStack)

public override void SetItem(int slot, ItemStack item);

Remarks

A slot kept behind item access changes only in a click, which moves its items through the access; set it through the access instead.

StillValid(IPlayer)

protected internal virtual bool StillValid(IPlayer player);

Whether the menu stays open for player, checked every tick and on every click, after its MenuContainer.BlockEntity's checks. True by default.

WriteOpenData(IPlayer, INetStreamWriter)

protected internal virtual void WriteOpenData(IPlayer player, INetStreamWriter writer);

Writes extra data modded clients get as the menu opens for player, like a NeoForge mod's openMenu with a buffer: NeoForge clients, and the Obsidian client, read it to build their side of the menu. Other clients get the menu without it. Nothing by default.

Fields

PlayerSlotCount

public const int PlayerSlotCount = 36;

How many of the player's slots follow the menu's own in its window: the main inventory's 27, then the hotbar's 9, like vanilla's menus.

On this page