Added support for commands with spaces in their names (#422)

This commit is contained in:
Dylan K. Taylor 2017-03-13 20:55:06 +00:00 committed by GitHub
parent d264a04db4
commit 3138e02acb

View File

@ -177,15 +177,35 @@ class SimpleCommandMap implements CommandMap{
return true; 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){ for($i = 0; $i < $count; ++$i){
return false; $commandName .= array_shift($args);
if(($command = $this->getCommand($commandName)) instanceof Command){
return $command;
}
$commandName .= " ";
} }
$sentCommandLabel = strtolower(array_shift($args)); return null;
$target = $this->getCommand($sentCommandLabel); }
public function dispatch(CommandSender $sender, $commandLine){
$args = explode(" ", $commandLine);
$sentCommandLabel = "";
$target = $this->matchCommand($sentCommandLabel, $args);
if($target === null){ if($target === null){
return false; return false;
@ -213,11 +233,7 @@ class SimpleCommandMap implements CommandMap{
} }
public function getCommand($name){ public function getCommand($name){
if(isset($this->knownCommands[$name])){ return $this->knownCommands[$name] ?? null;
return $this->knownCommands[$name];
}
return null;
} }
/** /**
@ -235,7 +251,7 @@ class SimpleCommandMap implements CommandMap{
$values = $this->server->getCommandAliases(); $values = $this->server->getCommandAliases();
foreach($values as $alias => $commandStrings){ 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])); $this->server->getLogger()->warning($this->server->getLanguage()->translateString("pocketmine.command.alias.illegal", [$alias]));
continue; continue;
} }
@ -246,7 +262,8 @@ class SimpleCommandMap implements CommandMap{
$recursive = ""; $recursive = "";
foreach($commandStrings as $commandString){ foreach($commandStrings as $commandString){
$args = explode(" ", $commandString); $args = explode(" ", $commandString);
$command = $this->getCommand($args[0]); $commandName = "";
$command = $this->matchCommand($commandName, $args);
if($command === null){ if($command === null){
@ -254,7 +271,7 @@ class SimpleCommandMap implements CommandMap{
$bad .= ", "; $bad .= ", ";
} }
$bad .= $commandString; $bad .= $commandString;
}elseif($args[0] === $alias){ }elseif($commandName === $alias){
if($recursive !== ""){ if($recursive !== ""){
$recursive .= ", "; $recursive .= ", ";
} }