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