PocketMine-MP 5.15.1 git-08c6e63aac45a450fe3b437616ee8aa2500e04e2
SubChunkPacket.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
23use function count;
24
26 public const NETWORK_ID = ProtocolInfo::SUB_CHUNK_PACKET;
27
28 private int $dimension;
29 private SubChunkPosition $baseSubChunkPosition;
30 private ListWithBlobHashes|ListWithoutBlobHashes $entries;
31
35 public static function create(int $dimension, SubChunkPosition $baseSubChunkPosition, ListWithBlobHashes|ListWithoutBlobHashes $entries) : self{
36 $result = new self;
37 $result->dimension = $dimension;
38 $result->baseSubChunkPosition = $baseSubChunkPosition;
39 $result->entries = $entries;
40 return $result;
41 }
42
43 public function isCacheEnabled() : bool{ return $this->entries instanceof ListWithBlobHashes; }
44
45 public function getDimension() : int{ return $this->dimension; }
46
47 public function getBaseSubChunkPosition() : SubChunkPosition{ return $this->baseSubChunkPosition; }
48
49 public function getEntries() : ListWithBlobHashes|ListWithoutBlobHashes{ return $this->entries; }
50
51 protected function decodePayload(PacketSerializer $in) : void{
52 $cacheEnabled = $in->getBool();
53 $this->dimension = $in->getVarInt();
54 $this->baseSubChunkPosition = SubChunkPosition::read($in);
55
56 $count = $in->getLInt();
57 if($cacheEnabled){
58 $entries = [];
59 for($i = 0; $i < $count; $i++){
60 $entries[] = EntryWithBlobHash::read($in);
61 }
62 $this->entries = new ListWithBlobHashes($entries);
63 }else{
64 $entries = [];
65 for($i = 0; $i < $count; $i++){
66 $entries[] = EntryWithoutBlobHash::read($in);
67 }
68 $this->entries = new ListWithoutBlobHashes($entries);
69 }
70 }
71
72 protected function encodePayload(PacketSerializer $out) : void{
73 $out->putBool($this->entries instanceof ListWithBlobHashes);
74 $out->putVarInt($this->dimension);
75 $this->baseSubChunkPosition->write($out);
76
77 $out->putLInt(count($this->entries->getEntries()));
78
79 foreach($this->entries->getEntries() as $entry){
80 $entry->write($out);
81 }
82 }
83
84 public function handle(PacketHandlerInterface $handler) : bool{
85 return $handler->handleSubChunk($this);
86 }
87}
handle(PacketHandlerInterface $handler)
static create(int $dimension, SubChunkPosition $baseSubChunkPosition, ListWithBlobHashes|ListWithoutBlobHashes $entries)