Added AnalogRedstoneSignalEmitterTrait

This commit is contained in:
Dylan K. Taylor 2021-02-06 18:33:14 +00:00
parent 02b0036cbe
commit 48ef8771cd
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
5 changed files with 52 additions and 25 deletions

View File

@ -23,6 +23,7 @@ declare(strict_types=1);
namespace pocketmine\block; namespace pocketmine\block;
use pocketmine\block\utils\AnalogRedstoneSignalEmitterTrait;
use pocketmine\block\utils\BlockDataSerializer; use pocketmine\block\utils\BlockDataSerializer;
use pocketmine\item\Item; use pocketmine\item\Item;
use pocketmine\math\AxisAlignedBB; use pocketmine\math\AxisAlignedBB;
@ -35,12 +36,11 @@ use function round;
use const M_PI; use const M_PI;
class DaylightSensor extends Transparent{ class DaylightSensor extends Transparent{
use AnalogRedstoneSignalEmitterTrait;
/** @var BlockIdentifierFlattened */ /** @var BlockIdentifierFlattened */
protected $idInfo; protected $idInfo;
/** @var int */
protected $signalStrength = 0;
/** @var bool */ /** @var bool */
protected $inverted = false; protected $inverted = false;

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace pocketmine\block; namespace pocketmine\block;
use pocketmine\block\tile\Comparator; use pocketmine\block\tile\Comparator;
use pocketmine\block\utils\AnalogRedstoneSignalEmitterTrait;
use pocketmine\block\utils\BlockDataSerializer; use pocketmine\block\utils\BlockDataSerializer;
use pocketmine\block\utils\HorizontalFacingTrait; use pocketmine\block\utils\HorizontalFacingTrait;
use pocketmine\item\Item; use pocketmine\item\Item;
@ -36,6 +37,7 @@ use function assert;
class RedstoneComparator extends Flowable{ class RedstoneComparator extends Flowable{
use HorizontalFacingTrait; use HorizontalFacingTrait;
use AnalogRedstoneSignalEmitterTrait;
/** @var BlockIdentifierFlattened */ /** @var BlockIdentifierFlattened */
protected $idInfo; protected $idInfo;
@ -44,8 +46,6 @@ class RedstoneComparator extends Flowable{
protected $isSubtractMode = false; protected $isSubtractMode = false;
/** @var bool */ /** @var bool */
protected $powered = false; protected $powered = false;
/** @var int */
protected $signalStrength = 0;
public function __construct(BlockIdentifierFlattened $idInfo, string $name, ?BlockBreakInfo $breakInfo = null){ public function __construct(BlockIdentifierFlattened $idInfo, string $name, ?BlockBreakInfo $breakInfo = null){
parent::__construct($idInfo, $name, $breakInfo ?? BlockBreakInfo::instant()); parent::__construct($idInfo, $name, $breakInfo ?? BlockBreakInfo::instant());
@ -106,16 +106,6 @@ class RedstoneComparator extends Flowable{
return $this; return $this;
} }
public function getSignalStrength() : int{
return $this->signalStrength;
}
/** @return $this */
public function setSignalStrength(int $signalStrength) : self{
$this->signalStrength = $signalStrength;
return $this;
}
/** /**
* @return AxisAlignedBB[] * @return AxisAlignedBB[]
*/ */

View File

@ -23,23 +23,22 @@ declare(strict_types=1);
namespace pocketmine\block; namespace pocketmine\block;
use pocketmine\block\utils\AnalogRedstoneSignalEmitterTrait;
use pocketmine\block\utils\BlockDataSerializer; use pocketmine\block\utils\BlockDataSerializer;
class RedstoneWire extends Flowable{ class RedstoneWire extends Flowable{
use AnalogRedstoneSignalEmitterTrait;
/** @var int */
protected $power = 0;
public function __construct(BlockIdentifier $idInfo, string $name, ?BlockBreakInfo $breakInfo = null){ public function __construct(BlockIdentifier $idInfo, string $name, ?BlockBreakInfo $breakInfo = null){
parent::__construct($idInfo, $name, $breakInfo ?? BlockBreakInfo::instant()); parent::__construct($idInfo, $name, $breakInfo ?? BlockBreakInfo::instant());
} }
public function readStateFromData(int $id, int $stateMeta) : void{ public function readStateFromData(int $id, int $stateMeta) : void{
$this->power = BlockDataSerializer::readBoundedInt("power", $stateMeta, 0, 15); $this->signalStrength = BlockDataSerializer::readBoundedInt("signalStrength", $stateMeta, 0, 15);
} }
protected function writeStateToMeta() : int{ protected function writeStateToMeta() : int{
return $this->power; return $this->signalStrength;
} }
public function getStateBitmask() : int{ public function getStateBitmask() : int{

View File

@ -23,19 +23,18 @@ declare(strict_types=1);
namespace pocketmine\block; namespace pocketmine\block;
use pocketmine\block\utils\AnalogRedstoneSignalEmitterTrait;
use pocketmine\block\utils\BlockDataSerializer; use pocketmine\block\utils\BlockDataSerializer;
abstract class WeightedPressurePlate extends PressurePlate{ abstract class WeightedPressurePlate extends PressurePlate{
use AnalogRedstoneSignalEmitterTrait;
/** @var int */
protected $power = 0;
protected function writeStateToMeta() : int{ protected function writeStateToMeta() : int{
return $this->power; return $this->signalStrength;
} }
public function readStateFromData(int $id, int $stateMeta) : void{ public function readStateFromData(int $id, int $stateMeta) : void{
$this->power = BlockDataSerializer::readBoundedInt("power", $stateMeta, 0, 15); $this->signalStrength = BlockDataSerializer::readBoundedInt("signalStrength", $stateMeta, 0, 15);
} }
public function getStateBitmask() : int{ public function getStateBitmask() : int{

View File

@ -0,0 +1,39 @@
<?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;
trait AnalogRedstoneSignalEmitterTrait{
protected int $signalStrength = 0;
public function getOutputSignalStrength() : int{ return $this->signalStrength; }
/** @return $this */
public function setOutputSignalStrength(int $signalStrength) : self{
if($signalStrength < 0 || $signalStrength > 15){
throw new \InvalidArgumentException("Signal strength must be in range 0-15");
}
$this->signalStrength = $signalStrength;
return $this;
}
}