From d60fca0a1c33ace9d158335fb9c5a9d7c08540c1 Mon Sep 17 00:00:00 2001 From: ShockedPlot7560 Date: Fri, 8 Sep 2023 13:25:26 +0200 Subject: [PATCH] Age blocks logic moved into dedicated trait (#5962) --- src/block/Cactus.php | 21 ++----------- src/block/CaveVines.php | 17 ++-------- src/block/ChorusFlower.php | 24 +++------------ src/block/CocoaBlock.php | 15 ++------- src/block/Crops.php | 28 +++++------------ src/block/Fire.php | 21 ++----------- src/block/FrostedIce.php | 21 ++----------- src/block/NetherVines.php | 24 ++------------- src/block/NetherWartPlant.php | 21 ++----------- src/block/Sugarcane.php | 21 ++----------- src/block/SweetBerryBush.php | 22 +++---------- src/block/utils/AgeableTrait.php | 53 ++++++++++++++++++++++++++++++++ 12 files changed, 92 insertions(+), 196 deletions(-) create mode 100644 src/block/utils/AgeableTrait.php diff --git a/src/block/Cactus.php b/src/block/Cactus.php index 81d94474b..046affe72 100644 --- a/src/block/Cactus.php +++ b/src/block/Cactus.php @@ -23,9 +23,9 @@ declare(strict_types=1); namespace pocketmine\block; +use pocketmine\block\utils\AgeableTrait; use pocketmine\block\utils\BlockEventHelper; use pocketmine\block\utils\SupportType; -use pocketmine\data\runtime\RuntimeDataDescriber; use pocketmine\entity\Entity; use pocketmine\event\entity\EntityDamageByBlockEvent; use pocketmine\event\entity\EntityDamageEvent; @@ -37,25 +37,10 @@ use pocketmine\player\Player; use pocketmine\world\BlockTransaction; class Cactus extends Transparent{ + use AgeableTrait; + public const MAX_AGE = 15; - protected int $age = 0; - - protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{ - $w->boundedInt(4, 0, self::MAX_AGE, $this->age); - } - - public function getAge() : int{ return $this->age; } - - /** @return $this */ - public function setAge(int $age) : self{ - if($age < 0 || $age > self::MAX_AGE){ - throw new \InvalidArgumentException("Age must be in range 0 ... " . self::MAX_AGE); - } - $this->age = $age; - return $this; - } - public function hasEntityCollision() : bool{ return true; } diff --git a/src/block/CaveVines.php b/src/block/CaveVines.php index 18e820bfa..5f9c4c610 100644 --- a/src/block/CaveVines.php +++ b/src/block/CaveVines.php @@ -23,6 +23,7 @@ declare(strict_types=1); namespace pocketmine\block; +use pocketmine\block\utils\AgeableTrait; use pocketmine\block\utils\BlockEventHelper; use pocketmine\block\utils\SupportType; use pocketmine\data\runtime\RuntimeDataDescriber; @@ -38,9 +39,10 @@ use pocketmine\world\sound\GlowBerriesPickSound; use function mt_rand; class CaveVines extends Flowable{ + use AgeableTrait; + public const MAX_AGE = 25; - protected int $age = 0; protected bool $berries = false; protected bool $head = false; @@ -66,19 +68,6 @@ class CaveVines extends Flowable{ return $this; } - public function getAge() : int{ - return $this->age; - } - - /** @return $this */ - public function setAge(int $age) : self{ - if($age < 0 || $age > self::MAX_AGE){ - throw new \InvalidArgumentException("Age must be in range 0-" . self::MAX_AGE); - } - $this->age = $age; - return $this; - } - public function canClimb() : bool{ return true; } diff --git a/src/block/ChorusFlower.php b/src/block/ChorusFlower.php index 5c5077f22..74366a68e 100644 --- a/src/block/ChorusFlower.php +++ b/src/block/ChorusFlower.php @@ -23,7 +23,7 @@ declare(strict_types=1); namespace pocketmine\block; -use pocketmine\data\runtime\RuntimeDataDescriber; +use pocketmine\block\utils\AgeableTrait; use pocketmine\entity\projectile\Projectile; use pocketmine\event\block\StructureGrowEvent; use pocketmine\item\Item; @@ -39,31 +39,17 @@ use pocketmine\world\sound\ChorusFlowerDieSound; use pocketmine\world\sound\ChorusFlowerGrowSound; use pocketmine\world\World; use function array_rand; +use function min; use function mt_rand; final class ChorusFlower extends Flowable{ + use AgeableTrait; + public const MIN_AGE = 0; public const MAX_AGE = 5; private const MAX_STEM_HEIGHT = 5; - private int $age = self::MIN_AGE; - - protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{ - $w->boundedInt(3, self::MIN_AGE, self::MAX_AGE, $this->age); - } - - public function getAge() : int{ return $this->age; } - - /** @return $this */ - public function setAge(int $age) : self{ - if($age < self::MIN_AGE || $age > self::MAX_AGE){ - throw new \InvalidArgumentException("Age must be in the range " . self::MIN_AGE . " ... " . self::MAX_AGE); - } - $this->age = $age; - return $this; - } - protected function recalculateCollisionBoxes() : array{ return [AxisAlignedBB::one()]; } @@ -181,7 +167,7 @@ final class ChorusFlower extends Flowable{ if($tx === null){ $tx = new BlockTransaction($this->position->getWorld()); } - $tx->addBlock($this->position->getSide($facing), (clone $this)->setAge($this->getAge() + $ageChange)); + $tx->addBlock($this->position->getSide($facing), (clone $this)->setAge(min(self::MAX_AGE, $this->getAge() + $ageChange))); return $tx; } diff --git a/src/block/CocoaBlock.php b/src/block/CocoaBlock.php index fcd9f4e0f..a76514249 100644 --- a/src/block/CocoaBlock.php +++ b/src/block/CocoaBlock.php @@ -23,6 +23,7 @@ declare(strict_types=1); namespace pocketmine\block; +use pocketmine\block\utils\AgeableTrait; use pocketmine\block\utils\BlockEventHelper; use pocketmine\block\utils\HorizontalFacingTrait; use pocketmine\block\utils\SupportType; @@ -41,27 +42,15 @@ use function mt_rand; class CocoaBlock extends Transparent{ use HorizontalFacingTrait; + use AgeableTrait; public const MAX_AGE = 2; - protected int $age = 0; - protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{ $w->horizontalFacing($this->facing); $w->boundedInt(2, 0, self::MAX_AGE, $this->age); } - public function getAge() : int{ return $this->age; } - - /** @return $this */ - public function setAge(int $age) : self{ - if($age < 0 || $age > self::MAX_AGE){ - throw new \InvalidArgumentException("Age must be in range 0 ... " . self::MAX_AGE); - } - $this->age = $age; - return $this; - } - /** * @return AxisAlignedBB[] */ diff --git a/src/block/Crops.php b/src/block/Crops.php index d6e84c424..87966f1db 100644 --- a/src/block/Crops.php +++ b/src/block/Crops.php @@ -23,8 +23,8 @@ declare(strict_types=1); namespace pocketmine\block; +use pocketmine\block\utils\AgeableTrait; use pocketmine\block\utils\BlockEventHelper; -use pocketmine\data\runtime\RuntimeDataDescriber; use pocketmine\item\Fertilizer; use pocketmine\item\Item; use pocketmine\math\Facing; @@ -34,25 +34,10 @@ use pocketmine\world\BlockTransaction; use function mt_rand; abstract class Crops extends Flowable{ + use AgeableTrait; + public const MAX_AGE = 7; - protected int $age = 0; - - protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{ - $w->boundedInt(3, 0, self::MAX_AGE, $this->age); - } - - public function getAge() : int{ return $this->age; } - - /** @return $this */ - public function setAge(int $age) : self{ - if($age < 0 || $age > self::MAX_AGE){ - throw new \InvalidArgumentException("Age must be in range 0 ... " . self::MAX_AGE); - } - $this->age = $age; - return $this; - } - public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ if($blockReplace->getSide(Facing::DOWN)->getTypeId() === BlockTypeIds::FARMLAND){ return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player); @@ -64,10 +49,11 @@ abstract class Crops extends Flowable{ public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{ if($this->age < self::MAX_AGE && $item instanceof Fertilizer){ $block = clone $this; - $block->age += mt_rand(2, 5); - if($block->age > self::MAX_AGE){ - $block->age = self::MAX_AGE; + $tempAge = $block->age + mt_rand(2, 5); + if($tempAge > self::MAX_AGE){ + $tempAge = self::MAX_AGE; } + $block->age = $tempAge; if(BlockEventHelper::grow($this, $block, $player)){ $item->pop(); } diff --git a/src/block/Fire.php b/src/block/Fire.php index fbe765432..5487c34ed 100644 --- a/src/block/Fire.php +++ b/src/block/Fire.php @@ -23,9 +23,9 @@ declare(strict_types=1); namespace pocketmine\block; +use pocketmine\block\utils\AgeableTrait; use pocketmine\block\utils\BlockEventHelper; use pocketmine\block\utils\SupportType; -use pocketmine\data\runtime\RuntimeDataDescriber; use pocketmine\event\block\BlockBurnEvent; use pocketmine\math\Facing; use pocketmine\world\format\Chunk; @@ -36,25 +36,10 @@ use function min; use function mt_rand; class Fire extends BaseFire{ + use AgeableTrait; + public const MAX_AGE = 15; - protected int $age = 0; - - protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{ - $w->boundedInt(4, 0, self::MAX_AGE, $this->age); - } - - public function getAge() : int{ return $this->age; } - - /** @return $this */ - public function setAge(int $age) : self{ - if($age < 0 || $age > self::MAX_AGE){ - throw new \InvalidArgumentException("Age must be in range 0 ... " . self::MAX_AGE); - } - $this->age = $age; - return $this; - } - protected function getFireDamage() : int{ return 1; } diff --git a/src/block/FrostedIce.php b/src/block/FrostedIce.php index 039fe49f3..3e8592306 100644 --- a/src/block/FrostedIce.php +++ b/src/block/FrostedIce.php @@ -23,30 +23,15 @@ declare(strict_types=1); namespace pocketmine\block; +use pocketmine\block\utils\AgeableTrait; use pocketmine\block\utils\BlockEventHelper; -use pocketmine\data\runtime\RuntimeDataDescriber; use function mt_rand; class FrostedIce extends Ice{ + use AgeableTrait; + public const MAX_AGE = 3; - protected int $age = 0; - - protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{ - $w->boundedInt(2, 0, self::MAX_AGE, $this->age); - } - - public function getAge() : int{ return $this->age; } - - /** @return $this */ - public function setAge(int $age) : self{ - if($age < 0 || $age > self::MAX_AGE){ - throw new \InvalidArgumentException("Age must be in range 0 ... " . self::MAX_AGE); - } - $this->age = $age; - return $this; - } - public function onNearbyBlockChange() : void{ $this->position->getWorld()->scheduleDelayedBlockUpdate($this->position, mt_rand(20, 40)); } diff --git a/src/block/NetherVines.php b/src/block/NetherVines.php index 968a9b92e..ac04e05e3 100644 --- a/src/block/NetherVines.php +++ b/src/block/NetherVines.php @@ -23,9 +23,9 @@ declare(strict_types=1); namespace pocketmine\block; +use pocketmine\block\utils\AgeableTrait; use pocketmine\block\utils\FortuneDropHelper; use pocketmine\block\utils\SupportType; -use pocketmine\data\runtime\RuntimeDataDescriber; use pocketmine\entity\Entity; use pocketmine\event\block\StructureGrowEvent; use pocketmine\item\Fertilizer; @@ -41,36 +41,18 @@ use function mt_rand; * This class is used for Weeping & Twisting vines, because they have same behaviour */ class NetherVines extends Flowable{ + use AgeableTrait; + public const MAX_AGE = 25; /** Direction the vine grows towards. */ private int $growthFace; - protected int $age = 0; - public function __construct(BlockIdentifier $idInfo, string $name, BlockTypeInfo $typeInfo, int $growthFace){ $this->growthFace = $growthFace; parent::__construct($idInfo, $name, $typeInfo); } - protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{ - $w->boundedInt(5, 0, self::MAX_AGE, $this->age); - } - - public function getAge() : int{ - return $this->age; - } - - /** @return $this */ - public function setAge(int $age) : self{ - if($age < 0 || $age > self::MAX_AGE){ - throw new \InvalidArgumentException("Age must be in range 0-" . self::MAX_AGE); - } - - $this->age = $age; - return $this; - } - public function isAffectedBySilkTouch() : bool{ return true; } diff --git a/src/block/NetherWartPlant.php b/src/block/NetherWartPlant.php index d7e587441..b6c1d847d 100644 --- a/src/block/NetherWartPlant.php +++ b/src/block/NetherWartPlant.php @@ -23,9 +23,9 @@ declare(strict_types=1); namespace pocketmine\block; +use pocketmine\block\utils\AgeableTrait; use pocketmine\block\utils\BlockEventHelper; use pocketmine\block\utils\FortuneDropHelper; -use pocketmine\data\runtime\RuntimeDataDescriber; use pocketmine\item\Item; use pocketmine\math\Facing; use pocketmine\math\Vector3; @@ -34,25 +34,10 @@ use pocketmine\world\BlockTransaction; use function mt_rand; class NetherWartPlant extends Flowable{ + use AgeableTrait; + public const MAX_AGE = 3; - protected int $age = 0; - - protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{ - $w->boundedInt(2, 0, self::MAX_AGE, $this->age); - } - - public function getAge() : int{ return $this->age; } - - /** @return $this */ - public function setAge(int $age) : self{ - if($age < 0 || $age > self::MAX_AGE){ - throw new \InvalidArgumentException("Age must be in range 0 ..." . self::MAX_AGE); - } - $this->age = $age; - return $this; - } - public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ $down = $this->getSide(Facing::DOWN); if($down->getTypeId() === BlockTypeIds::SOUL_SAND){ diff --git a/src/block/Sugarcane.php b/src/block/Sugarcane.php index 3757e4577..d2abe2c95 100644 --- a/src/block/Sugarcane.php +++ b/src/block/Sugarcane.php @@ -23,8 +23,8 @@ declare(strict_types=1); namespace pocketmine\block; +use pocketmine\block\utils\AgeableTrait; use pocketmine\block\utils\BlockEventHelper; -use pocketmine\data\runtime\RuntimeDataDescriber; use pocketmine\item\Fertilizer; use pocketmine\item\Item; use pocketmine\math\Facing; @@ -34,14 +34,10 @@ use pocketmine\world\BlockTransaction; use pocketmine\world\Position; class Sugarcane extends Flowable{ + use AgeableTrait; + public const MAX_AGE = 15; - protected int $age = 0; - - protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{ - $w->boundedInt(4, 0, self::MAX_AGE, $this->age); - } - private function seekToBottom() : Position{ $world = $this->position->getWorld(); $bottom = $this->position; @@ -74,17 +70,6 @@ class Sugarcane extends Flowable{ return $grew; } - public function getAge() : int{ return $this->age; } - - /** @return $this */ - public function setAge(int $age) : self{ - if($age < 0 || $age > self::MAX_AGE){ - throw new \InvalidArgumentException("Age must be in range 0 ... " . self::MAX_AGE); - } - $this->age = $age; - return $this; - } - public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{ if($item instanceof Fertilizer){ if($this->grow($this->seekToBottom(), $player)){ diff --git a/src/block/SweetBerryBush.php b/src/block/SweetBerryBush.php index ef1169df5..bd3cf2871 100644 --- a/src/block/SweetBerryBush.php +++ b/src/block/SweetBerryBush.php @@ -23,9 +23,9 @@ declare(strict_types=1); namespace pocketmine\block; +use pocketmine\block\utils\AgeableTrait; use pocketmine\block\utils\BlockEventHelper; use pocketmine\block\utils\FortuneDropHelper; -use pocketmine\data\runtime\RuntimeDataDescriber; use pocketmine\entity\Entity; use pocketmine\entity\Living; use pocketmine\event\entity\EntityDamageByBlockEvent; @@ -39,27 +39,13 @@ use pocketmine\world\BlockTransaction; use function mt_rand; class SweetBerryBush extends Flowable{ + use AgeableTrait; + public const STAGE_SAPLING = 0; public const STAGE_BUSH_NO_BERRIES = 1; public const STAGE_BUSH_SOME_BERRIES = 2; public const STAGE_MATURE = 3; - - protected int $age = self::STAGE_SAPLING; - - protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{ - $w->boundedInt(3, self::STAGE_SAPLING, self::STAGE_MATURE, $this->age); - } - - public function getAge() : int{ return $this->age; } - - /** @return $this */ - public function setAge(int $age) : self{ - if($age < self::STAGE_SAPLING || $age > self::STAGE_MATURE){ - throw new \InvalidArgumentException("Age must be in range 0-3"); - } - $this->age = $age; - return $this; - } + public const MAX_AGE = self::STAGE_MATURE; public function getBerryDropAmount() : int{ if($this->age === self::STAGE_MATURE){ diff --git a/src/block/utils/AgeableTrait.php b/src/block/utils/AgeableTrait.php new file mode 100644 index 000000000..6ef1ce826 --- /dev/null +++ b/src/block/utils/AgeableTrait.php @@ -0,0 +1,53 @@ +boundedInt((int) ceil(log(self::MAX_AGE, 2)), 0, self::MAX_AGE, $this->age); + } + + public function getAge() : int{ return $this->age; } + + /** + * @return $this + */ + public function setAge(int $age) : self{ + if($age < 0 || $age > self::MAX_AGE){ + throw new \InvalidArgumentException("Age must be in range 0 ... " . self::MAX_AGE); + } + $this->age = $age; + return $this; + } +}