Block: get rid of getNonPersistentStateBitmask(), add writeStateToItemMeta()

this is more flexible and less confusing.
This commit is contained in:
Dylan K. Taylor 2021-08-02 19:17:24 +01:00
parent 01b48a21d9
commit bdac98beaf
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
13 changed files with 50 additions and 53 deletions

View File

@ -54,8 +54,8 @@ class Anvil extends Transparent implements Fallable{
return 0b1111; return 0b1111;
} }
public function getNonPersistentStateBitmask() : int{ protected function writeStateToItemMeta() : int{
return 0b11; return $this->damage << 2;
} }
public function getDamage() : int{ return $this->damage; } public function getDamage() : int{ return $this->damage; }

View File

@ -30,8 +30,6 @@ use pocketmine\block\utils\DyeColor;
use pocketmine\data\bedrock\DyeColorIdMap; use pocketmine\data\bedrock\DyeColorIdMap;
use pocketmine\item\Banner as ItemBanner; use pocketmine\item\Banner as ItemBanner;
use pocketmine\item\Item; use pocketmine\item\Item;
use pocketmine\item\ItemFactory;
use pocketmine\item\ItemIds;
use pocketmine\math\AxisAlignedBB; use pocketmine\math\AxisAlignedBB;
use pocketmine\math\Vector3; use pocketmine\math\Vector3;
use pocketmine\player\Player; use pocketmine\player\Player;
@ -126,8 +124,8 @@ abstract class BaseBanner extends Transparent{
} }
} }
public function asItem() : Item{ protected function writeStateToItemMeta() : int{
return ItemFactory::getInstance()->get(ItemIds::BANNER, DyeColorIdMap::getInstance()->toInvertedId($this->color)); return DyeColorIdMap::getInstance()->toInvertedId($this->color);
} }
public function getDropsForCompatibleTool(Item $item) : array{ public function getDropsForCompatibleTool(Item $item) : array{

View File

@ -30,7 +30,6 @@ use pocketmine\block\utils\DyeColor;
use pocketmine\block\utils\HorizontalFacingTrait; use pocketmine\block\utils\HorizontalFacingTrait;
use pocketmine\data\bedrock\DyeColorIdMap; use pocketmine\data\bedrock\DyeColorIdMap;
use pocketmine\item\Item; use pocketmine\item\Item;
use pocketmine\item\ItemFactory;
use pocketmine\lang\KnownTranslationKeys; use pocketmine\lang\KnownTranslationKeys;
use pocketmine\lang\TranslationContainer; use pocketmine\lang\TranslationContainer;
use pocketmine\math\AxisAlignedBB; use pocketmine\math\AxisAlignedBB;
@ -197,8 +196,8 @@ class Bed extends Transparent{
return []; return [];
} }
public function asItem() : Item{ protected function writeStateToItemMeta() : int{
return ItemFactory::getInstance()->get($this->idInfo->getItemId(), DyeColorIdMap::getInstance()->toId($this->color)); return DyeColorIdMap::getInstance()->toId($this->color);
} }
public function getAffectedBlocks() : array{ public function getAffectedBlocks() : array{

View File

@ -98,7 +98,7 @@ class Block{
public function asItem() : Item{ public function asItem() : Item{
return ItemFactory::getInstance()->get( return ItemFactory::getInstance()->get(
$this->idInfo->getItemId(), $this->idInfo->getItemId(),
$this->idInfo->getVariant() | ($this->writeStateToMeta() & ~$this->getNonPersistentStateBitmask()) $this->idInfo->getVariant() | $this->writeStateToItemMeta()
); );
} }
@ -108,6 +108,10 @@ class Block{
return $this->idInfo->getVariant() | $stateMeta; return $this->idInfo->getVariant() | $stateMeta;
} }
protected function writeStateToItemMeta() : int{
return 0;
}
/** /**
* Returns a bitmask used to extract state bits from block metadata. * Returns a bitmask used to extract state bits from block metadata.
*/ */
@ -115,10 +119,6 @@ class Block{
return 0; return 0;
} }
public function getNonPersistentStateBitmask() : int{
return $this->getStateBitmask();
}
protected function writeStateToMeta() : int{ protected function writeStateToMeta() : int{
return 0; return 0;
} }

View File

@ -44,12 +44,12 @@ final class Coral extends BaseCoral{
return CoralTypeIdMap::getInstance()->toId($this->coralType); return CoralTypeIdMap::getInstance()->toId($this->coralType);
} }
public function getStateBitmask() : int{ protected function writeStateToItemMeta() : int{
return 0b0111; return $this->writeStateToMeta();
} }
public function getNonPersistentStateBitmask() : int{ public function getStateBitmask() : int{
return 0b0000; return 0b0111;
} }
public function readStateFromWorld() : void{ public function readStateFromWorld() : void{

View File

@ -52,12 +52,12 @@ final class CoralBlock extends Opaque{
return ($this->dead ? BlockLegacyMetadata::CORAL_BLOCK_FLAG_DEAD : 0) | CoralTypeIdMap::getInstance()->toId($this->coralType); return ($this->dead ? BlockLegacyMetadata::CORAL_BLOCK_FLAG_DEAD : 0) | CoralTypeIdMap::getInstance()->toId($this->coralType);
} }
public function getStateBitmask() : int{ protected function writeStateToItemMeta() : int{
return 0b1111; return $this->writeStateToMeta();
} }
public function getNonPersistentStateBitmask() : int{ public function getStateBitmask() : int{
return 0; return 0b1111;
} }
public function getCoralType() : CoralType{ return $this->coralType; } public function getCoralType() : CoralType{ return $this->coralType; }

View File

@ -41,12 +41,12 @@ class Dirt extends Opaque{
return $this->coarse ? BlockLegacyMetadata::DIRT_FLAG_COARSE : 0; return $this->coarse ? BlockLegacyMetadata::DIRT_FLAG_COARSE : 0;
} }
public function getStateBitmask() : int{ protected function writeStateToItemMeta() : int{
return 0b1; return $this->writeStateToMeta();
} }
public function getNonPersistentStateBitmask() : int{ public function getStateBitmask() : int{
return 0; return 0b1;
} }
public function isCoarse() : bool{ return $this->coarse; } public function isCoarse() : bool{ return $this->coarse; }

View File

@ -64,7 +64,7 @@ final class FloorCoralFan extends BaseCoral{
//TODO: HACK! workaround dead flag being lost when broken / blockpicked (original impl only uses first ID) //TODO: HACK! workaround dead flag being lost when broken / blockpicked (original impl only uses first ID)
return ItemFactory::getInstance()->get( return ItemFactory::getInstance()->get(
$this->dead ? ItemIds::CORAL_FAN_DEAD : ItemIds::CORAL_FAN, $this->dead ? ItemIds::CORAL_FAN_DEAD : ItemIds::CORAL_FAN,
CoralTypeIdMap::getInstance()->toId($this->coralType) $this->writeStateToItemMeta()
); );
} }
@ -73,12 +73,12 @@ final class FloorCoralFan extends BaseCoral{
CoralTypeIdMap::getInstance()->toId($this->coralType); CoralTypeIdMap::getInstance()->toId($this->coralType);
} }
public function getStateBitmask() : int{ protected function writeStateToItemMeta() : int{
return 0b1111; return CoralTypeIdMap::getInstance()->toId($this->coralType);
} }
public function getNonPersistentStateBitmask() : int{ public function getStateBitmask() : int{
return 0b1000; return 0b1111;
} }
public function getAxis() : int{ return $this->axis; } public function getAxis() : int{ return $this->axis; }

View File

@ -27,8 +27,6 @@ use pocketmine\block\tile\Skull as TileSkull;
use pocketmine\block\utils\BlockDataSerializer; use pocketmine\block\utils\BlockDataSerializer;
use pocketmine\block\utils\SkullType; use pocketmine\block\utils\SkullType;
use pocketmine\item\Item; use pocketmine\item\Item;
use pocketmine\item\ItemFactory;
use pocketmine\item\ItemIds;
use pocketmine\math\AxisAlignedBB; use pocketmine\math\AxisAlignedBB;
use pocketmine\math\Facing; use pocketmine\math\Facing;
use pocketmine\math\Vector3; use pocketmine\math\Vector3;
@ -142,7 +140,7 @@ class Skull extends Flowable{
return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player); return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player);
} }
public function asItem() : Item{ protected function writeStateToItemMeta() : int{
return ItemFactory::getInstance()->get(ItemIds::SKULL, $this->skullType->getMagicNumber()); return $this->skullType->getMagicNumber();
} }
} }

View File

@ -35,12 +35,12 @@ class Sponge extends Opaque{
$this->wet = ($stateMeta & BlockLegacyMetadata::SPONGE_FLAG_WET) !== 0; $this->wet = ($stateMeta & BlockLegacyMetadata::SPONGE_FLAG_WET) !== 0;
} }
public function getStateBitmask() : int{ protected function writeStateToItemMeta() : int{
return 0b1; return $this->writeStateToMeta();
} }
public function getNonPersistentStateBitmask() : int{ public function getStateBitmask() : int{
return 0; return 0b1;
} }
public function isWet() : bool{ return $this->wet; } public function isWet() : bool{ return $this->wet; }

View File

@ -53,12 +53,14 @@ class TNT extends Opaque{
return ($this->unstable ? BlockLegacyMetadata::TNT_FLAG_UNSTABLE : 0) | ($this->worksUnderwater ? BlockLegacyMetadata::TNT_FLAG_UNDERWATER : 0); return ($this->unstable ? BlockLegacyMetadata::TNT_FLAG_UNSTABLE : 0) | ($this->worksUnderwater ? BlockLegacyMetadata::TNT_FLAG_UNDERWATER : 0);
} }
protected function writeStateToItemMeta() : int{
return $this->worksUnderwater ? BlockLegacyMetadata::TNT_FLAG_UNDERWATER : 0;
}
public function getStateBitmask() : int{ public function getStateBitmask() : int{
return 0b11; return 0b11;
} }
public function getNonPersistentStateBitmask() : int{ return 0b1; }
public function isUnstable() : bool{ return $this->unstable; } public function isUnstable() : bool{ return $this->unstable; }
/** @return $this */ /** @return $this */

View File

@ -96,18 +96,18 @@ final class WallCoralFan extends BaseCoral{
return (BlockDataSerializer::writeCoralFacing($this->facing) << 2) | ($this->dead ? BlockLegacyMetadata::CORAL_FAN_HANG_FLAG_DEAD : 0) | $coralTypeFlag; return (BlockDataSerializer::writeCoralFacing($this->facing) << 2) | ($this->dead ? BlockLegacyMetadata::CORAL_FAN_HANG_FLAG_DEAD : 0) | $coralTypeFlag;
} }
public function getStateBitmask() : int{ protected function writeStateToItemMeta() : int{
return 0b1111; return CoralTypeIdMap::getInstance()->toId($this->coralType);
} }
public function getNonPersistentStateBitmask() : int{ public function getStateBitmask() : int{
return 0b1110; return 0b1111;
} }
public function asItem() : Item{ public function asItem() : Item{
return ItemFactory::getInstance()->get( return ItemFactory::getInstance()->get(
$this->dead ? ItemIds::CORAL_FAN_DEAD : ItemIds::CORAL_FAN, $this->dead ? ItemIds::CORAL_FAN_DEAD : ItemIds::CORAL_FAN,
CoralTypeIdMap::getInstance()->toId($this->coralType) $this->writeStateToItemMeta()
); );
} }

View File

@ -47,17 +47,17 @@ trait ColorInMetadataTrait{
return DyeColorIdMap::getInstance()->toId($this->color); return DyeColorIdMap::getInstance()->toId($this->color);
} }
/**
* @see Block::writeStateToItemMeta()
*/
protected function writeStateToItemMeta() : int{
return DyeColorIdMap::getInstance()->toId($this->color);
}
/** /**
* @see Block::getStateBitmask() * @see Block::getStateBitmask()
*/ */
public function getStateBitmask() : int{ public function getStateBitmask() : int{
return 0b1111; return 0b1111;
} }
/**
* @see Block::getNonPersistentStateBitmask()
*/
public function getNonPersistentStateBitmask() : int{
return 0;
}
} }