mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-05-17 11:18:52 +00:00
Add a wrapper type for blockstate NBT
This commit is contained in:
parent
4d935aa8b6
commit
cab9b6c862
88
src/data/bedrock/blockstate/BlockStateData.php
Normal file
88
src/data/bedrock/blockstate/BlockStateData.php
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
* ____ _ _ __ __ _ __ __ ____
|
||||||
|
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||||
|
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||||
|
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||||
|
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* @author PocketMine Team
|
||||||
|
* @link http://www.pocketmine.net/
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace pocketmine\data\bedrock\blockstate;
|
||||||
|
|
||||||
|
use pocketmine\nbt\NbtException;
|
||||||
|
use pocketmine\nbt\tag\CompoundTag;
|
||||||
|
use function array_keys;
|
||||||
|
use function count;
|
||||||
|
use function implode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Contains the common information found in a serialized blockstate.
|
||||||
|
*/
|
||||||
|
final class BlockStateData{
|
||||||
|
/**
|
||||||
|
* Bedrock version of the most recent backwards-incompatible change to blockstates.
|
||||||
|
*/
|
||||||
|
public const CURRENT_VERSION =
|
||||||
|
(1 << 24) | //major
|
||||||
|
(16 << 16) | //minor
|
||||||
|
(210 << 8) | //patch
|
||||||
|
(3); //revision
|
||||||
|
|
||||||
|
public const TAG_NAME = "name";
|
||||||
|
public const TAG_STATES = "states";
|
||||||
|
public const TAG_VERSION = "version";
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
private string $name,
|
||||||
|
private CompoundTag $states,
|
||||||
|
private int $version
|
||||||
|
){}
|
||||||
|
|
||||||
|
public function getName() : string{ return $this->name; }
|
||||||
|
|
||||||
|
public function getStates() : CompoundTag{ return $this->states; }
|
||||||
|
|
||||||
|
public function getVersion() : int{ return $this->version; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws BlockStateDeserializeException
|
||||||
|
*/
|
||||||
|
public static function fromNbt(CompoundTag $nbt) : self{
|
||||||
|
try{
|
||||||
|
$name = $nbt->getString(self::TAG_NAME);
|
||||||
|
$states = $nbt->getCompoundTag(self::TAG_STATES) ?? throw new BlockStateDeserializeException("Missing tag \"" . self::TAG_STATES . "\"");
|
||||||
|
$version = $nbt->getInt(self::TAG_VERSION, 0);
|
||||||
|
}catch(NbtException $e){
|
||||||
|
throw new BlockStateDeserializeException($e->getMessage(), 0, $e);
|
||||||
|
}
|
||||||
|
|
||||||
|
$allKeys = $nbt->getValue();
|
||||||
|
unset($allKeys[self::TAG_NAME], $allKeys[self::TAG_STATES], $allKeys[self::TAG_VERSION]);
|
||||||
|
if(count($allKeys) !== 0){
|
||||||
|
throw new BlockStateDeserializeException("Unexpected extra keys: " . implode(", ", array_keys($allKeys)));
|
||||||
|
}
|
||||||
|
|
||||||
|
return new self($name, $states, $version);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function toNbt() : CompoundTag{
|
||||||
|
return CompoundTag::create()
|
||||||
|
->setString(self::TAG_NAME, $this->name)
|
||||||
|
->setInt(self::TAG_VERSION, $this->version)
|
||||||
|
->setTag(self::TAG_STATES, $this->states);
|
||||||
|
}
|
||||||
|
}
|
@ -37,7 +37,6 @@ use pocketmine\data\bedrock\blockstate\BlockStateStringValues as StringValues;
|
|||||||
use pocketmine\data\bedrock\blockstate\BlockTypeNames as Ids;
|
use pocketmine\data\bedrock\blockstate\BlockTypeNames as Ids;
|
||||||
use pocketmine\math\Axis;
|
use pocketmine\math\Axis;
|
||||||
use pocketmine\math\Facing;
|
use pocketmine\math\Facing;
|
||||||
use pocketmine\nbt\NbtException;
|
|
||||||
use pocketmine\nbt\tag\CompoundTag;
|
use pocketmine\nbt\tag\CompoundTag;
|
||||||
use function array_key_exists;
|
use function array_key_exists;
|
||||||
use function min;
|
use function min;
|
||||||
@ -2501,20 +2500,12 @@ final class BlockStateDeserializer{
|
|||||||
|
|
||||||
/** @throws BlockStateDeserializeException */
|
/** @throws BlockStateDeserializeException */
|
||||||
public function deserialize(CompoundTag $blockState) : Block{
|
public function deserialize(CompoundTag $blockState) : Block{
|
||||||
try{
|
$blockStateData = BlockStateData::fromNbt($blockState);
|
||||||
$id = $blockState->getString("name");
|
|
||||||
$states = $blockState->getCompoundTag("states");
|
|
||||||
}catch(NbtException $e){
|
|
||||||
throw new BlockStateDeserializeException("Error reading blockstate NBT: " . $e->getMessage(), 0, $e);
|
|
||||||
}
|
|
||||||
|
|
||||||
if($states === null){
|
|
||||||
throw new BlockStateDeserializeException("\"states\" tag must always be present, even if it has no data");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
$id = $blockStateData->getName();
|
||||||
if(!array_key_exists($id, $this->deserializeFuncs)){
|
if(!array_key_exists($id, $this->deserializeFuncs)){
|
||||||
throw new BlockStateDeserializeException("Unknown block ID \"$id\"");
|
throw new BlockStateDeserializeException("Unknown block ID \"$id\"");
|
||||||
}
|
}
|
||||||
return $this->deserializeFuncs[$id](new BlockStateReader($states));
|
return $this->deserializeFuncs[$id](new BlockStateReader($blockStateData));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -31,7 +31,6 @@ use pocketmine\data\bedrock\blockstate\BlockStateStringValues as StringValues;
|
|||||||
use pocketmine\math\Axis;
|
use pocketmine\math\Axis;
|
||||||
use pocketmine\math\Facing;
|
use pocketmine\math\Facing;
|
||||||
use pocketmine\nbt\tag\ByteTag;
|
use pocketmine\nbt\tag\ByteTag;
|
||||||
use pocketmine\nbt\tag\CompoundTag;
|
|
||||||
use pocketmine\nbt\tag\IntTag;
|
use pocketmine\nbt\tag\IntTag;
|
||||||
use pocketmine\nbt\tag\StringTag;
|
use pocketmine\nbt\tag\StringTag;
|
||||||
use pocketmine\nbt\tag\Tag;
|
use pocketmine\nbt\tag\Tag;
|
||||||
@ -39,13 +38,9 @@ use function get_class;
|
|||||||
|
|
||||||
final class BlockStateReader{
|
final class BlockStateReader{
|
||||||
|
|
||||||
private CompoundTag $nbt;
|
public function __construct(
|
||||||
|
private BlockStateData $data
|
||||||
public function __construct(CompoundTag $nbt){
|
){}
|
||||||
$this->nbt = $nbt;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getNbt() : CompoundTag{ return $this->nbt; }
|
|
||||||
|
|
||||||
public function missingOrWrongTypeException(string $name, ?Tag $tag) : BlockStateDeserializeException{
|
public function missingOrWrongTypeException(string $name, ?Tag $tag) : BlockStateDeserializeException{
|
||||||
return new BlockStateDeserializeException("Property \"$name\" " . ($tag !== null ? "has unexpected type " . get_class($tag) : "is missing"));
|
return new BlockStateDeserializeException("Property \"$name\" " . ($tag !== null ? "has unexpected type " . get_class($tag) : "is missing"));
|
||||||
@ -60,7 +55,7 @@ final class BlockStateReader{
|
|||||||
|
|
||||||
/** @throws BlockStateDeserializeException */
|
/** @throws BlockStateDeserializeException */
|
||||||
public function readBool(string $name) : bool{
|
public function readBool(string $name) : bool{
|
||||||
$tag = $this->nbt->getTag($name);
|
$tag = $this->data->getStates()->getTag($name);
|
||||||
if($tag instanceof ByteTag){
|
if($tag instanceof ByteTag){
|
||||||
switch($tag->getValue()){
|
switch($tag->getValue()){
|
||||||
case 0: return false;
|
case 0: return false;
|
||||||
@ -73,7 +68,7 @@ final class BlockStateReader{
|
|||||||
|
|
||||||
/** @throws BlockStateDeserializeException */
|
/** @throws BlockStateDeserializeException */
|
||||||
public function readInt(string $name) : int{
|
public function readInt(string $name) : int{
|
||||||
$tag = $this->nbt->getTag($name);
|
$tag = $this->data->getStates()->getTag($name);
|
||||||
if($tag instanceof IntTag){
|
if($tag instanceof IntTag){
|
||||||
return $tag->getValue();
|
return $tag->getValue();
|
||||||
}
|
}
|
||||||
@ -92,7 +87,7 @@ final class BlockStateReader{
|
|||||||
/** @throws BlockStateDeserializeException */
|
/** @throws BlockStateDeserializeException */
|
||||||
public function readString(string $name) : string{
|
public function readString(string $name) : string{
|
||||||
//TODO: only allow a specific set of values (strings are primarily used for enums)
|
//TODO: only allow a specific set of values (strings are primarily used for enums)
|
||||||
$tag = $this->nbt->getTag($name);
|
$tag = $this->data->getStates()->getTag($name);
|
||||||
if($tag instanceof StringTag){
|
if($tag instanceof StringTag){
|
||||||
return $tag->getValue();
|
return $tag->getValue();
|
||||||
}
|
}
|
||||||
|
@ -143,7 +143,6 @@ use pocketmine\data\bedrock\blockstate\BlockStateWriter as Writer;
|
|||||||
use pocketmine\data\bedrock\blockstate\BlockTypeNames as Ids;
|
use pocketmine\data\bedrock\blockstate\BlockTypeNames as Ids;
|
||||||
use pocketmine\math\Axis;
|
use pocketmine\math\Axis;
|
||||||
use pocketmine\math\Facing;
|
use pocketmine\math\Facing;
|
||||||
use pocketmine\nbt\tag\CompoundTag;
|
|
||||||
use pocketmine\utils\AssumptionFailedError;
|
use pocketmine\utils\AssumptionFailedError;
|
||||||
use function class_parents;
|
use function class_parents;
|
||||||
use function get_class;
|
use function get_class;
|
||||||
@ -175,7 +174,7 @@ final class BlockStateSerializer{
|
|||||||
* @phpstan-template TBlockType of Block
|
* @phpstan-template TBlockType of Block
|
||||||
* @phpstan-param TBlockType $blockState
|
* @phpstan-param TBlockType $blockState
|
||||||
*/
|
*/
|
||||||
public function serialize(Block $blockState) : CompoundTag{
|
public function serialize(Block $blockState) : BlockStateData{
|
||||||
$typeId = $blockState->getTypeId();
|
$typeId = $blockState->getTypeId();
|
||||||
|
|
||||||
$locatedSerializer = $this->serializers[$typeId][get_class($blockState)] ?? null;
|
$locatedSerializer = $this->serializers[$typeId][get_class($blockState)] ?? null;
|
||||||
@ -204,7 +203,7 @@ final class BlockStateSerializer{
|
|||||||
|
|
||||||
/** @var Writer $writer */
|
/** @var Writer $writer */
|
||||||
$writer = $serializer($blockState);
|
$writer = $serializer($blockState);
|
||||||
return $writer->writeBlockStateNbt();
|
return $writer->getBlockStateData();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __construct(){
|
public function __construct(){
|
||||||
|
@ -246,10 +246,7 @@ final class BlockStateWriter{
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function writeBlockStateNbt() : CompoundTag{
|
public function getBlockStateData() : BlockStateData{
|
||||||
//TODO: add `version` field
|
return new BlockStateData($this->id, $this->states, BlockStateData::CURRENT_VERSION);
|
||||||
return CompoundTag::create()
|
|
||||||
->setString("name", $this->id)
|
|
||||||
->setTag("states", $this->states);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user