PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
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 
25 namespace pocketmine\command;
26 
30 
31 abstract class Command{
33  private $name;
34 
36  private $nextLabel;
37 
39  private $label;
40 
44  private $aliases = [];
45 
49  private $activeAliases = [];
50 
52  private $commandMap = null;
53 
55  protected $description = "";
56 
58  protected $usageMessage;
59 
61  private $permission = null;
62 
64  private $permissionMessage = null;
65 
67  public $timings;
68 
75  public function __construct($name, $description = "", $usageMessage = null, array $aliases = []){
76  $this->name = $name;
77  $this->nextLabel = $name;
78  $this->label = $name;
79  $this->description = $description;
80  $this->usageMessage = $usageMessage === null ? "/" . $name : $usageMessage;
81  $this->aliases = $aliases;
82  $this->activeAliases = (array) $aliases;
83  $this->timings = new TimingsHandler("** Command: " . $name);
84  }
85 
93  public abstract function execute(CommandSender $sender, $commandLabel, array $args);
94 
98  public function getName(){
99  return $this->name;
100  }
101 
105  public function getPermission(){
106  return $this->permission;
107  }
108 
112  public function setPermission($permission){
113  $this->permission = $permission;
114  }
115 
121  public function testPermission(CommandSender $target){
122  if($this->testPermissionSilent($target)){
123  return true;
124  }
125 
126  if($this->permissionMessage === null){
127  $target->sendMessage(TextFormat::RED . "You don't have permissions to use this command.");
128  }elseif($this->permissionMessage !== ""){
129  $target->sendMessage(str_replace("<permission>", $this->permission, $this->permissionMessage));
130  }
131 
132  return false;
133  }
134 
140  public function testPermissionSilent(CommandSender $target){
141  if($this->permission === null or $this->permission === ""){
142  return true;
143  }
144 
145  foreach(explode(";", $this->permission) as $permission){
146  if($target->hasPermission($permission)){
147  return true;
148  }
149  }
150 
151  return false;
152  }
153 
157  public function getLabel(){
158  return $this->label;
159  }
160 
161  public function setLabel($name){
162  $this->nextLabel = $name;
163  if(!$this->isRegistered()){
164  $this->timings = new TimingsHandler("** Command: " . $name);
165  $this->label = $name;
166 
167  return true;
168  }
169 
170  return false;
171  }
172 
180  public function register(CommandMap $commandMap){
181  if($this->allowChangesFrom($commandMap)){
182  $this->commandMap = $commandMap;
183 
184  return true;
185  }
186 
187  return false;
188  }
189 
195  public function unregister(CommandMap $commandMap){
196  if($this->allowChangesFrom($commandMap)){
197  $this->commandMap = null;
198  $this->activeAliases = $this->aliases;
199  $this->label = $this->nextLabel;
200 
201  return true;
202  }
203 
204  return false;
205  }
206 
212  private function allowChangesFrom(CommandMap $commandMap){
213  return $this->commandMap === null or $this->commandMap === $commandMap;
214  }
215 
219  public function isRegistered(){
220  return $this->commandMap !== null;
221  }
222 
226  public function getAliases(){
227  return $this->activeAliases;
228  }
229 
233  public function getPermissionMessage(){
234  return $this->permissionMessage;
235  }
236 
240  public function getDescription(){
241  return $this->description;
242  }
243 
247  public function getUsage(){
248  return $this->usageMessage;
249  }
250 
254  public function setAliases(array $aliases){
255  $this->aliases = $aliases;
256  if(!$this->isRegistered()){
257  $this->activeAliases = (array) $aliases;
258  }
259  }
260 
264  public function setDescription($description){
265  $this->description = $description;
266  }
267 
271  public function setPermissionMessage($permissionMessage){
272  $this->permissionMessage = $permissionMessage;
273  }
274 
278  public function setUsage($usage){
279  $this->usageMessage = $usage;
280  }
281 
287  public static function broadcastCommandMessage(CommandSender $source, $message, $sendToSource = true){
288  $result = $source->getName() . ": " . $message;
289 
290  //Command minecarts or command blocks are not implemented
291 
292  $users = Server::getInstance()->getPluginManager()->getPermissionSubscriptions(Server::BROADCAST_CHANNEL_ADMINISTRATIVE);
293  $colored = TextFormat::GRAY . TextFormat::ITALIC . "[$result" . TextFormat::GRAY . TextFormat::ITALIC . "]";
294  if($sendToSource === true and !($source instanceof ConsoleCommandSender)){
295  $source->sendMessage($message);
296  }
297 
298  foreach($users as $user){
299  if($user instanceof CommandSender){
300  if($user instanceof ConsoleCommandSender){
301  $user->sendMessage($result);
302  }elseif($user !== $source){
303  $user->sendMessage($colored);
304  }
305  }
306  }
307  }
308 
312  public function __toString(){
313  return $this->name;
314  }
315 }
execute(CommandSender $sender, $commandLabel, array $args)
__construct($name, $description="", $usageMessage=null, array $aliases=[])
Definition: Command.php:75
testPermissionSilent(CommandSender $target)
Definition: Command.php:140
setAliases(array $aliases)
Definition: Command.php:254
static getInstance()
Definition: Server.php:1444
setPermissionMessage($permissionMessage)
Definition: Command.php:271
unregister(CommandMap $commandMap)
Definition: Command.php:195
setDescription($description)
Definition: Command.php:264
static broadcastCommandMessage(CommandSender $source, $message, $sendToSource=true)
Definition: Command.php:287
testPermission(CommandSender $target)
Definition: Command.php:121
setPermission($permission)
Definition: Command.php:112