Introduction
This is a tutorial on how to make multi-paged inventory GUIs if you couldn't tell from the title. This tutorial will also teach you how to cycle through inventories like a boss!
Steps
This is a tutorial on how to make multi-paged inventory GUIs if you couldn't tell from the title. This tutorial will also teach you how to cycle through inventories like a boss!
Steps
- We need to create an array list to store our inventories. Note that array lists keep their order so it makes it perfect for a multi-paged menu/GUI.
Code:List<Inventory> inventories = new ArrayList<>();
- The next step is to figure out how many slots you want your inventories to have. Note, the size of the inventories must be a multiple of 9 (1 row) and no more than 54. Store the number in a variable called invSize or whatever you wish to name it.
Code:int invSize = 18;
- Now that we know what size our inventories need to be, we now need to know how many inventories to create. If the number of items that are going into the menu can change you need to create a flexible amount of inventories, otherwise you may run out of inventory space. Let's say we need to add player heads to the inventories for a bounty plugin and you have an array list of item stacks(player heads). To get the number of inventories create a new variable like so:
Code:int neededInventories = ((int) Math.ceil(heads.size() / invSize));
- Now comes the fun part, creating the inventories! To create the inventories just create a simple loop:
Code:for (int i = 0; i < neededInventories; i++) { Inventory inv = Bukkit.createInventory(null, invSize, "title"); inventories.add(inv); }
- Great! Now we need to add navigation to switch between pages. Yes, you could have done it in the previous step but I am just going to do it in a separate step so the tutorial isn't too clustered. We will loop through each inventory and add 3 buttons(previous page, close menu, and next page).
Code:for (Inventory inv : inventories) { ItemStack previous = new ItemStack(Material.SKULL_ITEM, 1); ItemMeta previousMeta = previous.getItemMeta(); previousMeta.setDisplayName(ChatColor.GOLD + "<< Previous Page"); previous.setItemMeta(previousMeta); ItemStack close = new ItemStack(Material.BARRIER, 1); ItemMeta closeMeta = close.getItemMeta(); closeMeta.setDisplayName(ChatColor.RED + "Close Menu"); close.setItemMeta(closeMeta); ItemStack next = new ItemStack(Material.SKULL_ITEM, 1); ItemMeta nextMeta = next.getItemMeta(); nextMeta.setDisplayName(ChatColor.GOLD + "Next Page >>"); next.setItemMeta(nextMeta); inv.setItem(9, previous); inv.setItem(13, close); inv.setItem(17, next); }
- Now that we have the navigation in our pages we need a way to open the menu. For this, we need to have a player variable. Once we have a player variable all we need to do is call the open inventory method.
Code:player.openInventory(inventories.get(0));
- Create a listener to handle an event called InventoryClickEvent. If the item they clicked is the next button call the close inventory method on the player and then call the open inventory and pass in inventories.get(inventories.indexOf(clicked inv) + 1). Do the same thing for the previous button except subtract 1 instead of adding. Also, something very important to keep in mind is that eventually someone may click the next/previous button while they are on the last/first page. This will cause an error. Make sure you the player is not on the last/first page.
Last edited: