Container Menus
PineLib provides a simple way to create container menus that can be used for various purposes, such as custom inventories, shops, or any other GUI-based interactions.
There are many different types of container menus available in PineLib, each with its own unique features and use cases:
| Class | Description |
|---|---|
ChestMenu | A chest inventory with 1-6 rows |
CrafterMenu | A crafter-type inventory |
DropperMenu | A dropper-type inventory |
ContainerMenu | Generic base class for any Bukkit Inventory |
AnvilMenu | An anvil-based text input menu |
MenuItem
Section titled “MenuItem”A MenuItem wraps a ItemStack with an optional click handler.
Obtaining a MenuItem
Section titled “Obtaining a MenuItem”// From a Material or ItemStack, with a click actionMenuItem.of(Material material, Consumer<InventoryClickEvent> clickAction)MenuItem.of(ItemStack item, Consumer<InventoryClickEvent> clickAction)
// From a Material or ItemStack, no click actionMenuItem.of(Material material)MenuItem.of(ItemStack item)
// Direct constructornew MenuItem(ItemStack item, Consumer<InventoryClickEvent> clickAction)Methods
Section titled “Methods”| Method | Description |
|---|---|
getItem() | Returns the underlying ItemStack |
setItem(ItemStack) | Replaces the underlying ItemStack |
getClickAction() | Returns the click action consumer |
setClickAction(Consumer<InventoryClickEvent>) | Replaces the click action |
ContainerMenu
Section titled “ContainerMenu”The base class for chest, crafter, and dropper menus. Can also be constructed directly with any Bukkit Inventory.
new ContainerMenu(Inventory inventory)Methods
Section titled “Methods”add(MenuItem menuItem, boolean strict)
Section titled “add(MenuItem menuItem, boolean strict)”Adds a MenuItem to the first available empty slot.
- If
strictistrue, throwsIllegalStateExceptionwhen no empty slot is found. - If
strictisfalse, the item is silently dropped if the inventory is full.
set(int slot, MenuItem menuItem)
Section titled “set(int slot, MenuItem menuItem)”Places a MenuItem at the given 0-based slot index, replacing any existing item.
get(int slot)
Section titled “get(int slot)”Returns the MenuItem at the given slot, or null if no item is set there.
fill(MenuItem menuItem)
Section titled “fill(MenuItem menuItem)”Fills all empty slots with the given MenuItem.
fill(MenuItem menuItem, boolean override)
Section titled “fill(MenuItem menuItem, boolean override)”Fills all slots with the given MenuItem. If override is true, existing items are replaced.
fill(MenuItem menuItem, boolean override, int start)
Section titled “fill(MenuItem menuItem, boolean override, int start)”Fills slots from start (inclusive) to the end of the inventory.
fill(MenuItem menuItem, boolean override, int start, int end)
Section titled “fill(MenuItem menuItem, boolean override, int start, int end)”Fills slots from start to end (both inclusive).
show(Player player)
Section titled “show(Player player)”Opens the menu for the specified player and registers per-player click/close listeners. The player must be online.
onClose(Consumer<InventoryCloseEvent> closeAction)
Section titled “onClose(Consumer<InventoryCloseEvent> closeAction)”Registers an action that fires whenever any player closes this inventory.
cleanup()
Section titled “cleanup()”Unregisters all event listeners. Should be called when the plugin is disabled or the menu is no longer needed.
ChestMenu
Section titled “ChestMenu”Extends ContainerMenu. Creates a chest inventory with a configurable number of rows.
new ChestMenu(Component title, int rows)rowsmust be between 1 and 6 (inclusive), otherwise anIllegalArgumentExceptionis thrown.
Example:
ChestMenu menu = new ChestMenu(Component.text("My Shop"), 3);menu.set(13, MenuItem.of(Material.DIAMOND, event -> player.sendMessage("Clicked!")));menu.show(player);CrafterMenu
Section titled “CrafterMenu”Extends ContainerMenu. Creates a crafter-type inventory.
new CrafterMenu(Component title)DropperMenu
Section titled “DropperMenu”Extends ContainerMenu. Creates a dropper-type inventory.
new DropperMenu(Component title)AnvilMenu
Section titled “AnvilMenu”An anvil-based menu that captures the text a player types into the rename field and passes the resulting ItemStack to a callback.
Constructor
Section titled “Constructor”new AnvilMenu(Component title, ItemStack startingItem, boolean autoClose, Consumer<ItemStack> clickAction)| Parameter | Description |
|---|---|
title | The title shown at the top of the anvil |
startingItem | The item placed in the first anvil slot |
autoClose | If true, the inventory closes automatically when the output slot is clicked |
clickAction | Called with the (optionally renamed) result ItemStack when the player clicks the output slot |
Methods
Section titled “Methods”| Method | Description |
|---|---|
show(Player player) | Opens the anvil menu for the specified player |
getMenuTitle() / setMenuTitle(Component) | Get or set the title |
getStartingItem() / setStartingItem(ItemStack) | Get or set the item in the first slot |
getResultItem() / setResultItem(ItemStack) | Get or set the item shown in the output slot |
setClickAction(Consumer<ItemStack>) | Replace the click callback |
cleanup() | Unregisters all listeners and ProtocolLib adapters |
Example:
AnvilMenu menu = new AnvilMenu( Component.text("Enter a name"), new ItemStack(Material.PAPER), true, result -> player.sendMessage("You typed: " + result.getItemMeta().getDisplayName()));menu.show(player);