Age blocks logic moved into dedicated trait (#5962)

This commit is contained in:
ShockedPlot7560 2023-09-08 13:25:26 +02:00 committed by GitHub
parent d3ab516ba4
commit d60fca0a1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 92 additions and 196 deletions

View File

@ -23,9 +23,9 @@ declare(strict_types=1);
namespace pocketmine\block; namespace pocketmine\block;
use pocketmine\block\utils\AgeableTrait;
use pocketmine\block\utils\BlockEventHelper; use pocketmine\block\utils\BlockEventHelper;
use pocketmine\block\utils\SupportType; use pocketmine\block\utils\SupportType;
use pocketmine\data\runtime\RuntimeDataDescriber;
use pocketmine\entity\Entity; use pocketmine\entity\Entity;
use pocketmine\event\entity\EntityDamageByBlockEvent; use pocketmine\event\entity\EntityDamageByBlockEvent;
use pocketmine\event\entity\EntityDamageEvent; use pocketmine\event\entity\EntityDamageEvent;
@ -37,25 +37,10 @@ use pocketmine\player\Player;
use pocketmine\world\BlockTransaction; use pocketmine\world\BlockTransaction;
class Cactus extends Transparent{ class Cactus extends Transparent{
use AgeableTrait;
public const MAX_AGE = 15; 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{ public function hasEntityCollision() : bool{
return true; return true;
} }

View File

@ -23,6 +23,7 @@ declare(strict_types=1);
namespace pocketmine\block; namespace pocketmine\block;
use pocketmine\block\utils\AgeableTrait;
use pocketmine\block\utils\BlockEventHelper; use pocketmine\block\utils\BlockEventHelper;
use pocketmine\block\utils\SupportType; use pocketmine\block\utils\SupportType;
use pocketmine\data\runtime\RuntimeDataDescriber; use pocketmine\data\runtime\RuntimeDataDescriber;
@ -38,9 +39,10 @@ use pocketmine\world\sound\GlowBerriesPickSound;
use function mt_rand; use function mt_rand;
class CaveVines extends Flowable{ class CaveVines extends Flowable{
use AgeableTrait;
public const MAX_AGE = 25; public const MAX_AGE = 25;
protected int $age = 0;
protected bool $berries = false; protected bool $berries = false;
protected bool $head = false; protected bool $head = false;
@ -66,19 +68,6 @@ class CaveVines extends Flowable{
return $this; 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{ public function canClimb() : bool{
return true; return true;
} }

View File

@ -23,7 +23,7 @@ declare(strict_types=1);
namespace pocketmine\block; namespace pocketmine\block;
use pocketmine\data\runtime\RuntimeDataDescriber; use pocketmine\block\utils\AgeableTrait;
use pocketmine\entity\projectile\Projectile; use pocketmine\entity\projectile\Projectile;
use pocketmine\event\block\StructureGrowEvent; use pocketmine\event\block\StructureGrowEvent;
use pocketmine\item\Item; use pocketmine\item\Item;
@ -39,31 +39,17 @@ use pocketmine\world\sound\ChorusFlowerDieSound;
use pocketmine\world\sound\ChorusFlowerGrowSound; use pocketmine\world\sound\ChorusFlowerGrowSound;
use pocketmine\world\World; use pocketmine\world\World;
use function array_rand; use function array_rand;
use function min;
use function mt_rand; use function mt_rand;
final class ChorusFlower extends Flowable{ final class ChorusFlower extends Flowable{
use AgeableTrait;
public const MIN_AGE = 0; public const MIN_AGE = 0;
public const MAX_AGE = 5; public const MAX_AGE = 5;
private const MAX_STEM_HEIGHT = 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{ protected function recalculateCollisionBoxes() : array{
return [AxisAlignedBB::one()]; return [AxisAlignedBB::one()];
} }
@ -181,7 +167,7 @@ final class ChorusFlower extends Flowable{
if($tx === null){ if($tx === null){
$tx = new BlockTransaction($this->position->getWorld()); $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; return $tx;
} }

View File

@ -23,6 +23,7 @@ declare(strict_types=1);
namespace pocketmine\block; namespace pocketmine\block;
use pocketmine\block\utils\AgeableTrait;
use pocketmine\block\utils\BlockEventHelper; use pocketmine\block\utils\BlockEventHelper;
use pocketmine\block\utils\HorizontalFacingTrait; use pocketmine\block\utils\HorizontalFacingTrait;
use pocketmine\block\utils\SupportType; use pocketmine\block\utils\SupportType;
@ -41,27 +42,15 @@ use function mt_rand;
class CocoaBlock extends Transparent{ class CocoaBlock extends Transparent{
use HorizontalFacingTrait; use HorizontalFacingTrait;
use AgeableTrait;
public const MAX_AGE = 2; public const MAX_AGE = 2;
protected int $age = 0;
protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{ protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
$w->horizontalFacing($this->facing); $w->horizontalFacing($this->facing);
$w->boundedInt(2, 0, self::MAX_AGE, $this->age); $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[] * @return AxisAlignedBB[]
*/ */

View File

@ -23,8 +23,8 @@ declare(strict_types=1);
namespace pocketmine\block; namespace pocketmine\block;
use pocketmine\block\utils\AgeableTrait;
use pocketmine\block\utils\BlockEventHelper; use pocketmine\block\utils\BlockEventHelper;
use pocketmine\data\runtime\RuntimeDataDescriber;
use pocketmine\item\Fertilizer; use pocketmine\item\Fertilizer;
use pocketmine\item\Item; use pocketmine\item\Item;
use pocketmine\math\Facing; use pocketmine\math\Facing;
@ -34,25 +34,10 @@ use pocketmine\world\BlockTransaction;
use function mt_rand; use function mt_rand;
abstract class Crops extends Flowable{ abstract class Crops extends Flowable{
use AgeableTrait;
public const MAX_AGE = 7; 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{ 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){ if($blockReplace->getSide(Facing::DOWN)->getTypeId() === BlockTypeIds::FARMLAND){
return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player); 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{ public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
if($this->age < self::MAX_AGE && $item instanceof Fertilizer){ if($this->age < self::MAX_AGE && $item instanceof Fertilizer){
$block = clone $this; $block = clone $this;
$block->age += mt_rand(2, 5); $tempAge = $block->age + mt_rand(2, 5);
if($block->age > self::MAX_AGE){ if($tempAge > self::MAX_AGE){
$block->age = self::MAX_AGE; $tempAge = self::MAX_AGE;
} }
$block->age = $tempAge;
if(BlockEventHelper::grow($this, $block, $player)){ if(BlockEventHelper::grow($this, $block, $player)){
$item->pop(); $item->pop();
} }

View File

@ -23,9 +23,9 @@ declare(strict_types=1);
namespace pocketmine\block; namespace pocketmine\block;
use pocketmine\block\utils\AgeableTrait;
use pocketmine\block\utils\BlockEventHelper; use pocketmine\block\utils\BlockEventHelper;
use pocketmine\block\utils\SupportType; use pocketmine\block\utils\SupportType;
use pocketmine\data\runtime\RuntimeDataDescriber;
use pocketmine\event\block\BlockBurnEvent; use pocketmine\event\block\BlockBurnEvent;
use pocketmine\math\Facing; use pocketmine\math\Facing;
use pocketmine\world\format\Chunk; use pocketmine\world\format\Chunk;
@ -36,25 +36,10 @@ use function min;
use function mt_rand; use function mt_rand;
class Fire extends BaseFire{ class Fire extends BaseFire{
use AgeableTrait;
public const MAX_AGE = 15; 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{ protected function getFireDamage() : int{
return 1; return 1;
} }

View File

@ -23,30 +23,15 @@ declare(strict_types=1);
namespace pocketmine\block; namespace pocketmine\block;
use pocketmine\block\utils\AgeableTrait;
use pocketmine\block\utils\BlockEventHelper; use pocketmine\block\utils\BlockEventHelper;
use pocketmine\data\runtime\RuntimeDataDescriber;
use function mt_rand; use function mt_rand;
class FrostedIce extends Ice{ class FrostedIce extends Ice{
use AgeableTrait;
public const MAX_AGE = 3; 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{ public function onNearbyBlockChange() : void{
$this->position->getWorld()->scheduleDelayedBlockUpdate($this->position, mt_rand(20, 40)); $this->position->getWorld()->scheduleDelayedBlockUpdate($this->position, mt_rand(20, 40));
} }

View File

@ -23,9 +23,9 @@ declare(strict_types=1);
namespace pocketmine\block; namespace pocketmine\block;
use pocketmine\block\utils\AgeableTrait;
use pocketmine\block\utils\FortuneDropHelper; use pocketmine\block\utils\FortuneDropHelper;
use pocketmine\block\utils\SupportType; use pocketmine\block\utils\SupportType;
use pocketmine\data\runtime\RuntimeDataDescriber;
use pocketmine\entity\Entity; use pocketmine\entity\Entity;
use pocketmine\event\block\StructureGrowEvent; use pocketmine\event\block\StructureGrowEvent;
use pocketmine\item\Fertilizer; 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 * This class is used for Weeping & Twisting vines, because they have same behaviour
*/ */
class NetherVines extends Flowable{ class NetherVines extends Flowable{
use AgeableTrait;
public const MAX_AGE = 25; public const MAX_AGE = 25;
/** Direction the vine grows towards. */ /** Direction the vine grows towards. */
private int $growthFace; private int $growthFace;
protected int $age = 0;
public function __construct(BlockIdentifier $idInfo, string $name, BlockTypeInfo $typeInfo, int $growthFace){ public function __construct(BlockIdentifier $idInfo, string $name, BlockTypeInfo $typeInfo, int $growthFace){
$this->growthFace = $growthFace; $this->growthFace = $growthFace;
parent::__construct($idInfo, $name, $typeInfo); 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{ public function isAffectedBySilkTouch() : bool{
return true; return true;
} }

View File

@ -23,9 +23,9 @@ declare(strict_types=1);
namespace pocketmine\block; namespace pocketmine\block;
use pocketmine\block\utils\AgeableTrait;
use pocketmine\block\utils\BlockEventHelper; use pocketmine\block\utils\BlockEventHelper;
use pocketmine\block\utils\FortuneDropHelper; use pocketmine\block\utils\FortuneDropHelper;
use pocketmine\data\runtime\RuntimeDataDescriber;
use pocketmine\item\Item; use pocketmine\item\Item;
use pocketmine\math\Facing; use pocketmine\math\Facing;
use pocketmine\math\Vector3; use pocketmine\math\Vector3;
@ -34,25 +34,10 @@ use pocketmine\world\BlockTransaction;
use function mt_rand; use function mt_rand;
class NetherWartPlant extends Flowable{ class NetherWartPlant extends Flowable{
use AgeableTrait;
public const MAX_AGE = 3; 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{ public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{
$down = $this->getSide(Facing::DOWN); $down = $this->getSide(Facing::DOWN);
if($down->getTypeId() === BlockTypeIds::SOUL_SAND){ if($down->getTypeId() === BlockTypeIds::SOUL_SAND){

View File

@ -23,8 +23,8 @@ declare(strict_types=1);
namespace pocketmine\block; namespace pocketmine\block;
use pocketmine\block\utils\AgeableTrait;
use pocketmine\block\utils\BlockEventHelper; use pocketmine\block\utils\BlockEventHelper;
use pocketmine\data\runtime\RuntimeDataDescriber;
use pocketmine\item\Fertilizer; use pocketmine\item\Fertilizer;
use pocketmine\item\Item; use pocketmine\item\Item;
use pocketmine\math\Facing; use pocketmine\math\Facing;
@ -34,14 +34,10 @@ use pocketmine\world\BlockTransaction;
use pocketmine\world\Position; use pocketmine\world\Position;
class Sugarcane extends Flowable{ class Sugarcane extends Flowable{
use AgeableTrait;
public const MAX_AGE = 15; 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{ private function seekToBottom() : Position{
$world = $this->position->getWorld(); $world = $this->position->getWorld();
$bottom = $this->position; $bottom = $this->position;
@ -74,17 +70,6 @@ class Sugarcane extends Flowable{
return $grew; 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{ public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
if($item instanceof Fertilizer){ if($item instanceof Fertilizer){
if($this->grow($this->seekToBottom(), $player)){ if($this->grow($this->seekToBottom(), $player)){

View File

@ -23,9 +23,9 @@ declare(strict_types=1);
namespace pocketmine\block; namespace pocketmine\block;
use pocketmine\block\utils\AgeableTrait;
use pocketmine\block\utils\BlockEventHelper; use pocketmine\block\utils\BlockEventHelper;
use pocketmine\block\utils\FortuneDropHelper; use pocketmine\block\utils\FortuneDropHelper;
use pocketmine\data\runtime\RuntimeDataDescriber;
use pocketmine\entity\Entity; use pocketmine\entity\Entity;
use pocketmine\entity\Living; use pocketmine\entity\Living;
use pocketmine\event\entity\EntityDamageByBlockEvent; use pocketmine\event\entity\EntityDamageByBlockEvent;
@ -39,27 +39,13 @@ use pocketmine\world\BlockTransaction;
use function mt_rand; use function mt_rand;
class SweetBerryBush extends Flowable{ class SweetBerryBush extends Flowable{
use AgeableTrait;
public const STAGE_SAPLING = 0; public const STAGE_SAPLING = 0;
public const STAGE_BUSH_NO_BERRIES = 1; public const STAGE_BUSH_NO_BERRIES = 1;
public const STAGE_BUSH_SOME_BERRIES = 2; public const STAGE_BUSH_SOME_BERRIES = 2;
public const STAGE_MATURE = 3; public const STAGE_MATURE = 3;
public const MAX_AGE = self::STAGE_MATURE;
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 function getBerryDropAmount() : int{ public function getBerryDropAmount() : int{
if($this->age === self::STAGE_MATURE){ if($this->age === self::STAGE_MATURE){

View File

@ -0,0 +1,53 @@
<?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\data\runtime\RuntimeDataDescriber;
use function ceil;
use function log;
/**
* This trait is used for blocks that have an age property.
* Need to add to the block the constant MAX_AGE.
*/
trait AgeableTrait{
protected int $age = 0;
protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
$w->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;
}
}