Use generic enumSet() for blocks with facing flags

This commit is contained in:
Dylan K. Taylor
2025-08-30 18:10:44 +01:00
parent e87e6cf19f
commit 4e82482a80
6 changed files with 15 additions and 85 deletions

View File

@ -33,6 +33,7 @@ use pocketmine\player\Player;
use pocketmine\world\BlockTransaction; use pocketmine\world\BlockTransaction;
use function array_intersect_key; use function array_intersect_key;
use function count; use function count;
use function spl_object_id;
class Vine extends Flowable{ class Vine extends Flowable{
@ -40,14 +41,14 @@ class Vine extends Flowable{
protected array $faces = []; protected array $faces = [];
protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{ protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
$w->horizontalFacingFlags($this->faces); $w->enumSet($this->faces, HorizontalFacingOption::cases());
} }
/** @return HorizontalFacingOption[] */ /** @return HorizontalFacingOption[] */
public function getFaces() : array{ return $this->faces; } public function getFaces() : array{ return $this->faces; }
public function hasFace(HorizontalFacingOption $face) : bool{ public function hasFace(HorizontalFacingOption $face) : bool{
return isset($this->faces[$face->value]); return isset($this->faces[spl_object_id($face)]);
} }
/** /**
@ -60,7 +61,7 @@ class Vine extends Flowable{
if(!$face instanceof HorizontalFacingOption){ if(!$face instanceof HorizontalFacingOption){
throw new \InvalidArgumentException("Expected array of HorizontalFacingOption"); throw new \InvalidArgumentException("Expected array of HorizontalFacingOption");
} }
$uniqueFaces[$face->value] = $face; $uniqueFaces[spl_object_id($face)] = $face;
} }
$this->faces = $uniqueFaces; $this->faces = $uniqueFaces;
return $this; return $this;
@ -69,9 +70,9 @@ class Vine extends Flowable{
/** @return $this */ /** @return $this */
public function setFace(HorizontalFacingOption $face, bool $value) : self{ public function setFace(HorizontalFacingOption $face, bool $value) : self{
if($value){ if($value){
$this->faces[$face->value] = $face; $this->faces[spl_object_id($face)] = $face;
}else{ }else{
unset($this->faces[$face->value]); unset($this->faces[spl_object_id($face)]);
} }
return $this; return $this;
} }
@ -105,7 +106,7 @@ class Vine extends Flowable{
} }
$this->faces = $blockReplace instanceof Vine ? $blockReplace->faces : []; $this->faces = $blockReplace instanceof Vine ? $blockReplace->faces : [];
$this->faces[$hzFacing->value] = $hzFacing; $this->faces[spl_object_id($hzFacing)] = $hzFacing;
return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player); return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player);
} }
@ -118,8 +119,8 @@ class Vine extends Flowable{
$supportedFaces = $up instanceof Vine ? array_intersect_key($this->faces, $up->faces) : []; $supportedFaces = $up instanceof Vine ? array_intersect_key($this->faces, $up->faces) : [];
foreach($this->faces as $face){ foreach($this->faces as $face){
if(!isset($supportedFaces[$face->value]) && !$this->getSide($face->toFacing())->isSolid()){ if(!isset($supportedFaces[spl_object_id($face)]) && !$this->getSide($face->toFacing())->isSolid()){
unset($this->faces[$face->value]); unset($this->faces[spl_object_id($face)]);
$changed = true; $changed = true;
} }
} }

View File

@ -25,6 +25,7 @@ namespace pocketmine\block\utils;
use pocketmine\data\runtime\RuntimeDataDescriber; use pocketmine\data\runtime\RuntimeDataDescriber;
use pocketmine\math\Facing; use pocketmine\math\Facing;
use function spl_object_id;
/** /**
* Used by blocks that can have multiple target faces in the area of one solid block, such as covering three sides of a corner. * Used by blocks that can have multiple target faces in the area of one solid block, such as covering three sides of a corner.
@ -35,14 +36,14 @@ trait MultiAnyFacingTrait{
protected array $faces = []; protected array $faces = [];
protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{ protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
$w->facingFlags($this->faces); $w->enumSet($this->faces, Facing::cases());
} }
/** @return Facing[] */ /** @return Facing[] */
public function getFaces() : array{ return $this->faces; } public function getFaces() : array{ return $this->faces; }
public function hasFace(Facing $face) : bool{ public function hasFace(Facing $face) : bool{
return isset($this->faces[$face->value]); return isset($this->faces[spl_object_id($face)]);
} }
/** /**
@ -52,7 +53,7 @@ trait MultiAnyFacingTrait{
public function setFaces(array $faces) : self{ public function setFaces(array $faces) : self{
$uniqueFaces = []; $uniqueFaces = [];
foreach($faces as $face){ foreach($faces as $face){
$uniqueFaces[$face->value] = $face; $uniqueFaces[spl_object_id($face)] = $face;
} }
$this->faces = $uniqueFaces; $this->faces = $uniqueFaces;
return $this; return $this;
@ -61,9 +62,9 @@ trait MultiAnyFacingTrait{
/** @return $this */ /** @return $this */
public function setFace(Facing $face, bool $value) : self{ public function setFace(Facing $face, bool $value) : self{
if($value){ if($value){
$this->faces[$face->value] = $face; $this->faces[spl_object_id($face)] = $face;
}else{ }else{
unset($this->faces[$face->value]); unset($this->faces[spl_object_id($face)]);
} }
return $this; return $this;
} }

View File

@ -47,16 +47,6 @@ interface RuntimeDataDescriber{
public function bool(bool &$value) : void; public function bool(bool &$value) : void;
/**
* @param Facing[] $faces
*/
public function facingFlags(array &$faces) : void;
/**
* @param HorizontalFacingOption[] $faces
*/
public function horizontalFacingFlags(array &$faces) : void;
public function facingExcept(Facing &$facing, Facing $except) : void; public function facingExcept(Facing &$facing, Facing $except) : void;
public function horizontalAxis(Axis &$axis) : void; public function horizontalAxis(Axis &$axis) : void;

View File

@ -77,34 +77,6 @@ final class RuntimeDataReader implements RuntimeDataDescriber{
$value = $this->readBool(); $value = $this->readBool();
} }
/**
* @param Facing[] $faces
*/
public function facingFlags(array &$faces) : void{
$result = [];
foreach(Facing::ALL as $facing){
if($this->readBool()){
$result[$facing->value] = $facing;
}
}
$faces = $result;
}
/**
* @param HorizontalFacingOption[] $faces
*/
public function horizontalFacingFlags(array &$faces) : void{
$result = [];
foreach(HorizontalFacingOption::cases() as $facing){
if($this->readBool()){
$result[$facing->value] = $facing;
}
}
$faces = $result;
}
public function facingExcept(Facing &$facing, Facing $except) : void{ public function facingExcept(Facing &$facing, Facing $except) : void{
$result = Facing::DOWN; $result = Facing::DOWN;
$this->enum($result); $this->enum($result);

View File

@ -52,14 +52,6 @@ final class RuntimeDataSizeCalculator implements RuntimeDataDescriber{
$this->addBits(1); $this->addBits(1);
} }
public function facingFlags(array &$faces) : void{
$this->addBits(count(Facing::cases()));
}
public function horizontalFacingFlags(array &$faces) : void{
$this->addBits(count(HorizontalFacingOption::cases()));
}
public function facingExcept(Facing &$facing, Facing $except) : void{ public function facingExcept(Facing &$facing, Facing $except) : void{
$this->enum($facing); $this->enum($facing);
} }

View File

@ -74,32 +74,6 @@ final class RuntimeDataWriter implements RuntimeDataDescriber{
$this->writeBool($value); $this->writeBool($value);
} }
/**
* @param Facing[] $faces
*/
public function facingFlags(array &$faces) : void{
$uniqueFaces = [];
foreach($faces as $face){
$uniqueFaces[$face->value] = true;
}
foreach(Facing::ALL as $facing){
$this->writeBool(isset($uniqueFaces[$facing->value]));
}
}
/**
* @param HorizontalFacingOption[] $faces
*/
public function horizontalFacingFlags(array &$faces) : void{
$uniqueFaces = [];
foreach($faces as $face){
$uniqueFaces[$face->value] = true;
}
foreach(HorizontalFacingOption::cases() as $facing){
$this->writeBool(isset($uniqueFaces[$facing->value]));
}
}
public function facingExcept(Facing &$facing, Facing $except) : void{ public function facingExcept(Facing &$facing, Facing $except) : void{
$this->enum($facing); $this->enum($facing);
} }