Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Your first Inventory Menu

xRa1ny edited this page May 23, 2023 · 1 revision

Want to create an interactive inventory?
Well with this API you are able to do that in no time!
To start off, create a new class that extends RInventoryMenu, lets call it 'MyMenu'

@InventoryMenuInfo(title = "", size = 0, background = null)
public class MyMenu extends RInventoryMenu {
    public MyMenu(@Nullable RInventoryMenu previousMenu) { // <- You can remove this from params if you dont need to parse it anyways...
        super(previousMenu); // <- Here we specify the last menu we had open, can be null if you wont use this
    }

    @Override
    public void setItems(@NotNull RUser user) {
        ...
    }

    @Override
    public void onOpen(@NotNull InventoryOpenEvent e, @NotNull RUser user) {
        ...
    }

    @Override
    public void onClose(@NotNull InventoryCloseEvent e, @NotNull RUser user) {
        ...
    }

    @Override
    public void onClick(@NotNull InventoryClickEvent e, @NotNull RUser user) {
        ...
    }
}

As you can see, here we are also using an annotation where we pass meta information to our menu
The size field, must always be a multiple of 9 because of Minecraft limitations
The background field, specifies the material that fills every slot of our inventory as background (item with no name gets created)

The setItems() Method gets called when a user opens this inventory, here we set every item we want in our menu
The onOpen() Method, as it suggests, gets called when this menu opens (before setItems())
The onClose() Method, as it suggests, gets called when this menu closes
The onClick() Method, as its suggests, gets called whenever a player click within the inventory of this menu

Good practice, is to always create a new instance for every opening sequence of this menu
For instance: we want to open a menu when a player right clicks an item, we create a new instance of this menu an call open() on it....

new MyMenu(null).open(user); // <- As the user instance, we can use RPlugin.getUserManager().getUser(player); if we dont have direct access to a user

DONE! Thats all we need to create a fully functioning InventoryMenu!

Your first PagedInventoryMenu

We approach this the same way we do when creating a normal InventoryMenu, but now extend RPagedInventoryMenu instead...

@InventoryMenuInfo(title = "", size = 0, background = null)
public class MyMenu extends RPagedInventoryMenu {
    public MyMenu(@Nullable RInventoryMenu previousMenu) {
        super(previousMenu);
    }

    @Override
    protected void onPageChange(int page) {
        ...
    }

    @Override
    public void setItems(@NotNull RUser user) {
        ...
    }

    @Override
    public void onOpen(@NotNull InventoryOpenEvent e, @NotNull RUser user) {
        ...
    }

    @Override
    public void onClose(@NotNull InventoryCloseEvent e, @NotNull RUser user) {
        ...
    }

    @Override
    public void onClick(@NotNull InventoryClickEvent e, @NotNull RUser user) {
        ...
    }
}

The only difference to a normal InventoryMenu is that we now have a method called onPageChange()
This Method will be called every time we call setPage()

DONE! Thats all we need to create a fully functioning PagedInventoryMenu!

Clone this wiki locally