Skip to content

Commit 14114f6

Browse files
authored
Implement Cactus Flower (#6838)
1 parent 15ac744 commit 14114f6

File tree

8 files changed

+99
-36
lines changed

8 files changed

+99
-36
lines changed

src/block/BlockTypeIds.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -826,8 +826,9 @@ private function __construct(){
826826
public const COPPER_CHAIN = 10796;
827827
public const COPPER_LANTERN = 10797;
828828
public const COPPER_TORCH = 10798;
829+
public const CACTUS_FLOWER = 10799;
829830

830-
public const FIRST_UNUSED_BLOCK_ID = 10799;
831+
public const FIRST_UNUSED_BLOCK_ID = 10800;
831832

832833
private static int $nextDynamicId = self::FIRST_UNUSED_BLOCK_ID;
833834

src/block/Cactus.php

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@
3333
use pocketmine\event\entity\EntityDamageEvent;
3434
use pocketmine\math\AxisAlignedBB;
3535
use pocketmine\math\Facing;
36+
use function mt_rand;
3637

3738
class Cactus extends Transparent implements Ageable{
3839
use AgeableTrait;
3940
use StaticSupportTrait;
4041

4142
public const MAX_AGE = 15;
43+
public const MAX_HEIGHT = 3;
4244

4345
public function hasEntityCollision() : bool{
4446
return true;
@@ -78,26 +80,52 @@ public function ticksRandomly() : bool{
7880
}
7981

8082
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);
93114
}
115+
return;
94116
}
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);
100117
}
101118
}
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);
102130
}
103131
}

src/block/CactusFlower.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
22+
declare(strict_types=1);
23+
24+
namespace pocketmine\block;
25+
26+
use pocketmine\block\utils\StaticSupportTrait;
27+
use pocketmine\math\Facing;
28+
29+
class CactusFlower extends Flowable{
30+
use StaticSupportTrait;
31+
32+
private function canBeSupportedAt(Block $block) : bool{
33+
$supportBlock = $block->getSide(Facing::DOWN);
34+
return
35+
$supportBlock->getSupportType(Facing::UP)->hasCenterSupport() ||
36+
$supportBlock->getTypeId() === BlockTypeIds::CACTUS;
37+
}
38+
39+
public function getFlameEncouragement() : int{
40+
return 60;
41+
}
42+
43+
public function getFlammability() : int{
44+
return 100;
45+
}
46+
}

src/block/VanillaBlocks.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@
162162
* @method static BrownMushroomBlock BROWN_MUSHROOM_BLOCK()
163163
* @method static BuddingAmethyst BUDDING_AMETHYST()
164164
* @method static Cactus CACTUS()
165+
* @method static CactusFlower CACTUS_FLOWER()
165166
* @method static Cake CAKE()
166167
* @method static CakeWithCandle CAKE_WITH_CANDLE()
167168
* @method static CakeWithDyedCandle CAKE_WITH_DYED_CANDLE()
@@ -1378,6 +1379,7 @@ public function getDropsForCompatibleTool(Item $item) : array{
13781379
return [];
13791380
}
13801381
});
1382+
self::register("cactus_flower", fn(BID $id) => new CactusFlower($id, "Cactus Flower", new Info(BreakInfo::instant())));
13811383

13821384
self::registerBlocksR13();
13831385
self::registerBlocksR14();

src/data/bedrock/block/convert/VanillaBlockMappings.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,8 @@ private static function registerSimpleIdOnlyMappings(BlockSerializerDeserializer
459459
$reg->mapSimple(Blocks::PINK_TULIP(), Ids::PINK_TULIP);
460460
$reg->mapSimple(Blocks::RED_TULIP(), Ids::RED_TULIP);
461461
$reg->mapSimple(Blocks::WHITE_TULIP(), Ids::WHITE_TULIP);
462+
463+
$reg->mapSimple(Blocks::CACTUS_FLOWER(), Ids::CACTUS_FLOWER);
462464
}
463465

464466
private static function registerColoredMappings(BlockSerializerDeserializerRegistrar $reg, CommonProperties $commonProperties) : void{

src/item/StringToItemParser.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ private static function registerBlocks(self $result) : void{
211211
$result->registerBlock("burning_furnace", fn() => Blocks::FURNACE());
212212
$result->registerBlock("bush", fn() => Blocks::DEAD_BUSH());
213213
$result->registerBlock("cactus", fn() => Blocks::CACTUS());
214+
$result->registerBlock("cactus_flower", fn() => Blocks::CACTUS_FLOWER());
214215
$result->registerBlock("cake", fn() => Blocks::CAKE());
215216
$result->registerBlock("cake_block", fn() => Blocks::CAKE());
216217
$result->registerBlock("calcite", fn() => Blocks::CALCITE());

tests/phpstan/configs/actual-problems.neon

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -90,36 +90,18 @@ parameters:
9090
count: 3
9191
path: ../../../src/block/Block.php
9292

93-
-
94-
message: '#^Parameter \#1 \$x of method pocketmine\\world\\World\:\:getBlockAt\(\) expects int, float\|int given\.$#'
95-
identifier: argument.type
96-
count: 1
97-
path: ../../../src/block/Cactus.php
98-
9993
-
10094
message: '#^Parameter \#1 \$x of method pocketmine\\world\\World\:\:isInWorld\(\) expects int, float\|int given\.$#'
10195
identifier: argument.type
10296
count: 1
10397
path: ../../../src/block/Cactus.php
10498

105-
-
106-
message: '#^Parameter \#2 \$y of method pocketmine\\world\\World\:\:getBlockAt\(\) expects int, float\|int given\.$#'
107-
identifier: argument.type
108-
count: 1
109-
path: ../../../src/block/Cactus.php
110-
11199
-
112100
message: '#^Parameter \#2 \$y of method pocketmine\\world\\World\:\:isInWorld\(\) expects int, float\|int given\.$#'
113101
identifier: argument.type
114102
count: 1
115103
path: ../../../src/block/Cactus.php
116104

117-
-
118-
message: '#^Parameter \#3 \$z of method pocketmine\\world\\World\:\:getBlockAt\(\) expects int, float\|int given\.$#'
119-
identifier: argument.type
120-
count: 1
121-
path: ../../../src/block/Cactus.php
122-
123105
-
124106
message: '#^Parameter \#3 \$z of method pocketmine\\world\\World\:\:isInWorld\(\) expects int, float\|int given\.$#'
125107
identifier: argument.type

tests/phpunit/block/block_factory_consistency_check.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
"BROWN_MUSHROOM_BLOCK": 11,
8383
"BUDDING_AMETHYST": 1,
8484
"CACTUS": 16,
85+
"CACTUS_FLOWER": 1,
8586
"CAKE": 7,
8687
"CAKE_WITH_CANDLE": 2,
8788
"CAKE_WITH_DYED_CANDLE": 32,

0 commit comments

Comments
 (0)