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
12 changed files with 92 additions and 196 deletions

View File

@ -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();
}