Lever: add block property APIs

This commit is contained in:
Dylan K. Taylor 2021-09-29 00:20:57 +01:00
parent c931c52617
commit 6a8280b1ba
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
2 changed files with 131 additions and 44 deletions

View File

@ -23,50 +23,72 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\utils\BlockDataSerializer;
use pocketmine\block\utils\LeverFacing;
use pocketmine\item\Item;
use pocketmine\math\Axis;
use pocketmine\math\Facing;
use pocketmine\math\Vector3;
use pocketmine\player\Player;
use pocketmine\utils\AssumptionFailedError;
use pocketmine\world\BlockTransaction;
use pocketmine\world\sound\RedstonePowerOffSound;
use pocketmine\world\sound\RedstonePowerOnSound;
class Lever extends Flowable{
protected const BOTTOM = 0;
protected const SIDE = 1;
protected const TOP = 2;
protected LeverFacing $facing;
protected bool $activated = false;
protected int $leverPos = self::BOTTOM;
protected int $facing = Facing::NORTH;
protected bool $powered = false;
public function __construct(BlockIdentifier $idInfo, string $name, BlockBreakInfo $breakInfo){
$this->facing = LeverFacing::UP_AXIS_X();
parent::__construct($idInfo, $name, $breakInfo);
}
protected function writeStateToMeta() : int{
if($this->leverPos === self::BOTTOM){
$rotationMeta = Facing::axis($this->facing) === Axis::Z ? 7 : 0;
}elseif($this->leverPos === self::TOP){
$rotationMeta = Facing::axis($this->facing) === Axis::Z ? 5 : 6;
}else{
$rotationMeta = 6 - BlockDataSerializer::writeHorizontalFacing($this->facing);
}
return $rotationMeta | ($this->powered ? BlockLegacyMetadata::LEVER_FLAG_POWERED : 0);
$rotationMeta = match($this->facing->id()){
LeverFacing::DOWN_AXIS_X()->id() => 0,
LeverFacing::EAST()->id() => 1,
LeverFacing::WEST()->id() => 2,
LeverFacing::SOUTH()->id() => 3,
LeverFacing::NORTH()->id() => 4,
LeverFacing::UP_AXIS_Z()->id() => 5,
LeverFacing::UP_AXIS_X()->id() => 6,
LeverFacing::DOWN_AXIS_Z()->id() => 7,
default => throw new AssumptionFailedError(),
};
return $rotationMeta | ($this->activated ? BlockLegacyMetadata::LEVER_FLAG_POWERED : 0);
}
public function readStateFromData(int $id, int $stateMeta) : void{
$rotationMeta = $stateMeta & 0x07;
if($rotationMeta === 5 or $rotationMeta === 6){
$this->leverPos = self::TOP;
$this->facing = $rotationMeta === 5 ? Facing::SOUTH : Facing::EAST;
}elseif($rotationMeta === 7 or $rotationMeta === 0){
$this->leverPos = self::BOTTOM;
$this->facing = $rotationMeta === 7 ? Facing::SOUTH : Facing::EAST;
}else{
$this->leverPos = self::SIDE;
$this->facing = BlockDataSerializer::readHorizontalFacing(6 - $rotationMeta);
}
$this->facing = match($rotationMeta){
0 => LeverFacing::DOWN_AXIS_X(),
1 => LeverFacing::EAST(),
2 => LeverFacing::WEST(),
3 => LeverFacing::SOUTH(),
4 => LeverFacing::NORTH(),
5 => LeverFacing::UP_AXIS_Z(),
6 => LeverFacing::UP_AXIS_X(),
7 => LeverFacing::DOWN_AXIS_Z(),
default => throw new AssumptionFailedError("0x07 mask should make this impossible"), //phpstan doesn't understand :(
};
$this->powered = ($stateMeta & BlockLegacyMetadata::LEVER_FLAG_POWERED) !== 0;
$this->activated = ($stateMeta & BlockLegacyMetadata::LEVER_FLAG_POWERED) !== 0;
}
public function getFacing() : LeverFacing{ return $this->facing; }
/** @return $this */
public function setFacing(LeverFacing $facing) : self{
$this->facing = $facing;
return $this;
}
public function isActivated() : bool{ return $this->activated; }
/** @return $this */
public function setActivated(bool $activated) : self{
$this->activated = $activated;
return $this;
}
public function getStateBitmask() : int{
@ -78,39 +100,37 @@ class Lever extends Flowable{
return false;
}
if(Facing::axis($face) === Axis::Y){
$selectUpDownPos = function(LeverFacing $x, LeverFacing $z) use ($player) : LeverFacing{
if($player !== null){
$this->facing = Facing::opposite($player->getHorizontalFacing());
return Facing::axis($player->getHorizontalFacing()) === Axis::X ? $x : $z;
}
$this->leverPos = $face === Facing::DOWN ? self::BOTTOM : self::TOP;
}else{
$this->facing = $face;
$this->leverPos = self::SIDE;
}
return $x;
};
$this->facing = match($face){
Facing::DOWN => $selectUpDownPos(LeverFacing::DOWN_AXIS_X(), LeverFacing::DOWN_AXIS_Z()),
Facing::UP => $selectUpDownPos(LeverFacing::UP_AXIS_X(), LeverFacing::UP_AXIS_Z()),
Facing::NORTH => LeverFacing::NORTH(),
Facing::SOUTH => LeverFacing::SOUTH(),
Facing::WEST => LeverFacing::WEST(),
Facing::EAST => LeverFacing::EAST(),
default => throw new AssumptionFailedError("Bad facing value"),
};
return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player);
}
public function onNearbyBlockChange() : void{
if($this->leverPos === self::BOTTOM){
$face = Facing::UP;
}elseif($this->leverPos === self::TOP){
$face = Facing::DOWN;
}else{
$face = Facing::opposite($this->facing);
}
if(!$this->getSide($face)->isSolid()){
if(!$this->getSide(Facing::opposite($this->facing->getFacing()))->isSolid()){
$this->position->getWorld()->useBreakOn($this->position);
}
}
public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null) : bool{
$this->powered = !$this->powered;
$this->activated = !$this->activated;
$this->position->getWorld()->setBlock($this->position, $this);
$this->position->getWorld()->addSound(
$this->position->add(0.5, 0.5, 0.5),
$this->powered ? new RedstonePowerOnSound() : new RedstonePowerOffSound()
$this->activated ? new RedstonePowerOnSound() : new RedstonePowerOffSound()
);
return true;
}

View File

@ -0,0 +1,67 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\block\utils;
use pocketmine\math\Facing;
use pocketmine\utils\EnumTrait;
/**
* This doc-block is generated automatically, do not modify it manually.
* This must be regenerated whenever registry members are added, removed or changed.
* @see build/generate-registry-annotations.php
* @generate-registry-docblock
*
* @method static LeverFacing DOWN_AXIS_X()
* @method static LeverFacing DOWN_AXIS_Z()
* @method static LeverFacing EAST()
* @method static LeverFacing NORTH()
* @method static LeverFacing SOUTH()
* @method static LeverFacing UP_AXIS_X()
* @method static LeverFacing UP_AXIS_Z()
* @method static LeverFacing WEST()
*/
final class LeverFacing{
use EnumTrait {
__construct as Enum___construct;
}
protected static function setup() : void{
self::registerAll(
new self("up_axis_x", Facing::UP),
new self("up_axis_z", Facing::UP),
new self("down_axis_x", Facing::DOWN),
new self("down_axis_z", Facing::DOWN),
new self("north", Facing::NORTH),
new self("east", Facing::EAST),
new self("south", Facing::SOUTH),
new self("west", Facing::WEST),
);
}
private function __construct(string $enumName, private int $facing){
$this->Enum___construct($enumName);
}
public function getFacing() : int{ return $this->facing; }
}