PocketMine-MP 5.35.1 git-f412a390b8f63d0311cc1d1c81046404153b8440
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;
39use function strtolower;
40use function trim;
41use const PHP_INT_MAX;
42
43abstract class Command{
44 private readonly string $namespace;
45 private readonly string $name;
46
48 private array $permission = [];
49 private Translatable|string|null $permissionMessage = null;
50
51 public function __construct(
52 string $namespace,
53 string $name,
54 private Translatable|string $description = "",
55 private Translatable|string|null $usageMessage = null
56 ){
57 if($namespace === ""){
58 throw new \InvalidArgumentException("Command namespace cannot be empty (set it to, for example, your plugin's name)");
59 }
60 if($name === ""){
61 throw new \InvalidArgumentException("Command name cannot be empty");
62 }
63 $this->namespace = strtolower(trim($namespace));
64 //TODO: case handling inconsistency preserved from old code
65 $this->name = trim($name);
66 }
67
75 abstract public function execute(CommandSender $sender, string $commandLabel, array $args);
76
77 final public function getNamespace() : string{
78 return $this->namespace;
79 }
80
85 final public function getName() : string{
86 return $this->name;
87 }
88
92 final public function getId() : string{
93 return "$this->namespace:$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
125 public function testPermission(string $context, CommandSender $target, ?string $permission = null) : bool{
126 if($this->testPermissionSilent($target, $permission)){
127 return true;
128 }
129
130 $message = $this->permissionMessage ?? KnownTranslationFactory::pocketmine_command_error_permission($context);
131 if($message instanceof Translatable){
132 $target->sendMessage($message->prefix(TextFormat::RED));
133 }elseif($message !== ""){
134 $target->sendMessage(str_replace("<permission>", $permission ?? implode(";", $this->permission), $message));
135 }
136
137 return false;
138 }
139
140 public function testPermissionSilent(CommandSender $target, ?string $permission = null) : bool{
141 $list = $permission !== null ? [$permission] : $this->permission;
142 foreach($list as $p){
143 if($target->hasPermission($p)){
144 return true;
145 }
146 }
147
148 return false;
149 }
150
151 public function getPermissionMessage() : Translatable|string|null{
152 return $this->permissionMessage;
153 }
154
155 public function getDescription() : Translatable|string{
156 return $this->description;
157 }
158
159 public function getUsage() : Translatable|string|null{
160 return $this->usageMessage;
161 }
162
163 public function setDescription(Translatable|string $description) : void{
164 $this->description = $description;
165 }
166
167 public function setPermissionMessage(Translatable|string $permissionMessage) : void{
168 $this->permissionMessage = $permissionMessage;
169 }
170
171 public function setUsage(Translatable|string|null $usage) : void{
172 $this->usageMessage = $usage;
173 }
174
175 public static function broadcastCommandMessage(CommandSender $source, Translatable|string $message, bool $sendToSource = true) : void{
176 $users = $source->getServer()->getBroadcastChannelSubscribers(Server::BROADCAST_CHANNEL_ADMINISTRATIVE);
177 $result = KnownTranslationFactory::chat_type_admin($source->getName(), $message);
178 $colored = $result->prefix(TextFormat::GRAY . TextFormat::ITALIC);
179
180 if($sendToSource){
181 $source->sendMessage($message);
182 }
183
184 foreach($users as $user){
185 if($user instanceof BroadcastLoggerForwarder){
186 $user->sendMessage($result);
187 }elseif($user !== $source){
188 $user->sendMessage($colored);
189 }
190 }
191 }
192}
testPermission(string $context, CommandSender $target, ?string $permission=null)
Definition Command.php:125
execute(CommandSender $sender, string $commandLabel, array $args)
setPermissions(array $permissions)
Definition Command.php:106