🇱🇷
zMenu
Marketplace
English
English
  • 🍾Getting started
  • 🔌Installing zMenu
  • ➕Supported Plugins
  • 👍zMenu+
  • â›°ī¸Configurations
    • â„šī¸Informations
    • 📜Commands and Permissions
    • đŸĒ§PlaceHolder
    • How to create an inventory step by step
    • 👨‍đŸ’ģInventories
    • âšī¸Buttons
    • 🏁Requirements
    • â˜ĸī¸Actions
    • đŸĒItems
    • Global Placeholders
    • 🔋Patterns
    • Commands
    • 🛝Player data
    • đŸĻŦConfig.json
  • API
    • â„šī¸Getting Started with zMenu API
    • Creating a Basic Button
    • Creating a Pagination Button
    • Creating a Custom Loader
    • Creating a Custom Action
    • Creating a Custom Material Loader
    • Creating a Custom Permission System
    • Creating and Using Inventory Files
  • Plugin Initialization and Registration
  • đŸ—ƒī¸Plugin's files
  • Update to new API
Powered by GitBook
On this page
  • Goal
  • Step 1: Create the Action Class
  • Step 2: Create an Action Loader
  • Step 3: Register the Action Loader
  • Step 4: Use It in an Inventory or Permission
  • Result
Edit on GitHub
  1. API

Creating a Custom Action

In this section, you'll learn how to define a custom action that can be triggered by a button or a condition.

Goal

We want to define an action that sends a message to the player. This action can be linked to a button click or to permission success/failure.

Step 1: Create the Action Class

Create a class that extends Action:

import fr.maxlego08.menu.api.button.Button;
import fr.maxlego08.menu.api.engine.InventoryEngine;
import fr.maxlego08.menu.api.requirement.Action;
import fr.maxlego08.menu.api.utils.Placeholders;
import net.kyori.adventure.text.Component;
import org.bukkit.entity.Player;

public class ExampleAction extends Action {

    @Override
    protected void execute(Player player, Button button, InventoryEngine inventoryEngine, Placeholders placeholders) {
        player.sendMessage(Component.text("You clicked on me"));
    }
}

This action simply sends a message when executed.

Step 2: Create an Action Loader

The loader defines how the action is loaded and which identifier it uses in the configuration.

import fr.maxlego08.example.actions.ExampleAction;
import fr.maxlego08.menu.api.loader.ActionLoader;
import fr.maxlego08.menu.api.requirement.Action;
import fr.maxlego08.menu.api.utils.TypedMapAccessor;

import java.io.File;

public class ExampleActionLoader extends ActionLoader {

    public ExampleActionLoader() {
        super("example-action", "example action");
    }

    @Override
    public Action load(String path, TypedMapAccessor accessor, File file) {
        return new ExampleAction();
    }
}

Step 3: Register the Action Loader

Inside your onEnable() method:

buttonManager.registerAction(new ExampleActionLoader());

Step 4: Use It in an Inventory or Permission

Example usage in an inventory:

basic:
  type: BASIC_EXAMPLE
  slot: 10
  key-example: basic
  item:
    material: DIAMOND
    name: "&7Basic"
  actions:
    - example-action

The action will be triggered when the button is clicked.

Result

Clicking the button will display:

You clicked on me
PreviousCreating a Custom LoaderNextCreating a Custom Material Loader

Last updated 1 day ago