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