44 private string $nextLabel;
45 private string $label;
51 private array $aliases = [];
57 private array $activeAliases = [];
66 private array $permission = [];
67 private ?
string $permissionMessage =
null;
75 $this->setLabel($name);
76 $this->setDescription($description);
77 $this->usageMessage = $usageMessage ?? (
"/" . $name);
89 public function getName() : string{
97 return $this->permission;
105 foreach($permissions as $perm){
106 if($permissionManager->getPermission($perm) ===
null){
107 throw new \InvalidArgumentException(
"Cannot use non-existing permission \"$perm\"");
110 $this->permission = $permissions;
113 public function setPermission(?
string $permission) : void{
114 $this->setPermissions($permission === null ? [] : explode(
";", $permission));
117 public function testPermission(CommandSender $target, ?
string $permission =
null) : bool{
118 if($this->testPermissionSilent($target, $permission)){
122 if($this->permissionMessage ===
null){
123 $target->sendMessage(KnownTranslationFactory::pocketmine_command_error_permission($this->name)->prefix(TextFormat::RED));
124 }elseif($this->permissionMessage !==
""){
125 $target->sendMessage(str_replace(
"<permission>", $permission ?? implode(
";", $this->permission), $this->permissionMessage));
131 public function testPermissionSilent(CommandSender $target, ?
string $permission =
null) : bool{
132 $list = $permission !== null ? [$permission] : $this->permission;
133 foreach($list as $p){
134 if($target->hasPermission($p)){
142 public function getLabel() : string{
146 public function setLabel(
string $name) : bool{
147 $this->nextLabel = $name;
148 if(!$this->isRegistered()){
149 $this->label = $name;
161 if($this->allowChangesFrom($commandMap)){
162 $this->commandMap = $commandMap;
170 public function unregister(CommandMap $commandMap) : bool{
171 if($this->allowChangesFrom($commandMap)){
172 $this->commandMap =
null;
173 $this->activeAliases = $this->aliases;
174 $this->label = $this->nextLabel;
182 private function allowChangesFrom(CommandMap $commandMap) : bool{
183 return $this->commandMap === null || $this->commandMap === $commandMap;
186 public function isRegistered() : bool{
187 return $this->commandMap !== null;
195 return $this->activeAliases;
198 public function getPermissionMessage() : ?string{
199 return $this->permissionMessage;
203 return $this->description;
206 public function getUsage() : Translatable|string{
207 return $this->usageMessage;
215 $this->aliases = $aliases;
216 if(!$this->isRegistered()){
217 $this->activeAliases = $aliases;
221 public function setDescription(
Translatable|
string $description) : void{
222 $this->description = $description;
225 public function setPermissionMessage(
string $permissionMessage) : void{
226 $this->permissionMessage = $permissionMessage;
229 public function setUsage(Translatable|
string $usage) : void{
230 $this->usageMessage = $usage;
233 public static function broadcastCommandMessage(CommandSender $source, Translatable|
string $message,
bool $sendToSource =
true) : void{
234 $users = $source->getServer()->getBroadcastChannelSubscribers(Server::BROADCAST_CHANNEL_ADMINISTRATIVE);
235 $result = KnownTranslationFactory::chat_type_admin($source->getName(), $message);
236 $colored = $result->prefix(TextFormat::GRAY . TextFormat::ITALIC);
239 $source->sendMessage($message);
242 foreach($users as $user){
243 if($user instanceof BroadcastLoggerForwarder){
244 $user->sendMessage($result);
245 }elseif($user !== $source){
246 $user->sendMessage($colored);
251 public function __toString() : string{