PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
Loading...
Searching...
No Matches
SubChunkRequestPacket.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;
23use function count;
24
26 public const NETWORK_ID = ProtocolInfo::SUB_CHUNK_REQUEST_PACKET;
27
28 private int $dimension;
29 private SubChunkPosition $basePosition;
34 private array $entries;
35
41 public static function create(int $dimension, SubChunkPosition $basePosition, array $entries) : self{
42 $result = new self;
43 $result->dimension = $dimension;
44 $result->basePosition = $basePosition;
45 $result->entries = $entries;
46 return $result;
47 }
48
49 public function getDimension() : int{ return $this->dimension; }
50
51 public function getBasePosition() : SubChunkPosition{ return $this->basePosition; }
52
57 public function getEntries() : array{ return $this->entries; }
58
59 protected function decodePayload(ByteBufferReader $in) : void{
60 $this->dimension = VarInt::readSignedInt($in);
61 $this->basePosition = SubChunkPosition::read($in);
62
63 $this->entries = [];
64 for($i = 0, $count = LE::readUnsignedInt($in); $i < $count; $i++){
65 $this->entries[] = SubChunkPositionOffset::read($in);
66 }
67 }
68
69 protected function encodePayload(ByteBufferWriter $out) : void{
70 VarInt::writeSignedInt($out, $this->dimension);
71 $this->basePosition->write($out);
72
73 LE::writeUnsignedInt($out, count($this->entries));
74 foreach($this->entries as $entry){
75 $entry->write($out);
76 }
77 }
78
79 public function handle(PacketHandlerInterface $handler) : bool{
80 return $handler->handleSubChunkRequest($this);
81 }
82}
static create(int $dimension, SubChunkPosition $basePosition, array $entries)