PocketMine-MP 5.16.1 git-585aee9386a787c95e73dd0a05ffca8329606b68
Stem.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\block;
25
31use function array_rand;
32use function mt_rand;
33
34abstract class Stem extends Crops{
35 protected int $facing = Facing::UP;
36
37 protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
38 parent::describeBlockOnlyState($w);
39 $w->facingExcept($this->facing, Facing::DOWN);
40 }
41
42 public function getFacing() : int{ return $this->facing; }
43
45 public function setFacing(int $facing) : self{
46 if($facing === Facing::DOWN){
47 throw new \InvalidArgumentException("DOWN is not a valid facing for this block");
48 }
49 $this->facing = $facing;
50 return $this;
51 }
52
53 abstract protected function getPlant() : Block;
54
55 public function onNearbyBlockChange() : void{
56 if($this->facing !== Facing::UP && !$this->getSide($this->facing)->hasSameTypeId($this->getPlant())){
57 $this->position->getWorld()->setBlock($this->position, $this->setFacing(Facing::UP));
58 }
59 parent::onNearbyBlockChange();
60 }
61
62 public function ticksRandomly() : bool{
63 return $this->age < self::MAX_AGE || $this->facing === Facing::UP;
64 }
65
66 public function onRandomTick() : void{
67 if($this->facing === Facing::UP && CropGrowthHelper::canGrow($this)){
68 $world = $this->position->getWorld();
69 if($this->age < self::MAX_AGE){
70 $block = clone $this;
71 ++$block->age;
72 BlockEventHelper::grow($this, $block, null);
73 }else{
74 $grow = $this->getPlant();
75 foreach(Facing::HORIZONTAL as $side){
76 if($this->getSide($side)->hasSameTypeId($grow)){
77 return;
78 }
79 }
80
81 $facing = Facing::HORIZONTAL[array_rand(Facing::HORIZONTAL)];
82 $side = $this->getSide($facing);
83 if($side->getTypeId() === BlockTypeIds::AIR && $side->getSide(Facing::DOWN)->hasTypeTag(BlockTypeTags::DIRT)){
84 if(BlockEventHelper::grow($side, $grow, null)){
85 $this->position->getWorld()->setBlock($this->position, $this->setFacing($facing));
86 }
87 }
88 }
89 }
90 }
91
92 public function getDropsForCompatibleTool(Item $item) : array{
93 return [
94 $this->asItem()->setCount(mt_rand(0, 2))
95 ];
96 }
97}
setFacing(int $facing)
Definition: Stem.php:45
describeBlockOnlyState(RuntimeDataDescriber $w)
Definition: Stem.php:37
getDropsForCompatibleTool(Item $item)
Definition: Stem.php:92