PocketMine-MP 5.23.2 git-45917d495c14f3ce37bbf0848262ad8a470030f3
Loading...
Searching...
No Matches
SimpleCommandMap.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
24namespace pocketmine\command;
25
75use function array_shift;
76use function count;
77use function implode;
78use function str_contains;
79use function strcasecmp;
80use function strtolower;
81use function trim;
82
84
89 protected array $knownCommands = [];
90
91 public function __construct(private Server $server){
92 $this->setDefaultCommands();
93 }
94
95 private function setDefaultCommands() : void{
96 $this->registerAll("pocketmine", [
97 new BanCommand(),
98 new BanIpCommand(),
99 new BanListCommand(),
100 new ClearCommand(),
102 new DeopCommand(),
103 new DifficultyCommand(),
104 new DumpMemoryCommand(),
105 new EffectCommand(),
106 new EnchantCommand(),
107 new GamemodeCommand(),
109 new GiveCommand(),
110 new HelpCommand(),
111 new KickCommand(),
112 new KillCommand(),
113 new ListCommand(),
114 new MeCommand(),
115 new OpCommand(),
116 new PardonCommand(),
117 new PardonIpCommand(),
118 new ParticleCommand(),
119 new PluginsCommand(),
120 new SaveCommand(),
121 new SaveOffCommand(),
122 new SaveOnCommand(),
123 new SayCommand(),
124 new SeedCommand(),
126 new SpawnpointCommand(),
127 new StatusCommand(),
128 new StopCommand(),
129 new TeleportCommand(),
130 new TellCommand(),
131 new TimeCommand(),
132 new TimingsCommand(),
133 new TitleCommand(),
135 new VersionCommand(),
136 new WhitelistCommand(),
137 new XpCommand(),
138 ]);
139 }
140
141 public function registerAll(string $fallbackPrefix, array $commands) : void{
142 foreach($commands as $command){
143 $this->register($fallbackPrefix, $command);
144 }
145 }
146
147 public function register(string $fallbackPrefix, Command $command, ?string $label = null) : bool{
148 if(count($command->getPermissions()) === 0){
149 throw new \InvalidArgumentException("Commands must have a permission set");
150 }
151
152 if($label === null){
153 $label = $command->getLabel();
154 }
155 $label = trim($label);
156 $fallbackPrefix = strtolower(trim($fallbackPrefix));
157
158 $registered = $this->registerAlias($command, false, $fallbackPrefix, $label);
159
160 $aliases = $command->getAliases();
161 foreach($aliases as $index => $alias){
162 if(!$this->registerAlias($command, true, $fallbackPrefix, $alias)){
163 unset($aliases[$index]);
164 }
165 }
166 $command->setAliases($aliases);
167
168 if(!$registered){
169 $command->setLabel($fallbackPrefix . ":" . $label);
170 }
171
172 $command->register($this);
173
174 return $registered;
175 }
176
177 public function unregister(Command $command) : bool{
178 foreach(Utils::promoteKeys($this->knownCommands) as $lbl => $cmd){
179 if($cmd === $command){
180 unset($this->knownCommands[$lbl]);
181 }
182 }
183
184 $command->unregister($this);
185
186 return true;
187 }
188
189 private function registerAlias(Command $command, bool $isAlias, string $fallbackPrefix, string $label) : bool{
190 $this->knownCommands[$fallbackPrefix . ":" . $label] = $command;
191 if(($command instanceof VanillaCommand || $isAlias) && isset($this->knownCommands[$label])){
192 return false;
193 }
194
195 if(isset($this->knownCommands[$label]) && $this->knownCommands[$label]->getLabel() === $label){
196 return false;
197 }
198
199 if(!$isAlias){
200 $command->setLabel($label);
201 }
202
203 $this->knownCommands[$label] = $command;
204
205 return true;
206 }
207
208 public function dispatch(CommandSender $sender, string $commandLine) : bool{
209 $args = CommandStringHelper::parseQuoteAware($commandLine);
210
211 $sentCommandLabel = array_shift($args);
212 if($sentCommandLabel !== null && ($target = $this->getCommand($sentCommandLabel)) !== null){
213 $timings = Timings::getCommandDispatchTimings($target->getLabel());
214 $timings->startTiming();
215
216 try{
217 if($target->testPermission($sender)){
218 $target->execute($sender, $sentCommandLabel, $args);
219 }
220 }catch(InvalidCommandSyntaxException $e){
221 $sender->sendMessage($sender->getLanguage()->translate(KnownTranslationFactory::commands_generic_usage($target->getUsage())));
222 }finally{
223 $timings->stopTiming();
224 }
225 return true;
226 }
227
228 $sender->sendMessage(KnownTranslationFactory::pocketmine_command_notFound($sentCommandLabel ?? "", "/help")->prefix(TextFormat::RED));
229 return false;
230 }
231
232 public function clearCommands() : void{
233 foreach($this->knownCommands as $command){
234 $command->unregister($this);
235 }
236 $this->knownCommands = [];
237 $this->setDefaultCommands();
238 }
239
240 public function getCommand(string $name) : ?Command{
241 return $this->knownCommands[$name] ?? null;
242 }
243
248 public function getCommands() : array{
249 return $this->knownCommands;
250 }
251
252 public function registerServerAliases() : void{
253 $values = $this->server->getCommandAliases();
254
255 foreach(Utils::stringifyKeys($values) as $alias => $commandStrings){
256 if(str_contains($alias, ":")){
257 $this->server->getLogger()->warning($this->server->getLanguage()->translate(KnownTranslationFactory::pocketmine_command_alias_illegal($alias)));
258 continue;
259 }
260
261 $targets = [];
262 $bad = [];
263 $recursive = [];
264
265 foreach($commandStrings as $commandString){
266 $args = CommandStringHelper::parseQuoteAware($commandString);
267 $commandName = array_shift($args) ?? "";
268 $command = $this->getCommand($commandName);
269
270 if($command === null){
271 $bad[] = $commandString;
272 }elseif(strcasecmp($commandName, $alias) === 0){
273 $recursive[] = $commandString;
274 }else{
275 $targets[] = $commandString;
276 }
277 }
278
279 if(count($recursive) > 0){
280 $this->server->getLogger()->warning($this->server->getLanguage()->translate(KnownTranslationFactory::pocketmine_command_alias_recursive($alias, implode(", ", $recursive))));
281 continue;
282 }
283
284 if(count($bad) > 0){
285 $this->server->getLogger()->warning($this->server->getLanguage()->translate(KnownTranslationFactory::pocketmine_command_alias_notFound($alias, implode(", ", $bad))));
286 continue;
287 }
288
289 //These registered commands have absolute priority
290 $lowerAlias = strtolower($alias);
291 if(count($targets) > 0){
292 $this->knownCommands[$lowerAlias] = new FormattedCommandAlias($lowerAlias, $targets);
293 }else{
294 unset($this->knownCommands[$lowerAlias]);
295 }
296
297 }
298 }
299}
registerAll(string $fallbackPrefix, array $commands)