SimpleTutorial - Vilsol/MenuEngine GitHub Wiki

To start using the engine, you first need to make your first menu. You can do that by creating a new class that is extending the MenuModel. It automatically makes a constructor, and you only have to pass 2 arguments: The size of the inventory (Must be divisible by 9) and the name of the inventory.

Example:

public class MainMenu extends MenuModel {

	public MainMenu() {
		super(9, "Main Menu");
	}
	
}

Now we need to make an item that we want to put inside the inventory. Let's make it so when you left click it, it sends you one message and when you right click it shows another message.

Example:

public class ExampleItem implements MenuItem {

	@Override
	public void registerItem() {
		MenuItem.items.put(this.getClass(), this);
	}

	@Override
	public void execute(Player plr, ClickType click) {
		plr.closeInventory();
		
		if(click == ClickType.LEFT){
			plr.sendMessage("You Left Clicked!");
		}else if(click == ClickType.RIGHT){
			plr.sendMessage("You Right Clicked!");
		}
	}

	@Override
	public ItemStack getItem() {
		return new Builder(Material.ARROW).setName("Example Item").getItem();
	}
	
}

As you can see, the MenuItem interface contains 3 methods, the first one is a static one, which can be copied from one item to another without making any changes. Then we have the method which gets called when the item will be clicked. The last one is the method that gets called when the engine asks for the visual design of the item.

Now to add this item to the first slot in our inventory, we can simply add one line to the MainMenu, and it will be done.

Example:

public class MainMenu extends MenuModel {

	public MainMenu() {
		super(9, "Main Menu");
		getMenu().addItem(ExampleItem.class, 0);
	}
	
}

Note that you want to pass the class of the item, not an actual instance of the object!

The last step is to actually register the menu and the item to the engine, so they would become usable. You want to always register items before you actually use them inside the menus!

Example:

public class TestPlugin extends JavaPlugin {
	
	public void onEnable(){

		// Register Items
		new ExampleItem().registerItem();
		
		// Register Menus
		new MainMenu();
	}
	
}

Note that for menus you only want to create a new object, but for items you need to call their .registerItem() method as well!

Now to open the menu, you can just get your menu from the HashMap, and show it to a player.

Example:

MenuModel.menus.get(MainMenu.class).getMenu().showToPlayer(player);