Skip to content

MenuBuilder

virtualWinter edited this page Aug 30, 2025 · 2 revisions

MenuBuilder

The MenuBuilder is a fluent builder that simplifies the creation of paginated menus. It provides a chainable interface for setting all the properties of a menu, from its title and size to its items and actions.

Basic Usage

The most common way to use the builder is to create a new instance, configure it, and then build and open it for a player.

MenuBuilder.create("My Awesome Menu", 54)
    .items(myItemList)
    .buildAndOpen(player);

Key Methods

  • .title(String title): Sets the title of the menu inventory.
  • .size(int size): Sets the size of the inventory (must be a multiple of 9).
  • .items(List<ItemStack> items): Sets the list of items to be displayed.
  • .addItem(ItemStack item): Adds a single item to the menu.
  • .addItem(ItemStack item, Consumer<InventoryClickEvent> action): Adds an item with a specific click action.
  • .onOpen(Consumer<Player> onOpen): Sets a callback to be executed when the menu is opened.
  • .onClose(Consumer<Player> onClose): Sets a callback to be executed when the menu is closed.
  • .build(): Builds the PaginatedMenu instance.
  • .buildAndOpen(Player player): Builds the menu and immediately opens it for a player.

Pre-configured Builders

The MenuBuilder also provides static factory methods for common menu types:

  • MenuBuilder.itemList(String title, List<ItemStack> items): Creates a simple menu to display a list of items.
  • MenuBuilder.confirmation(String title, Consumer<Player> confirmAction, Consumer<Player> cancelAction): Creates a confirmation dialog with "Confirm" and "Cancel" buttons.

Example

import club.catmc.utils.menu.MenuBuilder;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import java.util.ArrayList;
import java.util.List;

public class MenuExamples {

    public void openSimpleMenu(Player player) {
        List<ItemStack> items = new ArrayList<>();
        for (int i = 1; i <= 50; i++) {
            items.add(new ItemStack(Material.DIAMOND, i));
        }

        MenuBuilder.create("My Awesome Menu", 54)
            .items(items)
            .onOpen(p -> p.sendMessage("Welcome to the menu!"))
            .onClose(p -> p.sendMessage("You closed the menu."))
            .buildAndOpen(player);
    }
}

Clone this wiki locally