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