Repository navigation
Command Overloading
vape edited this page Aug 11, 2026
·
1 revision
OverloadedCommand maps PHP Closure parameter signatures directly to client-side Bedrock command overload structures, autocompleters, and type parsers.
| Aspect | Traditional PMMP Command | OverloadedCommand Engine |
|---|---|---|
| Argument Parsing | Manual string array token parsing ($args[0], $args[1]) | Automatic PHP type parsing (Player, float, int, Item, Vector3) |
| Client Autocomplete | Single string hint or manual enum data packets | Native Bedrock client syntax hints, target selectors & item icons |
| Syntax Validation | Custom if-checks for argument count & types | Automatic type-checking & typed error responses |
| Overload Branching | Single onCommand method with nested switch/if statements |
Multiple type-safe Closure handlers |
| Permissions | Global command permission check | Independent permission requirements per overload branch |
namespace myplugin\command;
use pocketmine\command\CommandSender;
use pocketmine\command\OverloadedCommand;
use pocketmine\command\overload\ItemArgumentParser;
use pocketmine\item\Item;
use pocketmine\player\Player;
class TeleportCommand extends OverloadedCommand{
public function __construct(){
parent::__construct("tp", "Teleport command", "/tp <target>");
// Overload 1: /tp <target: Player>
$this->addOverload(function(Player $sender, Player $target) : void {
$sender->teleport($target->getPosition());
$sender->sendMessage("Teleported to " . $target->getName());
});
// Overload 2: /tp <target: Player> <x: float> <y: float> <z: float>
$this->addOverload(function(CommandSender $sender, Player $target, float $x, float $y, float $z) : void {
$target->teleport($target->getPosition()->withComponents($x, $y, $z));
$sender->sendMessage("Teleported " . $target->getName() . " to coordinates.");
}, permission: "myplugin.teleport.others");
}
}CommandOverload inspects parameter typehints to select standard parsers:
| PHP Closure Typehint | Bedrock Client Autocomplete UI | Parsed Result |
|---|---|---|
| Player | Target selector (@a, @p, player names) | Player instance |
| int | Integer numerical input | int |
| float | Coordinate / Float input | float |
| string | String token | string |
| bool | true / false enum | bool |
| Vector3 | World position (~ ~ ~ or absolute coords) | Vector3 instance |
For custom argument parsing (like Bedrock item autocomplete for /give), pass $explicitParsers array as the 3rd argument to addOverload():
use pocketmine\command\overload\ItemArgumentParser;
use pocketmine\command\overload\IntegerArgumentParser;
use pocketmine\item\Item;
use pocketmine\player\Player;
$this->addOverload(
function(Player $sender, Player $target, Item $item, int $amount) : void {
$item->setCount($amount);
$target->getInventory()->addItem($item);
},
explicitParsers: [
"item" => new ItemArgumentParser(),
"amount" => new IntegerArgumentParser(1, 64)
]
);Create subcommand structures by declaring literal string parameters in your closure handler:
// Overload: /arena join <arenaName: string>
$this->addOverload(function(Player $sender, string $subcommand, string $arenaName) : void {
if($subcommand !== "join") return;
// Join arena logic
});
// Overload: /arena leave
$this->addOverload(function(Player $sender, string $subcommand) : void {
if($subcommand !== "leave") return;
// Leave arena logic
});Using ItemArgumentParser maps parameter suggestions to Bedrock canonical item dictionary entries.
Clients see:
- Bedrock item icons in the command suggestion popup.
- Client-side autocomplete for all vanilla item identifiers (e.g.
diamond_sword,golden_apple).