Create simple menu

Here we gonna talk about how create a menu

Methods

onCloseMenu [OVERRIDE]

This event is called when a player close menu

protected abstract void onCloseMenu(Player player);
onOpenMenu [OVERRIDE]

This event is called when a player open menu

protected abstract void onOpenMenu(Player player);
addItem

There is 2 system to add any items : initially and dynamically.

Dynamically

This will add item only for 1 specific user. If you call this method in constructor, your constructor gonna crash. So use this method on events

protected void addItem(Player player, IGuiItem item, int slot) throws PlayerNotReaderException

Initially

this method must be used in constructor. It will place item in all menus for all players.

protected void addItem(IGuiItem item, int slot)
refreshPage

This method can be called to refresh a player menu.

protected void refreshPage(Player player)

Code example

What's simple menu ?

simple menu is only 1 chest page menu. It does not allow you to create several page.

Create your first menu

public class MySimpleMenu extends ChestMenu {
    public MySimpleMenu(JavaPlugin plugin) {
        int menuSize = 27;
        super(plugin, "My Menu Title", menuSize);
    }
    
    @Override
    protected void onCloseMenu(Player player) {
        //event when user open your menu
    }
    
    @Override
    protected void onOpenMenu(Player player) {
        //event when user close your menu
    }
}

Now you have your menu, let's add it to menu system.

public MyPlugin extends JavaPlugin {
    GuiManager guiManager;
    
    public MyPlugin() {
    
    }
    
    private void initMyMenus() {
        MySimpleMenu simpleMenu = new MySimpleMenu(this);
        guiManager.registerMenus(simpleMenu, "MySimpleMenu");
    }
}

To init GuiManager, please refer to this documentation :

Init your plugin

Add items in menu

public class MySimpleMenu extends ChestMenu {
    public MySimpleMenu(JavaPlugin plugin) {
        ...
        GuiItem item = new GuiItem(...);
        // add item in slot 2
        addItem(item, 2);
    }
    
    @Override
    protected void onOpenMenu(Player player) {
        GuiItem otherItem = new GuiItem(...);
        // place only for the player otherItem in slot 3
        addItem(player, otherItem, 3);
    }
}

Refresh user page

public class MySimpleMenu extends ChestMenu {
    public MySimpleMenu(JavaPlugin plugin) {
        ...
    }
    
    @Override
    protected void onOpenMenu(Player player) {
        GuiItem otherItem = new GuiItem(...)
            .setDisableEvent(true)
            .setAction(ItemActions.LEFT_CLICK, new ItemGuiAction() {
                    @Override
                    public void run(Player player, JavaPlugin plugin) {
                        refreshPage(player);
                    }
                });
        // when player gonna click on item, its menu will refresh
        addItem(player, otherItem, 3);
    }
}

Last updated