Skip to content

Creating an inventory

Dalton edited this page Apr 26, 2022 · 11 revisions

Creating an Inventory is rather simple, you take a PluginInventory class and initialize it like so:

Javadocs for this class

PluginInventory inventory = new PluginInventory(9, "&a&lInventory");

You may notice there is 2 arguments, the 1st argument is for the inventory slot count, which goes by increments of 9 up to a maximum of 54. The second argument is what the inventory will be named.

Changing Inventory properties after creating the PluginInventory instance.

This is rather simple, it may be achieved like so:

DO NOTE!! You cannot change a Inventories plugin name once the inventory is opened, this is a limitation in Bukkit itself, without creating some hacky work-around

PluginInventory inventory = new PluginInventory(9, "&a&lInventory")
    .setInventorySize(18)
    .setDisplayName("&b&lNew Name")
    .setInventoryType(InventoryType.ANVIL); // Events for all types of inventories have no been added yet, with time :)

Another thing to mention, ALL methods of PluginInventory are chainable.

Handling Inventory specific events

An inventory can have specific event handling for when the inventory opens, closes and is clicked

Notice You are NOT meant to use the onClick() event as a way of handling items clicked, for that view Clickable Items this guide shows you how to handle specific item click events as well as adding items to the inventory, with a very unique API :)

PluginInventory inventory = new PluginInventory(9, "&a&lInventory")
    .onOpen((onOpen) -> {
        InventoryOpenEvent event = onOpen.openEvent();
        // Do things with event, or just in general
    })
    .onClose((onClose) -> {
        InventoryCloseEvent event = onClose.closeEvent();
        // Do things with event or just in general
    })
    .onClick((onClick) -> {
        InventoryClickEvent event = onClick.clickEvent();
        // Do things with event, or just in general

        // IMPORTANT NOTE ABOUT CANCELLING INVENTORY CLICK EVENTS
        // This event is processed after a ClickableItem's click event has been fired
        // If you set a ClickableItem's click event to not cancelled, but cancel in this event
        // this event will take priority over the cancellation
    });

Showing the inventory to the player

After you add all your ClickableItems, Inventory events, and Inventory property changes you want, you can now show that inventory to a Player on the server

I recommend keeping some type of singleton reference to the PluginInventory class, that way you are constantly reconstructing a PluginInventory

Here is how you open the inventory to a player:

PluginInventory inventory = new PluginInventory(9, "&a&lInventory")
    .open(typeOfPlayer);

// You could of course also just do this

inventory.open(typeOfPlayer);

Obviously, the open() argument takes an instance of a Player, there is no need to register events for this plugin as they are automatically handled