PocketMine-MP 5.15.1 git-5ef247620a7c6301a849b54e5ef1009217729fc8
LightPopulationTask.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
24namespace pocketmine\world\light;
25
30use pocketmine\world\format\LightArray;
34use function igbinary_serialize;
35use function igbinary_unserialize;
36
38 private const TLS_KEY_COMPLETION_CALLBACK = "onCompletion";
39
40 public string $chunk;
41
42 private string $resultHeightMap;
43 private string $resultSkyLightArrays;
44 private string $resultBlockLightArrays;
45
49 public function __construct(Chunk $chunk, \Closure $onCompletion){
50 $this->chunk = FastChunkSerializer::serializeTerrain($chunk);
51 $this->storeLocal(self::TLS_KEY_COMPLETION_CALLBACK, $onCompletion);
52 }
53
54 public function onRun() : void{
55 $chunk = FastChunkSerializer::deserializeTerrain($this->chunk);
56
57 $manager = new SimpleChunkManager(World::Y_MIN, World::Y_MAX);
58 $manager->setChunk(0, 0, $chunk);
59
60 $blockFactory = RuntimeBlockStateRegistry::getInstance();
61 foreach([
62 "Block" => new BlockLightUpdate(new SubChunkExplorer($manager), $blockFactory->lightFilter, $blockFactory->light),
63 "Sky" => new SkyLightUpdate(new SubChunkExplorer($manager), $blockFactory->lightFilter, $blockFactory->blocksDirectSkyLight),
64 ] as $name => $update){
65 $update->recalculateChunk(0, 0);
66 $update->execute();
67 }
68
69 $chunk->setLightPopulated();
70
71 $this->resultHeightMap = igbinary_serialize($chunk->getHeightMapArray());
72 $skyLightArrays = [];
73 $blockLightArrays = [];
74 foreach($chunk->getSubChunks() as $y => $subChunk){
75 $skyLightArrays[$y] = $subChunk->getBlockSkyLightArray();
76 $blockLightArrays[$y] = $subChunk->getBlockLightArray();
77 }
78 $this->resultSkyLightArrays = igbinary_serialize($skyLightArrays);
79 $this->resultBlockLightArrays = igbinary_serialize($blockLightArrays);
80 }
81
82 public function onCompletion() : void{
84 $heightMapArray = igbinary_unserialize($this->resultHeightMap);
85
87 $skyLightArrays = igbinary_unserialize($this->resultSkyLightArrays);
89 $blockLightArrays = igbinary_unserialize($this->resultBlockLightArrays);
90
95 $callback = $this->fetchLocal(self::TLS_KEY_COMPLETION_CALLBACK);
96 $callback($blockLightArrays, $skyLightArrays, $heightMapArray);
97 }
98}
storeLocal(string $key, mixed $complexData)
Definition: AsyncTask.php:214
__construct(Chunk $chunk, \Closure $onCompletion)