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