Skip to content

Inventories

Nico Britze edited this page Jan 4, 2024 · 4 revisions

Protocolize supports the usage of inventories. With protocolize, you can create your own custom GUIs or even manipulate other inventories without changing the server side contents.

Creating an inventory

Creating a custom inventory is pretty straight forward. Just construct a new instance of dev.simplix.protocolize.api.inventory.Inventory and you are good to go.

Inventory inventory = new Inventory(InventoryType.GENERIC_9X4);
inventory.title(ChatElement.ofLegacyText("§9Inventory"));

This will create a chest inventory with 4 rows. Feel free to check out other InventoryTypes as well!

Setting items

Protocolize comes with it's own ItemStack class. You can set those ItemStacks by simply calling the item method of Inventory.

inventory.item(0, new ItemStack(ItemType.GLASS_PANE));

This will set a glass pane into the first slot of the inventory.

Showing an inventory

After you have set some items into your inventory, it is time to show a player your work. Simply call obtain an instance of ProtocolizePlayer using the ProtocolizePlayerProvider and call the openInventory method and your custom GUI opens up for that specified player.

ProtocolizePlayer player = Protocolize.playerProvider().player(playerUniqueId);
player.openInventory(inventory);

Handle inventory clicks

The Inventory class allows to register a Consumer<InventoryClick> using the onClick method.

Inventory inventory = new Inventory(InventoryType.GENERIC_9X4);
inventory.title(ChatElement.ofLegacyText("§9Inventory"));
inventory.onClick(click -> {
    player.sendMessage("Clicked on slot " + click.slot());
});

You also can also add a Consumer for closing the inventory.

Manipulating the player inventory

When you need to change contents of the player inventory, you can do this by obtaining the players inventory using:

ProtocolizePlayer player = Protocolize.playerProvider().player(playerUniqueId);
PlayerInventory inventory = player.proxyInventory();

Now you can set custom items using the item method.

inventory.item(40, new ItemStack(ItemType.BLAZE_ROD));

Please note that you will have to use the minecraft slot index numbers. Not the bukkit converted ones. Click here for the minecraft slot index numbers.

After changing the contents of the player inventory, it is neccessary to call inventory.update();.