> For the complete documentation index, see [llms.txt](https://kap35.gitbook.io/kap-easy-menu-plugin/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kap35.gitbook.io/kap-easy-menu-plugin/dev-manual/create-a-menu/create-simple-menu.md).

# Create simple menu

## Methods

<details>

<summary>onCloseMenu [OVERRIDE]</summary>

This event is called when a player close menu

```java
protected abstract void onCloseMenu(Player player);
```

</details>

<details>

<summary>onOpenMenu [OVERRIDE]</summary>

This event is called when a player open menu

```java
protected abstract void onOpenMenu(Player player);
```

</details>

<details>

<summary>addItem</summary>

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

{% code overflow="wrap" %}

```java
protected void addItem(Player player, IGuiItem item, int slot) throws PlayerNotReaderException
```

{% endcode %}

#### Initially

this method must be used in constructor. It will place item in all menus for all players.

```java
protected void addItem(IGuiItem item, int slot)
```

</details>

<details>

<summary>refreshPage</summary>

This method can be called to refresh a player menu.

```java
protected void refreshPage(Player player)
```

</details>

## Code example

{% tabs %}
{% tab title="Version 2." %}

## 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

```java
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.

```java
public MyPlugin extends JavaPlugin {
    GuiManager guiManager;
    
    public MyPlugin() {
    
    }
    
    private void initMyMenus() {
        MySimpleMenu simpleMenu = new MySimpleMenu(this);
        guiManager.registerMenus(simpleMenu, "MySimpleMenu");
    }
}
```

{% hint style="warning" %}
To init GuiManager, please refer to this documentation :&#x20;

[Init your plugin](/kap-easy-menu-plugin/dev-manual/init-your-plugin.md)
{% endhint %}

## Add items in menu

```java
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

```java
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);
    }
}
```

{% endtab %}

{% tab title="Version 1." %}
{% hint style="danger" %}
This version is outdated. Please download latest release with versioning check system.

Some methods explained before does not exist in this version.
{% endhint %}

## What's important ?

There is 3 methods very important:&#x20;

* 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:&#x20;

```java
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:&#x20;

<pre class="language-java" data-overflow="wrap" data-line-numbers><code class="lang-java">public class MyMenu extends GuiMenu {

    public MyMenu(MyPlugin plugin) {...}

<strong>    @Override
</strong>    protected void initGUI() {
        super.initGUI();
        System.out.println("Init My Menu !");
    }
</code></pre>

### Event methods

There are 2 event methods :&#x20;

* 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).

```java
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:&#x20;

```java
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

```java
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 !");
    }
}


```

{% endtab %}
{% endtabs %}
