Skip to content

Menus | Creating Click Actions

Mantice edited this page Dec 19, 2023 · 1 revision

Menu Actions determine what happens when you click on any item in a menu!
Follow this example to create a new Action!

public class CloseAction extends Action {

    public CloseAction() {
        // "close" is this Action's ID! IDs must be unique!
        super("close");
    }

    @Override
    public void run(@NotNull final Player player, @NotNull final String commandLine) {
        player.closeInventory();
    }

}

Once you have defined what your action will do (in the run method), you only need to initialize it!
To initialize it, call new on it anywhere! Here's an example on your main class' onEnable method:

@Override
public void onEnable() {
    new CloseAction();
}

Here's an example of using this Action in a menu!

contents:
  'close':
    material: BARRIER
    data: 0
    custom-model-data: 0
    display-name: '&cClose'
    lore: []
    slots: 49
    left-click-actions:
      - '[close]'

The commandLine parameter in the Action's run method is what you put after [close]!
For example, if you put "[close] Hello!" then the commandLine variable will be "Hello!"

Clone this wiki locally