diff --git a/src/pocketmine/command/SimpleCommandMap.php b/src/pocketmine/command/SimpleCommandMap.php index 56c133549..48e30d1ac 100644 --- a/src/pocketmine/command/SimpleCommandMap.php +++ b/src/pocketmine/command/SimpleCommandMap.php @@ -177,15 +177,35 @@ class SimpleCommandMap implements CommandMap{ return true; } - public function dispatch(CommandSender $sender, $commandLine){ - $args = explode(" ", $commandLine); + /** + * Returns a command to match the specified command line, or null if no matching command was found. + * This method is intended to provide capability for handling commands with spaces in their name. + * The referenced parameters will be modified accordingly depending on the resulting matched command. + * + * @param string &$commandName + * @param string[] &$args + * + * @return Command|null + */ + public function matchCommand(string &$commandName, array &$args){ + $count = max(count($args), 255); - if(count($args) === 0){ - return false; + for($i = 0; $i < $count; ++$i){ + $commandName .= array_shift($args); + if(($command = $this->getCommand($commandName)) instanceof Command){ + return $command; + } + + $commandName .= " "; } - $sentCommandLabel = strtolower(array_shift($args)); - $target = $this->getCommand($sentCommandLabel); + return null; + } + + public function dispatch(CommandSender $sender, $commandLine){ + $args = explode(" ", $commandLine); + $sentCommandLabel = ""; + $target = $this->matchCommand($sentCommandLabel, $args); if($target === null){ return false; @@ -213,11 +233,7 @@ class SimpleCommandMap implements CommandMap{ } public function getCommand($name){ - if(isset($this->knownCommands[$name])){ - return $this->knownCommands[$name]; - } - - return null; + return $this->knownCommands[$name] ?? null; } /** @@ -235,7 +251,7 @@ class SimpleCommandMap implements CommandMap{ $values = $this->server->getCommandAliases(); foreach($values as $alias => $commandStrings){ - if(strpos($alias, ":") !== false or strpos($alias, " ") !== false){ + if(strpos($alias, ":") !== false){ $this->server->getLogger()->warning($this->server->getLanguage()->translateString("pocketmine.command.alias.illegal", [$alias])); continue; } @@ -246,7 +262,8 @@ class SimpleCommandMap implements CommandMap{ $recursive = ""; foreach($commandStrings as $commandString){ $args = explode(" ", $commandString); - $command = $this->getCommand($args[0]); + $commandName = ""; + $command = $this->matchCommand($commandName, $args); if($command === null){ @@ -254,7 +271,7 @@ class SimpleCommandMap implements CommandMap{ $bad .= ", "; } $bad .= $commandString; - }elseif($args[0] === $alias){ + }elseif($commandName === $alias){ if($recursive !== ""){ $recursive .= ", "; }