PocketMine-MP 5.21.2 git-b2aa6396c3cc2cafdd815eacc360e1ad89599899
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 ?string $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 if($this->permissionMessage === null){
116 $target->sendMessage(KnownTranslationFactory::pocketmine_command_error_permission($this->name)->prefix(TextFormat::RED));
117 }elseif($this->permissionMessage !== ""){
118 $target->sendMessage(str_replace("<permission>", $permission ?? implode(";", $this->permission), $this->permissionMessage));
119 }
120
121 return false;
122 }
123
124 public function testPermissionSilent(CommandSender $target, ?string $permission = null) : bool{
125 $list = $permission !== null ? [$permission] : $this->permission;
126 foreach($list as $p){
127 if($target->hasPermission($p)){
128 return true;
129 }
130 }
131
132 return false;
133 }
134
135 public function getLabel() : string{
136 return $this->label;
137 }
138
139 public function setLabel(string $name) : bool{
140 $this->nextLabel = $name;
141 if(!$this->isRegistered()){
142 $this->label = $name;
143
144 return true;
145 }
146
147 return false;
148 }
149
153 public function register(CommandMap $commandMap) : bool{
154 if($this->allowChangesFrom($commandMap)){
155 $this->commandMap = $commandMap;
156
157 return true;
158 }
159
160 return false;
161 }
162
163 public function unregister(CommandMap $commandMap) : bool{
164 if($this->allowChangesFrom($commandMap)){
165 $this->commandMap = null;
166 $this->activeAliases = $this->aliases;
167 $this->label = $this->nextLabel;
168
169 return true;
170 }
171
172 return false;
173 }
174
175 private function allowChangesFrom(CommandMap $commandMap) : bool{
176 return $this->commandMap === null || $this->commandMap === $commandMap;
177 }
178
179 public function isRegistered() : bool{
180 return $this->commandMap !== null;
181 }
182
186 public function getAliases() : array{
187 return $this->activeAliases;
188 }
189
190 public function getPermissionMessage() : ?string{
191 return $this->permissionMessage;
192 }
193
194 public function getDescription() : Translatable|string{
195 return $this->description;
196 }
197
198 public function getUsage() : Translatable|string{
199 return $this->usageMessage;
200 }
201
205 public function setAliases(array $aliases) : void{
206 $this->aliases = $aliases;
207 if(!$this->isRegistered()){
208 $this->activeAliases = $aliases;
209 }
210 }
211
212 public function setDescription(Translatable|string $description) : void{
213 $this->description = $description;
214 }
215
216 public function setPermissionMessage(string $permissionMessage) : void{
217 $this->permissionMessage = $permissionMessage;
218 }
219
220 public function setUsage(Translatable|string $usage) : void{
221 $this->usageMessage = $usage;
222 }
223
224 public static function broadcastCommandMessage(CommandSender $source, Translatable|string $message, bool $sendToSource = true) : void{
225 $users = $source->getServer()->getBroadcastChannelSubscribers(Server::BROADCAST_CHANNEL_ADMINISTRATIVE);
226 $result = KnownTranslationFactory::chat_type_admin($source->getName(), $message);
227 $colored = $result->prefix(TextFormat::GRAY . TextFormat::ITALIC);
228
229 if($sendToSource){
230 $source->sendMessage($message);
231 }
232
233 foreach($users as $user){
234 if($user instanceof BroadcastLoggerForwarder){
235 $user->sendMessage($result);
236 }elseif($user !== $source){
237 $user->sendMessage($colored);
238 }
239 }
240 }
241
242 public function __toString() : string{
243 return $this->name;
244 }
245}
__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:205
setPermissions(array $permissions)
Definition Command.php:96