PocketMine-MP 5.28.3 git-d5a1007c80fcee27feb2251cf5dcf1ad5a59a85c
Loading...
Searching...
No Matches
BlockStateData.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;
25
32use function array_keys;
33use function count;
34use function implode;
35
39final class BlockStateData{
40 public const CURRENT_VERSION = WorldDataVersions::BLOCK_STATES;
41
42 public const TAG_NAME = "name";
43 public const TAG_STATES = "states";
44 public const TAG_VERSION = "version";
45
50 public function __construct(
51 private string $name,
52 private array $states,
53 private int $version
54 ){}
55
60 public static function current(string $name, array $states) : self{
61 return new self($name, $states, self::CURRENT_VERSION);
62 }
63
64 public function getName() : string{ return $this->name; }
65
70 public function getStates() : array{ return $this->states; }
71
72 public function getState(string $name) : ?Tag{
73 return $this->states[$name] ?? null;
74 }
75
76 public function getVersion() : int{ return $this->version; }
77
78 public function getVersionAsString() : string{
79 $major = ($this->version >> 24) & 0xff;
80 $minor = ($this->version >> 16) & 0xff;
81 $patch = ($this->version >> 8) & 0xff;
82 $revision = $this->version & 0xff;
83 return "$major.$minor.$patch.$revision";
84 }
85
89 public static function fromNbt(CompoundTag $nbt) : self{
90 try{
91 $name = $nbt->getString(self::TAG_NAME);
92 $states = $nbt->getCompoundTag(self::TAG_STATES) ?? throw new BlockStateDeserializeException("Missing tag \"" . self::TAG_STATES . "\"");
93 $version = $nbt->getInt(self::TAG_VERSION, 0);
94 //TODO: read version from VersionInfo::TAG_WORLD_DATA_VERSION - we may need it to fix up old blockstates
95 }catch(NbtException $e){
96 throw new BlockStateDeserializeException($e->getMessage(), 0, $e);
97 }
98
99 $allKeys = $nbt->getValue();
100 unset($allKeys[self::TAG_NAME], $allKeys[self::TAG_STATES], $allKeys[self::TAG_VERSION], $allKeys[VersionInfo::TAG_WORLD_DATA_VERSION]);
101 if(count($allKeys) !== 0){
102 throw new BlockStateDeserializeException("Unexpected extra keys: " . implode(", ", array_keys($allKeys)));
103 }
104
105 return new self($name, $states->getValue(), $version);
106 }
107
111 public function toVanillaNbt() : CompoundTag{
112 $statesTag = CompoundTag::create();
113 foreach(Utils::stringifyKeys($this->states) as $key => $value){
114 $statesTag->setTag($key, $value);
115 }
116 return CompoundTag::create()
117 ->setString(self::TAG_NAME, $this->name)
118 ->setInt(self::TAG_VERSION, $this->version)
119 ->setTag(self::TAG_STATES, $statesTag);
120 }
121
126 public function toNbt() : CompoundTag{
127 return $this->toVanillaNbt()
128 ->setLong(VersionInfo::TAG_WORLD_DATA_VERSION, VersionInfo::WORLD_DATA_VERSION);
129 }
130
131 public function equals(self $that) : bool{
132 if($this->name !== $that->name || count($this->states) !== count($that->states)){
133 return false;
134 }
135 foreach(Utils::stringifyKeys($this->states) as $k => $v){
136 if(!isset($that->states[$k]) || !$that->states[$k]->equals($v)){
137 return false;
138 }
139 }
140
141 return true;
142 }
143}
__construct(private string $name, private array $states, private int $version)
static current(string $name, array $states)
setString(string $name, string $value)