PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
Loading...
Searching...
No Matches
SubChunkPacketEntryCommon.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\types;
16
17use pmmp\encoding\Byte;
18use pmmp\encoding\ByteBufferReader;
19use pmmp\encoding\ByteBufferWriter;
22
24
25 public function __construct(
26 private SubChunkPositionOffset $offset,
27 private int $requestResult,
28 private string $terrainData,
29 private ?SubChunkPacketHeightMapInfo $heightMap,
30 private ?SubChunkPacketHeightMapInfo $renderHeightMap
31 ){}
32
33 public function getOffset() : SubChunkPositionOffset{ return $this->offset; }
34
35 public function getRequestResult() : int{ return $this->requestResult; }
36
37 public function getTerrainData() : string{ return $this->terrainData; }
38
39 public function getHeightMap() : ?SubChunkPacketHeightMapInfo{ return $this->heightMap; }
40
41 public function getRenderHeightMap() : ?SubChunkPacketHeightMapInfo{ return $this->renderHeightMap; }
42
43 public static function read(ByteBufferReader $in, bool $cacheEnabled) : self{
44 $offset = SubChunkPositionOffset::read($in);
45
46 $requestResult = Byte::readUnsigned($in);
47
48 $data = !$cacheEnabled || $requestResult !== SubChunkRequestResult::SUCCESS_ALL_AIR ? CommonTypes::getString($in) : "";
49
50 $heightMapDataType = Byte::readUnsigned($in);
51 $heightMapData = match ($heightMapDataType) {
52 SubChunkPacketHeightMapType::NO_DATA => null,
53 SubChunkPacketHeightMapType::DATA => SubChunkPacketHeightMapInfo::read($in),
54 SubChunkPacketHeightMapType::ALL_TOO_HIGH => SubChunkPacketHeightMapInfo::allTooHigh(),
55 SubChunkPacketHeightMapType::ALL_TOO_LOW => SubChunkPacketHeightMapInfo::allTooLow(),
56 default => throw new PacketDecodeException("Unknown heightmap data type $heightMapDataType")
57 };
58
59 $renderHeightMapDataType = Byte::readUnsigned($in);
60 $renderHeightMapData = match ($renderHeightMapDataType) {
61 SubChunkPacketHeightMapType::NO_DATA => null,
62 SubChunkPacketHeightMapType::DATA => SubChunkPacketHeightMapInfo::read($in),
63 SubChunkPacketHeightMapType::ALL_TOO_HIGH => SubChunkPacketHeightMapInfo::allTooHigh(),
64 SubChunkPacketHeightMapType::ALL_TOO_LOW => SubChunkPacketHeightMapInfo::allTooLow(),
65 SubChunkPacketHeightMapType::ALL_COPIED => $heightMapData,
66 default => throw new PacketDecodeException("Unknown render heightmap data type $renderHeightMapDataType")
67 };
68
69 return new self(
70 $offset,
71 $requestResult,
72 $data,
73 $heightMapData,
74 $renderHeightMapData
75 );
76 }
77
78 public function write(ByteBufferWriter $out, bool $cacheEnabled) : void{
79 $this->offset->write($out);
80
81 Byte::writeUnsigned($out, $this->requestResult);
82
83 if(!$cacheEnabled || $this->requestResult !== SubChunkRequestResult::SUCCESS_ALL_AIR){
84 CommonTypes::putString($out, $this->terrainData);
85 }
86
87 if($this->heightMap === null){
88 Byte::writeUnsigned($out, SubChunkPacketHeightMapType::NO_DATA);
89 }elseif($this->heightMap->isAllTooLow()){
90 Byte::writeUnsigned($out, SubChunkPacketHeightMapType::ALL_TOO_LOW);
91 }elseif($this->heightMap->isAllTooHigh()){
92 Byte::writeUnsigned($out, SubChunkPacketHeightMapType::ALL_TOO_HIGH);
93 }else{
94 $heightMapData = $this->heightMap; //avoid PHPStan purity issue
95 Byte::writeUnsigned($out, SubChunkPacketHeightMapType::DATA);
96 $heightMapData->write($out);
97 }
98
99 if($this->renderHeightMap === null){
100 Byte::writeUnsigned($out, SubChunkPacketHeightMapType::ALL_COPIED);
101 }elseif($this->renderHeightMap->isAllTooLow()){
102 Byte::writeUnsigned($out, SubChunkPacketHeightMapType::ALL_TOO_LOW);
103 }elseif($this->renderHeightMap->isAllTooHigh()){
104 Byte::writeUnsigned($out, SubChunkPacketHeightMapType::ALL_TOO_HIGH);
105 }else{
106 $renderHeightMapData = $this->renderHeightMap; //avoid PHPStan purity issue
107 Byte::writeUnsigned($out, SubChunkPacketHeightMapType::DATA);
108 $renderHeightMapData->write($out);
109 }
110 }
111}