PocketMine-MP 5.15.1 git-5ef247620a7c6301a849b54e5ef1009217729fc8
SubChunk.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\world\format;
25
26use function array_map;
27use function array_values;
28use function count;
29
31 public const COORD_BIT_SIZE = 4;
32 public const COORD_MASK = ~(~0 << self::COORD_BIT_SIZE);
33 public const EDGE_LENGTH = 1 << self::COORD_BIT_SIZE;
34
40 public function __construct(
41 private int $emptyBlockId,
42 private array $blockLayers,
43 private PalettedBlockArray $biomes,
44 private ?LightArray $skyLight = null,
45 private ?LightArray $blockLight = null
46 ){}
47
53 public function isEmptyAuthoritative() : bool{
54 $this->collectGarbage();
55 return $this->isEmptyFast();
56 }
57
62 public function isEmptyFast() : bool{
63 return count($this->blockLayers) === 0;
64 }
65
70 public function getEmptyBlockId() : int{ return $this->emptyBlockId; }
71
72 public function getBlockStateId(int $x, int $y, int $z) : int{
73 if(count($this->blockLayers) === 0){
74 return $this->emptyBlockId;
75 }
76 return $this->blockLayers[0]->get($x, $y, $z);
77 }
78
79 public function setBlockStateId(int $x, int $y, int $z, int $block) : void{
80 if(count($this->blockLayers) === 0){
81 $this->blockLayers[] = new PalettedBlockArray($this->emptyBlockId);
82 }
83 $this->blockLayers[0]->set($x, $y, $z, $block);
84 }
85
89 public function getBlockLayers() : array{
90 return $this->blockLayers;
91 }
92
93 public function getHighestBlockAt(int $x, int $z) : ?int{
94 if(count($this->blockLayers) === 0){
95 return null;
96 }
97 for($y = self::EDGE_LENGTH - 1; $y >= 0; --$y){
98 if($this->blockLayers[0]->get($x, $y, $z) !== $this->emptyBlockId){
99 return $y;
100 }
101 }
102
103 return null; //highest block not in this subchunk
104 }
105
106 public function getBiomeArray() : PalettedBlockArray{ return $this->biomes; }
107
108 public function getBlockSkyLightArray() : LightArray{
109 return $this->skyLight ??= LightArray::fill(0);
110 }
111
112 public function setBlockSkyLightArray(LightArray $data) : void{
113 $this->skyLight = $data;
114 }
115
116 public function getBlockLightArray() : LightArray{
117 return $this->blockLight ??= LightArray::fill(0);
118 }
119
120 public function setBlockLightArray(LightArray $data) : void{
121 $this->blockLight = $data;
122 }
123
127 public function __debugInfo() : array{
128 return [];
129 }
130
131 public function collectGarbage() : void{
132 foreach($this->blockLayers as $k => $layer){
133 $layer->collectGarbage();
134
135 foreach($layer->getPalette() as $p){
136 if($p !== $this->emptyBlockId){
137 continue 2;
138 }
139 }
140 unset($this->blockLayers[$k]);
141 }
142 $this->blockLayers = array_values($this->blockLayers);
143 $this->biomes->collectGarbage();
144
145 if($this->skyLight !== null && $this->skyLight->isUniform(0)){
146 $this->skyLight = null;
147 }
148 if($this->blockLight !== null && $this->blockLight->isUniform(0)){
149 $this->blockLight = null;
150 }
151 }
152
153 public function __clone(){
154 $this->blockLayers = array_map(function(PalettedBlockArray $array) : PalettedBlockArray{
155 return clone $array;
156 }, $this->blockLayers);
157 $this->biomes = clone $this->biomes;
158
159 if($this->skyLight !== null){
160 $this->skyLight = clone $this->skyLight;
161 }
162 if($this->blockLight !== null){
163 $this->blockLight = clone $this->blockLight;
164 }
165 }
166}
__construct(private int $emptyBlockId, private array $blockLayers, private PalettedBlockArray $biomes, private ?LightArray $skyLight=null, private ?LightArray $blockLight=null)
Definition: SubChunk.php:40