22declare(strict_types=1);
32use
function array_map;
33use
function igbinary_serialize;
34use
function igbinary_unserialize;
40 private const TLS_KEY_ON_COMPLETION =
"onCompletion";
42 private ?
string $chunk;
44 private string $adjacentChunks;
56 array $adjacentChunks,
57 \Closure $onCompletion
61 $this->adjacentChunks = igbinary_serialize(array_map(
66 $this->
storeLocal(self::TLS_KEY_ON_COMPLETION, $onCompletion);
71 if($context ===
null){
72 throw new AssumptionFailedError(
"Generator context should have been initialized before any PopulationTask execution");
74 $generator = $context->getGenerator();
80 $serialChunks = igbinary_unserialize($this->adjacentChunks);
82 function(?
string $serialized) : ?
Chunk{
83 if($serialized ===
null){
87 $chunk->clearTerrainDirtyFlags();
93 self::setOrGenerateChunk($manager, $generator, $this->chunkX, $this->chunkZ, $chunk);
97 foreach($chunks as $relativeChunkHash => $c){
98 World::getXZ($relativeChunkHash, $relativeX, $relativeZ);
99 $resultChunks[$relativeChunkHash] = self::setOrGenerateChunk($manager, $generator, $this->chunkX + $relativeX, $this->chunkZ + $relativeZ, $c);
101 $chunks = $resultChunks;
103 $generator->populateChunk($manager, $this->chunkX, $this->chunkZ);
104 $chunk = $manager->getChunk($this->chunkX, $this->chunkZ);
106 throw new AssumptionFailedError(
"We just generated this chunk, so it must exist");
108 $chunk->setPopulated();
113 foreach($chunks as $relativeChunkHash => $c){
116 $this->adjacentChunks = igbinary_serialize($serialChunks) ??
throw new AssumptionFailedError(
"igbinary_serialize() returned null");
119 private static function setOrGenerateChunk(SimpleChunkManager $manager, Generator $generator,
int $chunkX,
int $chunkZ, ?Chunk $chunk) : Chunk{
120 $manager->setChunk($chunkX, $chunkZ, $chunk ?? new Chunk([], false));
122 $generator->generateChunk($manager, $chunkX, $chunkZ);
123 $chunk = $manager->getChunk($chunkX, $chunkZ);
125 throw new AssumptionFailedError(
"We just set this chunk, so it must exist");
136 $onCompletion = $this->fetchLocal(self::TLS_KEY_ON_COMPLETION);
138 $chunk = $this->chunk !==
null ?
139 FastChunkSerializer::deserializeTerrain($this->chunk) :
146 $serialAdjacentChunks = igbinary_unserialize($this->adjacentChunks);
147 $adjacentChunks = [];
148 foreach($serialAdjacentChunks as $relativeChunkHash => $c){
150 $adjacentChunks[$relativeChunkHash] = FastChunkSerializer::deserializeTerrain($c);
154 $onCompletion($chunk, $adjacentChunks);
storeLocal(string $key, mixed $complexData)
static getXZ(int $hash, ?int &$x, ?int &$z)
__construct(private int $worldId, private int $chunkX, private int $chunkZ, ?Chunk $chunk, array $adjacentChunks, \Closure $onCompletion)