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