The easiest way to create commands is to declare them in plugin.yml.
1
Declare command in plugin.yml
plugin.yml
name: MyPluginversion: 1.0.0main: MyPlugin\Mainapi: [5.0.0]commands: heal: description: Heal yourself or another player usage: /heal [player] aliases: [hp, health] permission: myplugin.command.heal permission-message: §cYou don't have permission to use this commandpermissions: myplugin.command.heal: description: Allows using /heal command default: op
In API 5.0+, all commands MUST have a permission field. This is a security requirement.
2
Implement onCommand()
Handle the command in your main class:
src/MyPlugin/Main.php
<?phpdeclare(strict_types=1);namespace MyPlugin;use pocketmine\plugin\PluginBase;use pocketmine\command\Command;use pocketmine\command\CommandSender;use pocketmine\player\Player;class Main extends PluginBase { public function onCommand( CommandSender $sender, Command $command, string $label, array $args ) : bool { switch($command->getName()) { case "heal": return $this->handleHeal($sender, $args); } return false; } private function handleHeal(CommandSender $sender, array $args) : bool { // Only players can be healed if(!$sender instanceof Player) { $sender->sendMessage("§cOnly players can use this command"); return true; } // Heal the sender if(count($args) === 0) { $sender->setHealth($sender->getMaxHealth()); $sender->sendMessage("§aYou have been healed!"); return true; } // Heal another player if(count($args) === 1) { $target = $this->getServer()->getPlayerByPrefix($args[0]); if($target === null) { $sender->sendMessage("§cPlayer not found: " . $args[0]); return true; } $target->setHealth($target->getMaxHealth()); $target->sendMessage("§aYou have been healed by " . $sender->getName()); $sender->sendMessage("§aHealed " . $target->getName()); return true; } // Too many arguments - return false to show usage return false; }}
Return true if the command was handled (even if there was an error). Return false to show the usage message.
use pocketmine\command\CommandSender;use pocketmine\player\Player;use pocketmine\console\ConsoleCommandSender;public function onCommand( CommandSender $sender, Command $command, string $label, array $args) : bool { // Check if sender is a player if($sender instanceof Player) { $sender->sendMessage("You are a player!"); $sender->setHealth(20); } // Check if sender is console elseif($sender instanceof ConsoleCommandSender) { $sender->sendMessage("You are the console!"); } // Works for both players and console $sender->sendMessage("This works for anyone"); return true;}
public function onCommand( CommandSender $sender, Command $command, string $label, array $args) : bool { if(!$sender instanceof Player) { $sender->sendMessage("§cThis command can only be used in-game"); return true; } // Now we know $sender is a Player $sender->setHealth($sender->getMaxHealth()); return true;}
Permissions are checked automatically, but you can add custom checks:
public function onCommand( CommandSender $sender, Command $command, string $label, array $args) : bool { // Check specific permission if(!$sender->hasPermission("myplugin.vip")) { $sender->sendMessage("§cYou need VIP rank!"); return true; } // Check if sender is OP if(!$sender->getServer()->isOp($sender->getName())) { $sender->sendMessage("§cYou must be OP"); return true; } return true;}
commands: teleport: description: Teleport to a player or coordinates usage: /teleport <player|x y z> [target] aliases: [tp] permission: myplugin.command.teleportpermissions: myplugin.command.teleport: description: Use /teleport default: op myplugin.command.teleport.others: description: Teleport other players default: op