56 array $adjacentChunks,
57 \Closure $onCompletion
59 $this->chunk = $chunk !==
null ? FastChunkSerializer::serializeTerrain($chunk) :
null;
61 $this->adjacentChunks = igbinary_serialize(array_map(
62 fn(?
Chunk $c) => $c !==
null ? FastChunkSerializer::serializeTerrain($c) :
null,
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();
77 $chunk = $this->chunk !==
null ? FastChunkSerializer::deserializeTerrain($this->chunk) : null;
83 $serialChunks = igbinary_unserialize($this->adjacentChunks);
85 function(?
string $serialized) : ?
Chunk{
86 if($serialized ===
null){
89 $chunk = FastChunkSerializer::deserializeTerrain($serialized);
90 $chunk->clearTerrainDirtyFlags();
96 self::setOrGenerateChunk($manager, $generator, $this->chunkX, $this->chunkZ, $chunk);
99 foreach($chunks as $relativeChunkHash => $c){
100 World::getXZ($relativeChunkHash, $relativeX, $relativeZ);
101 $resultChunks[$relativeChunkHash] = self::setOrGenerateChunk($manager, $generator, $this->chunkX + $relativeX, $this->chunkZ + $relativeZ, $c);
103 $chunks = $resultChunks;
105 $generator->populateChunk($manager, $this->chunkX, $this->chunkZ);
106 $chunk = $manager->getChunk($this->chunkX, $this->chunkZ);
108 throw new AssumptionFailedError(
"We just generated this chunk, so it must exist");
110 $chunk->setPopulated();
112 $this->chunk = FastChunkSerializer::serializeTerrain($chunk);
115 foreach($chunks as $relativeChunkHash => $c){
116 $serialChunks[$relativeChunkHash] = $c->isTerrainDirty() ? FastChunkSerializer::serializeTerrain($c) : null;
118 $this->adjacentChunks = igbinary_serialize($serialChunks) ??
throw new AssumptionFailedError(
"igbinary_serialize() returned null");
121 private static function setOrGenerateChunk(SimpleChunkManager $manager, Generator $generator,
int $chunkX,
int $chunkZ, ?Chunk $chunk) : Chunk{
122 $manager->setChunk($chunkX, $chunkZ, $chunk ?? new Chunk([], false));
138 $onCompletion = $this->fetchLocal(self::TLS_KEY_ON_COMPLETION);
140 $chunk = $this->chunk !==
null ?
141 FastChunkSerializer::deserializeTerrain($this->chunk) :
148 $serialAdjacentChunks = igbinary_unserialize($this->adjacentChunks);
149 $adjacentChunks = [];
150 foreach($serialAdjacentChunks as $relativeChunkHash => $c){
152 $adjacentChunks[$relativeChunkHash] = FastChunkSerializer::deserializeTerrain($c);
156 $onCompletion($chunk, $adjacentChunks);