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;
80 $serialChunks = igbinary_unserialize($this->adjacentChunks);
82 function(?
string $serialized) : ?
Chunk{
83 if($serialized ===
null){
86 $chunk = FastChunkSerializer::deserializeTerrain($serialized);
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();
110 $this->chunk = FastChunkSerializer::serializeTerrain($chunk);
113 foreach($chunks as $relativeChunkHash => $c){
114 $serialChunks[$relativeChunkHash] = $c->isTerrainDirty() ? FastChunkSerializer::serializeTerrain($c) : null;
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));
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);