From efaf9311b332e49dc643de5237b73b1613e627cc Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Wed, 30 Apr 2025 17:38:16 +0100 Subject: [PATCH] Extract population business logic from PopulationTask --- src/world/generator/PopulationTask.php | 40 +++---------- src/world/generator/PopulationUtils.php | 74 +++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 31 deletions(-) create mode 100644 src/world/generator/PopulationUtils.php diff --git a/src/world/generator/PopulationTask.php b/src/world/generator/PopulationTask.php index bad134324..a8366a306 100644 --- a/src/world/generator/PopulationTask.php +++ b/src/world/generator/PopulationTask.php @@ -27,8 +27,6 @@ use pocketmine\scheduler\AsyncTask; use pocketmine\utils\AssumptionFailedError; use pocketmine\world\format\Chunk; use pocketmine\world\format\io\FastChunkSerializer; -use pocketmine\world\SimpleChunkManager; -use pocketmine\world\World; use function array_map; use function igbinary_serialize; use function igbinary_unserialize; @@ -71,8 +69,6 @@ class PopulationTask extends AsyncTask{ if($context === null){ throw new AssumptionFailedError("Generator context should have been initialized before any PopulationTask execution"); } - $generator = $context->getGenerator(); - $manager = new SimpleChunkManager($context->getWorldMinY(), $context->getWorldMaxY()); $chunk = $this->chunk !== null ? FastChunkSerializer::deserializeTerrain($this->chunk) : null; @@ -93,21 +89,15 @@ class PopulationTask extends AsyncTask{ $serialChunks ); - self::setOrGenerateChunk($manager, $generator, $this->chunkX, $this->chunkZ, $chunk); - - $resultChunks = []; //this is just to keep phpstan's type inference happy - foreach($chunks as $relativeChunkHash => $c){ - World::getXZ($relativeChunkHash, $relativeX, $relativeZ); - $resultChunks[$relativeChunkHash] = self::setOrGenerateChunk($manager, $generator, $this->chunkX + $relativeX, $this->chunkZ + $relativeZ, $c); - } - $chunks = $resultChunks; - - $generator->populateChunk($manager, $this->chunkX, $this->chunkZ); - $chunk = $manager->getChunk($this->chunkX, $this->chunkZ); - if($chunk === null){ - throw new AssumptionFailedError("We just generated this chunk, so it must exist"); - } - $chunk->setPopulated(); + [$chunk, $chunks] = PopulationUtils::populateChunkWithAdjacents( + $context->getWorldMinY(), + $context->getWorldMaxY(), + $context->getGenerator(), + $this->chunkX, + $this->chunkZ, + $chunk, + $chunks + ); $this->chunk = FastChunkSerializer::serializeTerrain($chunk); @@ -118,18 +108,6 @@ class PopulationTask extends AsyncTask{ $this->adjacentChunks = igbinary_serialize($serialChunks) ?? throw new AssumptionFailedError("igbinary_serialize() returned null"); } - private static function setOrGenerateChunk(SimpleChunkManager $manager, Generator $generator, int $chunkX, int $chunkZ, ?Chunk $chunk) : Chunk{ - $manager->setChunk($chunkX, $chunkZ, $chunk ?? new Chunk([], false)); - if($chunk === null){ - $generator->generateChunk($manager, $chunkX, $chunkZ); - $chunk = $manager->getChunk($chunkX, $chunkZ); - if($chunk === null){ - throw new AssumptionFailedError("We just set this chunk, so it must exist"); - } - } - return $chunk; - } - public function onCompletion() : void{ /** * @var \Closure $onCompletion diff --git a/src/world/generator/PopulationUtils.php b/src/world/generator/PopulationUtils.php new file mode 100644 index 000000000..84840ee3e --- /dev/null +++ b/src/world/generator/PopulationUtils.php @@ -0,0 +1,74 @@ +setChunk($chunkX, $chunkZ, $chunk ?? new Chunk([], false)); + if($chunk === null){ + $generator->generateChunk($manager, $chunkX, $chunkZ); + $chunk = $manager->getChunk($chunkX, $chunkZ); + if($chunk === null){ + throw new AssumptionFailedError("We just set this chunk, so it must exist"); + } + } + return $chunk; + } + + /** + * @param Chunk[]|null[] $adjacentChunks + * @phpstan-param array $adjacentChunks + * + * @return Chunk[]|Chunk[][] + * @phpstan-return array{Chunk, array} + */ + public static function populateChunkWithAdjacents(int $minY, int $maxY, Generator $generator, int $chunkX, int $chunkZ, ?Chunk $centerChunk, array $adjacentChunks) : array{ + $manager = new SimpleChunkManager($minY, $maxY); + self::setOrGenerateChunk($manager, $generator, $chunkX, $chunkZ, $centerChunk); + + $resultChunks = []; //this is just to keep phpstan's type inference happy + foreach($adjacentChunks as $relativeChunkHash => $c){ + World::getXZ($relativeChunkHash, $relativeX, $relativeZ); + $resultChunks[$relativeChunkHash] = self::setOrGenerateChunk($manager, $generator, $chunkX + $relativeX, $chunkZ + $relativeZ, $c); + } + $adjacentChunks = $resultChunks; + + $generator->populateChunk($manager, $chunkX, $chunkZ); + $centerChunk = $manager->getChunk($chunkX, $chunkZ); + if($centerChunk === null){ + throw new AssumptionFailedError("We just generated this chunk, so it must exist"); + } + $centerChunk->setPopulated(); + return [$centerChunk, $adjacentChunks]; + } +}