PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
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
125 private function buildCommand(string $formatString, array $args) : ?string{
126 $index = 0;
127 while(($index = strpos($formatString, '$', $index)) !== false){
128 $start = $index;
129 if($index > 0 && $formatString[$start - 1] === "\\"){
130 $formatString = substr($formatString, 0, $start - 1) . substr($formatString, $start);
131 //offset is now pointing at the next character because we just deleted the \
132 continue;
133 }
134
135 $info = self::extractPlaceholderInfo($formatString, $index);
136 if($info === null){
137 throw new \InvalidArgumentException("Invalid replacement token");
138 }
139 [$fullPlaceholder, $required, $position, $rest] = $info;
140 $position--; //array offsets start at 0, but placeholders start at 1
141
142 if($required && $position >= count($args)){
143 throw new \InvalidArgumentException("Missing required argument " . ($position + 1));
144 }
145
146 $replacement = self::buildReplacement($args, $position, $rest);
147 if($replacement === null){
148 return null;
149 }
150
151 $end = $index + strlen($fullPlaceholder);
152 $formatString = substr($formatString, 0, $start) . $replacement . substr($formatString, $end);
153
154 $index = $start + strlen($replacement);
155 }
156
157 return $formatString;
158 }
159
164 private static function buildReplacement(array $args, int $position, bool $rest) : ?string{
165 if($rest && $position < count($args)){
166 $replacement = "";
167 for($i = $position, $c = count($args); $i < $c; ++$i){
168 if($i !== $position){
169 $replacement .= " ";
170 }
171
172 $replacement .= $args[$i];
173 }
174
175 return $replacement;
176 }elseif($position < count($args)){
177 return $args[$position];
178 }
179
180 return null;
181 }
182
186 private static function extractPlaceholderInfo(string $commandString, int $offset) : ?array{
187 if(preg_match(self::FORMAT_STRING_REGEX, $commandString, $matches, 0, $offset) !== 1){
188 return null;
189 }
190
191 $fullPlaceholder = $matches[0];
192
193 $required = ($matches[1] ?? "") !== "";
194 $position = (int) $matches[2];
195 $variadic = ($matches[3] ?? "") !== "";
196
197 return [$fullPlaceholder, $required, $position, $variadic];
198 }
199}
execute(CommandSender $sender, string $commandLabel, array $args)
__construct(string $alias, private array $formatStrings)