46 use StaticSupportTrait;
48 public const MIN_AGE = 0;
49 public const MAX_AGE = 5;
51 private const MAX_STEM_HEIGHT = 5;
57 private function canBeSupportedAt(
Block $block) : bool{
58 $position = $block->position;
59 $world = $position->getWorld();
60 $down = $world->getBlock($position->down());
62 if($down->getTypeId() === BlockTypeIds::END_STONE || $down->getTypeId() === BlockTypeIds::CHORUS_PLANT){
66 $plantAdjacent =
false;
67 foreach($position->sidesAroundAxis(Axis::Y) as $sidePosition){
68 $block = $world->getBlock($sidePosition);
70 if($block->
getTypeId() === BlockTypeIds::CHORUS_PLANT){
74 $plantAdjacent =
true;
75 }elseif($block->
getTypeId() !== BlockTypeIds::AIR){
80 return $plantAdjacent;
84 $this->position->getWorld()->useBreakOn($this->position);
90 private function scanStem() : array{
91 $world = $this->position->getWorld();
94 $endStoneBelow =
false;
95 for($yOffset = 0; $yOffset < self::MAX_STEM_HEIGHT; $yOffset++, $stemHeight++){
96 $down = $world->getBlock($this->position->down($yOffset + 1));
98 if($down->getTypeId() !== BlockTypeIds::CHORUS_PLANT){
99 if($down->getTypeId() === BlockTypeIds::END_STONE){
100 $endStoneBelow =
true;
106 return [$stemHeight, $endStoneBelow];
109 private function allHorizontalBlocksEmpty(World $world, Vector3 $position, ?
int $except) : bool{
110 foreach($position->sidesAroundAxis(Axis::Y) as $facing => $sidePosition){
111 if($facing === $except){
114 if($world->getBlock($sidePosition)->getTypeId() !== BlockTypeIds::AIR){
122 private function canGrowUpwards(
int $stemHeight,
bool $endStoneBelow) : bool{
123 $world = $this->position->getWorld();
125 $up = $this->position->up();
128 !$world->isInWorld($up->x, $up->y, $up->z) ||
129 $world->getBlock($up)->getTypeId() !== BlockTypeIds::AIR ||
132 $world->isInWorld($up->x, $up->y + 1, $up->z) &&
133 $world->getBlock($up->up())->getTypeId() !== BlockTypeIds::AIR
139 if($this->getSide(Facing::DOWN)->getTypeId() !== BlockTypeIds::AIR){
140 if($stemHeight >= self::MAX_STEM_HEIGHT){
144 if($stemHeight > 1 && $stemHeight > mt_rand(0, $endStoneBelow ? 4 : 3)){
149 return $this->allHorizontalBlocksEmpty($world, $up,
null);
152 private function grow(
int $facing,
int $ageChange, ?BlockTransaction $tx) : BlockTransaction{
154 $tx =
new BlockTransaction($this->position->getWorld());
156 $tx->addBlock($this->position->getSide($facing), (clone $this)->setAge(min(self::MAX_AGE, $this->age + $ageChange)));
161 public function ticksRandomly() : bool{ return $this->age < self::MAX_AGE; }
164 $world = $this->position->getWorld();
166 if($this->age >= self::MAX_AGE){
172 [$stemHeight, $endStoneBelow] = $this->scanStem();
173 if($this->canGrowUpwards($stemHeight, $endStoneBelow)){
174 $tx = $this->grow(Facing::UP, 0, $tx);
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])){
182 $facingVisited[$facing] =
true;
184 $sidePosition = $this->position->getSide($facing);
186 $world->getBlock($sidePosition)->getTypeId() === BlockTypeIds::AIR &&
187 $world->getBlock($sidePosition->down())->getTypeId() === BlockTypeIds::AIR &&
188 $this->allHorizontalBlocksEmpty($world, $sidePosition, Facing::opposite($facing))
190 $tx = $this->grow($facing, 1, $tx);
196 $tx->addBlock($this->position, VanillaBlocks::CHORUS_PLANT());
197 $ev =
new StructureGrowEvent($this, $tx,
null);
199 if(!$ev->isCancelled() && $tx->apply()){
200 $world->addSound($this->position->add(0.5, 0.5, 0.5),
new ChorusFlowerGrowSound());
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));