Skip to content

v3 KamiMenu

Jake Moore edited this page Dec 21, 2024 · 2 revisions

❗ This Documentation is Outdated! It is recommended to use v4 with its documentation instead.

See V4 Getting Started for more information.

⚠️ Usage ⚠️

The gui/menu feature is only available in spigot-jar.

KamiMenu is the KamiCommon solution to menus.
It provides a simple way to create menus and listen to click callbacks.
This feature is only available in the spigot-jar module of KamiCommon.

KamiMenu Creation

This is an example, there are a lot of additional class constructors for additional uses.

KamiMenu menu1 = new KamiMenu("&cMenu Title {name}", 6); // (title, rows)
KamiMenu menu2 = new KamiMenu("&cMenu Title", InventoryType.HOPPER); // (title, type)

Item Management

// Set Item Directly (no click callback)
menu.setItem(0, new ItemStack(Material.DIAMOND));

// Set item as `MenuItem` with methods for setting the click callback
menu.addMenuItem(new ItemStack(Material.STONE), 1).setMenuClick((plr, clickType) -> {
    plr.sendMessage("You clicked the stone using " + clickType.name());
});

// You can also lazily set the click callback if you use item IDs
menu.addMenuItem("MyItemId", new ItemStack(Material.EMERALD), 2);
menu.setMenuClick("MyItemId", (p, c) -> {
    p.sendMessage("You clicked the emerald: " + c.name());
});

Filler Items

A default GRAY_STAINED_GLASS_PANE filler item (with no name) is configured to fill all empty slots prior to opening the gui.

To disable this filler:

// Option 1: Set the filler item to null
menu.setFillerItem(null);

// Option 2: Leave the filler, but disable it
Optional.ofNullable(menu.getFillerItem()).ifPresent(item -> item.setEnabled(false));

To modify the filler item

// Must provide a MenuItem, which wraps the item and an enabled boolean
menu.setFillerItem(new MenuItem(true, new ItemBuilder(XMaterial.BLACK_STAINED_GLASS_PANE)));

Menu State Callbacks

// Called immediately after the menu is opened for the player
menu.addOpenCallback((player, inventoryView) -> {
    // your code here
});

// Called prior to any inventory click logic (prior to callbacks)
// Return false to prevent calling a click callback
menu.addClickPredicate((predicate) -> {
    return true;
});

// Called during the InventoryCloseEvent
menu.addCloseConsumer((player) -> {
    // your code here
});

Player Inventory Clicks

Player slot callbacks are triggered when the player clicks a slot in their own inventory.
They are guaranteed to provide a valid player inventory slot number.
Individual slot callbacks can be registered, or a general callback for any slot.

// Register a callback for all slots (see parameter s for slot number)
menu.onPlayerSlotClick((plr, s) -> {
    // player clicks their own inventory with slot: s
});

// Register a callback for a specific slot
menu.onPlayerSlotClick(0, (plr, s) -> {
    // for this method s would be 0, since we only listen to slot 0
});

Opening the Menu

// Open the menu for a player
menu.openMenu(player);