Menu
Namespace Mc.Scripting.Inventories · Kind class (sealed)
public sealed class Menu : IDisposable, IInventory, IPluginResourceA custom GUI: a container with icons and click handlers that the client shows like a chest, a hopper or a dispenser. The menu is owned by the plugin and closed for every viewer when the plugin unloads or reloads.
Remarks
Section titled “Remarks”A menu is locked. The engine keeps it a display: a click that could take an icon out or put an item in is denied (and the whole menu resynced to the client) unless a handler allows it with MenuClick.Allow; drags over its slots, shift clicks, double clicks, number keys, the off hand key and the drop key cannot move items out of it or into it; the creative clone click gives no copy of an icon. Whatever a handler decides, the server is authoritative: it only performs what vanilla would perform for the click, and a cancelled click changes nothing.
Create menus with Menus.Chest (plugin.Menus) or the static factories, which belong to the plugin whose code is running (OnEnable, an event handler, a command, a scheduled task); items in the slots are ordinary stacks, so the menu doubles as a small shared inventory (Menu.Get, Menu.Set).
Properties
Section titled “Properties”| Member | Description |
|---|---|
IEnumerable<ItemStack> Contents { get; } |
|
bool IsDisposed { get; } |
Whether the menu was disposed (or its plugin unloaded). |
bool LockPlayerInventory { get; set; } |
Whether every click in the menu is denied unless allowed, also the ones that only touch the player’s own inventory (false by default: the player can rearrange his own slots). |
int Rows { get; } |
The number of rows (1 for a hopper, 3 for a dispenser). |
int Size { get; } |
The number of slots of the menu itself (the player’s own slots are not counted). |
Component Title { get; set; } |
The title shown above the slots; changing it re-opens the screen of the viewers with the same contents. |
MenuType Type { get; } |
The vanilla menu type. |
IReadOnlySet<Player> Viewers { get; } |
The players who have the menu open (a snapshot). |
Contents
Section titled “Contents”public IEnumerable<ItemStack> Contents { get; }IsDisposed
Section titled “IsDisposed”public bool IsDisposed { get; }Whether the menu was disposed (or its plugin unloaded).
LockPlayerInventory
Section titled “LockPlayerInventory”public bool LockPlayerInventory { get; set; }Whether every click in the menu is denied unless allowed, also the ones that only touch the player’s own inventory (false by default: the player can rearrange his own slots).
public int Rows { get; }The number of rows (1 for a hopper, 3 for a dispenser).
public int Size { get; }The number of slots of the menu itself (the player’s own slots are not counted).
public Component Title { get; set; }The title shown above the slots; changing it re-opens the screen of the viewers with the same contents.
public MenuType Type { get; }The vanilla menu type.
Viewers
Section titled “Viewers”public IReadOnlySet<Player> Viewers { get; }The players who have the menu open (a snapshot).
Methods
Section titled “Methods”| Member | Description |
|---|---|
int AddItem(ItemStack stack) |
|
void Clear() |
|
int Close() |
Closes the menu for every viewer. |
bool Close(Player player) |
Closes the menu for one viewer (his cursor stack goes back to him). |
void Dispose() |
Closes the menu for everybody and frees it (what unloading the plugin does). |
Menu FillEmpty(ItemStack icon) |
Sets the icon of every empty slot (a border or a background). |
ItemStack Get(int slot) |
|
ItemStack Icon(int slot) |
The icon the plugin set for a slot with {{cref:M:Mc.Scripting.Inventories.Menu.Slot(System.Int32,Mc.Scripting.Inventories.ItemStack,System.Action{Mc.Scripting.Inventories.MenuClick})}} (the stack in the slot now is Menu.Get). |
bool IsOpenFor(Player player) |
Whether a player has this menu open. |
Menu OnClick(Action<MenuClick> handler) |
Called for every click in the menu (the menu’s own slots and the player’s), before the handler of the slot. |
Menu OnClose(Action<Player> handler) |
Sets what happens when a player closes the menu (or it is closed for him). |
Menu OnClose(Action<Player, MenuCloseReason> handler) |
Sets what happens when a player closes the menu, with the reason. |
bool Open(Player player) |
Opens the menu for a player, closing whatever container he has open. |
void Refresh() |
Sends the changed slots to the viewers now. |
int RemoveItem(ItemStack stack) |
|
void Set(int slot, ItemStack stack) |
|
Menu Slot(int slot, ItemStack icon, Action<MenuClick>? onClick = null) |
Sets the icon and the click handler of a slot. |
static Menu Chest(int rows, Component title) |
Creates a menu of 9-wide rows for the plugin whose code is running. |
static Menu Dispenser(Component title) |
Creates a dispenser-style menu (3 x 3 slots) for the plugin whose code is running. |
static Menu Hopper(Component title) |
Creates a hopper menu (5 slots) for the plugin whose code is running. |
AddItem
Section titled “AddItem”public int AddItem(ItemStack stack)Parameters
| Name | Type | Description |
|---|---|---|
stack |
ItemStack |
Returns int
public void Clear()public int Close()Closes the menu for every viewer.
Returns int: How many players had it open.
public bool Close(Player player)Closes the menu for one viewer (his cursor stack goes back to him).
Parameters
| Name | Type | Description |
|---|---|---|
player |
Player |
The player. |
Returns bool: True when the player had the menu open.
Dispose
Section titled “Dispose”public void Dispose()Closes the menu for everybody and frees it (what unloading the plugin does).
FillEmpty
Section titled “FillEmpty”public Menu FillEmpty(ItemStack icon)Sets the icon of every empty slot (a border or a background).
Parameters
| Name | Type | Description |
|---|---|---|
icon |
ItemStack |
The icon. |
Returns Menu: This menu, for chaining.
public ItemStack Get(int slot)Parameters
| Name | Type | Description |
|---|---|---|
slot |
int |
Returns ItemStack
public ItemStack Icon(int slot)The icon the plugin set for a slot with {{cref:M:Mc.Scripting.Inventories.Menu.Slot(System.Int32,Mc.Scripting.Inventories.ItemStack,System.Action{Mc.Scripting.Inventories.MenuClick})}} (the stack in the slot now is Menu.Get).
Parameters
| Name | Type | Description |
|---|---|---|
slot |
int |
The slot. |
Returns ItemStack: The stack it was given.
IsOpenFor
Section titled “IsOpenFor”public bool IsOpenFor(Player player)Whether a player has this menu open.
Parameters
| Name | Type | Description |
|---|---|---|
player |
Player |
The player. |
Returns bool: True when he does.
OnClick
Section titled “OnClick”public Menu OnClick(Action<MenuClick> handler)Called for every click in the menu (the menu’s own slots and the player’s), before the handler of the slot.
Parameters
| Name | Type | Description |
|---|---|---|
handler |
Action<MenuClick> |
The handler. |
Returns Menu: This menu, for chaining.
OnClose
Section titled “OnClose”public Menu OnClose(Action<Player> handler)Sets what happens when a player closes the menu (or it is closed for him).
Parameters
| Name | Type | Description |
|---|---|---|
handler |
Action<Player> |
The handler (the cursor stack has already been given back). |
Returns Menu: This menu, for chaining.
OnClose
Section titled “OnClose”public Menu OnClose(Action<Player, MenuCloseReason> handler)Sets what happens when a player closes the menu, with the reason.
Parameters
| Name | Type | Description |
|---|---|---|
handler |
Action<Player, MenuCloseReason> |
The handler. |
Returns Menu: This menu, for chaining.
public bool Open(Player player)Opens the menu for a player, closing whatever container he has open. InventoryOpenEvent (kind Custom) fires first and can cancel it.
Parameters
| Name | Type | Description |
|---|---|---|
player |
Player |
The player. |
Returns bool: True when the menu is open, false when a handler cancelled it or the player is offline.
Refresh
Section titled “Refresh”public void Refresh()Sends the changed slots to the viewers now. (They are synced every tick anyway; this is for code that needs the packets within the current tick.)
RemoveItem
Section titled “RemoveItem”public int RemoveItem(ItemStack stack)Parameters
| Name | Type | Description |
|---|---|---|
stack |
ItemStack |
Returns int
public void Set(int slot, ItemStack stack)Parameters
| Name | Type | Description |
|---|---|---|
slot |
int |
|
stack |
ItemStack |
public Menu Slot(int slot, ItemStack icon, Action<MenuClick>? onClick = null)Sets the icon and the click handler of a slot.
Parameters
| Name | Type | Description |
|---|---|---|
slot |
int |
The slot (0 to Menu.Size - 1). |
icon |
ItemStack |
What the slot shows (an ordinary stack; the empty stack clears it). |
onClick |
Action<MenuClick>? = null |
Called when a player clicks the slot (the click is denied unless the handler allows it). |
Returns Menu: This menu, for chaining.
public static Menu Chest(int rows, Component title)Creates a menu of 9-wide rows for the plugin whose code is running.
Parameters
| Name | Type | Description |
|---|---|---|
rows |
int |
1 to 6 rows. |
title |
Component |
The title shown above the slots. |
Returns Menu: The menu.
Throws
System.InvalidOperationException: No plugin code is running (useplugin.Menus.Chestthere).
Dispenser
Section titled “Dispenser”public static Menu Dispenser(Component title)Creates a dispenser-style menu (3 x 3 slots) for the plugin whose code is running.
Parameters
| Name | Type | Description |
|---|---|---|
title |
Component |
The title. |
Returns Menu: The menu.
Hopper
Section titled “Hopper”public static Menu Hopper(Component title)Creates a hopper menu (5 slots) for the plugin whose code is running.
Parameters
| Name | Type | Description |
|---|---|---|
title |
Component |
The title. |
Returns Menu: The menu.
See also
Section titled “See also”Component · IInventory · IPluginResource · ItemStack · MenuClick · MenuCloseReason · MenuType · Player