PocketMine-MP 5.31.1 git-c65f740ce5006911c3bac72aa5e6c3707846bd6e
Loading...
Searching...
No Matches
ChorusFlower.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
27use pocketmine\block\utils\AgeableTrait;
28use pocketmine\block\utils\StaticSupportTrait;
40use function array_rand;
41use function min;
42use function mt_rand;
43
44final class ChorusFlower extends Flowable implements Ageable{
45 use AgeableTrait;
46 use StaticSupportTrait;
47
48 public const MIN_AGE = 0;
49 public const MAX_AGE = 5;
50
51 private const MAX_STEM_HEIGHT = 5;
52
53 protected function recalculateCollisionBoxes() : array{
54 return [AxisAlignedBB::one()];
55 }
56
57 private function canBeSupportedAt(Block $block) : bool{
58 $position = $block->position;
59 $world = $position->getWorld();
60 $down = $world->getBlock($position->down());
61
62 if($down->getTypeId() === BlockTypeIds::END_STONE || $down->getTypeId() === BlockTypeIds::CHORUS_PLANT){
63 return true;
64 }
65
66 $plantAdjacent = false;
67 foreach($position->sidesAroundAxis(Axis::Y) as $sidePosition){
68 $block = $world->getBlock($sidePosition);
69
70 if($block->getTypeId() === BlockTypeIds::CHORUS_PLANT){
71 if($plantAdjacent){ //at most one plant may be horizontally adjacent
72 return false;
73 }
74 $plantAdjacent = true;
75 }elseif($block->getTypeId() !== BlockTypeIds::AIR){
76 return false;
77 }
78 }
79
80 return $plantAdjacent;
81 }
82
83 public function onProjectileHit(Projectile $projectile, RayTraceResult $hitResult) : void{
84 $this->position->getWorld()->useBreakOn($this->position);
85 }
86
90 private function scanStem() : array{
91 $world = $this->position->getWorld();
92
93 $stemHeight = 0;
94 $endStoneBelow = false;
95 for($yOffset = 0; $yOffset < self::MAX_STEM_HEIGHT; $yOffset++, $stemHeight++){
96 $down = $world->getBlock($this->position->down($yOffset + 1));
97
98 if($down->getTypeId() !== BlockTypeIds::CHORUS_PLANT){
99 if($down->getTypeId() === BlockTypeIds::END_STONE){
100 $endStoneBelow = true;
101 }
102 break;
103 }
104 }
105
106 return [$stemHeight, $endStoneBelow];
107 }
108
109 private function allHorizontalBlocksEmpty(World $world, Vector3 $position, ?int $except) : bool{
110 foreach($position->sidesAroundAxis(Axis::Y) as $facing => $sidePosition){
111 if($facing === $except){
112 continue;
113 }
114 if($world->getBlock($sidePosition)->getTypeId() !== BlockTypeIds::AIR){
115 return false;
116 }
117 }
118
119 return true;
120 }
121
122 private function canGrowUpwards(int $stemHeight, bool $endStoneBelow) : bool{
123 $world = $this->position->getWorld();
124
125 $up = $this->position->up();
126 if(
127 //the space above must be empty and writable
128 !$world->isInWorld($up->x, $up->y, $up->z) ||
129 $world->getBlock($up)->getTypeId() !== BlockTypeIds::AIR ||
130 (
131 //the space above that must be empty, but doesn't need to be writable
132 $world->isInWorld($up->x, $up->y + 1, $up->z) &&
133 $world->getBlock($up->up())->getTypeId() !== BlockTypeIds::AIR
134 )
135 ){
136 return false;
137 }
138
139 if($this->getSide(Facing::DOWN)->getTypeId() !== BlockTypeIds::AIR){
140 if($stemHeight >= self::MAX_STEM_HEIGHT){
141 return false;
142 }
143
144 if($stemHeight > 1 && $stemHeight > mt_rand(0, $endStoneBelow ? 4 : 3)){ //chance decreases for each added block of chorus plant
145 return false;
146 }
147 }
148
149 return $this->allHorizontalBlocksEmpty($world, $up, null);
150 }
151
152 private function grow(int $facing, int $ageChange, ?BlockTransaction $tx) : BlockTransaction{
153 if($tx === null){
154 $tx = new BlockTransaction($this->position->getWorld());
155 }
156 $tx->addBlock($this->position->getSide($facing), (clone $this)->setAge(min(self::MAX_AGE, $this->age + $ageChange)));
157
158 return $tx;
159 }
160
161 public function ticksRandomly() : bool{ return $this->age < self::MAX_AGE; }
162
163 public function onRandomTick() : void{
164 $world = $this->position->getWorld();
165
166 if($this->age >= self::MAX_AGE){
167 return;
168 }
169
170 $tx = null;
171
172 [$stemHeight, $endStoneBelow] = $this->scanStem();
173 if($this->canGrowUpwards($stemHeight, $endStoneBelow)){
174 $tx = $this->grow(Facing::UP, 0, $tx);
175 }else{
176 $facingVisited = [];
177 for($attempts = 0, $maxAttempts = mt_rand(0, $endStoneBelow ? 4 : 3); $attempts < $maxAttempts; $attempts++){
178 $facing = Facing::HORIZONTAL[array_rand(Facing::HORIZONTAL)];
179 if(isset($facingVisited[$facing])){
180 continue;
181 }
182 $facingVisited[$facing] = true;
183
184 $sidePosition = $this->position->getSide($facing);
185 if(
186 $world->getBlock($sidePosition)->getTypeId() === BlockTypeIds::AIR &&
187 $world->getBlock($sidePosition->down())->getTypeId() === BlockTypeIds::AIR &&
188 $this->allHorizontalBlocksEmpty($world, $sidePosition, Facing::opposite($facing))
189 ){
190 $tx = $this->grow($facing, 1, $tx);
191 }
192 }
193 }
194
195 if($tx !== null){
196 $tx->addBlock($this->position, VanillaBlocks::CHORUS_PLANT());
197 $ev = new StructureGrowEvent($this, $tx, null);
198 $ev->call();
199 if(!$ev->isCancelled() && $tx->apply()){
200 $world->addSound($this->position->add(0.5, 0.5, 0.5), new ChorusFlowerGrowSound());
201 }
202 }else{
203 $world->addSound($this->position->add(0.5, 0.5, 0.5), new ChorusFlowerDieSound());
204 $this->position->getWorld()->setBlock($this->position, $this->setAge(self::MAX_AGE));
205 }
206 }
207}
onProjectileHit(Projectile $projectile, RayTraceResult $hitResult)