mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-04-20 16:00:20 +00:00
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:
parent
d94391af57
commit
4b9d170954
@ -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{
|
||||
|
@ -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());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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; }
|
||||
}
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
@ -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; }
|
||||
|
Loading…
x
Reference in New Issue
Block a user