PocketMine-MP 5.39.3 git-400eb2dddf91a9c112aa09f3b498ffc8c85e98ed
Loading...
Searching...
No Matches
FormattedCommandAlias.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
32use function array_shift;
33use function count;
34use function implode;
35use function preg_match;
36use function strlen;
37use function strpos;
38use function substr;
39
52 private const FORMAT_STRING_REGEX = '/\G\$(\$)?((?!0)+\d+)(-)?/';
53
57 public function __construct(
58 string $alias,
59 private array $formatStrings
60 ){
61 parent::__construct($alias, KnownTranslationFactory::pocketmine_command_userDefined_description());
62 }
63
64 public function execute(CommandSender $sender, string $commandLabel, array $args){
65 $commands = [];
66 $result = true;
67
68 foreach($this->formatStrings as $formatString){
69 try{
70 $formatArgs = CommandStringHelper::parseQuoteAware($formatString);
71 $unresolved = [];
72 $processedArgs = [];
73 foreach($formatArgs as $formatArg){
74 $processedArg = $this->buildCommand($formatArg, $args);
75 if($processedArg === null){
76 $unresolved[] = $formatArg;
77 }elseif(count($unresolved) !== 0){
78 //unresolved args are OK only if they are at the end of the string - we can't have holes in the args list
79 throw new \InvalidArgumentException("Unable to resolve format arguments (" . implode(", ", $unresolved) . ") in command string \"$formatString\" due to missing arguments");
80 }else{
81 $processedArgs[] = $processedArg;
82 }
83 }
84 $commands[] = $processedArgs;
85 }catch(\InvalidArgumentException $e){
86 $sender->sendMessage(TextFormat::RED . $e->getMessage());
87 return false;
88 }
89 }
90
91 $commandMap = $sender->getServer()->getCommandMap();
92 foreach($commands as $commandArgs){
93 //this approximately duplicates the logic found in SimpleCommandMap::dispatch()
94 //this is to allow directly invoking the commands without having to rebuild a command string and parse it
95 //again for no reason
96 //TODO: a method on CommandMap to invoke a command with pre-parsed arguments would probably be a good idea
97 //for a future major version
98 $commandLabel = array_shift($commandArgs);
99 if($commandLabel === null){
100 throw new AssumptionFailedError("This should have been checked before construction");
101 }
102
103 if(($target = $commandMap->getCommand($commandLabel)) !== null){
104 $timings = Timings::getCommandDispatchTimings($target->getLabel());
105 $timings->startTiming();
106
107 try{
108 $target->execute($sender, $commandLabel, $commandArgs);
110 $sender->sendMessage($sender->getLanguage()->translate(KnownTranslationFactory::commands_generic_usage($target->getUsage())));
111 }finally{
112 $timings->stopTiming();
113 }
114 }else{
115 $sender->sendMessage($sender->getLanguage()->translate(KnownTranslationFactory::pocketmine_command_notFound($commandLabel, "/help")->prefix(TextFormat::RED)));
116
117 //to match the behaviour of SimpleCommandMap::dispatch()
118 //this shouldn't normally happen, but might happen if the command was unregistered or modified after
119 //the alias was installed
120 $result = false;
121 }
122 }
123
124 return $result;
125 }
126
131 private function buildCommand(string $formatString, array $args) : ?string{
132 $index = 0;
133 while(($index = strpos($formatString, '$', $index)) !== false){
134 $start = $index;
135 if($index > 0 && $formatString[$start - 1] === "\\"){
136 $formatString = substr($formatString, 0, $start - 1) . substr($formatString, $start);
137 //offset is now pointing at the next character because we just deleted the \
138 continue;
139 }
140
141 $info = self::extractPlaceholderInfo($formatString, $index);
142 if($info === null){
143 throw new \InvalidArgumentException("Invalid replacement token");
144 }
145 [$fullPlaceholder, $required, $position, $rest] = $info;
146 $position--; //array offsets start at 0, but placeholders start at 1
147
148 if($required && $position >= count($args)){
149 throw new \InvalidArgumentException("Missing required argument " . ($position + 1));
150 }
151
152 $replacement = self::buildReplacement($args, $position, $rest);
153 if($replacement === null){
154 return null;
155 }
156
157 $end = $index + strlen($fullPlaceholder);
158 $formatString = substr($formatString, 0, $start) . $replacement . substr($formatString, $end);
159
160 $index = $start + strlen($replacement);
161 }
162
163 return $formatString;
164 }
165
170 private static function buildReplacement(array $args, int $position, bool $rest) : ?string{
171 if($rest && $position < count($args)){
172 $replacement = "";
173 for($i = $position, $c = count($args); $i < $c; ++$i){
174 if($i !== $position){
175 $replacement .= " ";
176 }
177
178 $replacement .= $args[$i];
179 }
180
181 return $replacement;
182 }elseif($position < count($args)){
183 return $args[$position];
184 }
185
186 return null;
187 }
188
192 private static function extractPlaceholderInfo(string $commandString, int $offset) : ?array{
193 if(preg_match(self::FORMAT_STRING_REGEX, $commandString, $matches, 0, $offset) !== 1){
194 return null;
195 }
196
197 $fullPlaceholder = $matches[0];
198
199 $required = ($matches[1] ?? "") !== "";
200 $position = (int) $matches[2];
201 $variadic = ($matches[3] ?? "") !== "";
202
203 return [$fullPlaceholder, $required, $position, $variadic];
204 }
205}
execute(CommandSender $sender, string $commandLabel, array $args)
__construct(string $alias, private array $formatStrings)