Skip to content

2. Creating an Empty Menu

ColonelHedgehog edited this page Jan 27, 2015 · 3 revisions

Once you've set up MenuAPI, you're going to want to get to work. This tutorial is oging to teach you how to make the canvas for your inventory menu. My explanation is going to be pretty short, and will just teach you how to use the Menu object. If you want to learn how to add items and deal with coordinates, refer to the bottom.

First, let's find a place to create our Menu. Here's an idea:

    public void onRightClickEntity(PlayerInteractEvent event)
    {
        if(event.getAction() == Action.RIGHT_CLICK_BLOCK)
        {
            if(event.getClickedBlock().getType() == Material.SIGN || event.getClickedBlock().getType() == Material.WALL_SIGN)
            {
                Sign sign = (Sign) event.getClickedBlock().getState();
                if(sign.getLine(0).equalsIgnoreCase("[Butcher]"))
                {
                    // ...
                }
            }
        }
    }

Basically, its just going to do what we want it to do if we right-click a sign that has a first line equalling "[Butcher]".

Now, I cut it off at // ... just for referrence. At this point, we'll need to go through the steps of creating our Inventory.

First, use the Bukkit#createInventory() method to create a new Inventory:

Inventory inventory = Bukkit.createInventory(player, size, name);

player:

Type: Player
The owner of the inventory. In this case, it should be event.getPlayer();

size:

Type: int
This is the size of the inventory in slots. This must always be evenly divisible by 9. To test this, just get a calculator, put your desired number of slots in and divide it by 9. If you get a decimal number, you cannot use it.

Recommended inventory sizes are: 9, 18, 27, 36, 45, 54 WARNING: Anything above 54 will be glitchy, whether or not it's divisible by 9. For big inventories, 54 is the safest to use.

name:

Type: String
This can be up to 32 characters long. Anything longer will throw an error into the console and your inventory will not work. You can use colors here, but some won't show up very well (namely §7 and §e). For general purposes, it's best to use their dark alternatives (the ChatColors whose names start with "DARK").

Now that we've made the inventory, we'll need to create it as a menu. Something you should note is the Menu will be registered from the moment it is initialized. When you use the Menu#close() method, however, it will de-register.

To create it, simply use the following method:

Menu menu = new Menu(inventory);

You can do lots of things from here. But those are for different tutorials. To open your tutorial, just do this:

menu.openFor(player);

Close it using:

menu.close(); // NOTE: This is deprecated. Use menu.close(Player); for best results.

Remember, an Inventory menu should never be stored in a variable outside of the method. Read on at the related link below to learn how to populate your inventory.

Related

3 - Using MenuObjects

Clone this wiki locally