22declare(strict_types=1);
24namespace pocketmine\data\bedrock\block;
31use
function array_keys;
48 public const TAG_NAME =
"name";
49 public const TAG_STATES =
"states";
50 public const TAG_VERSION =
"version";
58 private array $states,
66 public static function current(
string $name, array $states) : self{
70 public function getName() : string{ return $this->name; }
76 public function getStates() : array{ return $this->states; }
78 public function getState(
string $name) : ?
Tag{
79 return $this->states[$name] ?? null;
82 public function getVersion() : int{ return $this->version; }
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";
97 $name = $nbt->getString(self::TAG_NAME);
99 $version = $nbt->getInt(self::TAG_VERSION, 0);
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)));
111 return new self($name, $states->getValue(), $version);
119 foreach(Utils::stringifyKeys($this->states) as $key => $value){
120 $statesTag->
setTag($key, $value);
122 return CompoundTag::create()
123 ->setString(self::TAG_NAME, $this->name)
124 ->setInt(self::TAG_VERSION, $this->version)
125 ->setTag(self::TAG_STATES, $statesTag);
133 return $this->toVanillaNbt()
137 public function equals(
self $that) : bool{
138 if($this->name !== $that->name || count($this->states) !== count($that->states)){
141 foreach(Utils::stringifyKeys($this->states) as $k => $v){
142 if(!isset($that->states[$k]) || !$that->states[$k]->equals($v)){
__construct(private string $name, private array $states, private int $version)
static fromNbt(CompoundTag $nbt)
static current(string $name, array $states)
getCompoundTag(string $name)
setTag(string $name, Tag $tag)