PocketMine-MP 5.33.2 git-1133d49c924b4358c79d44eeb97dcbf56cb4d1eb
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
32use function array_keys;
33use function count;
34use function get_class;
35use function implode;
36
37final class BlockStateReader{
38
43 private array $unusedStates;
44
45 public function __construct(
46 private BlockStateData $data
47 ){
48 $this->unusedStates = $this->data->getStates();
49 }
50
51 public function missingOrWrongTypeException(string $name, ?Tag $tag) : BlockStateDeserializeException{
52 return new BlockStateDeserializeException("Property \"$name\" " . ($tag !== null ? "has unexpected type " . get_class($tag) : "is missing"));
53 }
54
55 public function badValueException(string $name, string $stringifiedValue, ?string $reason = null) : BlockStateDeserializeException{
57 "Property \"$name\" has unexpected value \"$stringifiedValue\"" . (
58 $reason !== null ? " ($reason)" : ""
59 ));
60 }
61
63 public function readBool(string $name) : bool{
64 unset($this->unusedStates[$name]);
65 $tag = $this->data->getState($name);
66 if($tag instanceof ByteTag){
67 switch($tag->getValue()){
68 case 0: return false;
69 case 1: return true;
70 default: throw $this->badValueException($name, (string) $tag->getValue());
71 }
72 }
73 throw $this->missingOrWrongTypeException($name, $tag);
74 }
75
77 public function readInt(string $name) : int{
78 unset($this->unusedStates[$name]);
79 $tag = $this->data->getState($name);
80 if($tag instanceof IntTag){
81 return $tag->getValue();
82 }
83 throw $this->missingOrWrongTypeException($name, $tag);
84 }
85
87 public function readBoundedInt(string $name, int $min, int $max) : int{
88 $result = $this->readInt($name);
89 if($result < $min || $result > $max){
90 throw $this->badValueException($name, (string) $result, "Must be inside the range $min ... $max");
91 }
92 return $result;
93 }
94
96 public function readString(string $name) : string{
97 unset($this->unusedStates[$name]);
98 //TODO: only allow a specific set of values (strings are primarily used for enums)
99 $tag = $this->data->getState($name);
100 if($tag instanceof StringTag){
101 return $tag->getValue();
102 }
103 throw $this->missingOrWrongTypeException($name, $tag);
104 }
105
109 public function ignored(string $name) : void{
110 if($this->data->getState($name) !== null){
111 unset($this->unusedStates[$name]);
112 }else{
113 throw $this->missingOrWrongTypeException($name, null);
114 }
115 }
116
120 public function todo(string $name) : void{
121 $this->ignored($name);
122 }
123
127 public function checkUnreadProperties() : void{
128 if(count($this->unusedStates) > 0){
129 throw new BlockStateDeserializeException("Unread properties: " . implode(", ", array_keys($this->unusedStates)));
130 }
131 }
132}