PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
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
27namespace pocketmine\command;
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
51 private array $aliases = [];
52
57 private array $activeAliases = [];
58
59 private ?CommandMap $commandMap = null;
60
61 protected Translatable|string $description = "";
62
63 protected Translatable|string $usageMessage;
64
66 private array $permission = [];
67 private ?string $permissionMessage = null;
68
73 public function __construct(string $name, Translatable|string $description = "", Translatable|string|null $usageMessage = null, array $aliases = []){
74 $this->name = $name;
75 $this->setLabel($name);
76 $this->setDescription($description);
77 $this->usageMessage = $usageMessage ?? ("/" . $name);
78 $this->setAliases($aliases);
79 }
80
87 abstract public function execute(CommandSender $sender, string $commandLabel, array $args);
88
89 public function getName() : string{
90 return $this->name;
91 }
92
96 public function getPermissions() : array{
97 return $this->permission;
98 }
99
103 public function setPermissions(array $permissions) : void{
104 $permissionManager = PermissionManager::getInstance();
105 foreach($permissions as $perm){
106 if($permissionManager->getPermission($perm) === null){
107 throw new \InvalidArgumentException("Cannot use non-existing permission \"$perm\"");
108 }
109 }
110 $this->permission = $permissions;
111 }
112
113 public function setPermission(?string $permission) : void{
114 $this->setPermissions($permission === null ? [] : explode(";", $permission));
115 }
116
117 public function testPermission(CommandSender $target, ?string $permission = null) : bool{
118 if($this->testPermissionSilent($target, $permission)){
119 return true;
120 }
121
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));
126 }
127
128 return false;
129 }
130
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)){
135 return true;
136 }
137 }
138
139 return false;
140 }
141
142 public function getLabel() : string{
143 return $this->label;
144 }
145
146 public function setLabel(string $name) : bool{
147 $this->nextLabel = $name;
148 if(!$this->isRegistered()){
149 $this->label = $name;
150
151 return true;
152 }
153
154 return false;
155 }
156
160 public function register(CommandMap $commandMap) : bool{
161 if($this->allowChangesFrom($commandMap)){
162 $this->commandMap = $commandMap;
163
164 return true;
165 }
166
167 return false;
168 }
169
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;
175
176 return true;
177 }
178
179 return false;
180 }
181
182 private function allowChangesFrom(CommandMap $commandMap) : bool{
183 return $this->commandMap === null || $this->commandMap === $commandMap;
184 }
185
186 public function isRegistered() : bool{
187 return $this->commandMap !== null;
188 }
189
194 public function getAliases() : array{
195 return $this->activeAliases;
196 }
197
198 public function getPermissionMessage() : ?string{
199 return $this->permissionMessage;
200 }
201
202 public function getDescription() : Translatable|string{
203 return $this->description;
204 }
205
206 public function getUsage() : Translatable|string{
207 return $this->usageMessage;
208 }
209
214 public function setAliases(array $aliases) : void{
215 $this->aliases = $aliases;
216 if(!$this->isRegistered()){
217 $this->activeAliases = $aliases;
218 }
219 }
220
221 public function setDescription(Translatable|string $description) : void{
222 $this->description = $description;
223 }
224
225 public function setPermissionMessage(string $permissionMessage) : void{
226 $this->permissionMessage = $permissionMessage;
227 }
228
229 public function setUsage(Translatable|string $usage) : void{
230 $this->usageMessage = $usage;
231 }
232
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);
237
238 if($sendToSource){
239 $source->sendMessage($message);
240 }
241
242 foreach($users as $user){
243 if($user instanceof BroadcastLoggerForwarder){
244 $user->sendMessage($result);
245 }elseif($user !== $source){
246 $user->sendMessage($colored);
247 }
248 }
249 }
250
251 public function __toString() : string{
252 return $this->name;
253 }
254}
__construct(string $name, Translatable|string $description="", Translatable|string|null $usageMessage=null, array $aliases=[])
Definition Command.php:73
execute(CommandSender $sender, string $commandLabel, array $args)
setAliases(array $aliases)
Definition Command.php:214
setPermissions(array $permissions)
Definition Command.php:103