PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
Loading...
Searching...
No Matches
NetworkChunkPublisherUpdatePacket.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;
24use function count;
25
27 public const NETWORK_ID = ProtocolInfo::NETWORK_CHUNK_PUBLISHER_UPDATE_PACKET;
28
29 public BlockPosition $blockPosition;
30 public int $radius;
32 public array $savedChunks = [];
33
34 public const MAX_SAVED_CHUNKS = 9216;
35
40 public static function create(BlockPosition $blockPosition, int $radius, array $savedChunks) : self{
41 $result = new self;
42 $result->blockPosition = $blockPosition;
43 $result->radius = $radius;
44 $result->savedChunks = $savedChunks;
45 return $result;
46 }
47
48 protected function decodePayload(ByteBufferReader $in) : void{
49 $this->blockPosition = CommonTypes::getSignedBlockPosition($in);
50 $this->radius = VarInt::readUnsignedInt($in);
51
52 $count = LE::readUnsignedInt($in);
53 if($count > self::MAX_SAVED_CHUNKS){
54 throw new PacketDecodeException("Expected at most " . self::MAX_SAVED_CHUNKS . " saved chunks, got " . $count);
55 }
56 for($i = 0, $this->savedChunks = []; $i < $count; $i++){
57 $this->savedChunks[] = ChunkPosition::read($in);
58 }
59 }
60
61 protected function encodePayload(ByteBufferWriter $out) : void{
62 CommonTypes::putSignedBlockPosition($out, $this->blockPosition);
63 VarInt::writeUnsignedInt($out, $this->radius);
64
65 LE::writeUnsignedInt($out, count($this->savedChunks));
66 foreach($this->savedChunks as $chunk){
67 $chunk->write($out);
68 }
69 }
70
71 public function handle(PacketHandlerInterface $handler) : bool{
72 return $handler->handleNetworkChunkPublisherUpdate($this);
73 }
74}
static create(BlockPosition $blockPosition, int $radius, array $savedChunks)