PocketMine-MP 5.28.1 git-88cdc2eb67c40075559c3ef51418b418cd5488e9
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
30use function array_map;
31use function igbinary_serialize;
32use function igbinary_unserialize;
33
38 private const TLS_KEY_ON_COMPLETION = "onCompletion";
39
40 private ?string $chunk;
41
42 private string $adjacentChunks;
43
49 public function __construct(
50 private int $worldId,
51 private int $chunkX,
52 private int $chunkZ,
53 ?Chunk $chunk,
54 array $adjacentChunks,
55 \Closure $onCompletion
56 ){
57 $this->chunk = $chunk !== null ? FastChunkSerializer::serializeTerrain($chunk) : null;
58
59 $this->adjacentChunks = igbinary_serialize(array_map(
60 fn(?Chunk $c) => $c !== null ? FastChunkSerializer::serializeTerrain($c) : null,
61 $adjacentChunks
62 )) ?? throw new AssumptionFailedError("igbinary_serialize() returned null");
63
64 $this->storeLocal(self::TLS_KEY_ON_COMPLETION, $onCompletion);
65 }
66
67 public function onRun() : void{
68 $context = ThreadLocalGeneratorContext::fetch($this->worldId);
69 if($context === null){
70 throw new AssumptionFailedError("Generator context should have been initialized before any PopulationTask execution");
71 }
72
73 $chunk = $this->chunk !== null ? FastChunkSerializer::deserializeTerrain($this->chunk) : null;
74
79 $serialChunks = igbinary_unserialize($this->adjacentChunks);
80 $chunks = array_map(
81 function(?string $serialized) : ?Chunk{
82 if($serialized === null){
83 return null;
84 }
85 $chunk = FastChunkSerializer::deserializeTerrain($serialized);
86 $chunk->clearTerrainDirtyFlags(); //this allows us to avoid sending existing chunks back to the main thread if they haven't changed during generation
87 return $chunk;
88 },
89 $serialChunks
90 );
91
93 $context->getWorldMinY(),
94 $context->getWorldMaxY(),
95 $context->getGenerator(),
96 $this->chunkX,
97 $this->chunkZ,
98 $chunk,
99 $chunks
100 );
101
102 $this->chunk = FastChunkSerializer::serializeTerrain($chunk);
103
104 $serialChunks = [];
105 foreach($chunks as $relativeChunkHash => $c){
106 $serialChunks[$relativeChunkHash] = $c->isTerrainDirty() ? FastChunkSerializer::serializeTerrain($c) : null;
107 }
108 $this->adjacentChunks = igbinary_serialize($serialChunks) ?? throw new AssumptionFailedError("igbinary_serialize() returned null");
109 }
110
111 public function onCompletion() : void{
116 $onCompletion = $this->fetchLocal(self::TLS_KEY_ON_COMPLETION);
117
118 $chunk = $this->chunk !== null ?
119 FastChunkSerializer::deserializeTerrain($this->chunk) :
120 throw new AssumptionFailedError("Center chunk should never be null");
121
126 $serialAdjacentChunks = igbinary_unserialize($this->adjacentChunks);
127 $adjacentChunks = [];
128 foreach($serialAdjacentChunks as $relativeChunkHash => $c){
129 if($c !== null){
130 $adjacentChunks[$relativeChunkHash] = FastChunkSerializer::deserializeTerrain($c);
131 }
132 }
133
134 $onCompletion($chunk, $adjacentChunks);
135 }
136}
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)