PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
Loading...
Searching...
No Matches
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 count;
28
30 public const COORD_BIT_SIZE = 4;
31 public const COORD_MASK = ~(~0 << self::COORD_BIT_SIZE);
32 public const EDGE_LENGTH = 1 << self::COORD_BIT_SIZE;
33
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
90 public function getBlockLayers() : array{
91 return $this->blockLayers;
92 }
93
94 public function getHighestBlockAt(int $x, int $z) : ?int{
95 if(count($this->blockLayers) === 0){
96 return null;
97 }
98 for($y = self::EDGE_LENGTH - 1; $y >= 0; --$y){
99 if($this->blockLayers[0]->get($x, $y, $z) !== $this->emptyBlockId){
100 return $y;
101 }
102 }
103
104 return null; //highest block not in this subchunk
105 }
106
107 public function getBiomeArray() : PalettedBlockArray{ return $this->biomes; }
108
109 public function getBlockSkyLightArray() : LightArray{
110 return $this->skyLight ??= LightArray::fill(0);
111 }
112
113 public function setBlockSkyLightArray(LightArray $data) : void{
114 $this->skyLight = $data;
115 }
116
117 public function getBlockLightArray() : LightArray{
118 return $this->blockLight ??= LightArray::fill(0);
119 }
120
121 public function setBlockLightArray(LightArray $data) : void{
122 $this->blockLight = $data;
123 }
124
128 public function __debugInfo() : array{
129 return [];
130 }
131
132 public function collectGarbage() : void{
133 $cleanedLayers = [];
134 foreach($this->blockLayers as $layer){
135 $layer->collectGarbage();
136
137 foreach($layer->getPalette() as $p){
138 if($p !== $this->emptyBlockId){
139 $cleanedLayers[] = $layer;
140 continue 2;
141 }
142 }
143 }
144 $this->blockLayers = $cleanedLayers;
145 $this->biomes->collectGarbage();
146
147 if($this->skyLight !== null && $this->skyLight->isUniform(0)){
148 $this->skyLight = null;
149 }
150 if($this->blockLight !== null && $this->blockLight->isUniform(0)){
151 $this->blockLight = null;
152 }
153 }
154
155 public function __clone(){
156 $this->blockLayers = array_map(function(PalettedBlockArray $array) : PalettedBlockArray{
157 return clone $array;
158 }, $this->blockLayers);
159 $this->biomes = clone $this->biomes;
160
161 if($this->skyLight !== null){
162 $this->skyLight = clone $this->skyLight;
163 }
164 if($this->blockLight !== null){
165 $this->blockLight = clone $this->blockLight;
166 }
167 }
168}
__construct(private int $emptyBlockId, private array $blockLayers, private PalettedBlockArray $biomes, private ?LightArray $skyLight=null, private ?LightArray $blockLight=null)
Definition SubChunk.php:40