45 use StaticSupportTrait;
47 public const MIN_AGE = 0;
48 public const MAX_AGE = 5;
50 private const MAX_STEM_HEIGHT = 5;
56 private function canBeSupportedAt(
Block $block) : bool{
57 $position = $block->position;
58 $world = $position->getWorld();
59 $down = $world->getBlock($position->down());
61 if($down->getTypeId() === BlockTypeIds::END_STONE || $down->getTypeId() === BlockTypeIds::CHORUS_PLANT){
65 $plantAdjacent =
false;
66 foreach($position->sidesAroundAxis(Axis::Y) as $sidePosition){
67 $block = $world->getBlock($sidePosition);
69 if($block->
getTypeId() === BlockTypeIds::CHORUS_PLANT){
73 $plantAdjacent =
true;
74 }elseif($block->
getTypeId() !== BlockTypeIds::AIR){
79 return $plantAdjacent;
83 $this->position->getWorld()->useBreakOn($this->position);
89 private function scanStem() : array{
90 $world = $this->position->getWorld();
93 $endStoneBelow =
false;
94 for($yOffset = 0; $yOffset < self::MAX_STEM_HEIGHT; $yOffset++, $stemHeight++){
95 $down = $world->getBlock($this->position->down($yOffset + 1));
97 if($down->getTypeId() !== BlockTypeIds::CHORUS_PLANT){
98 if($down->getTypeId() === BlockTypeIds::END_STONE){
99 $endStoneBelow =
true;
105 return [$stemHeight, $endStoneBelow];
108 private function allHorizontalBlocksEmpty(World $world, Vector3 $position, ?
int $except) : bool{
109 foreach($position->sidesAroundAxis(Axis::Y) as $facing => $sidePosition){
110 if($facing === $except){
113 if($world->getBlock($sidePosition)->getTypeId() !== BlockTypeIds::AIR){
121 private function canGrowUpwards(
int $stemHeight,
bool $endStoneBelow) : bool{
122 $world = $this->position->getWorld();
124 $up = $this->position->up();
127 !$world->isInWorld($up->x, $up->y, $up->z) ||
128 $world->getBlock($up)->getTypeId() !== BlockTypeIds::AIR ||
131 $world->isInWorld($up->x, $up->y + 1, $up->z) &&
132 $world->getBlock($up->up())->getTypeId() !== BlockTypeIds::AIR
138 if($this->getSide(Facing::DOWN)->getTypeId() !== BlockTypeIds::AIR){
139 if($stemHeight >= self::MAX_STEM_HEIGHT){
143 if($stemHeight > 1 && $stemHeight > mt_rand(0, $endStoneBelow ? 4 : 3)){
148 return $this->allHorizontalBlocksEmpty($world, $up,
null);
151 private function grow(
int $facing,
int $ageChange, ?BlockTransaction $tx) : BlockTransaction{
153 $tx =
new BlockTransaction($this->position->getWorld());
155 $tx->addBlock($this->position->getSide($facing), (clone $this)->setAge(min(self::MAX_AGE, $this->age + $ageChange)));
160 public function ticksRandomly() : bool{ return $this->age < self::MAX_AGE; }
163 $world = $this->position->getWorld();
165 if($this->age >= self::MAX_AGE){
171 [$stemHeight, $endStoneBelow] = $this->scanStem();
172 if($this->canGrowUpwards($stemHeight, $endStoneBelow)){
173 $tx = $this->grow(Facing::UP, 0, $tx);
176 for($attempts = 0, $maxAttempts = mt_rand(0, $endStoneBelow ? 4 : 3); $attempts < $maxAttempts; $attempts++){
177 $facing = Facing::HORIZONTAL[array_rand(Facing::HORIZONTAL)];
178 if(isset($facingVisited[$facing])){
181 $facingVisited[$facing] =
true;
183 $sidePosition = $this->position->getSide($facing);
185 $world->getBlock($sidePosition)->getTypeId() === BlockTypeIds::AIR &&
186 $world->getBlock($sidePosition->down())->getTypeId() === BlockTypeIds::AIR &&
187 $this->allHorizontalBlocksEmpty($world, $sidePosition, Facing::opposite($facing))
189 $tx = $this->grow($facing, 1, $tx);
195 $tx->addBlock($this->position, VanillaBlocks::CHORUS_PLANT());
196 $ev =
new StructureGrowEvent($this, $tx,
null);
198 if(!$ev->isCancelled() && $tx->apply()){
199 $world->addSound($this->position->add(0.5, 0.5, 0.5),
new ChorusFlowerGrowSound());
202 $world->addSound($this->position->add(0.5, 0.5, 0.5),
new ChorusFlowerDieSound());
203 $this->position->getWorld()->setBlock($this->position, $this->setAge(self::MAX_AGE));