PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
BaseChunk.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 
22 namespace pocketmine\level\format\generic;
23 
32 
33 abstract class BaseChunk extends BaseFullChunk implements Chunk{
34 
36  protected $sections = [];
37 
51  protected function __construct($provider, $x, $z, array $sections, $biomeIds = null, array $biomeColors = [], array $heightMap = [], array $entities = [], array $tiles = []){
52  $this->provider = $provider;
53  $this->x = (int) $x;
54  $this->z = (int) $z;
55  foreach($sections as $Y => $section){
56  if($section instanceof ChunkSection){
57  $this->sections[$Y] = $section;
58  }else{
59  throw new ChunkException("Received invalid ChunkSection instance");
60  }
61 
62  if($Y >= self::SECTION_COUNT){
63  throw new ChunkException("Invalid amount of chunks");
64  }
65  }
66 
67  if(strlen($biomeIds) === 256){
68  $this->biomeIds = $biomeIds;
69  }else{
70  $this->biomeIds = str_repeat("\x01", 256);
71  }
72 
73  if(count($biomeColors) === 256){
74  $this->biomeColors = $biomeColors;
75  }else{
76  $this->biomeColors = array_fill(0, 256, Binary::readInt("\x00\x85\xb2\x4a"));
77  }
78 
79  if(count($heightMap) === 256){
80  $this->heightMap = $heightMap;
81  }else{
82  $this->heightMap = array_fill(0, 256, 127);
83  }
84 
85  $this->NBTtiles = $tiles;
86  $this->NBTentities = $entities;
87  }
88 
89  public function getBlock($x, $y, $z, &$blockId, &$meta = null){
90  $full = $this->sections[$y >> 4]->getFullBlock($x, $y & 0x0f, $z);
91  $blockId = $full >> 4;
92  $meta = $full & 0x0f;
93  }
94 
95  public function getFullBlock($x, $y, $z){
96  return $this->sections[$y >> 4]->getFullBlock($x, $y & 0x0f, $z);
97  }
98 
99  public function setBlock($x, $y, $z, $blockId = null, $meta = null){
100  try{
101  $this->hasChanged = true;
102  return $this->sections[$y >> 4]->setBlock($x, $y & 0x0f, $z, $blockId & 0xff, $meta & 0x0f);
103  }catch(ChunkException $e){
104  $level = $this->getProvider();
105  $this->setInternalSection($Y = $y >> 4, $level::createChunkSection($Y));
106  return $this->sections[$y >> 4]->setBlock($x, $y & 0x0f, $z, $blockId & 0xff, $meta & 0x0f);
107  }
108  }
109 
110  public function getBlockId($x, $y, $z){
111  return $this->sections[$y >> 4]->getBlockId($x, $y & 0x0f, $z);
112  }
113 
114  public function setBlockId($x, $y, $z, $id){
115  try{
116  $this->sections[$y >> 4]->setBlockId($x, $y & 0x0f, $z, $id);
117  $this->hasChanged = true;
118  }catch(ChunkException $e){
119  $level = $this->getProvider();
120  $this->setInternalSection($Y = $y >> 4, $level::createChunkSection($Y));
121  $this->setBlockId($x, $y, $z, $id);
122  }
123  }
124 
125  public function getBlockData($x, $y, $z){
126  return $this->sections[$y >> 4]->getBlockData($x, $y & 0x0f, $z);
127  }
128 
129  public function setBlockData($x, $y, $z, $data){
130  try{
131  $this->sections[$y >> 4]->setBlockData($x, $y & 0x0f, $z, $data);
132  $this->hasChanged = true;
133  }catch(ChunkException $e){
134  $level = $this->getProvider();
135  $this->setInternalSection($Y = $y >> 4, $level::createChunkSection($Y));
136  $this->setBlockData($x, $y, $z, $data);
137  }
138  }
139 
140  public function getBlockSkyLight($x, $y, $z){
141  return $this->sections[$y >> 4]->getBlockSkyLight($x, $y & 0x0f, $z);
142  }
143 
144  public function setBlockSkyLight($x, $y, $z, $data){
145  try{
146  $this->sections[$y >> 4]->getBlockSkyLight($x, $y & 0x0f, $z, $data);
147  $this->hasChanged = true;
148  }catch(ChunkException $e){
149  $level = $this->getProvider();
150  $this->setInternalSection($Y = $y >> 4, $level::createChunkSection($Y));
151  $this->setBlockSkyLight($x, $y, $z, $data);
152  }
153  }
154 
155  public function getBlockLight($x, $y, $z){
156  return $this->sections[$y >> 4]->getBlockSkyLight($x, $y & 0x0f, $z);
157  }
158 
159  public function setBlockLight($x, $y, $z, $data){
160  try{
161  $this->sections[$y >> 4]->getBlockSkyLight($x, $y & 0x0f, $z, $data);
162  $this->hasChanged = true;
163  }catch(ChunkException $e){
164  $level = $this->getProvider();
165  $this->setInternalSection($Y = $y >> 4, $level::createChunkSection($Y));
166  $this->setBlockLight($x, $y, $z, $data);
167  }
168  }
169 
170  public function getBlockIdColumn($x, $z){
171  $column = "";
172  for($y = 0; $y < Chunk::SECTION_COUNT; ++$y){
173  $column .= $this->sections[$y]->getBlockIdColumn($x, $z);
174  }
175 
176  return $column;
177  }
178 
179  public function getBlockDataColumn($x, $z){
180  $column = "";
181  for($y = 0; $y < Chunk::SECTION_COUNT; ++$y){
182  $column .= $this->sections[$y]->getBlockDataColumn($x, $z);
183  }
184 
185  return $column;
186  }
187 
188  public function getBlockSkyLightColumn($x, $z){
189  $column = "";
190  for($y = 0; $y < Chunk::SECTION_COUNT; ++$y){
191  $column .= $this->sections[$y]->getBlockSkyLightColumn($x, $z);
192  }
193 
194  return $column;
195  }
196 
197  public function getBlockLightColumn($x, $z){
198  $column = "";
199  for($y = 0; $y < Chunk::SECTION_COUNT; ++$y){
200  $column .= $this->sections[$y]->getBlockLightColumn($x, $z);
201  }
202 
203  return $column;
204  }
205 
206  public function isSectionEmpty($fY){
207  return $this->sections[(int) $fY] instanceof EmptyChunkSection;
208  }
209 
210  public function getSection($fY){
211  return $this->sections[(int) $fY];
212  }
213 
214  public function setSection($fY, ChunkSection $section){
215  if(substr_count($section->getIdArray(), "\x00") === 4096 and substr_count($section->getDataArray(), "\x00") === 2048){
216  $this->sections[(int) $fY] = new EmptyChunkSection($fY);
217  }else{
218  $this->sections[(int) $fY] = $section;
219  }
220  $this->hasChanged = true;
221  }
222 
223  private function setInternalSection($fY, ChunkSection $section){
224  $this->sections[(int) $fY] = $section;
225  $this->hasChanged = true;
226  }
227 
228  public function load($generate = true){
229  return $this->getProvider() === null ? false : $this->getProvider()->getChunk($this->getX(), $this->getZ(), true) instanceof Chunk;
230  }
231 
232  public function getBlockIdArray(){
233  $blocks = "";
234  for($y = 0; $y < Chunk::SECTION_COUNT; ++$y){
235  $blocks .= $this->sections[$y]->getIdArray();
236  }
237 
238  return $blocks;
239  }
240 
241  public function getBlockDataArray(){
242  $data = "";
243  for($y = 0; $y < Chunk::SECTION_COUNT; ++$y){
244  $data .= $this->sections[$y]->getDataArray();
245  }
246 
247  return $data;
248  }
249 
250  public function getBlockSkyLightArray(){
251  $skyLight = "";
252  for($y = 0; $y < Chunk::SECTION_COUNT; ++$y){
253  $skyLight .= $this->sections[$y]->getSkyLightArray();
254  }
255 
256  return $skyLight;
257  }
258 
259  public function getBlockLightArray(){
260  $blockLight = "";
261  for($y = 0; $y < Chunk::SECTION_COUNT; ++$y){
262  $blockLight .= $this->sections[$y]->getLightArray();
263  }
264 
265  return $blockLight;
266  }
267 
271  public function getSections(){
272  return $this->sections;
273  }
274 
275 }
setSection($fY, ChunkSection $section)
Definition: BaseChunk.php:214
__construct($provider, $x, $z, array $sections, $biomeIds=null, array $biomeColors=[], array $heightMap=[], array $entities=[], array $tiles=[])
Definition: BaseChunk.php:51
setBlock($x, $y, $z, $blockId=null, $meta=null)
Definition: BaseChunk.php:99
getBlock($x, $y, $z, &$blockId, &$meta=null)
Definition: BaseChunk.php:89