PocketMine-MP 5.19.1 git-5cc1068cd43264d3363295eb8d6901e02f467897
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 ){}
28
29 public function getOffset() : SubChunkPositionOffset{ return $this->offset; }
30
31 public function getRequestResult() : int{ return $this->requestResult; }
32
33 public function getTerrainData() : string{ return $this->terrainData; }
34
35 public function getHeightMap() : ?SubChunkPacketHeightMapInfo{ return $this->heightMap; }
36
37 public static function read(PacketSerializer $in, bool $cacheEnabled) : self{
38 $offset = SubChunkPositionOffset::read($in);
39
40 $requestResult = $in->getByte();
41
42 $data = !$cacheEnabled || $requestResult !== SubChunkRequestResult::SUCCESS_ALL_AIR ? $in->getString() : "";
43
44 $heightMapDataType = $in->getByte();
45 $heightMapData = match ($heightMapDataType) {
46 SubChunkPacketHeightMapType::NO_DATA => null,
47 SubChunkPacketHeightMapType::DATA => SubChunkPacketHeightMapInfo::read($in),
48 SubChunkPacketHeightMapType::ALL_TOO_HIGH => SubChunkPacketHeightMapInfo::allTooHigh(),
49 SubChunkPacketHeightMapType::ALL_TOO_LOW => SubChunkPacketHeightMapInfo::allTooLow(),
50 default => throw new PacketDecodeException("Unknown heightmap data type $heightMapDataType")
51 };
52
53 return new self(
54 $offset,
55 $requestResult,
56 $data,
57 $heightMapData
58 );
59 }
60
61 public function write(PacketSerializer $out, bool $cacheEnabled) : void{
62 $this->offset->write($out);
63
64 $out->putByte($this->requestResult);
65
66 if(!$cacheEnabled || $this->requestResult !== SubChunkRequestResult::SUCCESS_ALL_AIR){
67 $out->putString($this->terrainData);
68 }
69
70 if($this->heightMap === null){
71 $out->putByte(SubChunkPacketHeightMapType::NO_DATA);
72 }elseif($this->heightMap->isAllTooLow()){
73 $out->putByte(SubChunkPacketHeightMapType::ALL_TOO_LOW);
74 }elseif($this->heightMap->isAllTooHigh()){
75 $out->putByte(SubChunkPacketHeightMapType::ALL_TOO_HIGH);
76 }else{
77 $heightMapData = $this->heightMap; //avoid PHPStan purity issue
78 $out->putByte(SubChunkPacketHeightMapType::DATA);
79 $heightMapData->write($out);
80 }
81 }
82}