Skip to content

Commit

Permalink
StructureVoid: add serialization and block state
Browse files Browse the repository at this point in the history
  • Loading branch information
Bqleine committed Sep 9, 2023
1 parent f7452e4 commit 4dea6e6
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/block/StructureVoid.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,18 @@

namespace pocketmine\block;

use pocketmine\math\AxisAlignedBB;
use pocketmine\data\runtime\RuntimeDataDescriber;
use pocketmine\block\utils\StructureVoidType;

class StructureVoid extends Transparent{
private StructureVoidType $type;

public function __construct(BlockIdentifier $idInfo, string $name, BlockTypeInfo $typeInfo){
$this->type = StructureVoidType::VOID;
parent::__construct($idInfo, $name, $typeInfo);
}

/**
* @return AxisAlignedBB[]
*/
Expand All @@ -34,4 +45,18 @@ protected function recalculateCollisionBoxes() : array{
public function isSolid() : bool{
return false;
}

public function describeBlockItemState(RuntimeDataDescriber $w) : void{
$w->enum($this->type);
}

public function getType() : StructureVoidType{
return $this->type;
}

/** @return $this */
public function setType(StructureVoidType $type) : self{
$this->type = $type;
return $this;
}
}
29 changes: 29 additions & 0 deletions src/block/utils/StructureVoidType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?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\block\utils;

enum StructureVoidType{
case VOID;
case AIR;
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
use pocketmine\block\StoneButton;
use pocketmine\block\Stonecutter;
use pocketmine\block\StonePressurePlate;
use pocketmine\block\StructureVoid;
use pocketmine\block\Sugarcane;
use pocketmine\block\SweetBerryBush;
use pocketmine\block\TNT;
Expand Down Expand Up @@ -1543,6 +1544,10 @@ private function registerSerializers() : void{
return Writer::create(Ids::REEDS)
->writeInt(StateNames::AGE, $block->getAge());
});
$this->map(Blocks::STRUCTURE_VOID(), function(StructureVoid $block) : Writer{
return Writer::create(Ids::STRUCTURE_VOID)
->writeStructureVoidType($block->getType());
});
$this->map(Blocks::SUNFLOWER(), fn(DoublePlant $block) => Helper::encodeDoublePlant($block, StringValues::DOUBLE_PLANT_TYPE_SUNFLOWER, Writer::create(Ids::DOUBLE_PLANT)));
$this->map(Blocks::SWEET_BERRY_BUSH(), function(SweetBerryBush $block) : Writer{
return Writer::create(Ids::SWEET_BERRY_BUSH)
Expand Down
10 changes: 10 additions & 0 deletions src/data/bedrock/block/convert/BlockStateReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use pocketmine\block\utils\CoralType;
use pocketmine\block\utils\DyeColor;
use pocketmine\block\utils\SlabType;
use pocketmine\block\utils\StructureVoidType;
use pocketmine\block\utils\WallConnectionType;
use pocketmine\data\bedrock\block\BlockLegacyMetadata;
use pocketmine\data\bedrock\block\BlockStateData;
Expand Down Expand Up @@ -349,6 +350,15 @@ public function readWallConnectionType(string $name) : ?WallConnectionType{
};
}

/** @throws BlockStateDeserializeException */
public function readStructureVoidType() : ?StructureVoidType{
return match($type = $this->readInt(BlockStateNames::STRUCTURE_VOID_TYPE)){
0 => StructureVoidType::VOID,
1 => StructureVoidType::AIR,
default => throw $this->badValueException(BlockStateNames::STRUCTURE_VOID_TYPE, strval($type)),
};
}

/**
* Explicitly mark a property as unused, so it doesn't get flagged as an error when debug mode is enabled
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1438,6 +1438,10 @@ private function registerDeserializers() : void{
return Blocks::STONECUTTER()
->setFacing($in->readHorizontalFacing());
});
$this->map(Ids::STRUCTURE_VOID, function(Reader $in) : Block{
return Blocks::STRUCTURE_VOID()
->setType($in->readStructureVoidType());

Check failure on line 1443 in src/data/bedrock/block/convert/BlockStateToObjectDeserializer.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis (ubuntu-20.04, 8.1)

Parameter #1 $type of method pocketmine\block\StructureVoid::setType() expects pocketmine\block\utils\StructureVoidType, pocketmine\block\utils\StructureVoidType|null given.

Check failure on line 1443 in src/data/bedrock/block/convert/BlockStateToObjectDeserializer.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis (ubuntu-20.04, 8.2)

Parameter #1 $type of method pocketmine\block\StructureVoid::setType() expects pocketmine\block\utils\StructureVoidType, pocketmine\block\utils\StructureVoidType|null given.
});
$this->map(Ids::SWEET_BERRY_BUSH, function(Reader $in) : Block{
//berry bush only wants 0-3, but it can be bigger in MCPE due to misuse of GROWTH state which goes up to 7
$growth = $in->readBoundedInt(StateNames::GROWTH, 0, 7);
Expand Down
10 changes: 10 additions & 0 deletions src/data/bedrock/block/convert/BlockStateWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use pocketmine\block\utils\CoralType;
use pocketmine\block\utils\DyeColor;
use pocketmine\block\utils\SlabType;
use pocketmine\block\utils\StructureVoidType;
use pocketmine\block\utils\WallConnectionType;
use pocketmine\block\utils\WoodType;
use pocketmine\data\bedrock\block\BlockLegacyMetadata;
Expand Down Expand Up @@ -314,6 +315,15 @@ public function writeWallConnectionType(string $name, ?WallConnectionType $wallC
return $this;
}

/** @return $this */
public function writeStructureVoidType(StructureVoidType $structureVoidType) : self{
$this->writeInt(BlockStateNames::STRUCTURE_VOID_TYPE, match($structureVoidType){
StructureVoidType::VOID => 0,
StructureVoidType::AIR => 0,
});
return $this;
}

public function getBlockStateData() : BlockStateData{
return BlockStateData::current($this->id, $this->states);
}
Expand Down

0 comments on commit 4dea6e6

Please sign in to comment.