mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-06-10 05:34:54 +00:00
Make Block::decodeState() and encodeState() more codegen-friendly
This commit is contained in:
parent
b8d1b00985
commit
cf34f88a67
@ -47,28 +47,11 @@ class BrewingStand extends Transparent{
|
|||||||
public function getRequiredStateDataBits() : int{ return 3; }
|
public function getRequiredStateDataBits() : int{ return 3; }
|
||||||
|
|
||||||
protected function decodeState(RuntimeDataReader $r) : void{
|
protected function decodeState(RuntimeDataReader $r) : void{
|
||||||
$result = [];
|
$this->setSlots($r->readBrewingStandSlots());
|
||||||
foreach([
|
|
||||||
BrewingStandSlot::EAST(),
|
|
||||||
BrewingStandSlot::NORTHWEST(),
|
|
||||||
BrewingStandSlot::SOUTHWEST(),
|
|
||||||
] as $member){
|
|
||||||
if($r->readBool()){
|
|
||||||
$result[$member->id()] = $member;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->setSlots($result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function encodeState(RuntimeDataWriter $w) : void{
|
protected function encodeState(RuntimeDataWriter $w) : void{
|
||||||
foreach([
|
$w->writeBrewingStandSlots($this->getSlots());
|
||||||
BrewingStandSlot::EAST(),
|
|
||||||
BrewingStandSlot::NORTHWEST(),
|
|
||||||
BrewingStandSlot::SOUTHWEST(),
|
|
||||||
] as $member){
|
|
||||||
$w->writeBool(isset($this->slots[$member->id()]));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function recalculateCollisionBoxes() : array{
|
protected function recalculateCollisionBoxes() : array{
|
||||||
|
@ -43,15 +43,11 @@ class Vine extends Flowable{
|
|||||||
public function getRequiredStateDataBits() : int{ return 4; }
|
public function getRequiredStateDataBits() : int{ return 4; }
|
||||||
|
|
||||||
protected function decodeState(RuntimeDataReader $r) : void{
|
protected function decodeState(RuntimeDataReader $r) : void{
|
||||||
foreach(Facing::HORIZONTAL as $facing){
|
$this->faces = $r->readHorizontalFacingFlags();
|
||||||
$this->setFace($facing, $r->readBool());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function encodeState(RuntimeDataWriter $w) : void{
|
protected function encodeState(RuntimeDataWriter $w) : void{
|
||||||
foreach(Facing::HORIZONTAL as $facing){
|
$w->writeHorizontalFacingFlags($this->faces);
|
||||||
$w->writeBool($this->hasFace($facing));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @return int[] */
|
/** @return int[] */
|
||||||
|
@ -23,6 +23,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace pocketmine\data\runtime;
|
namespace pocketmine\data\runtime;
|
||||||
|
|
||||||
|
use pocketmine\block\utils\BrewingStandSlot;
|
||||||
use pocketmine\block\utils\WallConnectionType;
|
use pocketmine\block\utils\WallConnectionType;
|
||||||
use pocketmine\math\Axis;
|
use pocketmine\math\Axis;
|
||||||
use pocketmine\math\Facing;
|
use pocketmine\math\Facing;
|
||||||
@ -71,6 +72,20 @@ final class RuntimeDataReader{
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return int[]
|
||||||
|
*/
|
||||||
|
public function readHorizontalFacingFlags() : array{
|
||||||
|
$result = [];
|
||||||
|
foreach(Facing::HORIZONTAL as $facing){
|
||||||
|
if($this->readBool()){
|
||||||
|
$result[$facing] = $facing;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
public function readFacing() : int{
|
public function readFacing() : int{
|
||||||
return match($this->readInt(3)){
|
return match($this->readInt(3)){
|
||||||
0 => Facing::DOWN,
|
0 => Facing::DOWN,
|
||||||
@ -121,5 +136,24 @@ final class RuntimeDataReader{
|
|||||||
return $connections;
|
return $connections;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return BrewingStandSlot[]
|
||||||
|
* @phpstan-return array<int, BrewingStandSlot>
|
||||||
|
*/
|
||||||
|
public function readBrewingStandSlots() : array{
|
||||||
|
$result = [];
|
||||||
|
foreach([
|
||||||
|
BrewingStandSlot::EAST(),
|
||||||
|
BrewingStandSlot::NORTHWEST(),
|
||||||
|
BrewingStandSlot::SOUTHWEST(),
|
||||||
|
] as $member){
|
||||||
|
if($this->readBool()){
|
||||||
|
$result[$member->id()] = $member;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
public function getOffset() : int{ return $this->offset; }
|
public function getOffset() : int{ return $this->offset; }
|
||||||
}
|
}
|
||||||
|
@ -23,10 +23,12 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace pocketmine\data\runtime;
|
namespace pocketmine\data\runtime;
|
||||||
|
|
||||||
|
use pocketmine\block\utils\BrewingStandSlot;
|
||||||
use pocketmine\block\utils\WallConnectionType;
|
use pocketmine\block\utils\WallConnectionType;
|
||||||
use pocketmine\math\Axis;
|
use pocketmine\math\Axis;
|
||||||
use pocketmine\math\Facing;
|
use pocketmine\math\Facing;
|
||||||
use pocketmine\utils\AssumptionFailedError;
|
use pocketmine\utils\AssumptionFailedError;
|
||||||
|
use function array_flip;
|
||||||
|
|
||||||
final class RuntimeDataWriter{
|
final class RuntimeDataWriter{
|
||||||
use RuntimeEnumSerializerTrait;
|
use RuntimeEnumSerializerTrait;
|
||||||
@ -78,6 +80,20 @@ final class RuntimeDataWriter{
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int[] $faces
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function writeHorizontalFacingFlags(array $faces) : self{
|
||||||
|
$uniqueFaces = array_flip($faces);
|
||||||
|
foreach(Facing::HORIZONTAL as $facing){
|
||||||
|
$this->writeBool(isset($uniqueFaces[$facing]));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
public function writeFacing(int $facing) : self{
|
public function writeFacing(int $facing) : self{
|
||||||
return $this->writeInt(3, match($facing){
|
return $this->writeInt(3, match($facing){
|
||||||
0 => Facing::DOWN,
|
0 => Facing::DOWN,
|
||||||
@ -125,6 +141,24 @@ final class RuntimeDataWriter{
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param BrewingStandSlot[] $slots
|
||||||
|
* @phpstan-param array<int, BrewingStandSlot> $slots
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function writeBrewingStandSlots(array $slots) : self{
|
||||||
|
foreach([
|
||||||
|
BrewingStandSlot::EAST(),
|
||||||
|
BrewingStandSlot::NORTHWEST(),
|
||||||
|
BrewingStandSlot::SOUTHWEST(),
|
||||||
|
] as $member){
|
||||||
|
$this->writeBool(isset($slots[$member->id()]));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
public function getValue() : int{ return $this->value; }
|
public function getValue() : int{ return $this->value; }
|
||||||
|
|
||||||
public function getOffset() : int{ return $this->offset; }
|
public function getOffset() : int{ return $this->offset; }
|
||||||
|
Loading…
x
Reference in New Issue
Block a user