🇱🇷
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 Your Main Class
  • Step 2: Define plugin.yml
  • Result
Edit on GitHub

Plugin Initialization and Registration

In this final section, you'll learn how everything comes together in the main plugin class.

Goal

Ensure that all components (loaders, actions, permissibles, inventories) are registered properly when the plugin is enabled.

Step 1: Create Your Main Class

Example: ZMenuExample.java

import fr.maxlego08.example.buttons.BooksButton;
import fr.maxlego08.example.loaders.BasicLoader;
import fr.maxlego08.example.loaders.ExampleActionLoader;
import fr.maxlego08.example.loaders.ExampleMaterialLoader;
import fr.maxlego08.example.loaders.ExamplePermissibleLoader;
import fr.maxlego08.menu.api.ButtonManager;
import fr.maxlego08.menu.api.InventoryManager;
import fr.maxlego08.menu.api.exceptions.InventoryException;
import fr.maxlego08.menu.api.loader.NoneLoader;
import fr.maxlego08.menu.api.pattern.PatternManager;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;

public final class ZMenuExample extends JavaPlugin {

    @Override
    public void onEnable() {

        this.saveDefaultConfig();

        InventoryManager inventoryManager = getProvider(InventoryManager.class);
        ButtonManager buttonManager = getProvider(ButtonManager.class);
        PatternManager patternManager = getProvider(PatternManager.class);

        // Register custom material loader
        inventoryManager.registerMaterialLoader(new ExampleMaterialLoader());

        // Register custom button loaders
        buttonManager.register(new BasicLoader(this));
        buttonManager.register(new NoneLoader(this, BooksButton.class, "PAGINATION_EXAMPLE"));

        // Register actions and permissions
        buttonManager.registerAction(new ExampleActionLoader());
        buttonManager.registerPermissible(new ExamplePermissibleLoader(buttonManager));

        // Load inventory file
        try {
            inventoryManager.loadInventoryOrSaveResource(this, "inventories/example.yml");
        } catch (InventoryException exception) {
            exception.printStackTrace();
        }
    }

    @Override
    public void onDisable() {
        // Any cleanup if needed
    }

    private <T> T getProvider(Class<T> classz) {
        RegisteredServiceProvider<T> provider = getServer().getServicesManager().getRegistration(classz);
        if (provider == null) {
            throw new RuntimeException("Unable to retrieve the provider " + classz);
        }
        return provider.getProvider();
    }
}

Step 2: Define plugin.yml

name: zMenuExample
version: '1.0.0'
main: fr.maxlego08.example.ZMenuExample
api-version: '1.21'
authors: [Maxlego08]
description: Example for zMenu
website: https://groupez.dev/

Result

Once compiled and installed on your server with zMenu installed, your plugin will:

  • Register all custom logic

  • Load inventories

  • Be ready to use /menu open example or call menus programmatically

PreviousCreating and Using Inventory FilesNextPlugin's files

Last updated 1 day ago