44 private string $nextLabel;
45 private string $label;
48 private array $aliases = [];
51 private array $activeAliases = [];
60 private array $permission = [];
61 private ?
string $permissionMessage =
null;
68 $this->setLabel($name);
69 $this->setDescription($description);
70 $this->usageMessage = $usageMessage ?? (
"/" . $name);
82 public function getName() : string{
90 return $this->permission;
98 foreach($permissions as $perm){
99 if($permissionManager->getPermission($perm) ===
null){
100 throw new \InvalidArgumentException(
"Cannot use non-existing permission \"$perm\"");
103 $this->permission = $permissions;
106 public function setPermission(?
string $permission) : void{
107 $this->setPermissions($permission === null ? [] : explode(
";", $permission));
110 public function testPermission(CommandSender $target, ?
string $permission =
null) : bool{
111 if($this->testPermissionSilent($target, $permission)){
115 if($this->permissionMessage ===
null){
116 $target->sendMessage(KnownTranslationFactory::pocketmine_command_error_permission($this->name)->prefix(TextFormat::RED));
117 }elseif($this->permissionMessage !==
""){
118 $target->sendMessage(str_replace(
"<permission>", $permission ?? implode(
";", $this->permission), $this->permissionMessage));
124 public function testPermissionSilent(CommandSender $target, ?
string $permission =
null) : bool{
125 $list = $permission !== null ? [$permission] : $this->permission;
126 foreach($list as $p){
127 if($target->hasPermission($p)){
135 public function getLabel() : string{
139 public function setLabel(
string $name) : bool{
140 $this->nextLabel = $name;
141 if(!$this->isRegistered()){
142 $this->label = $name;
154 if($this->allowChangesFrom($commandMap)){
155 $this->commandMap = $commandMap;
163 public function unregister(CommandMap $commandMap) : bool{
164 if($this->allowChangesFrom($commandMap)){
165 $this->commandMap =
null;
166 $this->activeAliases = $this->aliases;
167 $this->label = $this->nextLabel;
175 private function allowChangesFrom(CommandMap $commandMap) : bool{
176 return $this->commandMap === null || $this->commandMap === $commandMap;
179 public function isRegistered() : bool{
180 return $this->commandMap !== null;
187 return $this->activeAliases;
190 public function getPermissionMessage() : ?string{
191 return $this->permissionMessage;
195 return $this->description;
198 public function getUsage() : Translatable|string{
199 return $this->usageMessage;
206 $this->aliases = $aliases;
207 if(!$this->isRegistered()){
208 $this->activeAliases = $aliases;
212 public function setDescription(
Translatable|
string $description) : void{
213 $this->description = $description;
216 public function setPermissionMessage(
string $permissionMessage) : void{
217 $this->permissionMessage = $permissionMessage;
220 public function setUsage(Translatable|
string $usage) : void{
221 $this->usageMessage = $usage;
224 public static function broadcastCommandMessage(CommandSender $source, Translatable|
string $message,
bool $sendToSource =
true) : void{
225 $users = $source->getServer()->getBroadcastChannelSubscribers(Server::BROADCAST_CHANNEL_ADMINISTRATIVE);
226 $result = KnownTranslationFactory::chat_type_admin($source->getName(), $message);
227 $colored = $result->prefix(TextFormat::GRAY . TextFormat::ITALIC);
230 $source->sendMessage($message);
233 foreach($users as $user){
234 if($user instanceof BroadcastLoggerForwarder){
235 $user->sendMessage($result);
236 }elseif($user !== $source){
237 $user->sendMessage($colored);
242 public function __toString() : string{