Skip to content

Create a simple command

This example shows how to create a basic command using the Core API.

The command itself does not execute logic, but instead displays a help page. This is useful as a base command that can later be extended with sub commands.

→ Deep dive: [[Commands|Core-API/Command-&-Interaction/Commands]]


Steps

  1. Create a class extending PixelCommand
  2. Add @CommandDefinition (label + rank)
  3. Implement onPlayerCommand
  4. Register the command

Implementation

package net.mixelpixel.example.bukkit.command;

import net.mixelpixel.core.api.command.CommandDefinition;
import net.mixelpixel.core.api.rank.PixelRank;
import net.mixelpixel.core.api.util.PixelColor;
import net.mixelpixel.core.bukkit.api.command.PixelCommand;
import net.mixelpixel.core.bukkit.api.player.BukkitPixelPlayer;
import net.mixelpixel.example.bukkit.ExamplePlugin;

/**
 * Example: Simple command
 * Shows how to create a command.
 *
 * This command acts as a base command and displays a help page.
 * Sub commands can be added to extend its functionality.
 */
public class ClearCommand extends PixelCommand<ExamplePlugin> {

    @CommandDefinition(
            label = "clear",
            // required rank to execute the command
            rank = PixelRank.ADMIN
    )
    public ClearCommand(ExamplePlugin plugin) {
        super(plugin);
    }

    @Override
    public void onPlayerCommand(BukkitPixelPlayer player, String label, String[] args) {
        // creates a help page out of all registered sub commands
        String helpPage = getHelpPage(player, PixelColor.RED);
        player.sendMessage(helpPage);
    }

}

Registering the command

CommandHandler commandHandler = BukkitCoreLibrary.getCommandHandler();

ClearCommand clearCommand = new ClearCommand(plugin);
commandHandler.registerCommand(clearCommand);

Explanation

  • PixelCommand is the base class for all commands
  • @CommandDefinition defines:
  • the command label (/clear)
  • the required rank
  • onPlayerCommand is executed when a player runs the command
  • getHelpPage(...) generates a default help/usage page

Notes

  • This command acts as a base command
  • It is intended to be extended with sub commands
  • Keeping base commands simple helps structuring larger systems

Next step

[[Create a sub command|Core-API/Build-your-first-feature/Start-Here/Create-a-sub-command]]