44 private string $nextLabel;
45 private string $label;
48 private array $aliases = [];
51 private array $activeAliases = [];
60 private array $permission = [];
61 private Translatable|
string|
null $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 $message = $this->permissionMessage ?? KnownTranslationFactory::pocketmine_command_error_permission($this->name);
116 if($message instanceof Translatable){
117 $target->sendMessage($message->prefix(TextFormat::RED));
118 }elseif($message !==
""){
119 $target->sendMessage(str_replace(
"<permission>", $permission ?? implode(
";", $this->permission), $message));
125 public function testPermissionSilent(CommandSender $target, ?
string $permission =
null) : bool{
126 $list = $permission !== null ? [$permission] : $this->permission;
127 foreach($list as $p){
128 if($target->hasPermission($p)){
136 public function getLabel() : string{
140 public function setLabel(
string $name) : bool{
141 $this->nextLabel = $name;
142 if(!$this->isRegistered()){
143 $this->label = $name;
155 if($this->allowChangesFrom($commandMap)){
156 $this->commandMap = $commandMap;
164 public function unregister(CommandMap $commandMap) : bool{
165 if($this->allowChangesFrom($commandMap)){
166 $this->commandMap =
null;
167 $this->activeAliases = $this->aliases;
168 $this->label = $this->nextLabel;
176 private function allowChangesFrom(CommandMap $commandMap) : bool{
177 return $this->commandMap === null || $this->commandMap === $commandMap;
180 public function isRegistered() : bool{
181 return $this->commandMap !== null;
188 return $this->activeAliases;
191 public function getPermissionMessage() :
Translatable|string|null{
192 return $this->permissionMessage;
196 return $this->description;
199 public function getUsage() : Translatable|string{
200 return $this->usageMessage;
207 $this->aliases = $aliases;
208 if(!$this->isRegistered()){
209 $this->activeAliases = $aliases;
213 public function setDescription(
Translatable|
string $description) : void{
214 $this->description = $description;
217 public function setPermissionMessage(Translatable|
string $permissionMessage) : void{
218 $this->permissionMessage = $permissionMessage;
221 public function setUsage(Translatable|
string $usage) : void{
222 $this->usageMessage = $usage;
225 public static function broadcastCommandMessage(CommandSender $source, Translatable|
string $message,
bool $sendToSource =
true) : void{
226 $users = $source->getServer()->getBroadcastChannelSubscribers(Server::BROADCAST_CHANNEL_ADMINISTRATIVE);
227 $result = KnownTranslationFactory::chat_type_admin($source->getName(), $message);
228 $colored = $result->prefix(TextFormat::GRAY . TextFormat::ITALIC);
231 $source->sendMessage($message);
234 foreach($users as $user){
235 if($user instanceof BroadcastLoggerForwarder){
236 $user->sendMessage($result);
237 }elseif($user !== $source){
238 $user->sendMessage($colored);
243 public function __toString() : string{