PocketMine-MP 5.19.1 git-5cc1068cd43264d3363295eb8d6901e02f467897
ChunkRequestTask.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\network\mcpe;
25
39use function chr;
40
42 private const TLS_KEY_PROMISE = "promise";
43
44 protected string $chunk;
45 protected int $chunkX;
46 protected int $chunkZ;
48 private int $dimensionId;
51 private string $tiles;
52
56 public function __construct(int $chunkX, int $chunkZ, int $dimensionId, Chunk $chunk, CompressBatchPromise $promise, Compressor $compressor){
57 $this->compressor = new NonThreadSafeValue($compressor);
58
59 $this->chunk = FastChunkSerializer::serializeTerrain($chunk);
60 $this->chunkX = $chunkX;
61 $this->chunkZ = $chunkZ;
62 $this->dimensionId = $dimensionId;
63 $this->tiles = ChunkSerializer::serializeTiles($chunk);
64
65 $this->storeLocal(self::TLS_KEY_PROMISE, $promise);
66 }
67
68 public function onRun() : void{
69 $chunk = FastChunkSerializer::deserializeTerrain($this->chunk);
70 $dimensionId = $this->dimensionId;
71
72 $subCount = ChunkSerializer::getSubChunkCount($chunk, $dimensionId);
73 $converter = TypeConverter::getInstance();
74 $payload = ChunkSerializer::serializeFullChunk($chunk, $dimensionId, $converter->getBlockTranslator(), $this->tiles);
75
76 $stream = new BinaryStream();
77 PacketBatch::encodePackets($stream, [LevelChunkPacket::create(new ChunkPosition($this->chunkX, $this->chunkZ), $dimensionId, $subCount, false, null, $payload)]);
78
79 $compressor = $this->compressor->deserialize();
80 $this->setResult(chr($compressor->getNetworkId()) . $compressor->compress($stream->getBuffer()));
81 }
82
83 public function onCompletion() : void{
85 $promise = $this->fetchLocal(self::TLS_KEY_PROMISE);
86 $promise->resolve($this->getResult());
87 }
88}
__construct(int $chunkX, int $chunkZ, int $dimensionId, Chunk $chunk, CompressBatchPromise $promise, Compressor $compressor)
static create(ChunkPosition $chunkPosition, int $dimensionId, int $subChunkCount, bool $clientSubChunkRequestsEnabled, ?array $usedBlobHashes, string $extraPayload)
static encodePackets(BinaryStream $stream, array $packets)
Definition: PacketBatch.php:85
static serializeFullChunk(Chunk $chunk, int $dimensionId, BlockTranslator $blockTranslator, ?string $tiles=null)
static getSubChunkCount(Chunk $chunk, int $dimensionId)
storeLocal(string $key, mixed $complexData)
Definition: AsyncTask.php:214