PocketMine-MP 5.42.2 git-40e2639b20bdc4ddba49d9f7f5fa0d5e92aa266f
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 isClientSubChunkRequestsEnabled() : bool{
78 return $this->clientSubChunkRequestsEnabled;
79 }
80
82 public function isClientSubChunkRequestEnabled() : bool{
83 return $this->clientSubChunkRequestsEnabled;
84 }
85
86 public function isCacheEnabled() : bool{
87 return $this->usedBlobHashes !== null;
88 }
89
93 public function getUsedBlobHashes() : ?array{
94 return $this->usedBlobHashes;
95 }
96
97 public function getExtraPayload() : string{
98 return $this->extraPayload;
99 }
100
101 protected function decodePayload(ByteBufferReader $in) : void{
102 $this->chunkPosition = ChunkPosition::read($in);
103 $this->dimensionId = VarInt::readSignedInt($in);
104
105 $subChunkCountButNotReally = VarInt::readUnsignedInt($in);
106 if($subChunkCountButNotReally === self::CLIENT_REQUEST_FULL_COLUMN_FAKE_COUNT){
107 $this->clientSubChunkRequestsEnabled = true;
108 $this->subChunkCount = PHP_INT_MAX;
109 }elseif($subChunkCountButNotReally === self::CLIENT_REQUEST_TRUNCATED_COLUMN_FAKE_COUNT){
110 $this->clientSubChunkRequestsEnabled = true;
111 $this->subChunkCount = LE::readUnsignedShort($in);
112 }else{
113 $this->clientSubChunkRequestsEnabled = false;
114 $this->subChunkCount = $subChunkCountButNotReally;
115 }
116
117 $cacheEnabled = CommonTypes::getBool($in);
118 if($cacheEnabled){
119 $this->usedBlobHashes = [];
120 $count = VarInt::readUnsignedInt($in);
121 if($count > self::MAX_BLOB_HASHES){
122 throw new PacketDecodeException("Expected at most " . self::MAX_BLOB_HASHES . " blob hashes, got " . $count);
123 }
124 for($i = 0; $i < $count; ++$i){
125 $this->usedBlobHashes[] = LE::readUnsignedLong($in);
126 }
127 }
128 $this->extraPayload = CommonTypes::getString($in);
129 }
130
131 protected function encodePayload(ByteBufferWriter $out) : void{
132 $this->chunkPosition->write($out);
133 VarInt::writeSignedInt($out, $this->dimensionId);
134
135 if($this->clientSubChunkRequestsEnabled){
136 if($this->subChunkCount === PHP_INT_MAX){
137 VarInt::writeUnsignedInt($out, self::CLIENT_REQUEST_FULL_COLUMN_FAKE_COUNT);
138 }else{
139 VarInt::writeUnsignedInt($out, self::CLIENT_REQUEST_TRUNCATED_COLUMN_FAKE_COUNT);
140 LE::writeUnsignedShort($out, $this->subChunkCount);
141 }
142 }else{
143 VarInt::writeUnsignedInt($out, $this->subChunkCount);
144 }
145
146 CommonTypes::putBool($out, $this->usedBlobHashes !== null);
147 if($this->usedBlobHashes !== null){
148 VarInt::writeUnsignedInt($out, count($this->usedBlobHashes));
149 foreach($this->usedBlobHashes as $hash){
150 LE::writeUnsignedLong($out, $hash);
151 }
152 }
153 CommonTypes::putString($out, $this->extraPayload);
154 }
155
156 public function handle(PacketHandlerInterface $handler) : bool{
157 return $handler->handleLevelChunk($this);
158 }
159}
static create(ChunkPosition $chunkPosition, int $dimensionId, int $subChunkCount, bool $clientSubChunkRequestsEnabled, ?array $usedBlobHashes, string $extraPayload)