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