PocketMine-MP 5.27.1 git-9af3cde03fabbe4129c79e46dc87ffa0fff446e6
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 array_values;
37use function explode;
38use function implode;
39use function str_replace;
40use const PHP_INT_MAX;
41
42abstract class Command{
43
44 private string $name;
45
46 private string $nextLabel;
47 private string $label;
48
53 private array $aliases = [];
54
59 private array $activeAliases = [];
60
61 private ?CommandMap $commandMap = null;
62
63 protected Translatable|string $description = "";
64
65 protected Translatable|string $usageMessage;
66
68 private array $permission = [];
69 private Translatable|string|null $permissionMessage = null;
70
75 public function __construct(string $name, Translatable|string $description = "", Translatable|string|null $usageMessage = null, array $aliases = []){
76 $this->name = $name;
77 $this->setLabel($name);
78 $this->setDescription($description);
79 $this->usageMessage = $usageMessage ?? ("/" . $name);
80 $this->setAliases($aliases);
81 }
82
90 abstract public function execute(CommandSender $sender, string $commandLabel, array $args);
91
92 public function getName() : string{
93 return $this->name;
94 }
95
99 public function getPermissions() : array{
100 return $this->permission;
101 }
102
106 public function setPermissions(array $permissions) : void{
107 $permissionManager = PermissionManager::getInstance();
108 foreach($permissions as $perm){
109 if($permissionManager->getPermission($perm) === null){
110 throw new \InvalidArgumentException("Cannot use non-existing permission \"$perm\"");
111 }
112 }
113 $this->permission = $permissions;
114 }
115
116 public function setPermission(?string $permission) : void{
117 $this->setPermissions($permission === null ? [] : explode(";", $permission, limit: PHP_INT_MAX));
118 }
119
120 public function testPermission(CommandSender $target, ?string $permission = null) : bool{
121 if($this->testPermissionSilent($target, $permission)){
122 return true;
123 }
124
125 $message = $this->permissionMessage ?? KnownTranslationFactory::pocketmine_command_error_permission($this->name);
126 if($message instanceof Translatable){
127 $target->sendMessage($message->prefix(TextFormat::RED));
128 }elseif($message !== ""){
129 $target->sendMessage(str_replace("<permission>", $permission ?? implode(";", $this->permission), $message));
130 }
131
132 return false;
133 }
134
135 public function testPermissionSilent(CommandSender $target, ?string $permission = null) : bool{
136 $list = $permission !== null ? [$permission] : $this->permission;
137 foreach($list as $p){
138 if($target->hasPermission($p)){
139 return true;
140 }
141 }
142
143 return false;
144 }
145
146 public function getLabel() : string{
147 return $this->label;
148 }
149
150 public function setLabel(string $name) : bool{
151 $this->nextLabel = $name;
152 if(!$this->isRegistered()){
153 $this->label = $name;
154
155 return true;
156 }
157
158 return false;
159 }
160
164 public function register(CommandMap $commandMap) : bool{
165 if($this->allowChangesFrom($commandMap)){
166 $this->commandMap = $commandMap;
167
168 return true;
169 }
170
171 return false;
172 }
173
174 public function unregister(CommandMap $commandMap) : bool{
175 if($this->allowChangesFrom($commandMap)){
176 $this->commandMap = null;
177 $this->activeAliases = $this->aliases;
178 $this->label = $this->nextLabel;
179
180 return true;
181 }
182
183 return false;
184 }
185
186 private function allowChangesFrom(CommandMap $commandMap) : bool{
187 return $this->commandMap === null || $this->commandMap === $commandMap;
188 }
189
190 public function isRegistered() : bool{
191 return $this->commandMap !== null;
192 }
193
198 public function getAliases() : array{
199 return $this->activeAliases;
200 }
201
202 public function getPermissionMessage() : Translatable|string|null{
203 return $this->permissionMessage;
204 }
205
206 public function getDescription() : Translatable|string{
207 return $this->description;
208 }
209
210 public function getUsage() : Translatable|string{
211 return $this->usageMessage;
212 }
213
218 public function setAliases(array $aliases) : void{
219 $aliases = array_values($aliases); //because plugins can and will pass crap
220 $this->aliases = $aliases;
221 if(!$this->isRegistered()){
222 $this->activeAliases = $aliases;
223 }
224 }
225
226 public function setDescription(Translatable|string $description) : void{
227 $this->description = $description;
228 }
229
230 public function setPermissionMessage(Translatable|string $permissionMessage) : void{
231 $this->permissionMessage = $permissionMessage;
232 }
233
234 public function setUsage(Translatable|string $usage) : void{
235 $this->usageMessage = $usage;
236 }
237
238 public static function broadcastCommandMessage(CommandSender $source, Translatable|string $message, bool $sendToSource = true) : void{
239 $users = $source->getServer()->getBroadcastChannelSubscribers(Server::BROADCAST_CHANNEL_ADMINISTRATIVE);
240 $result = KnownTranslationFactory::chat_type_admin($source->getName(), $message);
241 $colored = $result->prefix(TextFormat::GRAY . TextFormat::ITALIC);
242
243 if($sendToSource){
244 $source->sendMessage($message);
245 }
246
247 foreach($users as $user){
248 if($user instanceof BroadcastLoggerForwarder){
249 $user->sendMessage($result);
250 }elseif($user !== $source){
251 $user->sendMessage($colored);
252 }
253 }
254 }
255
256 public function __toString() : string{
257 return $this->name;
258 }
259}
__construct(string $name, Translatable|string $description="", Translatable|string|null $usageMessage=null, array $aliases=[])
Definition Command.php:75
execute(CommandSender $sender, string $commandLabel, array $args)
setAliases(array $aliases)
Definition Command.php:218
setPermissions(array $permissions)
Definition Command.php:106