PocketMine-MP 5.28.3 git-d5a1007c80fcee27feb2251cf5dcf1ad5a59a85c
Loading...
Searching...
No Matches
PopulationTask.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
25
31use function array_map;
32use function igbinary_serialize;
33use function igbinary_unserialize;
34
45 private const TLS_KEY_ON_COMPLETION = "onCompletion";
46
47 private ?string $chunk;
48
49 private string $adjacentChunks;
50
56 public function __construct(
57 private int $worldId,
58 private int $chunkX,
59 private int $chunkZ,
60 ?Chunk $chunk,
61 array $adjacentChunks,
62 \Closure $onCompletion
63 ){
64 $this->chunk = $chunk !== null ? FastChunkSerializer::serializeTerrain($chunk) : null;
65
66 $this->adjacentChunks = igbinary_serialize(array_map(
67 fn(?Chunk $c) => $c !== null ? FastChunkSerializer::serializeTerrain($c) : null,
68 $adjacentChunks
69 )) ?? throw new AssumptionFailedError("igbinary_serialize() returned null");
70
71 $this->storeLocal(self::TLS_KEY_ON_COMPLETION, $onCompletion);
72 }
73
74 public function onRun() : void{
75 $context = ThreadLocalGeneratorContext::fetch($this->worldId);
76 if($context === null){
77 throw new AssumptionFailedError("Generator context should have been initialized before any PopulationTask execution");
78 }
79
80 $chunk = $this->chunk !== null ? FastChunkSerializer::deserializeTerrain($this->chunk) : null;
81
86 $serialChunks = igbinary_unserialize($this->adjacentChunks);
87 $chunks = array_map(
88 function(?string $serialized) : ?Chunk{
89 if($serialized === null){
90 return null;
91 }
92 $chunk = FastChunkSerializer::deserializeTerrain($serialized);
93 $chunk->clearTerrainDirtyFlags(); //this allows us to avoid sending existing chunks back to the main thread if they haven't changed during generation
94 return $chunk;
95 },
96 $serialChunks
97 );
98
100 $context->getWorldMinY(),
101 $context->getWorldMaxY(),
102 $context->getGenerator(),
103 $this->chunkX,
104 $this->chunkZ,
105 $chunk,
106 $chunks
107 );
108
109 $this->chunk = FastChunkSerializer::serializeTerrain($chunk);
110
111 $serialChunks = [];
112 foreach($chunks as $relativeChunkHash => $c){
113 $serialChunks[$relativeChunkHash] = $c->isTerrainDirty() ? FastChunkSerializer::serializeTerrain($c) : null;
114 }
115 $this->adjacentChunks = igbinary_serialize($serialChunks) ?? throw new AssumptionFailedError("igbinary_serialize() returned null");
116 }
117
118 public function onCompletion() : void{
123 $onCompletion = $this->fetchLocal(self::TLS_KEY_ON_COMPLETION);
124
125 $chunk = $this->chunk !== null ?
126 FastChunkSerializer::deserializeTerrain($this->chunk) :
127 throw new AssumptionFailedError("Center chunk should never be null");
128
133 $serialAdjacentChunks = igbinary_unserialize($this->adjacentChunks);
134 $adjacentChunks = [];
135 foreach($serialAdjacentChunks as $relativeChunkHash => $c){
136 if($c !== null){
137 $adjacentChunks[$relativeChunkHash] = FastChunkSerializer::deserializeTerrain($c);
138 }
139 }
140
141 $onCompletion($chunk, $adjacentChunks);
142 }
143}
storeLocal(string $key, mixed $complexData)
__construct(private int $worldId, private int $chunkX, private int $chunkZ, ?Chunk $chunk, array $adjacentChunks, \Closure $onCompletion)
static populateChunkWithAdjacents(int $minY, int $maxY, Generator $generator, int $chunkX, int $chunkZ, ?Chunk $centerChunk, array $adjacentChunks)