Nuke Block->meta, split into variant and state properties, lots of cleanup

This is a major change to the way block metadata is handled within the PM core. This separates variant metadata (which really ought to be part of the ID) from state metadata, and in a couple of cases flattens separate states of blocks together.

The result of this is that invalid variants can be much more easily detected, and additionally state handling is much cleaner since meta is only needed at the serialize layer instead of throughout the code.
This commit is contained in:
Dylan K. Taylor
2018-09-21 19:28:10 +01:00
parent a55ab54ddb
commit 56d9943b0d
178 changed files with 2012 additions and 2120 deletions

View File

@ -28,6 +28,7 @@ use pocketmine\item\Item;
use pocketmine\item\TieredTool;
use pocketmine\math\AxisAlignedBB;
use pocketmine\math\Bearing;
use pocketmine\math\Facing;
use pocketmine\math\Vector3;
use pocketmine\Player;
@ -37,10 +38,19 @@ class Anvil extends Fallable{
public const TYPE_SLIGHTLY_DAMAGED = 4;
public const TYPE_VERY_DAMAGED = 8;
protected $id = self::ANVIL;
/** @var int */
protected $facing = Facing::NORTH;
public function __construct(int $meta = 0){
$this->setDamage($meta);
protected function writeStateToMeta() : int{
return Bearing::fromFacing($this->facing);
}
public function readStateFromMeta(int $meta) : void{
$this->facing = Bearing::toFacing($meta);
}
public function getStateBitmask() : int{
return 0b11;
}
public function isTransparent() : bool{
@ -55,19 +65,6 @@ class Anvil extends Fallable{
return 6000;
}
public function getVariantBitmask() : int{
return 0x0c;
}
public function getName() : string{
static $names = [
self::TYPE_NORMAL => "Anvil",
self::TYPE_SLIGHTLY_DAMAGED => "Slightly Damaged Anvil",
self::TYPE_VERY_DAMAGED => "Very Damaged Anvil"
];
return $names[$this->getVariant()] ?? "Anvil";
}
public function getToolType() : int{
return BlockToolType::TYPE_PICKAXE;
}
@ -79,7 +76,7 @@ class Anvil extends Fallable{
public function recalculateBoundingBox() : ?AxisAlignedBB{
$inset = 0.125;
if($this->meta & 0x01){ //east/west
if(Facing::axis($this->facing) === Facing::AXIS_X){
return new AxisAlignedBB(0, 0, $inset, 1, 1, 1 - $inset);
}else{
return new AxisAlignedBB($inset, 0, 0, 1 - $inset, 1, 1);
@ -95,8 +92,9 @@ class Anvil extends Fallable{
}
public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{
$direction = $player !== null ? Bearing::rotate($player->getDirection(), 1) : 0;
$this->meta = $this->getVariant() | $direction;
if($player !== null){
$this->facing = Bearing::toFacing(Bearing::rotate($player->getDirection(), 1));
}
return parent::place($item, $blockReplace, $blockClicked, $face, $clickVector, $player);
}
}