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 :
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);
}
}
This version is outdated. Please download latest release with versioning check system.
Some methods explained before does not exist in this version.
What's important ?
There is 3 methods very important:
Constructor function
Init function
Update function
Constructor
The constructor method need 1 argument : your plugin class. This is for your actions in game by clicking on items.
The construction have to be written like this:
public class MyMenu extends GuiMenu {
public MyMenu(MyPlugin plugin) {
// the 1st argument is your plugin class
// the 2nd argument is the number of slots in your menu
// the 3rd argument is your menu title
// the 4th argument set if your menu is static
super(plugin, 9, "My Menu Title", true);
}
Init method
The init method is call only once : when your menu is instantiate. This mean all items you gonna add in your menu by this function cannot be updated. Or if you want to update it, you have to erease it in update.
This method have to be written like this:
public class MyMenu extends GuiMenu {
public MyMenu(MyPlugin plugin) {...}
@Override
protected void initGUI() {
super.initGUI();
System.out.println("Init My Menu !");
}
Event methods
There are 2 event methods :
onOpenMenu
onCloseMenu
Each of them are used by super. So do not forget to call the super method.
Each of them get as argument the player who opened the menu. You can add in the run time items in your menu. But do not forget to set your menu non static (in constructor).
public class MyMenu extends GuiMenu {
public MyMenu(MyPlugin plugin) {...}
@Override
protected void initGUI() {...}
@Override
protected void onOpenMenu(Player player) {
super.onOpenMenu(player);
System.out.println("Open My Menu !");
}
@Override
protected void onCloseMenu(Player player) {
super.onCloseMenu(player);
System.out.println("Close My Menu !");
}
Let's add this menu
You have to go back in your main plugin class and follow the code:
import fr.kap35.kapeasymenu.Menu.GuiMenu;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
public class MyPlugin extends JavaPlugin {
GuiManager guiManager;
@Override
public void onEnable() {
...
addYourMenus();
}
private void addYourMenus() {
guiManager.registerMenus(new MyMenu(this), "MyMenu");
}
}
Quick Start
import fr.kap35.kapeasymenu.Menu.GuiMenu;
public class MyMenu extends GuiMenu {
public MyMenu(MyPlugin plugin) {
super(plugin, 9, "My Super Menu");
}
@Override
protected void initGUI() {
super.initGUI();
System.out.println("Init My Menu !");
}
@Override
protected void onOpenMenu(Player player) {
super.onOpenMenu(player);
System.out.println("Open My Menu !");
}
@Override
protected void onCloseMenu(Player player) {
super.onCloseMenu(player);
System.out.println("Close My Menu !");
}
}
Last updated