Creating a GUI Inventory - Historical

Applied By ItsMeGlare: Sep 8, 2020 at 7:02 PM

Creating a GUI Inventory
Here is a snippet for creating a GUI Inventory. I simply begin by creating an inventory with nine slots. Then, I initialize any items in the inventory, adding an example sword and helmet. For this method, I use another called "createGuiItem", which allows me to create an item easily with a name, description, and a material. I have also added a method to open the inventory, and then an event handler for handling what happens when items are clicked.

Many people want to create a system like this in their plugins, but they feel they have to ask in the development section first. With this, you can start simple, and add to it as you wish.

Code (Java):

public class ExampleGui implements Listener {
    private final Inventory inv;

    public ExampleGui() {
        // Create a new inventory, with no owner (as this isn't a real inventory), a size of nine, called example
        inv = Bukkit.createInventory(null, 9, "Example");

        // Put the items into the inventory
        initializeItems();
    }

    // You can call this whenever you want to put the items in
    public void initializeItems() {
        inv.addItem(createGuiItem(Material.DIAMOND_SWORD, "Example Sword", "§aFirst line of the lore", "§bSecond line of the lore"));
        inv.addItem(createGuiItem(Material.IRON_HELMET, "§bExample Helmet", "§aFirst line of the lore", "§bSecond line of the lore"));
    }

    // Nice little method to create a gui item with a custom name, and description
    protected ItemStack createGuiItem(final Material material, final String name, final String... lore) {
        final ItemStack item = new ItemStack(material, 1);
        final ItemMeta meta = item.getItemMeta();

        // Set the name of the item
        meta.setDisplayName(name);

        // Set the lore of the item
        meta.setLore(Arrays.asList(lore));

        item.setItemMeta(meta);

        return item;
    }

    // You can open the inventory with this
    public void openInventory(final HumanEntity ent) {
        ent.openInventory(inv);
    }

    // Check for clicks on items
    @EventHandler
    public void onInventoryClick(final InventoryClickEvent e) {
        if (e.getInventory() != inv) return;

        e.setCancelled(true);

        final ItemStack clickedItem = e.getCurrentItem();

        // verify current item is not null
        if (clickedItem == null || clickedItem.getType() == Material.AIR) return;

        final Player p = (Player) e.getWhoClicked();

        // Using slots click is a best option for your inventory click's
        p.sendMessage("You clicked at slot " + e.getRawSlot());
    }

    // Cancel dragging in our inventory
    @EventHandler
    public void onInventoryClick(final InventoryDragEvent e) {
        if (e.getInventory() == inv) {
          e.setCancelled(true);
        }
    }
}
 
Of course for more complex inventory GUIs you probably want to use a more abstracted way of handling elements and click.

You could also use one of the many libraries (or get inspired by them), here are some that are worth checking out: