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
22
declare(strict_types=1);
23
24
namespace
pocketmine\data\bedrock\block\convert;
25
26
use
pocketmine\data\bedrock\block\BlockStateData
;
27
use
pocketmine\data\bedrock\block\BlockStateDeserializeException
;
28
use
pocketmine\nbt\tag\ByteTag
;
29
use
pocketmine\nbt\tag\IntTag
;
30
use
pocketmine\nbt\tag\StringTag
;
31
use
pocketmine\nbt\tag\Tag
;
32
use
function
array_keys;
33
use
function
count;
34
use
function
get_class;
35
use
function
implode;
36
37
final
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
{
56
return
new
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
}
pocketmine\data\bedrock\block\BlockStateData
Definition
BlockStateData.php:39
pocketmine\data\bedrock\block\BlockStateDeserializeException
Definition
BlockStateDeserializeException.php:26
pocketmine\data\bedrock\block\convert\BlockStateReader
Definition
BlockStateReader.php:37
pocketmine\data\bedrock\block\convert\BlockStateReader\readString
readString(string $name)
Definition
BlockStateReader.php:96
pocketmine\data\bedrock\block\convert\BlockStateReader\readInt
readInt(string $name)
Definition
BlockStateReader.php:77
pocketmine\data\bedrock\block\convert\BlockStateReader\ignored
ignored(string $name)
Definition
BlockStateReader.php:109
pocketmine\data\bedrock\block\convert\BlockStateReader\checkUnreadProperties
checkUnreadProperties()
Definition
BlockStateReader.php:127
pocketmine\data\bedrock\block\convert\BlockStateReader\readBool
readBool(string $name)
Definition
BlockStateReader.php:63
pocketmine\data\bedrock\block\convert\BlockStateReader\readBoundedInt
readBoundedInt(string $name, int $min, int $max)
Definition
BlockStateReader.php:87
pocketmine\data\bedrock\block\convert\BlockStateReader\todo
todo(string $name)
Definition
BlockStateReader.php:120
pocketmine\nbt\tag\ByteTag
Definition
ByteTag.php:30
pocketmine\nbt\tag\IntTag
Definition
IntTag.php:30
pocketmine\nbt\tag\StringTag
Definition
StringTag.php:33
pocketmine\nbt\tag\Tag
Definition
Tag.php:28
pocketmine\nbt\tag\Tag\getValue
getValue()
src
data
bedrock
block
convert
BlockStateReader.php
Generated by
1.12.0