Merge branch 'master' into r13-world-support

This commit is contained in:
Dylan K. Taylor 2021-09-29 00:22:11 +01:00
commit 57341d8a1b
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; namespace pocketmine\block;
use pocketmine\block\utils\BlockDataSerializer; use pocketmine\block\utils\LeverFacing;
use pocketmine\item\Item; use pocketmine\item\Item;
use pocketmine\math\Axis; use pocketmine\math\Axis;
use pocketmine\math\Facing; use pocketmine\math\Facing;
use pocketmine\math\Vector3; use pocketmine\math\Vector3;
use pocketmine\player\Player; use pocketmine\player\Player;
use pocketmine\utils\AssumptionFailedError;
use pocketmine\world\BlockTransaction; use pocketmine\world\BlockTransaction;
use pocketmine\world\sound\RedstonePowerOffSound; use pocketmine\world\sound\RedstonePowerOffSound;
use pocketmine\world\sound\RedstonePowerOnSound; use pocketmine\world\sound\RedstonePowerOnSound;
class Lever extends Flowable{ class Lever extends Flowable{
protected const BOTTOM = 0; protected LeverFacing $facing;
protected const SIDE = 1; protected bool $activated = false;
protected const TOP = 2;
protected int $leverPos = self::BOTTOM; public function __construct(BlockIdentifier $idInfo, string $name, BlockBreakInfo $breakInfo){
protected int $facing = Facing::NORTH; $this->facing = LeverFacing::UP_AXIS_X();
protected bool $powered = false; parent::__construct($idInfo, $name, $breakInfo);
}
protected function writeStateToMeta() : int{ protected function writeStateToMeta() : int{
if($this->leverPos === self::BOTTOM){ $rotationMeta = match($this->facing->id()){
$rotationMeta = Facing::axis($this->facing) === Axis::Z ? 7 : 0; LeverFacing::DOWN_AXIS_X()->id() => 0,
}elseif($this->leverPos === self::TOP){ LeverFacing::EAST()->id() => 1,
$rotationMeta = Facing::axis($this->facing) === Axis::Z ? 5 : 6; LeverFacing::WEST()->id() => 2,
}else{ LeverFacing::SOUTH()->id() => 3,
$rotationMeta = 6 - BlockDataSerializer::writeHorizontalFacing($this->facing); LeverFacing::NORTH()->id() => 4,
} LeverFacing::UP_AXIS_Z()->id() => 5,
return $rotationMeta | ($this->powered ? BlockLegacyMetadata::LEVER_FLAG_POWERED : 0); 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{ public function readStateFromData(int $id, int $stateMeta) : void{
$rotationMeta = $stateMeta & 0x07; $rotationMeta = $stateMeta & 0x07;
if($rotationMeta === 5 or $rotationMeta === 6){ $this->facing = match($rotationMeta){
$this->leverPos = self::TOP; 0 => LeverFacing::DOWN_AXIS_X(),
$this->facing = $rotationMeta === 5 ? Facing::SOUTH : Facing::EAST; 1 => LeverFacing::EAST(),
}elseif($rotationMeta === 7 or $rotationMeta === 0){ 2 => LeverFacing::WEST(),
$this->leverPos = self::BOTTOM; 3 => LeverFacing::SOUTH(),
$this->facing = $rotationMeta === 7 ? Facing::SOUTH : Facing::EAST; 4 => LeverFacing::NORTH(),
}else{ 5 => LeverFacing::UP_AXIS_Z(),
$this->leverPos = self::SIDE; 6 => LeverFacing::UP_AXIS_X(),
$this->facing = BlockDataSerializer::readHorizontalFacing(6 - $rotationMeta); 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{ public function getStateBitmask() : int{
@ -78,39 +100,37 @@ class Lever extends Flowable{
return false; return false;
} }
if(Facing::axis($face) === Axis::Y){ $selectUpDownPos = function(LeverFacing $x, LeverFacing $z) use ($player) : LeverFacing{
if($player !== null){ 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; return $x;
}else{ };
$this->facing = $face; $this->facing = match($face){
$this->leverPos = self::SIDE; 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); return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player);
} }
public function onNearbyBlockChange() : void{ public function onNearbyBlockChange() : void{
if($this->leverPos === self::BOTTOM){ if(!$this->getSide(Facing::opposite($this->facing->getFacing()))->isSolid()){
$face = Facing::UP;
}elseif($this->leverPos === self::TOP){
$face = Facing::DOWN;
}else{
$face = Facing::opposite($this->facing);
}
if(!$this->getSide($face)->isSolid()){
$this->position->getWorld()->useBreakOn($this->position); $this->position->getWorld()->useBreakOn($this->position);
} }
} }
public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ 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()->setBlock($this->position, $this);
$this->position->getWorld()->addSound( $this->position->getWorld()->addSound(
$this->position->add(0.5, 0.5, 0.5), $this->position->add(0.5, 0.5, 0.5),
$this->powered ? new RedstonePowerOnSound() : new RedstonePowerOffSound() $this->activated ? new RedstonePowerOnSound() : new RedstonePowerOffSound()
); );
return true; 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; }
}