PocketMine-MP 5.15.1 git-ed158f8a1b0cfe334ac5f45febc0f633602014f2
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
31use function array_keys;
32use function count;
33use function implode;
34
38final class BlockStateData{
42 public const CURRENT_VERSION =
43 (1 << 24) | //major
44 (20 << 16) | //minor
45 (80 << 8) | //patch
46 (3); //revision
47
48 public const TAG_NAME = "name";
49 public const TAG_STATES = "states";
50 public const TAG_VERSION = "version";
51
56 public function __construct(
57 private string $name,
58 private array $states,
59 private int $version
60 ){}
61
66 public static function current(string $name, array $states) : self{
67 return new self($name, $states, self::CURRENT_VERSION);
68 }
69
70 public function getName() : string{ return $this->name; }
71
76 public function getStates() : array{ return $this->states; }
77
78 public function getState(string $name) : ?Tag{
79 return $this->states[$name] ?? null;
80 }
81
82 public function getVersion() : int{ return $this->version; }
83
84 public function getVersionAsString() : string{
85 $major = ($this->version >> 24) & 0xff;
86 $minor = ($this->version >> 16) & 0xff;
87 $patch = ($this->version >> 8) & 0xff;
88 $revision = $this->version & 0xff;
89 return "$major.$minor.$patch.$revision";
90 }
91
95 public static function fromNbt(CompoundTag $nbt) : self{
96 try{
97 $name = $nbt->getString(self::TAG_NAME);
98 $states = $nbt->getCompoundTag(self::TAG_STATES) ?? throw new BlockStateDeserializeException("Missing tag \"" . self::TAG_STATES . "\"");
99 $version = $nbt->getInt(self::TAG_VERSION, 0);
100 //TODO: read version from VersionInfo::TAG_WORLD_DATA_VERSION - we may need it to fix up old blockstates
101 }catch(NbtException $e){
102 throw new BlockStateDeserializeException($e->getMessage(), 0, $e);
103 }
104
105 $allKeys = $nbt->getValue();
106 unset($allKeys[self::TAG_NAME], $allKeys[self::TAG_STATES], $allKeys[self::TAG_VERSION], $allKeys[VersionInfo::TAG_WORLD_DATA_VERSION]);
107 if(count($allKeys) !== 0){
108 throw new BlockStateDeserializeException("Unexpected extra keys: " . implode(", ", array_keys($allKeys)));
109 }
110
111 return new self($name, $states->getValue(), $version);
112 }
113
117 public function toVanillaNbt() : CompoundTag{
118 $statesTag = CompoundTag::create();
119 foreach(Utils::stringifyKeys($this->states) as $key => $value){
120 $statesTag->setTag($key, $value);
121 }
122 return CompoundTag::create()
123 ->setString(self::TAG_NAME, $this->name)
124 ->setInt(self::TAG_VERSION, $this->version)
125 ->setTag(self::TAG_STATES, $statesTag);
126 }
127
132 public function toNbt() : CompoundTag{
133 return $this->toVanillaNbt()
134 ->setLong(VersionInfo::TAG_WORLD_DATA_VERSION, VersionInfo::WORLD_DATA_VERSION);
135 }
136
137 public function equals(self $that) : bool{
138 if($this->name !== $that->name || count($this->states) !== count($that->states)){
139 return false;
140 }
141 foreach(Utils::stringifyKeys($this->states) as $k => $v){
142 if(!isset($that->states[$k]) || !$that->states[$k]->equals($v)){
143 return false;
144 }
145 }
146
147 return true;
148 }
149}
__construct(private string $name, private array $states, private int $version)
static current(string $name, array $states)
setTag(string $name, Tag $tag)