name = $name; $this->setLabel($name); $this->setDescription($description); $this->usageMessage = $usageMessage ?? ("/" . $name); $this->setAliases($aliases); } /** * @param string[] $args * * @return mixed * @throws CommandException */ abstract public function execute(CommandSender $sender, string $commandLabel, array $args); public function getName() : string{ return $this->name; } public function getPermission() : ?string{ return $this->permission; } public function setPermission(?string $permission) : void{ if($permission !== null){ foreach(explode(";", $permission) as $perm){ if(PermissionManager::getInstance()->getPermission($perm) === null){ throw new \InvalidArgumentException("Cannot use non-existing permission \"$perm\""); } } } $this->permission = $permission; } public function testPermission(CommandSender $target, ?string $permission = null) : bool{ if($this->testPermissionSilent($target, $permission)){ return true; } if($this->permissionMessage === null){ $target->sendMessage(KnownTranslationFactory::pocketmine_command_error_permission($this->name)->prefix(TextFormat::RED)); }elseif($this->permissionMessage !== ""){ $target->sendMessage(str_replace("", $permission ?? $this->permission, $this->permissionMessage)); } return false; } public function testPermissionSilent(CommandSender $target, ?string $permission = null) : bool{ $permission ??= $this->permission; if($permission === null || $permission === ""){ return true; } foreach(explode(";", $permission) as $p){ if($target->hasPermission($p)){ return true; } } return false; } public function getLabel() : string{ return $this->label; } public function setLabel(string $name) : bool{ $this->nextLabel = $name; if(!$this->isRegistered()){ $this->timings = new TimingsHandler(Timings::INCLUDED_BY_OTHER_TIMINGS_PREFIX . "Command: " . $name); $this->label = $name; return true; } return false; } /** * Registers the command into a Command map */ public function register(CommandMap $commandMap) : bool{ if($this->allowChangesFrom($commandMap)){ $this->commandMap = $commandMap; return true; } return false; } public function unregister(CommandMap $commandMap) : bool{ if($this->allowChangesFrom($commandMap)){ $this->commandMap = null; $this->activeAliases = $this->aliases; $this->label = $this->nextLabel; return true; } return false; } private function allowChangesFrom(CommandMap $commandMap) : bool{ return $this->commandMap === null || $this->commandMap === $commandMap; } public function isRegistered() : bool{ return $this->commandMap !== null; } /** * @return string[] */ public function getAliases() : array{ return $this->activeAliases; } public function getPermissionMessage() : ?string{ return $this->permissionMessage; } public function getDescription() : Translatable|string{ return $this->description; } public function getUsage() : Translatable|string{ return $this->usageMessage; } /** * @param string[] $aliases */ public function setAliases(array $aliases) : void{ $this->aliases = $aliases; if(!$this->isRegistered()){ $this->activeAliases = $aliases; } } public function setDescription(Translatable|string $description) : void{ $this->description = $description; } public function setPermissionMessage(string $permissionMessage) : void{ $this->permissionMessage = $permissionMessage; } public function setUsage(Translatable|string $usage) : void{ $this->usageMessage = $usage; } public static function broadcastCommandMessage(CommandSender $source, Translatable|string $message, bool $sendToSource = true) : void{ $users = $source->getServer()->getBroadcastChannelSubscribers(Server::BROADCAST_CHANNEL_ADMINISTRATIVE); $result = KnownTranslationFactory::chat_type_admin($source->getName(), $message); $colored = $result->prefix(TextFormat::GRAY . TextFormat::ITALIC); if($sendToSource && !($source instanceof ConsoleCommandSender)){ $source->sendMessage($message); } foreach($users as $user){ if($user instanceof ConsoleCommandSender){ $user->sendMessage($result); }elseif($user !== $source){ $user->sendMessage($colored); } } } public function __toString() : string{ return $this->name; } }