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
- Create a class extending
PixelCommand - Add
@CommandDefinition(label + rank) - Implement
onPlayerCommand - 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
PixelCommandis the base class for all commands@CommandDefinitiondefines:- the command label (
/clear) - the required rank
onPlayerCommandis executed when a player runs the commandgetHelpPage(...)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]]