PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
Loading...
Searching...
No Matches
LevelChunkPacket.php
1<?php
2
3/*
4 * This file is part of BedrockProtocol.
5 * Copyright (C) 2014-2022 PocketMine Team <https://github.com/pmmp/BedrockProtocol>
6 *
7 * BedrockProtocol is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 */
12
13declare(strict_types=1);
14
15namespace pocketmine\network\mcpe\protocol;
16
17use pmmp\encoding\ByteBufferReader;
18use pmmp\encoding\ByteBufferWriter;
19use pmmp\encoding\LE;
20use pmmp\encoding\VarInt;
25use function count;
26use const PHP_INT_MAX;
27
29 public const NETWORK_ID = ProtocolInfo::LEVEL_CHUNK_PACKET;
30
34 private const CLIENT_REQUEST_FULL_COLUMN_FAKE_COUNT = Limits::UINT32_MAX;
39 private const CLIENT_REQUEST_TRUNCATED_COLUMN_FAKE_COUNT = Limits::UINT32_MAX - 1;
40
41 //this appears large enough for a world height of 1024 blocks - it may need to be increased in the future
42 private const MAX_BLOB_HASHES = 64;
43
44 private ChunkPosition $chunkPosition;
46 private int $dimensionId;
47 private int $subChunkCount;
48 private bool $clientSubChunkRequestsEnabled;
50 private ?array $usedBlobHashes = null;
51 private string $extraPayload;
52
58 public static function create(ChunkPosition $chunkPosition, int $dimensionId, int $subChunkCount, bool $clientSubChunkRequestsEnabled, ?array $usedBlobHashes, string $extraPayload) : self{
59 $result = new self;
60 $result->chunkPosition = $chunkPosition;
61 $result->dimensionId = $dimensionId;
62 $result->subChunkCount = $subChunkCount;
63 $result->clientSubChunkRequestsEnabled = $clientSubChunkRequestsEnabled;
64 $result->usedBlobHashes = $usedBlobHashes;
65 $result->extraPayload = $extraPayload;
66 return $result;
67 }
68
69 public function getChunkPosition() : ChunkPosition{ return $this->chunkPosition; }
70
71 public function getDimensionId() : int{ return $this->dimensionId; }
72
73 public function getSubChunkCount() : int{
74 return $this->subChunkCount;
75 }
76
77 public function isClientSubChunkRequestEnabled() : bool{
78 return $this->clientSubChunkRequestsEnabled;
79 }
80
81 public function isCacheEnabled() : bool{
82 return $this->usedBlobHashes !== null;
83 }
84
88 public function getUsedBlobHashes() : ?array{
89 return $this->usedBlobHashes;
90 }
91
92 public function getExtraPayload() : string{
93 return $this->extraPayload;
94 }
95
96 protected function decodePayload(ByteBufferReader $in) : void{
97 $this->chunkPosition = ChunkPosition::read($in);
98 $this->dimensionId = VarInt::readSignedInt($in);
99
100 $subChunkCountButNotReally = VarInt::readUnsignedInt($in);
101 if($subChunkCountButNotReally === self::CLIENT_REQUEST_FULL_COLUMN_FAKE_COUNT){
102 $this->clientSubChunkRequestsEnabled = true;
103 $this->subChunkCount = PHP_INT_MAX;
104 }elseif($subChunkCountButNotReally === self::CLIENT_REQUEST_TRUNCATED_COLUMN_FAKE_COUNT){
105 $this->clientSubChunkRequestsEnabled = true;
106 $this->subChunkCount = LE::readUnsignedShort($in);
107 }else{
108 $this->clientSubChunkRequestsEnabled = false;
109 $this->subChunkCount = $subChunkCountButNotReally;
110 }
111
112 $cacheEnabled = CommonTypes::getBool($in);
113 if($cacheEnabled){
114 $this->usedBlobHashes = [];
115 $count = VarInt::readUnsignedInt($in);
116 if($count > self::MAX_BLOB_HASHES){
117 throw new PacketDecodeException("Expected at most " . self::MAX_BLOB_HASHES . " blob hashes, got " . $count);
118 }
119 for($i = 0; $i < $count; ++$i){
120 $this->usedBlobHashes[] = LE::readUnsignedLong($in);
121 }
122 }
123 $this->extraPayload = CommonTypes::getString($in);
124 }
125
126 protected function encodePayload(ByteBufferWriter $out) : void{
127 $this->chunkPosition->write($out);
128 VarInt::writeSignedInt($out, $this->dimensionId);
129
130 if($this->clientSubChunkRequestsEnabled){
131 if($this->subChunkCount === PHP_INT_MAX){
132 VarInt::writeUnsignedInt($out, self::CLIENT_REQUEST_FULL_COLUMN_FAKE_COUNT);
133 }else{
134 VarInt::writeUnsignedInt($out, self::CLIENT_REQUEST_TRUNCATED_COLUMN_FAKE_COUNT);
135 LE::writeUnsignedShort($out, $this->subChunkCount);
136 }
137 }else{
138 VarInt::writeUnsignedInt($out, $this->subChunkCount);
139 }
140
141 CommonTypes::putBool($out, $this->usedBlobHashes !== null);
142 if($this->usedBlobHashes !== null){
143 VarInt::writeUnsignedInt($out, count($this->usedBlobHashes));
144 foreach($this->usedBlobHashes as $hash){
145 LE::writeUnsignedLong($out, $hash);
146 }
147 }
148 CommonTypes::putString($out, $this->extraPayload);
149 }
150
151 public function handle(PacketHandlerInterface $handler) : bool{
152 return $handler->handleLevelChunk($this);
153 }
154}
static create(ChunkPosition $chunkPosition, int $dimensionId, int $subChunkCount, bool $clientSubChunkRequestsEnabled, ?array $usedBlobHashes, string $extraPayload)