block: added HorizontalFacingTrait and AnyFacingTrait

these are primarily intended for deduplication of code and ability to cross-reference. Don't expect this API to remain the same.
This commit is contained in:
Dylan K. Taylor 2020-08-05 21:17:03 +01:00
parent 060c300d50
commit 56ae3d01da
23 changed files with 131 additions and 72 deletions

View File

@ -27,6 +27,7 @@ use pocketmine\block\inventory\AnvilInventory;
use pocketmine\block\utils\BlockDataSerializer;
use pocketmine\block\utils\Fallable;
use pocketmine\block\utils\FallableTrait;
use pocketmine\block\utils\HorizontalFacingTrait;
use pocketmine\item\Item;
use pocketmine\item\ToolTier;
use pocketmine\math\AxisAlignedBB;
@ -37,9 +38,7 @@ use pocketmine\world\BlockTransaction;
class Anvil extends Transparent implements Fallable{
use FallableTrait;
/** @var int */
protected $facing = Facing::NORTH;
use HorizontalFacingTrait;
public function __construct(BlockIdentifier $idInfo, string $name, ?BlockBreakInfo $breakInfo = null){
parent::__construct($idInfo, $name, $breakInfo ?? new BlockBreakInfo(5.0, BlockToolType::PICKAXE, ToolTier::WOOD()->getHarvestLevel(), 6000.0));

View File

@ -26,6 +26,7 @@ namespace pocketmine\block;
use pocketmine\block\tile\Bed as TileBed;
use pocketmine\block\utils\BlockDataSerializer;
use pocketmine\block\utils\DyeColor;
use pocketmine\block\utils\HorizontalFacingTrait;
use pocketmine\data\bedrock\DyeColorIdMap;
use pocketmine\item\Bed as ItemBed;
use pocketmine\item\Item;
@ -40,9 +41,8 @@ use pocketmine\world\BlockTransaction;
use pocketmine\world\World;
class Bed extends Transparent{
use HorizontalFacingTrait;
/** @var int */
protected $facing = Facing::NORTH;
/** @var bool */
protected $occupied = false;
/** @var bool */

View File

@ -23,6 +23,7 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\utils\AnyFacingTrait;
use pocketmine\block\utils\BlockDataSerializer;
use pocketmine\item\Item;
use pocketmine\math\Facing;
@ -33,9 +34,8 @@ use pocketmine\world\sound\RedstonePowerOffSound;
use pocketmine\world\sound\RedstonePowerOnSound;
abstract class Button extends Flowable{
use AnyFacingTrait;
/** @var int */
protected $facing = Facing::DOWN;
/** @var bool */
protected $pressed = false;

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\utils\BlockDataSerializer;
use pocketmine\block\utils\HorizontalFacingTrait;
use pocketmine\item\Item;
use pocketmine\math\Facing;
use pocketmine\math\Vector3;
@ -31,9 +32,7 @@ use pocketmine\player\Player;
use pocketmine\world\BlockTransaction;
class CarvedPumpkin extends Opaque{
/** @var int */
protected $facing = Facing::NORTH;
use HorizontalFacingTrait;
public function readStateFromData(int $id, int $stateMeta) : void{
$this->facing = BlockDataSerializer::readLegacyHorizontalFacing($stateMeta & 0x03);

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\utils\BlockDataSerializer;
use pocketmine\block\utils\HorizontalFacingTrait;
use pocketmine\item\Item;
use pocketmine\math\Facing;
use pocketmine\math\Vector3;
@ -31,9 +32,7 @@ use pocketmine\player\Player;
use pocketmine\world\BlockTransaction;
final class ChemistryTable extends Opaque{
/** @var int */
private $facing = Facing::NORTH;
use HorizontalFacingTrait;
public function readStateFromData(int $id, int $stateMeta) : void{
$this->facing = Facing::opposite(BlockDataSerializer::readLegacyHorizontalFacing($stateMeta & 0x3));

View File

@ -25,6 +25,7 @@ namespace pocketmine\block;
use pocketmine\block\tile\Chest as TileChest;
use pocketmine\block\utils\BlockDataSerializer;
use pocketmine\block\utils\HorizontalFacingTrait;
use pocketmine\item\Item;
use pocketmine\math\AxisAlignedBB;
use pocketmine\math\Facing;
@ -33,9 +34,7 @@ use pocketmine\player\Player;
use pocketmine\world\BlockTransaction;
class Chest extends Transparent{
/** @var int */
protected $facing = Facing::NORTH;
use HorizontalFacingTrait;
public function __construct(BlockIdentifier $idInfo, string $name, ?BlockBreakInfo $breakInfo = null){
parent::__construct($idInfo, $name, $breakInfo ?? new BlockBreakInfo(2.5, BlockToolType::AXE));

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\utils\BlockDataSerializer;
use pocketmine\block\utils\HorizontalFacingTrait;
use pocketmine\block\utils\TreeType;
use pocketmine\item\Fertilizer;
use pocketmine\item\Item;
@ -36,9 +37,8 @@ use pocketmine\world\BlockTransaction;
use function mt_rand;
class CocoaBlock extends Transparent{
use HorizontalFacingTrait;
/** @var int */
protected $facing = Facing::NORTH;
/** @var int */
protected $age = 0;

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\utils\BlockDataSerializer;
use pocketmine\block\utils\HorizontalFacingTrait;
use pocketmine\item\Item;
use pocketmine\math\AxisAlignedBB;
use pocketmine\math\Facing;
@ -33,8 +34,8 @@ use pocketmine\world\BlockTransaction;
use pocketmine\world\sound\DoorSound;
class Door extends Transparent{
/** @var int */
protected $facing = Facing::NORTH;
use HorizontalFacingTrait;
/** @var bool */
protected $top = false;
/** @var bool */

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\utils\BlockDataSerializer;
use pocketmine\block\utils\HorizontalFacingTrait;
use pocketmine\item\Item;
use pocketmine\math\AxisAlignedBB;
use pocketmine\math\Facing;
@ -32,9 +33,8 @@ use pocketmine\player\Player;
use pocketmine\world\BlockTransaction;
class EndPortalFrame extends Opaque{
use HorizontalFacingTrait;
/** @var int */
protected $facing = Facing::NORTH;
/** @var bool */
protected $eye = false;

View File

@ -23,6 +23,7 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\utils\AnyFacingTrait;
use pocketmine\block\utils\BlockDataSerializer;
use pocketmine\item\Item;
use pocketmine\math\AxisAlignedBB;
@ -32,9 +33,7 @@ use pocketmine\player\Player;
use pocketmine\world\BlockTransaction;
class EndRod extends Flowable{
/** @var int */
protected $facing = Facing::DOWN;
use AnyFacingTrait;
public function __construct(BlockIdentifier $idInfo, string $name, ?BlockBreakInfo $breakInfo = null){
parent::__construct($idInfo, $name, $breakInfo ?? BlockBreakInfo::instant());

View File

@ -25,6 +25,7 @@ namespace pocketmine\block;
use pocketmine\block\tile\EnderChest as TileEnderChest;
use pocketmine\block\utils\BlockDataSerializer;
use pocketmine\block\utils\HorizontalFacingTrait;
use pocketmine\item\Item;
use pocketmine\item\ToolTier;
use pocketmine\math\AxisAlignedBB;
@ -34,9 +35,7 @@ use pocketmine\player\Player;
use pocketmine\world\BlockTransaction;
class EnderChest extends Transparent{
/** @var int */
protected $facing = Facing::NORTH;
use HorizontalFacingTrait;
public function __construct(BlockIdentifier $idInfo, string $name, ?BlockBreakInfo $breakInfo = null){
parent::__construct($idInfo, $name, $breakInfo ?? new BlockBreakInfo(22.5, BlockToolType::PICKAXE, ToolTier::WOOD()->getHarvestLevel(), 3000.0));

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\utils\BlockDataSerializer;
use pocketmine\block\utils\HorizontalFacingTrait;
use pocketmine\item\Item;
use pocketmine\math\AxisAlignedBB;
use pocketmine\math\Facing;
@ -33,10 +34,10 @@ use pocketmine\world\BlockTransaction;
use pocketmine\world\sound\DoorSound;
class FenceGate extends Transparent{
use HorizontalFacingTrait;
/** @var bool */
protected $open = false;
/** @var int */
protected $facing = Facing::NORTH;
/** @var bool */
protected $inWall = false;

View File

@ -25,6 +25,7 @@ namespace pocketmine\block;
use pocketmine\block\tile\Furnace as TileFurnace;
use pocketmine\block\utils\BlockDataSerializer;
use pocketmine\block\utils\HorizontalFacingTrait;
use pocketmine\item\Item;
use pocketmine\item\ToolTier;
use pocketmine\math\Facing;
@ -33,11 +34,11 @@ use pocketmine\player\Player;
use pocketmine\world\BlockTransaction;
class Furnace extends Opaque{
use HorizontalFacingTrait;
/** @var BlockIdentifierFlattened */
protected $idInfo;
/** @var int */
protected $facing = Facing::NORTH;
/** @var bool */
protected $lit = false; //this is set based on the blockID

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\utils\BlockDataSerializer;
use pocketmine\block\utils\HorizontalFacingTrait;
use pocketmine\item\Item;
use pocketmine\item\ToolTier;
use pocketmine\math\Facing;
@ -32,9 +33,7 @@ use pocketmine\player\Player;
use pocketmine\world\BlockTransaction;
class GlazedTerracotta extends Opaque{
/** @var int */
protected $facing = Facing::NORTH;
use HorizontalFacingTrait;
public function __construct(BlockIdentifier $idInfo, string $name, ?BlockBreakInfo $breakInfo = null){
parent::__construct($idInfo, $name, $breakInfo ?? new BlockBreakInfo(1.4, BlockToolType::PICKAXE, ToolTier::WOOD()->getHarvestLevel()));

View File

@ -25,6 +25,7 @@ namespace pocketmine\block;
use pocketmine\block\tile\ItemFrame as TileItemFrame;
use pocketmine\block\utils\BlockDataSerializer;
use pocketmine\block\utils\HorizontalFacingTrait;
use pocketmine\item\Item;
use pocketmine\math\Facing;
use pocketmine\math\Vector3;
@ -33,10 +34,10 @@ use pocketmine\world\BlockTransaction;
use function lcg_value;
class ItemFrame extends Flowable{
use HorizontalFacingTrait;
public const ROTATIONS = 8;
/** @var int */
protected $facing = Facing::NORTH;
/** @var bool */
protected $hasMap = false; //makes frame appear large if set
/** @var Item|null */
@ -86,14 +87,6 @@ class ItemFrame extends Flowable{
return 0b111;
}
public function getFacing() : int{
return $this->facing;
}
public function setFacing(int $facing) : void{
$this->facing = $facing;
}
public function getFramedItem() : ?Item{
return $this->framedItem !== null ? clone $this->framedItem : null;
}

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\utils\BlockDataSerializer;
use pocketmine\block\utils\HorizontalFacingTrait;
use pocketmine\entity\Entity;
use pocketmine\entity\Living;
use pocketmine\item\Item;
@ -34,9 +35,7 @@ use pocketmine\player\Player;
use pocketmine\world\BlockTransaction;
class Ladder extends Transparent{
/** @var int */
protected $facing = Facing::NORTH;
use HorizontalFacingTrait;
public function __construct(BlockIdentifier $idInfo, string $name, ?BlockBreakInfo $breakInfo = null){
parent::__construct($idInfo, $name, $breakInfo ?? new BlockBreakInfo(0.4, BlockToolType::AXE));

View File

@ -25,6 +25,7 @@ namespace pocketmine\block;
use pocketmine\block\tile\Comparator;
use pocketmine\block\utils\BlockDataSerializer;
use pocketmine\block\utils\HorizontalFacingTrait;
use pocketmine\item\Item;
use pocketmine\math\AxisAlignedBB;
use pocketmine\math\Facing;
@ -34,11 +35,11 @@ use pocketmine\world\BlockTransaction;
use function assert;
class RedstoneComparator extends Flowable{
use HorizontalFacingTrait;
/** @var BlockIdentifierFlattened */
protected $idInfo;
/** @var int */
protected $facing = Facing::NORTH;
/** @var bool */
protected $isSubtractMode = false;
/** @var bool */
@ -85,20 +86,6 @@ class RedstoneComparator extends Flowable{
$tile->setSignalStrength($this->signalStrength);
}
/**
* TODO: ad hoc, move to interface
*/
public function getFacing() : int{
return $this->facing;
}
/**
* TODO: ad hoc, move to interface
*/
public function setFacing(int $facing) : void{
$this->facing = $facing;
}
public function isSubtractMode() : bool{
return $this->isSubtractMode;
}

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\utils\BlockDataSerializer;
use pocketmine\block\utils\HorizontalFacingTrait;
use pocketmine\item\Item;
use pocketmine\math\AxisAlignedBB;
use pocketmine\math\Facing;
@ -32,14 +33,14 @@ use pocketmine\player\Player;
use pocketmine\world\BlockTransaction;
class RedstoneRepeater extends Flowable{
use HorizontalFacingTrait;
/** @var BlockIdentifierFlattened */
protected $idInfo;
/** @var bool */
protected $powered = false;
/** @var int */
protected $facing = Facing::NORTH;
/** @var int */
protected $delay = 1;
public function __construct(BlockIdentifierFlattened $idInfo, string $name, ?BlockBreakInfo $breakInfo = null){

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\utils\BlockDataSerializer;
use pocketmine\block\utils\HorizontalFacingTrait;
use pocketmine\block\utils\StairShape;
use pocketmine\item\Item;
use pocketmine\math\AxisAlignedBB;
@ -33,9 +34,8 @@ use pocketmine\player\Player;
use pocketmine\world\BlockTransaction;
class Stair extends Transparent{
use HorizontalFacingTrait;
/** @var int */
protected $facing = Facing::NORTH;
/** @var bool */
protected $upsideDown = false;

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\utils\BlockDataSerializer;
use pocketmine\block\utils\HorizontalFacingTrait;
use pocketmine\item\Item;
use pocketmine\math\AxisAlignedBB;
use pocketmine\math\Facing;
@ -33,9 +34,8 @@ use pocketmine\world\BlockTransaction;
use pocketmine\world\sound\DoorSound;
class Trapdoor extends Transparent{
use HorizontalFacingTrait;
/** @var int */
protected $facing = Facing::NORTH;
/** @var bool */
protected $open = false;
/** @var bool */

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\utils\BlockDataSerializer;
use pocketmine\block\utils\HorizontalFacingTrait;
use pocketmine\item\Item;
use pocketmine\math\Facing;
use pocketmine\math\Vector3;
@ -31,9 +32,8 @@ use pocketmine\player\Player;
use pocketmine\world\BlockTransaction;
class TripwireHook extends Flowable{
use HorizontalFacingTrait;
/** @var int */
protected $facing = Facing::NORTH;
/** @var bool */
protected $connected = false;
/** @var bool */

View File

@ -0,0 +1,40 @@
<?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;
trait AnyFacingTrait{
/** @var int */
protected $facing = Facing::DOWN;
public function getFacing() : int{ return $this->facing; }
/** @return $this */
public function setFacing(int $facing) : self{
Facing::validate($this->facing);
$this->facing = $facing;
return $this;
}
}

View File

@ -0,0 +1,43 @@
<?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 function in_array;
trait HorizontalFacingTrait{
/** @var int */
protected $facing = Facing::NORTH;
public function getFacing() : int{ return $this->facing; }
/** @return $this */
public function setFacing(int $facing) : self{
if(!in_array($facing, Facing::HORIZONTAL, true)){
throw new \InvalidArgumentException("Facing must be horizontal");
}
$this->facing = $facing;
return $this;
}
}