Merge branch 'next-minor' into next-major

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

View File

@ -29,9 +29,9 @@ use pocketmine\lang\KnownTranslationFactory;
use pocketmine\timings\Timings; use pocketmine\timings\Timings;
use pocketmine\utils\AssumptionFailedError; use pocketmine\utils\AssumptionFailedError;
use pocketmine\utils\TextFormat; use pocketmine\utils\TextFormat;
use function array_map;
use function array_shift; use function array_shift;
use function count; use function count;
use function implode;
use function preg_match; use function preg_match;
use function strlen; use function strlen;
use function strpos; use function strpos;
@ -63,7 +63,20 @@ class FormattedCommandAlias extends Command{
foreach($this->formatStrings as $formatString){ foreach($this->formatStrings as $formatString){
try{ try{
$formatArgs = CommandStringHelper::parseQuoteAware($formatString); $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){ }catch(\InvalidArgumentException $e){
$sender->sendMessage(TextFormat::RED . $e->getMessage()); $sender->sendMessage(TextFormat::RED . $e->getMessage());
return false; return false;
@ -109,7 +122,7 @@ class FormattedCommandAlias extends Command{
/** /**
* @param string[] $args * @param string[] $args
*/ */
private function buildCommand(string $formatString, array $args) : string{ private function buildCommand(string $formatString, array $args) : ?string{
$index = 0; $index = 0;
while(($index = strpos($formatString, '$', $index)) !== false){ while(($index = strpos($formatString, '$', $index)) !== false){
$start = $index; $start = $index;
@ -131,6 +144,9 @@ class FormattedCommandAlias extends Command{
} }
$replacement = self::buildReplacement($args, $position, $rest); $replacement = self::buildReplacement($args, $position, $rest);
if($replacement === null){
return null;
}
$end = $index + strlen($fullPlaceholder); $end = $index + strlen($fullPlaceholder);
$formatString = substr($formatString, 0, $start) . $replacement . substr($formatString, $end); $formatString = substr($formatString, 0, $start) . $replacement . substr($formatString, $end);
@ -145,9 +161,9 @@ class FormattedCommandAlias extends Command{
* @param string[] $args * @param string[] $args
* @phpstan-param list<string> $args * @phpstan-param list<string> $args
*/ */
private static function buildReplacement(array $args, int $position, bool $rest) : string{ private static function buildReplacement(array $args, int $position, bool $rest) : ?string{
$replacement = "";
if($rest && $position < count($args)){ if($rest && $position < count($args)){
$replacement = "";
for($i = $position, $c = count($args); $i < $c; ++$i){ for($i = $position, $c = count($args); $i < $c; ++$i){
if($i !== $position){ if($i !== $position){
$replacement .= " "; $replacement .= " ";
@ -155,11 +171,13 @@ class FormattedCommandAlias extends Command{
$replacement .= $args[$i]; $replacement .= $args[$i];
} }
return $replacement;
}elseif($position < count($args)){ }elseif($position < count($args)){
$replacement .= $args[$position]; return $args[$position];
} }
return $replacement; return null;
} }
/** /**

View File

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