RuntimeDataDescriber: added dynamic method for reading and writing enum sets

this was previously only needed for brewing stands, but it's now become needed for chiselled bookshelves too.
This commit is contained in:
Dylan K. Taylor 2023-09-28 16:06:17 +01:00
parent d94391af57
commit 4b9d170954
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
6 changed files with 40 additions and 47 deletions

View File

@ -45,7 +45,7 @@ class BrewingStand extends Transparent{
protected array $slots = [];
protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
$w->brewingStandSlots($this->slots);
$w->enumSet($this->slots, BrewingStandSlot::cases());
}
protected function recalculateCollisionBoxes() : array{

View File

@ -50,7 +50,7 @@ class ChiseledBookshelf extends Opaque{
protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
$w->horizontalFacing($this->facing);
$w->chiseledBookshelfSlots($this->slots);
$w->enumSet($this->slots, ChiseledBookshelfSlot::cases());
}
/**

View File

@ -72,6 +72,8 @@ interface RuntimeDataDescriber extends RuntimeEnumDescriber{
/**
* @param BrewingStandSlot[] $slots
* @phpstan-param array<int, BrewingStandSlot> $slots
*
* @deprecated Use {@link enumSet()} instead.
*/
public function brewingStandSlots(array &$slots) : void;
@ -79,15 +81,19 @@ interface RuntimeDataDescriber extends RuntimeEnumDescriber{
public function straightOnlyRailShape(int &$railShape) : void;
/**
* @param ChiseledBookshelfSlot[] $slots
* @phpstan-param array<int, ChiseledBookshelfSlot> $slots
*/
public function chiseledBookshelfSlots(array &$slots) : void;
/**
* @phpstan-template T of \UnitEnum
* @phpstan-param T $case
*/
public function enum(\UnitEnum &$case) : void;
/**
* @param \UnitEnum[] &$set
* @param \UnitEnum[] $allCases
*
* @phpstan-template T of \UnitEnum
* @phpstan-param array<int, T> &$set
* @phpstan-param array<int, T> $allCases
*/
public function enumSet(array &$set, array $allCases) : void;
}

View File

@ -182,16 +182,11 @@ final class RuntimeDataReader implements RuntimeDataDescriber{
/**
* @param BrewingStandSlot[] $slots
* @phpstan-param array<int, BrewingStandSlot> $slots
*
* @deprecated Use {@link enumSet()} instead.
*/
public function brewingStandSlots(array &$slots) : void{
$result = [];
foreach(BrewingStandSlot::cases() as $member){
if($this->readBool()){
$result[spl_object_id($member)] = $member;
}
}
$slots = $result;
$this->enumSet($slots, BrewingStandSlot::cases());
}
public function railShape(int &$railShape) : void{
@ -212,20 +207,6 @@ final class RuntimeDataReader implements RuntimeDataDescriber{
$railShape = $result;
}
/**
* @param ChiseledBookshelfSlot[] $slots
* @phpstan-param array<int, ChiseledBookshelfSlot> $slots
*/
public function chiseledBookshelfSlots(array &$slots) : void{
$result = [];
foreach(ChiseledBookshelfSlot::cases() as $member){
if($this->readBool()){
$result[spl_object_id($member)] = $member;
}
}
$slots = $result;
}
public function enum(\UnitEnum &$case) : void{
$metadata = RuntimeEnumMetadata::from($case);
$raw = $this->readInt($metadata->bits);
@ -237,5 +218,15 @@ final class RuntimeDataReader implements RuntimeDataDescriber{
$case = $result;
}
public function enumSet(array &$set, array $allCases) : void{
$result = [];
foreach($allCases as $case){
if($this->readBool()){
$result[spl_object_id($case)] = $case;
}
}
$set = $result;
}
public function getOffset() : int{ return $this->offset; }
}

View File

@ -97,12 +97,12 @@ final class RuntimeDataSizeCalculator implements RuntimeDataDescriber{
$this->addBits(3);
}
public function chiseledBookshelfSlots(array &$slots) : void{
$this->addBits(count(ChiseledBookshelfSlot::cases()));
}
public function enum(\UnitEnum &$case) : void{
$metadata = RuntimeEnumMetadata::from($case);
$this->addBits($metadata->bits);
}
public function enumSet(array &$set, array $allCases) : void{
$this->addBits(count($allCases));
}
}

View File

@ -160,11 +160,11 @@ final class RuntimeDataWriter implements RuntimeDataDescriber{
/**
* @param BrewingStandSlot[] $slots
* @phpstan-param array<int, BrewingStandSlot> $slots
*
* @deprecated Use {@link enumSet()} instead.
*/
public function brewingStandSlots(array &$slots) : void{
foreach(BrewingStandSlot::cases() as $member){
$this->writeBool(isset($slots[spl_object_id($member)]));
}
$this->enumSet($slots, BrewingStandSlot::cases());
}
public function railShape(int &$railShape) : void{
@ -175,21 +175,17 @@ final class RuntimeDataWriter implements RuntimeDataDescriber{
$this->int(3, $railShape);
}
/**
* @param ChiseledBookshelfSlot[] $slots
* @phpstan-param array<int, ChiseledBookshelfSlot> $slots
*/
public function chiseledBookshelfSlots(array &$slots) : void{
foreach(ChiseledBookshelfSlot::cases() as $member){
$this->writeBool(isset($slots[spl_object_id($member)]));
}
}
public function enum(\UnitEnum &$case) : void{
$metadata = RuntimeEnumMetadata::from($case);
$this->writeInt($metadata->bits, $metadata->enumToInt($case));
}
public function enumSet(array &$set, array $allCases) : void{
foreach($allCases as $case){
$this->writeBool(isset($set[spl_object_id($case)]));
}
}
public function getValue() : int{ return $this->value; }
public function getOffset() : int{ return $this->offset; }