|
33 | 33 | use pocketmine\event\entity\EntityDamageEvent; |
34 | 34 | use pocketmine\math\AxisAlignedBB; |
35 | 35 | use pocketmine\math\Facing; |
| 36 | +use function mt_rand; |
36 | 37 |
|
37 | 38 | class Cactus extends Transparent implements Ageable{ |
38 | 39 | use AgeableTrait; |
39 | 40 | use StaticSupportTrait; |
40 | 41 |
|
41 | 42 | public const MAX_AGE = 15; |
| 43 | + public const MAX_HEIGHT = 3; |
42 | 44 |
|
43 | 45 | public function hasEntityCollision() : bool{ |
44 | 46 | return true; |
@@ -78,26 +80,52 @@ public function ticksRandomly() : bool{ |
78 | 80 | } |
79 | 81 |
|
80 | 82 | public function onRandomTick() : void{ |
81 | | - if(!$this->getSide(Facing::DOWN)->hasSameTypeId($this)){ |
82 | | - $world = $this->position->getWorld(); |
83 | | - if($this->age === self::MAX_AGE){ |
84 | | - for($y = 1; $y < 3; ++$y){ |
85 | | - if(!$world->isInWorld($this->position->x, $this->position->y + $y, $this->position->z)){ |
86 | | - break; |
87 | | - } |
88 | | - $b = $world->getBlockAt($this->position->x, $this->position->y + $y, $this->position->z); |
89 | | - if($b->getTypeId() === BlockTypeIds::AIR){ |
90 | | - BlockEventHelper::grow($b, VanillaBlocks::CACTUS(), null); |
91 | | - }else{ |
92 | | - break; |
| 83 | + $up = $this->getSide(Facing::UP); |
| 84 | + if($up->getTypeId() !== BlockTypeIds::AIR){ |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + $world = $this->position->getWorld(); |
| 89 | + |
| 90 | + if(!$world->isInWorld($up->position->x, $up->position->y, $up->position->z)){ |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + $height = 1; |
| 95 | + while($height < self::MAX_HEIGHT && $this->getSide(Facing::DOWN, $height)->hasSameTypeId($this)){ |
| 96 | + $height++; |
| 97 | + } |
| 98 | + |
| 99 | + if($this->age === 9){ |
| 100 | + $canGrowFlower = true; |
| 101 | + foreach(Facing::HORIZONTAL as $side){ |
| 102 | + if($up->getSide($side)->isSolid()){ |
| 103 | + $canGrowFlower = false; |
| 104 | + break; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + if($canGrowFlower){ |
| 109 | + $chance = $height >= self::MAX_HEIGHT ? 25 : 10; |
| 110 | + if(mt_rand(1, 100) <= $chance){ |
| 111 | + if(BlockEventHelper::grow($up, VanillaBlocks::CACTUS_FLOWER(), null)){ |
| 112 | + $this->age = 0; |
| 113 | + $world->setBlock($this->position, $this, update: false); |
93 | 114 | } |
| 115 | + return; |
94 | 116 | } |
95 | | - $this->age = 0; |
96 | | - $world->setBlock($this->position, $this, update: false); |
97 | | - }else{ |
98 | | - ++$this->age; |
99 | | - $world->setBlock($this->position, $this, update: false); |
100 | 117 | } |
101 | 118 | } |
| 119 | + |
| 120 | + if($this->age === self::MAX_AGE){ |
| 121 | + $this->age = 0; |
| 122 | + |
| 123 | + if($height < self::MAX_HEIGHT){ |
| 124 | + BlockEventHelper::grow($up, VanillaBlocks::CACTUS(), null); |
| 125 | + } |
| 126 | + }else{ |
| 127 | + ++$this->age; |
| 128 | + } |
| 129 | + $world->setBlock($this->position, $this, update: false); |
102 | 130 | } |
103 | 131 | } |
0 commit comments