PocketMine-MP 5.21.2 git-a6534ecbbbcf369264567d27e5ed70f7f5be9816
Loading...
Searching...
No Matches
Command.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
28
36use function explode;
37use function implode;
38use function str_replace;
39
40abstract class Command{
41
42 private string $name;
43
44 private string $nextLabel;
45 private string $label;
46
48 private array $aliases = [];
49
51 private array $activeAliases = [];
52
53 private ?CommandMap $commandMap = null;
54
55 protected Translatable|string $description = "";
56
57 protected Translatable|string $usageMessage;
58
60 private array $permission = [];
61 private Translatable|string|null $permissionMessage = null;
62
66 public function __construct(string $name, Translatable|string $description = "", Translatable|string|null $usageMessage = null, array $aliases = []){
67 $this->name = $name;
68 $this->setLabel($name);
69 $this->setDescription($description);
70 $this->usageMessage = $usageMessage ?? ("/" . $name);
71 $this->setAliases($aliases);
72 }
73
80 abstract public function execute(CommandSender $sender, string $commandLabel, array $args);
81
82 public function getName() : string{
83 return $this->name;
84 }
85
89 public function getPermissions() : array{
90 return $this->permission;
91 }
92
96 public function setPermissions(array $permissions) : void{
97 $permissionManager = PermissionManager::getInstance();
98 foreach($permissions as $perm){
99 if($permissionManager->getPermission($perm) === null){
100 throw new \InvalidArgumentException("Cannot use non-existing permission \"$perm\"");
101 }
102 }
103 $this->permission = $permissions;
104 }
105
106 public function setPermission(?string $permission) : void{
107 $this->setPermissions($permission === null ? [] : explode(";", $permission));
108 }
109
110 public function testPermission(CommandSender $target, ?string $permission = null) : bool{
111 if($this->testPermissionSilent($target, $permission)){
112 return true;
113 }
114
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));
120 }
121
122 return false;
123 }
124
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)){
129 return true;
130 }
131 }
132
133 return false;
134 }
135
136 public function getLabel() : string{
137 return $this->label;
138 }
139
140 public function setLabel(string $name) : bool{
141 $this->nextLabel = $name;
142 if(!$this->isRegistered()){
143 $this->label = $name;
144
145 return true;
146 }
147
148 return false;
149 }
150
154 public function register(CommandMap $commandMap) : bool{
155 if($this->allowChangesFrom($commandMap)){
156 $this->commandMap = $commandMap;
157
158 return true;
159 }
160
161 return false;
162 }
163
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;
169
170 return true;
171 }
172
173 return false;
174 }
175
176 private function allowChangesFrom(CommandMap $commandMap) : bool{
177 return $this->commandMap === null || $this->commandMap === $commandMap;
178 }
179
180 public function isRegistered() : bool{
181 return $this->commandMap !== null;
182 }
183
187 public function getAliases() : array{
188 return $this->activeAliases;
189 }
190
191 public function getPermissionMessage() : Translatable|string|null{
192 return $this->permissionMessage;
193 }
194
195 public function getDescription() : Translatable|string{
196 return $this->description;
197 }
198
199 public function getUsage() : Translatable|string{
200 return $this->usageMessage;
201 }
202
206 public function setAliases(array $aliases) : void{
207 $this->aliases = $aliases;
208 if(!$this->isRegistered()){
209 $this->activeAliases = $aliases;
210 }
211 }
212
213 public function setDescription(Translatable|string $description) : void{
214 $this->description = $description;
215 }
216
217 public function setPermissionMessage(Translatable|string $permissionMessage) : void{
218 $this->permissionMessage = $permissionMessage;
219 }
220
221 public function setUsage(Translatable|string $usage) : void{
222 $this->usageMessage = $usage;
223 }
224
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);
229
230 if($sendToSource){
231 $source->sendMessage($message);
232 }
233
234 foreach($users as $user){
235 if($user instanceof BroadcastLoggerForwarder){
236 $user->sendMessage($result);
237 }elseif($user !== $source){
238 $user->sendMessage($colored);
239 }
240 }
241 }
242
243 public function __toString() : string{
244 return $this->name;
245 }
246}
__construct(string $name, Translatable|string $description="", Translatable|string|null $usageMessage=null, array $aliases=[])
Definition Command.php:66
execute(CommandSender $sender, string $commandLabel, array $args)
setAliases(array $aliases)
Definition Command.php:206
setPermissions(array $permissions)
Definition Command.php:96