Skip to content

Inventory User Interface

Rick van Sloten edited this page Mar 19, 2020 · 4 revisions

Inventory User Interface

This wiki page is all about the Inventory User Interface features!

No more registering events, checking inventory information, and ambiguous slot checking. TabuuCore gives possibility to neatly code your Inventory User Interfaces.

InventoryUI

An InventoryUI is used to create a protected inventory.

Here is an example of an empty InventoryUI:

public class ExampleInterface extends InventoryFormUI {

    public ExampleInterface() {
        super("Inventory Title", InventorySize.SIX_ROWS);
    }

}

This creates a nine by six inventory with "Inventory Title" as title. The inventory is automatically registered to the system.

If you want to set the inventory title after the call to super. So that you can, for example, load the title from a configuration. You can do the following:

public ExampleInterface() {
    super("No Title", InventorySize.SIX_ROWS);
        
    String title = "Inventory Title";
        
    setTitle(title);
        
    reload();
}

To open this inventory you can do:

new ExampleInterface().open(player);

Drawing

Now that you have created an empty inventory you can override the draw function in which you can fill the inventory.

You can use the methods of InventoryCanvas to draw in the inventory. Most of these function use an IBrush, these brushes can create patterns in the slots they fill. The slots are automatically protected, and the items cannot be taken from the inventory.

Here is an example of an inventory with an alternating border:

@Override
protected void draw() {
    ItemStack 
            border1 = new ItemStack(Material.ORANGE_STAINED_GLASS_PANE),
            border2 = new ItemStack(Material.PURPLE_STAINED_GLASS_PANE);

    IBrush brush = new CheckerBrush(border1, border2);
        
    setBrush(brush);
        
    drawRectangle(new Vector2f(0, 0), new Vector2f(8, 5));
}

This will result in the following inventory:

With the onClick and onClickUI methods you can listen to clicks in the inventory. But you will see that the chapter bellow will give a much cleaner alternative.

InventoryFormUI

The InventoryFormUI extends from the InventoryUI, but has the additional feature of accepting Elements, such as: Buttons, ItemInputs, TextInputs, etc.

To create these Elements you will need to create a Style. These styles can differ per Element, and dictate how different aspects of the Element look.

Most Elements have an optional consumer, here you can define what should happen when a user interacts with the Element.

NOTE: The super.draw() which is called at the end of the function will actually draw all elements.

Bellow is the example inventory from before, but you can now put an item in the middle of the inventory and rename it with a button:

public class ExampleInterface extends InventoryFormUI {

    private ItemInput _itemInput;
    private TextInput _textInput;

    public ExampleInterface() {
        super("No Title", InventorySize.SIX_ROWS);

        String title = "Inventory Title";

        setTitle(title);

        reload();
    }

    @Override
    protected void draw() {
        ItemStack
                border1 = new ItemStack(Material.ORANGE_STAINED_GLASS_PANE),
                border2 = new ItemStack(Material.PURPLE_STAINED_GLASS_PANE);

        TextInputStyle
                textInputStyle = new TextInputStyle(Material.FEATHER, Material.BARRIER, Material.NAME_TAG, "Display Name:");

        Style
                itemInputStyle = new Style(Material.AIR, Material.BARRIER),
                renameButtonStyle = new Style(Material.EMERALD, Material.BARRIER);

        IBrush brush = new CheckerBrush(border1, border2);

        _textInput = new TextInput(textInputStyle, this);
        _itemInput = new ItemInput(itemInputStyle, false);
        Button renameButton = new Button(renameButtonStyle, this::onRenameButtonClick);

        setElement(new Vector2f(3, 2), _textInput);
        setElement(new Vector2f(4, 2), _itemInput);
        setElement(new Vector2f(5, 2), renameButton);

        setBrush(brush);
        drawRectangle(new Vector2f(0, 0), new Vector2f(8, 5));

        super.draw();
    }

    private void onRenameButtonClick(Player player) {
        ItemStack item = _itemInput.getValue();
        String name = _textInput.getValue();

        ItemMeta meta = item.getItemMeta();
        meta.setDisplayName(name);

        item.setItemMeta(meta);
    }
}

This will result in the following inventory and functionality:

Clone this wiki locally