Merge branch 'stable' into next-minor

This commit is contained in:
Dylan K. Taylor 2022-11-02 16:03:30 +00:00
commit 1671405cd0
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
2 changed files with 26 additions and 7 deletions

View File

@ -28,9 +28,9 @@ use pocketmine\command\utils\InvalidCommandSyntaxException;
use pocketmine\lang\KnownTranslationFactory;
use pocketmine\utils\AssumptionFailedError;
use pocketmine\utils\TextFormat;
use function array_map;
use function array_shift;
use function count;
use function implode;
use function preg_match;
use function strlen;
use function strpos;
@ -62,7 +62,20 @@ class FormattedCommandAlias extends Command{
foreach($this->formatStrings as $formatString){
try{
$formatArgs = CommandStringHelper::parseQuoteAware($formatString);
$commands[] = array_map(fn(string $formatArg) => $this->buildCommand($formatArg, $args), $formatArgs);
$unresolved = [];
$processedArgs = [];
foreach($formatArgs as $formatArg){
$processedArg = $this->buildCommand($formatArg, $args);
if($processedArg === null){
$unresolved[] = $formatArg;
}elseif(count($unresolved) !== 0){
//unresolved args are OK only if they are at the end of the string - we can't have holes in the args list
throw new \InvalidArgumentException("Unable to resolve format arguments (" . implode(", ", $unresolved) . ") in command string \"$formatString\" due to missing arguments");
}else{
$processedArgs[] = $processedArg;
}
}
$commands[] = $processedArgs;
}catch(\InvalidArgumentException $e){
$sender->sendMessage(TextFormat::RED . $e->getMessage());
return false;
@ -107,7 +120,7 @@ class FormattedCommandAlias extends Command{
/**
* @param string[] $args
*/
private function buildCommand(string $formatString, array $args) : string{
private function buildCommand(string $formatString, array $args) : ?string{
$index = 0;
while(($index = strpos($formatString, '$', $index)) !== false){
$start = $index;
@ -129,6 +142,9 @@ class FormattedCommandAlias extends Command{
}
$replacement = self::buildReplacement($args, $position, $rest);
if($replacement === null){
return null;
}
$end = $index + strlen($fullPlaceholder);
$formatString = substr($formatString, 0, $start) . $replacement . substr($formatString, $end);
@ -143,9 +159,9 @@ class FormattedCommandAlias extends Command{
* @param string[] $args
* @phpstan-param list<string> $args
*/
private static function buildReplacement(array $args, int $position, bool $rest) : string{
$replacement = "";
private static function buildReplacement(array $args, int $position, bool $rest) : ?string{
if($rest && $position < count($args)){
$replacement = "";
for($i = $position, $c = count($args); $i < $c; ++$i){
if($i !== $position){
$replacement .= " ";
@ -153,11 +169,13 @@ class FormattedCommandAlias extends Command{
$replacement .= $args[$i];
}
return $replacement;
}elseif($position < count($args)){
$replacement .= $args[$position];
return $args[$position];
}
return $replacement;
return null;
}
/**

View File

@ -109,6 +109,7 @@ abstract class Durable extends Item{
*/
protected function onBroken() : void{
$this->pop();
$this->setDamage(0); //the stack size may be greater than 1 if overstacked by a plugin
}
/**