PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
anvil/ChunkSection.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\anvil;
23 
25 
27 
28  private $y;
29  private $blocks;
30  private $data;
31  private $blockLight;
32  private $skyLight;
33 
34  public function __construct(Compound $nbt){
35  $this->y = (int) $nbt["Y"];
36  $this->blocks = (string) $nbt["Blocks"];
37  $this->data = (string) $nbt["Data"];
38  $this->blockLight = (string) $nbt["BlockLight"];
39  $this->skyLight = (string) $nbt["SkyLight"];
40  }
41 
42  public function getY(){
43  return $this->y;
44  }
45 
46  public function getBlockId($x, $y, $z){
47  return ord($this->blocks{($y << 8) + ($z << 4) + $x});
48  }
49 
50  public function setBlockId($x, $y, $z, $id){
51  $this->blocks{($y << 8) + ($z << 4) + $x} = chr($id);
52  }
53 
54  public function getBlockData($x, $y, $z){
55  $m = ord($this->data{($y << 7) + ($z << 3) + ($x >> 1)});
56  if(($x & 1) === 0){
57  return $m & 0x0F;
58  }else{
59  return $m >> 4;
60  }
61  }
62 
63  public function setBlockData($x, $y, $z, $data){
64  $i = ($y << 7) + ($z << 3) + ($x >> 1);
65  $old_m = ord($this->data{$i});
66  if(($x & 1) === 0){
67  $this->data{$i} = chr(($old_m & 0xf0) | ($data & 0x0f));
68  }else{
69  $this->data{$i} = chr((($data & 0x0f) << 4) | ($old_m & 0x0f));
70  }
71  }
72 
73  public function getBlock($x, $y, $z, &$blockId, &$meta = null){
74  $full = $this->getFullBlock($x, $y, $z);
75  $blockId = $full >> 4;
76  $meta = $full & 0x0f;
77  }
78 
79  public function getFullBlock($x, $y, $z){
80  $i = ($y << 8) + ($z << 4) + $x;
81  if(($x & 1) === 0){
82  return (ord($this->blocks{$i}) << 4) | (ord($this->data{$i >> 1}) & 0x0F);
83  }else{
84  return (ord($this->blocks{$i}) << 4) | (ord($this->data{$i >> 1}) >> 4);
85  }
86  }
87 
88  public function setBlock($x, $y, $z, $blockId = null, $meta = null){
89  $i = ($y << 8) + ($z << 4) + $x;
90 
91  $changed = false;
92 
93  if($blockId !== null){
94  $blockId = chr($blockId);
95  if($this->blocks{$i} !== $blockId){
96  $this->blocks{$i} = $blockId;
97  $changed = true;
98  }
99  }
100 
101  if($meta !== null){
102  $i >>= 1;
103  $old_m = ord($this->data{$i});
104  if(($x & 1) === 0){
105  $this->data{$i} = chr(($old_m & 0xf0) | ($meta & 0x0f));
106  if(($old_m & 0x0f) !== $meta){
107  $changed = true;
108  }
109  }else{
110  $this->data{$i} = chr((($meta & 0x0f) << 4) | ($old_m & 0x0f));
111  if((($old_m & 0xf0) >> 4) !== $meta){
112  $changed = true;
113  }
114  }
115  }
116 
117  return $changed;
118  }
119 
120  public function getBlockSkyLight($x, $y, $z){
121  $sl = ord($this->skyLight{($y << 7) + ($z << 3) + ($x >> 1)});
122  if(($x & 1) === 0){
123  return $sl & 0x0F;
124  }else{
125  return $sl >> 4;
126  }
127  }
128 
129  public function setBlockSkyLight($x, $y, $z, $level){
130  $i = ($y << 7) + ($z << 3) + ($x >> 1);
131  $old_sl = ord($this->skyLight{$i});
132  if(($x & 1) === 0){
133  $this->skyLight{$i} = chr(($old_sl & 0xf0) | ($level & 0x0f));
134  }else{
135  $this->skyLight{$i} = chr((($level & 0x0f) << 4) | ($old_sl & 0x0f));
136  }
137  }
138 
139  public function getBlockLight($x, $y, $z){
140  $l = ord($this->blockLight{($y << 7) + ($z << 3) + ($x >> 1)});
141  if(($x & 1) === 0){
142  return $l & 0x0F;
143  }else{
144  return $l >> 4;
145  }
146  }
147 
148  public function setBlockLight($x, $y, $z, $level){
149  $i = ($y << 7) + ($z << 3) + ($x >> 1);
150  $old_l = ord($this->blockLight{$i});
151  if(($x & 1) === 0){
152  $this->blockLight{$i} = chr(($old_l & 0xf0) | ($level & 0x0f));
153  }else{
154  $this->blockLight{$i} = chr((($level & 0x0f) << 4) | ($old_l & 0x0f));
155  }
156  }
157 
158  public function getBlockIdColumn($x, $z){
159  $i = ($z << 4) + $x;
160  $column = "";
161  for($y = 0; $y < 16; ++$y){
162  $column .= $this->blocks{($y << 8) + $i};
163  }
164 
165  return $column;
166  }
167 
168  public function getBlockDataColumn($x, $z){
169  $i = ($z << 3) + ($x >> 1);
170  $column = "";
171  if(($x & 1) === 0){
172  for($y = 0; $y < 16; $y += 2){
173  $column .= ($this->data{($y << 7) + $i} & "\x0f") | chr((ord($this->data{(($y + 1) << 7) + $i}) & 0x0f) << 4);
174  }
175  }else{
176  for($y = 0; $y < 16; $y += 2){
177  $column .= chr((ord($this->data{($y << 7) + $i}) & 0xf0) >> 4) | ($this->data{(($y + 1) << 7) + $i} & "\xf0");
178  }
179  }
180 
181  return $column;
182  }
183 
184  public function getBlockSkyLightColumn($x, $z){
185  $i = ($z << 3) + ($x >> 1);
186  $column = "";
187  if(($x & 1) === 0){
188  for($y = 0; $y < 16; $y += 2){
189  $column .= ($this->skyLight{($y << 7) + $i} & "\x0f") | chr((ord($this->skyLight{(($y + 1) << 7) + $i}) & 0x0f) << 4);
190  }
191  }else{
192  for($y = 0; $y < 16; $y += 2){
193  $column .= chr((ord($this->skyLight{($y << 7) + $i}) & 0xf0) >> 4) | ($this->skyLight{(($y + 1) << 7) + $i} & "\xf0");
194  }
195  }
196 
197  return $column;
198  }
199 
200  public function getBlockLightColumn($x, $z){
201  $i = ($z << 3) + ($x >> 1);
202  $column = "";
203  if(($x & 1) === 0){
204  for($y = 0; $y < 16; $y += 2){
205  $column .= ($this->blockLight{($y << 7) + $i} & "\x0f") | chr((ord($this->blockLight{(($y + 1) << 7) + $i}) & 0x0f) << 4);
206  }
207  }else{
208  for($y = 0; $y < 16; $y += 2){
209  $column .= chr((ord($this->blockLight{($y << 7) + $i}) & 0xf0) >> 4) | ($this->blockLight{(($y + 1) << 7) + $i} & "\xf0");
210  }
211  }
212 
213  return $column;
214  }
215 
216  public function getIdArray(){
217  return $this->blocks;
218  }
219 
220  public function getDataArray(){
221  return $this->data;
222  }
223 
224  public function getSkyLightArray(){
225  return $this->skyLight;
226  }
227 
228  public function getLightArray(){
229  return $this->blockLight;
230  }
231 
232 }
getBlock($x, $y, $z, &$blockId, &$meta=null)
setBlock($x, $y, $z, $blockId=null, $meta=null)