PocketMine-MP 5.33.2 git-09cc76ae2b49f1fe3ab0253e6e987fb82bd0a08f
Loading...
Searching...
No Matches
BlockStateReader.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\data\bedrock\block\convert;
25
26use pocketmine\block\utils\BellAttachmentType;
27use pocketmine\block\utils\SlabType;
28use pocketmine\block\utils\WallConnectionType;
43use function array_keys;
44use function count;
45use function get_class;
46use function implode;
47
48final class BlockStateReader{
49
54 private array $unusedStates;
55
56 public function __construct(
57 private BlockStateData $data
58 ){
59 $this->unusedStates = $this->data->getStates();
60 }
61
62 public function missingOrWrongTypeException(string $name, ?Tag $tag) : BlockStateDeserializeException{
63 return new BlockStateDeserializeException("Property \"$name\" " . ($tag !== null ? "has unexpected type " . get_class($tag) : "is missing"));
64 }
65
66 public function badValueException(string $name, string $stringifiedValue, ?string $reason = null) : BlockStateDeserializeException{
68 "Property \"$name\" has unexpected value \"$stringifiedValue\"" . (
69 $reason !== null ? " ($reason)" : ""
70 ));
71 }
72
74 public function readBool(string $name) : bool{
75 unset($this->unusedStates[$name]);
76 $tag = $this->data->getState($name);
77 if($tag instanceof ByteTag){
78 switch($tag->getValue()){
79 case 0: return false;
80 case 1: return true;
81 default: throw $this->badValueException($name, (string) $tag->getValue());
82 }
83 }
84 throw $this->missingOrWrongTypeException($name, $tag);
85 }
86
88 public function readInt(string $name) : int{
89 unset($this->unusedStates[$name]);
90 $tag = $this->data->getState($name);
91 if($tag instanceof IntTag){
92 return $tag->getValue();
93 }
94 throw $this->missingOrWrongTypeException($name, $tag);
95 }
96
98 public function readBoundedInt(string $name, int $min, int $max) : int{
99 $result = $this->readInt($name);
100 if($result < $min || $result > $max){
101 throw $this->badValueException($name, (string) $result, "Must be inside the range $min ... $max");
102 }
103 return $result;
104 }
105
107 public function readString(string $name) : string{
108 unset($this->unusedStates[$name]);
109 //TODO: only allow a specific set of values (strings are primarily used for enums)
110 $tag = $this->data->getState($name);
111 if($tag instanceof StringTag){
112 return $tag->getValue();
113 }
114 throw $this->missingOrWrongTypeException($name, $tag);
115 }
116
122 public function mapIntFromString(string $name, IntFromRawStateMap $map) : int{
123 $raw = $this->readString($name);
124
125 return $map->rawToValue($raw) ?? throw $this->badValueException($name, $raw);
126 }
127
133 public function mapIntFromInt(string $name, IntFromRawStateMap $map) : int{
134 $raw = $this->readInt($name);
135
136 return $map->rawToValue($raw) ?? throw $this->badValueException($name, (string) $raw);
137 }
138
143 public function readFacingDirection() : int{
144 return $this->mapIntFromInt(BlockStateNames::FACING_DIRECTION, ValueMappings::getInstance()->facing);
145 }
146
151 public function readBlockFace() : int{
152 return $this->mapIntFromString(BlockStateNames::MC_BLOCK_FACE, ValueMappings::getInstance()->blockFace);
153 }
154
160 public function readFacingFlags() : array{
161 $result = [];
162 $flags = $this->readBoundedInt(BlockStateNames::MULTI_FACE_DIRECTION_BITS, 0, 63);
163 foreach([
164 BlockLegacyMetadata::MULTI_FACE_DIRECTION_FLAG_DOWN => Facing::DOWN,
165 BlockLegacyMetadata::MULTI_FACE_DIRECTION_FLAG_UP => Facing::UP,
166 BlockLegacyMetadata::MULTI_FACE_DIRECTION_FLAG_NORTH => Facing::NORTH,
167 BlockLegacyMetadata::MULTI_FACE_DIRECTION_FLAG_SOUTH => Facing::SOUTH,
168 BlockLegacyMetadata::MULTI_FACE_DIRECTION_FLAG_WEST => Facing::WEST,
169 BlockLegacyMetadata::MULTI_FACE_DIRECTION_FLAG_EAST => Facing::EAST
170 ] as $flag => $facing){
171 if(($flags & $flag) !== 0){
172 $result[$facing] = $facing;
173 }
174 }
175
176 return $result;
177 }
178
183 public function readEndRodFacingDirection() : int{
184 $result = $this->readFacingDirection();
185 return Facing::axis($result) !== Axis::Y ? Facing::opposite($result) : $result;
186 }
187
192 public function readHorizontalFacing() : int{
193 return $this->mapIntFromInt(BlockStateNames::FACING_DIRECTION, ValueMappings::getInstance()->horizontalFacingClassic);
194 }
195
200 public function readWeirdoHorizontalFacing() : int{
201 return $this->mapIntFromInt(BlockStateNames::WEIRDO_DIRECTION, ValueMappings::getInstance()->horizontalFacing5Minus);
202 }
203
208 public function readLegacyHorizontalFacing() : int{
209 return $this->mapIntFromInt(BlockStateNames::DIRECTION, ValueMappings::getInstance()->horizontalFacingSWNE);
210 }
211
217 public function read5MinusHorizontalFacing() : int{
218 return $this->mapIntFromInt(BlockStateNames::DIRECTION, ValueMappings::getInstance()->horizontalFacing5Minus);
219 }
220
226 public function readCardinalHorizontalFacing() : int{
227 return $this->mapIntFromString(BlockStateNames::MC_CARDINAL_DIRECTION, ValueMappings::getInstance()->cardinalDirection);
228 }
229
234 public function readCoralFacing() : int{
235 return $this->mapIntFromInt(BlockStateNames::CORAL_DIRECTION, ValueMappings::getInstance()->horizontalFacingCoral);
236 }
237
242 public function readFacingWithoutDown() : int{
243 $result = $this->readFacingDirection();
244 if($result === Facing::DOWN){ //shouldn't be legal, but 1.13 allows it
245 $result = Facing::UP;
246 }
247 return $result;
248 }
249
254 public function readFacingWithoutUp() : int{
255 $result = $this->readFacingDirection();
256 if($result === Facing::UP){
257 $result = Facing::DOWN; //shouldn't be legal, but 1.13 allows it
258 }
259 return $result;
260 }
261
266 public function readPillarAxis() : int{
267 return $this->mapIntFromString(BlockStateNames::PILLAR_AXIS, ValueMappings::getInstance()->pillarAxis);
268 }
269
274 public function readSlabPosition() : SlabType{
275 return match($rawValue = $this->readString(BlockStateNames::MC_VERTICAL_HALF)){
276 StringValues::MC_VERTICAL_HALF_BOTTOM => SlabType::BOTTOM,
277 StringValues::MC_VERTICAL_HALF_TOP => SlabType::TOP,
278 default => throw $this->badValueException(BlockStateNames::MC_VERTICAL_HALF, $rawValue, "Invalid slab position"),
279 };
280 }
281
286 public function readTorchFacing() : int{
287 return $this->mapIntFromString(BlockStateNames::TORCH_FACING_DIRECTION, ValueMappings::getInstance()->torchFacing);
288 }
289
294 public function readBellAttachmentType() : BellAttachmentType{
295 return $this->readUnitEnum(BlockStateNames::ATTACHMENT, ValueMappings::getInstance()->bellAttachmentType);
296 }
297
302 public function readWallConnectionType(string $name) : ?WallConnectionType{
303 return match($type = $this->readString($name)){
304 //TODO: this looks a bit confusing due to use of EAST, but the values are the same for all connections
305 //we need to find a better way to auto-generate the constant names when they are reused
306 //for now, using these constants is better than nothing since it still gives static analysability
307 StringValues::WALL_CONNECTION_TYPE_EAST_NONE => null,
308 StringValues::WALL_CONNECTION_TYPE_EAST_SHORT => WallConnectionType::SHORT,
309 StringValues::WALL_CONNECTION_TYPE_EAST_TALL => WallConnectionType::TALL,
310 default => throw $this->badValueException($name, $type),
311 };
312 }
313
321 public function readUnitEnum(string $name, EnumFromRawStateMap $map) : \UnitEnum{
322 $value = $this->readString($name);
323
324 $mapped = $map->rawToValue($value);
325 if($mapped === null){
326 throw $this->badValueException($name, $value);
327 }
328 return $mapped;
329 }
330
334 public function ignored(string $name) : void{
335 if($this->data->getState($name) !== null){
336 unset($this->unusedStates[$name]);
337 }else{
338 throw $this->missingOrWrongTypeException($name, null);
339 }
340 }
341
345 public function todo(string $name) : void{
346 $this->ignored($name);
347 }
348
352 public function checkUnreadProperties() : void{
353 if(count($this->unusedStates) > 0){
354 throw new BlockStateDeserializeException("Unread properties: " . implode(", ", array_keys($this->unusedStates)));
355 }
356 }
357}
mapIntFromInt(string $name, IntFromRawStateMap $map)
readUnitEnum(string $name, EnumFromRawStateMap $map)
mapIntFromString(string $name, IntFromRawStateMap $map)