BasicGUI

This is your run-of-the-mill, chest-like inventory GUI. Creating one is simple - you may either instantiate a BasicGUI object or extend the class, passing along the title and size of your GUI.

Option 1: Object

Quick and easy, this method is ideal for infrequent use of your GUI:

// Create the object with title and size
BasicGUI bg = new BasicGUI(new GUI("Fruit Fan", Rows.SIX));
// Show a cute fruit in the first slot
bg.setItem(0, new Item(Material.APPLE));
// Open the inventory for a Player
bg.openInventory(player);

If your resource supports versions of Minecraft both before and after 1.13, you'll need to be careful with Materials. Passing as a String instead ("APPLE") may give you better results with some items, but we recommend using a library like XMaterial.

Note that nothing special happens when your Player clicks the Item. To add a response for that slot, see the Response page.

Option 2: Extended Class

This method is preferred if you intend to reuse your GUI frequently and/or show it to multiple players at once:

public class MyCustomGUI extends BasicGUI {
    public MyCustomGUI() {
        super(new GUI("Fruit Fanatic"), Rows.SIX);
    
        // Show a cute fruit in the first slot
        setItem(0, new Item(Material.APPLE));
    }
}

// Call #openInventory from another class or listener

Note that nothing special happens when your Player clicks the Item. To add a response for that slot, see the Response page.

If your resource supports versions of Minecraft both before and after 1.13, you'll need to be careful with Materials. Passing as a String instead ("APPLE") may give you better results with some items, but we recommend using a library like XMaterial.

As a bonus, let's add a fun way to exit the inventory:

public class MyCustomGUI extends BasicGUI {
    Player player;
    
    public MyCustomGUI(Player player) {
        super(new GUI("Fruit Fanatic"), Rows.SIX);
        this.player = player;
    
        // Show a cute fruit in the first slot
        setItem(0, new Item(Material.APPLE));
    
        // Kick the player if they hate a balanced breakfast
        Item barrier = new Item.ItemBuilder(Material.BARRIER)
            .displayName("Dislike fruit? Click here!").build();
        String kickMsg = "And stay out.";
        setItem(new ItemSection(53, barrier,
            kickMsg, new KickPlayerResponder(player, kickMsg)));
    }


    // Optional, applies if Response is set (i.e. KickPlayerResponder)
    @Override
    public void onOpen(final InventoryOpenEvent e) {
        if (player.getName().equals("Notch") {
            e.setCancelled(true);
        }
    }
    @Override
    public void onClose(final InventoryCloseEvent e) {
        Bukkit.getServer().broadcastMessage(player.getName() 
            + " is having a nice morning.");
    }
}

// Call #openInventory from another class or listener

Last updated