Skip to content

InventoryGUI

boxbeam edited this page Jul 31, 2024 · 6 revisions

Creating a GUI in an inventory can be a pain in Spigot. InventoryGUI helps make it considerably easier to create interactive GUIs with a variety of functionality.

The basic premise of a GUI is almost always the same in Minecraft: You have an inventory, and you want to have buttons that do various things. However, you have other items in the inventory that you don't want the player to be able to interact with, and you may sometimes have slots you want the player to be able to access. This is where InventoryGUI shines.

To create an InventoryGUI, all you have to do is call its constructor, passing an Inventory to it:

InventoryGUI gui = new InventoryGUI(Bukkit.createInventory(null, 27, "Name"));

Without needing any calls made to it, this InventoryGUI will now prevent players from modifying its contents in any way. If you want to add a button, you can easily do that. All you have to do is instantiate an ItemButton:

ItemButton button = ItemButton.create(new ItemBuilder(Material.EMERALD_BLOCK)
	.setName("Click me!"), e -> {
	e.getPlayer().sendMessage("You clicked me!");
});
gui.addButton(button, 13);

Now, this button will be added to slot 13 in the inventory, and whenever a player clicks it, they will be sent the message "You clicked me!"

InventoryGUI also has some other helpful methods, like fill, which will let you fill a section of slots in an inventory with the same item. Additionally, it will destroy itself and unregister all listeners when all viewers have closed it (though this behavior can be disabled by calling setDestroyOnClose(false)).

If you want players to be able to access certain slots of the inventory to place and remove items, you can open slots in the inventory with gui.openSlot(slotnumberhere); This will allow items to be placed and removed from that slot, and it will smartly correct shift-clicking to only fill slots that have been opened in the InventoryGUI. When the GUI is destroyed, all items in open slots will be returned to the last player to have viewed the inventory (though this behavior can be disabled by calling setReturnItems(false)).

You can also add a listener for when an open slot is clicked with setOnClickOpenSlot, or manually destroy the InventoryGUI with destroy().

InventoryGUI is fairly basic and minimal, but covers almost every common use case easily.

One thing slightly outside of the use case of the base InventoryGUI class, however, is paginated menus. You may want to have a list of non-interactive items, or a list of interactive buttons that can be placed into a menu. PaginationPanel makes this easy, allowing you to define a section of the GUI to be used to display elements. If you want to display a list of items, you could do it like this:

InventoryGUI gui = new InventoryGUI(Bukkit.createInventory(null, 27, "Items"));
PaginationPanel pages = new PaginationPanel(gui);
pages.addSlots(0, 18); // Add the first two rows of the inventory to the paged panel.
// You can also use addSlots(int x1, int y1, int x2, int y2) to define a rectangular area.
pages.addPagedItems(myItemList); // Bring your own myItemList
// Add buttons to navigate between pages
ItemButton next = ItemButton.create(new ItemBuilder(Material.EMERALD_BLOCK).setName("Next Page"), e -> pages.nextPage());
ItemButton prev = ItemButton.create(new ItemBuilder(Material.EMERALD_BLOCK).setName("Previous page"), e -> pages.prevPage());
gui.addButton(next, 18);
gui.addButton(prev, 26);
Clone this wiki locally