Skip to content
Asintoto edited this page Jul 20, 2024 · 6 revisions

Creating Menus using Basic

Here is a quick start guide to create Custom Menus using Basic


There are 2 data structures that you need to understand in order to create menus:

  • Buttons
  • Menus

Buttons

Buttons are items you can insert in the menu, they can instantiated using an ItemStack, a Material or an ItemCreator and a ButtonType. There are 4 Button types:

  • LOCKED -> The item cannot be moved outside or inside the menu
  • UNLOCKED -> The item can be moved like a normal item inside a chest
  • CLOSE -> It will close the menu when clicked
  • PREVIUS -> It will open up the Previus Menu (more info down below) If no ButtonType is given in the custructor, the default value will be ButtonType.LOCKED.
Button close = new Button(Material.BARRIER, ButtonType.CLOSE);
// This will create a barrier block button which closes the menu when clicked

Menus

A Menu Class is the class you have to extends in order to create you custom menu. When you create a custom class extending Menu, you must call in its constructor super(menu_size, menu_title) in order to create the proper inventory with the given size and title. All the menu creation process must be done inside the constructor (or in an outside methods that must be called in the constructor), to add a button to the menu just create you Button instance as shown before and then run the method addButton(button, slot), this will place the item in the given slot of the menu. You can also link menus together using the setPrevMenu(previus_menu) method where previus_menu is another class which extends Menu. By default the menu is locked, meaning nothing can go inside or outside (unless a button ha ButtonType.UNLOCKED), you can add more complex actions by overriding the method public void onClick(Player p, int slot) in order to performe all the actions you want when clicked a specific slot. In addition, you can override the method public void onClose(Player p) to performe actions when the menu is closed.

public class TestMenu extends Menu {
    public TestMenu() {
        super(27, "Test Menu");

        ItemStack closeButton = new ItemCreator(Material.BARRIER).setName(ColorLib.setColors("&4&lClose")).make();

        Button close = new Button(closeButton, ButtonType.CLOSE);

        addButton(close, 22);

        Button prev = new Button(Material.ARROW, ButtonType.PREVIUS);
        addButton(prev, 23);

        addButton(new Button(new ItemCreator(Material.DIAMOND).setGlowing()
                .setName(ColorLib.setColors("&c/give")).setLore("Click here to get something").make()), 15);

        setPrevMenu(new TestMenuPrev());
    }

    @Override
    public void onClick(Player p, int slot, InventoryAction action) {

           if (slot == 15) {
              if (action == InventoryAction.PICKUP_ALL) {   // Left click action
                  new ItemCreator(Material.DIAMOND).give(p);
                  p.sendMessage(ColorLib.setColors("&bYou got 1 Diamond"));
                  return;
              } else if (action == InventoryAction.PICKUP_HALF) {    // Right click action
                  new ItemCreator(Material.NETHERITE_INGOT).give(p);
                  p.sendMessage(ColorLib.setColors("&bYou got 1 Netherite Ingot"));
                  return;
              } else {   // Everything else (such as Shift Click or Drop)
                  p.sendMessage(ColorLib.setColors("&cYou got nothing lol"));
              }

        }
    }

    @Override
    public void onClose(Player p) {

           BasicPlayer bp = BasicPlayer.from(p);
           bp.sendMessage("&bMenu closed!")

        }
    }
}

If you want to allow the player to drag and drop item from their inventory to the menu (and be able to get them back), simple run the method setPlayerInventoryProtection(false) in your menu constructor.

public class TestMenu extends Menu {
    public TestMenu() {
        super(27, "&bTest Menu");
        setPlayerInventoryProtection(false);
    }
}

To open the menu to a given player simply make a new instance of the menu class, then run .open(Player p) method.

TestMenu menu = new TestMenu();
menu.open(player);
Clone this wiki locally