Skip to content

Create menu

MrCubee edited this page Jun 20, 2022 · 2 revisions

Simple chest menu

import fr.mrcubee.menu.ChestMenu;
import fr.mrcubee.menu.Menus;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;

public class YourMenu extends ChestMenu {

    protected YourMenu(final Menus manager) {
        /*
         * manager      | The instance that registers your menu.
         *                This is the only mandatory parameter to put in the constructor of all your menus.
         *                It must be placed in the first parameter.
         * "Menu title" | The title of your menu.
         * 32           | The size of your menu.
         */
        super(manager, "Your menu title", 54);

        /*
         * This method adds an item button to your inventory, and set actions for him.
         *
         * 0                                     | Location on which to place the item button.
         * new ItemStack(Material.STONE)         | The item you want to place.
         * (player, menu, itemStack, slot) -> {} | The actions you want when players click on them.
         */
        setItemButton(0, new ItemStack(Material.STONE), (player, menu, itemStack, slot) -> {
            player.sendMessage("You clicked on the stone.");
            player.closeInventory();
        });

        /*
         * This method defines actions at the close of the inventory.
         */
        setCloseButton((player, menu) -> {
            menu.delete();
        });
    }
}

Instantiate and open your menu

import fr.mrcubee.menu.Menus;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.java.JavaPlugin;

public class YourPlugin extends JavaPlugin implements Listener {

    private Menus manager;

    @Override
    public void onEnable() {
        /*
         * Create in your plugin the instance of the manager which will allow you to process the events of your different menus.
         * It will need to be accessible wherever you want to create menus.
         */
        this.manager = new Menus();
        
        /*
         * The handler needs to be registered as a listener of your plugin.
         */
        getServer().getPluginManager().registerEvents(this.manager, this);
    }

    @EventHandler
    public void playerJoin(final PlayerJoinEvent event) {
        /*
         * Create a new registered instance of your menu.
         */
        YourMenu menu = this.manager.createMenu(YourMenu.class);

        /*
         * Opens your menu to the player.
         */
        event.getPlayer().openInventory(menu.getInventory());
    }
}
Clone this wiki locally