PocketMine-MP 5.21.2 git-a6534ecbbbcf369264567d27e5ed70f7f5be9816
Loading...
Searching...
No Matches
SubChunkPacketHeightMapInfo.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
19use function array_fill;
20use function count;
21
23
28 public function __construct(private array $heights){
29 if(count($heights) !== 256){
30 throw new \InvalidArgumentException("Expected exactly 256 heightmap values");
31 }
32 }
33
35 public function getHeights() : array{ return $this->heights; }
36
37 public function getHeight(int $x, int $z) : int{
38 return $this->heights[(($z & 0xf) << 4) | ($x & 0xf)];
39 }
40
41 public static function read(PacketSerializer $in) : self{
42 $heights = [];
43 for($i = 0; $i < 256; ++$i){
44 $heights[] = Binary::signByte($in->getByte());
45 }
46 return new self($heights);
47 }
48
49 public function write(PacketSerializer $out) : void{
50 for($i = 0; $i < 256; ++$i){
51 $out->putByte(Binary::unsignByte($this->heights[$i]));
52 }
53 }
54
55 public static function allTooLow() : self{
56 return new self(array_fill(0, 256, -1));
57 }
58
59 public static function allTooHigh() : self{
60 return new self(array_fill(0, 256, 16));
61 }
62
63 public function isAllTooLow() : bool{
64 foreach($this->heights as $height){
65 if($height >= 0){
66 return false;
67 }
68 }
69 return true;
70 }
71
72 public function isAllTooHigh() : bool{
73 foreach($this->heights as $height){
74 if($height <= 15){
75 return false;
76 }
77 }
78 return true;
79 }
80}