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