45 private string $nextLabel;
46 private string $label;
52 private array $aliases = [];
58 private array $activeAliases = [];
67 private array $permission = [];
68 private Translatable|
string|
null $permissionMessage =
null;
76 $this->setLabel($name);
77 $this->setDescription($description);
78 $this->usageMessage = $usageMessage ?? (
"/" . $name);
91 public function getName() : string{
99 return $this->permission;
107 foreach($permissions as $perm){
108 if($permissionManager->getPermission($perm) ===
null){
109 throw new \InvalidArgumentException(
"Cannot use non-existing permission \"$perm\"");
112 $this->permission = $permissions;
115 public function setPermission(?
string $permission) : void{
116 $this->setPermissions($permission === null ? [] : explode(
";", $permission));
119 public function testPermission(CommandSender $target, ?
string $permission =
null) : bool{
120 if($this->testPermissionSilent($target, $permission)){
124 $message = $this->permissionMessage ?? KnownTranslationFactory::pocketmine_command_error_permission($this->name);
125 if($message instanceof Translatable){
126 $target->sendMessage($message->prefix(TextFormat::RED));
127 }elseif($message !==
""){
128 $target->sendMessage(str_replace(
"<permission>", $permission ?? implode(
";", $this->permission), $message));
134 public function testPermissionSilent(CommandSender $target, ?
string $permission =
null) : bool{
135 $list = $permission !== null ? [$permission] : $this->permission;
136 foreach($list as $p){
137 if($target->hasPermission($p)){
145 public function getLabel() : string{
149 public function setLabel(
string $name) : bool{
150 $this->nextLabel = $name;
151 if(!$this->isRegistered()){
152 $this->label = $name;
164 if($this->allowChangesFrom($commandMap)){
165 $this->commandMap = $commandMap;
173 public function unregister(CommandMap $commandMap) : bool{
174 if($this->allowChangesFrom($commandMap)){
175 $this->commandMap =
null;
176 $this->activeAliases = $this->aliases;
177 $this->label = $this->nextLabel;
185 private function allowChangesFrom(CommandMap $commandMap) : bool{
186 return $this->commandMap === null || $this->commandMap === $commandMap;
189 public function isRegistered() : bool{
190 return $this->commandMap !== null;
198 return $this->activeAliases;
201 public function getPermissionMessage() :
Translatable|string|null{
202 return $this->permissionMessage;
206 return $this->description;
209 public function getUsage() : Translatable|string{
210 return $this->usageMessage;
218 $aliases = array_values($aliases);
219 $this->aliases = $aliases;
220 if(!$this->isRegistered()){
221 $this->activeAliases = $aliases;
225 public function setDescription(
Translatable|
string $description) : void{
226 $this->description = $description;
229 public function setPermissionMessage(Translatable|
string $permissionMessage) : void{
230 $this->permissionMessage = $permissionMessage;
233 public function setUsage(Translatable|
string $usage) : void{
234 $this->usageMessage = $usage;
237 public static function broadcastCommandMessage(CommandSender $source, Translatable|
string $message,
bool $sendToSource =
true) : void{
238 $users = $source->getServer()->getBroadcastChannelSubscribers(Server::BROADCAST_CHANNEL_ADMINISTRATIVE);
239 $result = KnownTranslationFactory::chat_type_admin($source->getName(), $message);
240 $colored = $result->prefix(TextFormat::GRAY . TextFormat::ITALIC);
243 $source->sendMessage($message);
246 foreach($users as $user){
247 if($user instanceof BroadcastLoggerForwarder){
248 $user->sendMessage($result);
249 }elseif($user !== $source){
250 $user->sendMessage($colored);
255 public function __toString() : string{