Skip to main content

Commands

Using commands in a plugin enchants your plugin! Commands can automatically and easily do things for you!

Ok lets start by importing the Command and CommandSender classes using the use statement.

Main.php
// The Command
use pocketmine\command\Command;

// Person who does command
use pocketmine\command\CommandSender;

To set up the command we're going to use the default onCommand method and inside the method we will add the code, like this:

Main.php
public function onCommand(CommandSender $sender, Command $command, string $label, array $args) : bool {
switch($command->getName()) {
case "example":
$sender->sendMessage("Hello " . $sender->getName() . "!");

return true;
default:
throw new \AssertionError("This line will never be executed");
}
}

It will send a message to the command sender when the command sender executes the /example command.

note

A separate tutorial on commands with detailed explanation is planned, so we temporarily remove the rest of the topic. However you still can find it commented in the source file.

We apologize for the inconvenience.