Separated metadata handling from PillarRotationTrait

This commit is contained in:
Dylan K. Taylor 2020-11-14 16:21:50 +00:00
parent 7469f28f24
commit 06efad94b6
6 changed files with 86 additions and 56 deletions

View File

@ -46,7 +46,7 @@ use pocketmine\block\tile\Note as TileNote;
use pocketmine\block\tile\Skull as TileSkull; use pocketmine\block\tile\Skull as TileSkull;
use pocketmine\block\utils\DyeColor; use pocketmine\block\utils\DyeColor;
use pocketmine\block\utils\InvalidBlockStateException; use pocketmine\block\utils\InvalidBlockStateException;
use pocketmine\block\utils\PillarRotationTrait; use pocketmine\block\utils\PillarRotationInMetadataTrait;
use pocketmine\block\utils\TreeType; use pocketmine\block\utils\TreeType;
use pocketmine\data\bedrock\DyeColorIdMap; use pocketmine\data\bedrock\DyeColorIdMap;
use pocketmine\item\Item; use pocketmine\item\Item;
@ -293,7 +293,7 @@ class BlockFactory{
$purpurBreakInfo = new BlockBreakInfo(1.5, BlockToolType::PICKAXE, ToolTier::WOOD()->getHarvestLevel(), 30.0); $purpurBreakInfo = new BlockBreakInfo(1.5, BlockToolType::PICKAXE, ToolTier::WOOD()->getHarvestLevel(), 30.0);
$this->register(new Opaque(new BID(Ids::PURPUR_BLOCK, Meta::PURPUR_NORMAL), "Purpur Block", $purpurBreakInfo)); $this->register(new Opaque(new BID(Ids::PURPUR_BLOCK, Meta::PURPUR_NORMAL), "Purpur Block", $purpurBreakInfo));
$this->register(new class(new BID(Ids::PURPUR_BLOCK, Meta::PURPUR_PILLAR), "Purpur Pillar", $purpurBreakInfo) extends Opaque{ $this->register(new class(new BID(Ids::PURPUR_BLOCK, Meta::PURPUR_PILLAR), "Purpur Pillar", $purpurBreakInfo) extends Opaque{
use PillarRotationTrait; use PillarRotationInMetadataTrait;
}); });
$this->register(new Stair(new BID(Ids::PURPUR_STAIRS), "Purpur Stairs", $purpurBreakInfo)); $this->register(new Stair(new BID(Ids::PURPUR_STAIRS), "Purpur Stairs", $purpurBreakInfo));
@ -301,10 +301,10 @@ class BlockFactory{
$this->register(new Opaque(new BID(Ids::QUARTZ_BLOCK, Meta::QUARTZ_NORMAL), "Quartz Block", $quartzBreakInfo)); $this->register(new Opaque(new BID(Ids::QUARTZ_BLOCK, Meta::QUARTZ_NORMAL), "Quartz Block", $quartzBreakInfo));
$this->register(new Stair(new BID(Ids::QUARTZ_STAIRS), "Quartz Stairs", $quartzBreakInfo)); $this->register(new Stair(new BID(Ids::QUARTZ_STAIRS), "Quartz Stairs", $quartzBreakInfo));
$this->register(new class(new BID(Ids::QUARTZ_BLOCK, Meta::QUARTZ_CHISELED), "Chiseled Quartz Block", $quartzBreakInfo) extends Opaque{ $this->register(new class(new BID(Ids::QUARTZ_BLOCK, Meta::QUARTZ_CHISELED), "Chiseled Quartz Block", $quartzBreakInfo) extends Opaque{
use PillarRotationTrait; use PillarRotationInMetadataTrait;
}); });
$this->register(new class(new BID(Ids::QUARTZ_BLOCK, Meta::QUARTZ_PILLAR), "Quartz Pillar", $quartzBreakInfo) extends Opaque{ $this->register(new class(new BID(Ids::QUARTZ_BLOCK, Meta::QUARTZ_PILLAR), "Quartz Pillar", $quartzBreakInfo) extends Opaque{
use PillarRotationTrait; use PillarRotationInMetadataTrait;
}); });
$this->register(new Opaque(new BID(Ids::QUARTZ_BLOCK, Meta::QUARTZ_SMOOTH), "Smooth Quartz Block", $quartzBreakInfo)); //TODO: this has axis rotation in 1.9, unsure if a bug (https://bugs.mojang.com/browse/MCPE-39074) $this->register(new Opaque(new BID(Ids::QUARTZ_BLOCK, Meta::QUARTZ_SMOOTH), "Smooth Quartz Block", $quartzBreakInfo)); //TODO: this has axis rotation in 1.9, unsure if a bug (https://bugs.mojang.com/browse/MCPE-39074)
$this->register(new Stair(new BID(Ids::SMOOTH_QUARTZ_STAIRS), "Smooth Quartz Stairs", $quartzBreakInfo)); $this->register(new Stair(new BID(Ids::SMOOTH_QUARTZ_STAIRS), "Smooth Quartz Stairs", $quartzBreakInfo));

View File

@ -23,11 +23,11 @@ declare(strict_types=1);
namespace pocketmine\block; namespace pocketmine\block;
use pocketmine\block\utils\PillarRotationTrait; use pocketmine\block\utils\PillarRotationInMetadataTrait;
use pocketmine\item\ToolTier; use pocketmine\item\ToolTier;
class BoneBlock extends Opaque{ class BoneBlock extends Opaque{
use PillarRotationTrait; use PillarRotationInMetadataTrait;
public function __construct(BlockIdentifier $idInfo, string $name, ?BlockBreakInfo $breakInfo = null){ public function __construct(BlockIdentifier $idInfo, string $name, ?BlockBreakInfo $breakInfo = null){
parent::__construct($idInfo, $name, $breakInfo ?? new BlockBreakInfo(2.0, BlockToolType::PICKAXE, ToolTier::WOOD()->getHarvestLevel())); parent::__construct($idInfo, $name, $breakInfo ?? new BlockBreakInfo(2.0, BlockToolType::PICKAXE, ToolTier::WOOD()->getHarvestLevel()));

View File

@ -23,10 +23,10 @@ declare(strict_types=1);
namespace pocketmine\block; namespace pocketmine\block;
use pocketmine\block\utils\PillarRotationTrait; use pocketmine\block\utils\PillarRotationInMetadataTrait;
class HayBale extends Opaque{ class HayBale extends Opaque{
use PillarRotationTrait; use PillarRotationInMetadataTrait;
public function __construct(BlockIdentifier $idInfo, string $name, ?BlockBreakInfo $breakInfo = null){ public function __construct(BlockIdentifier $idInfo, string $name, ?BlockBreakInfo $breakInfo = null){
parent::__construct($idInfo, $name, $breakInfo ?? new BlockBreakInfo(0.5)); parent::__construct($idInfo, $name, $breakInfo ?? new BlockBreakInfo(0.5));

View File

@ -23,8 +23,8 @@ declare(strict_types=1);
namespace pocketmine\block; namespace pocketmine\block;
use pocketmine\block\utils\PillarRotationTrait; use pocketmine\block\utils\PillarRotationInMetadataTrait;
class Log extends Wood{ class Log extends Wood{
use PillarRotationTrait; use PillarRotationInMetadataTrait;
} }

View File

@ -0,0 +1,76 @@
<?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;
use pocketmine\math\Axis;
trait PillarRotationInMetadataTrait{
use PillarRotationTrait;
protected function getAxisMetaShift() : int{
return 2; //default
}
/**
* @see Block::writeStateToMeta()
*/
protected function writeStateToMeta() : int{
return $this->writeAxisToMeta();
}
/**
* @see Block::readStateFromData()
*/
public function readStateFromData(int $id, int $stateMeta) : void{
$this->readAxisFromMeta($stateMeta);
}
/**
* @see Block::getStateBitmask()
*/
public function getStateBitmask() : int{
return 0b11 << $this->getAxisMetaShift();
}
protected function readAxisFromMeta(int $meta) : void{
$axis = $meta >> $this->getAxisMetaShift();
$mapped = [
0 => Axis::Y,
1 => Axis::X,
2 => Axis::Z
][$axis] ?? null;
if($mapped === null){
throw new InvalidBlockStateException("Invalid axis meta $axis");
}
$this->axis = $mapped;
}
protected function writeAxisToMeta() : int{
return [
Axis::Y => 0,
Axis::Z => 2,
Axis::X => 1
][$this->axis] << $this->getAxisMetaShift();
}
}

View File

@ -36,52 +36,6 @@ trait PillarRotationTrait{
/** @var int */ /** @var int */
protected $axis = Axis::Y; protected $axis = Axis::Y;
protected function getAxisMetaShift() : int{
return 2; //default
}
/**
* @see Block::writeStateToMeta()
*/
protected function writeStateToMeta() : int{
return $this->writeAxisToMeta();
}
/**
* @see Block::readStateFromData()
*/
public function readStateFromData(int $id, int $stateMeta) : void{
$this->readAxisFromMeta($stateMeta);
}
/**
* @see Block::getStateBitmask()
*/
public function getStateBitmask() : int{
return 0b11 << $this->getAxisMetaShift();
}
protected function readAxisFromMeta(int $meta) : void{
$axis = $meta >> $this->getAxisMetaShift();
$mapped = [
0 => Axis::Y,
1 => Axis::X,
2 => Axis::Z
][$axis] ?? null;
if($mapped === null){
throw new InvalidBlockStateException("Invalid axis meta $axis");
}
$this->axis = $mapped;
}
protected function writeAxisToMeta() : int{
return [
Axis::Y => 0,
Axis::Z => 2,
Axis::X => 1
][$this->axis] << $this->getAxisMetaShift();
}
/** @see Axis */ /** @see Axis */
public function getAxis() : int{ return $this->axis; } public function getAxis() : int{ return $this->axis; }