PocketMine-MP 5.31.1 git-c65f740ce5006911c3bac72aa5e6c3707846bd6e
Loading...
Searching...
No Matches
Cactus.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;
29use pocketmine\block\utils\StaticSupportTrait;
30use pocketmine\block\utils\SupportType;
36
37class Cactus extends Transparent implements Ageable{
38 use AgeableTrait;
39 use StaticSupportTrait;
40
41 public const MAX_AGE = 15;
42
43 public function hasEntityCollision() : bool{
44 return true;
45 }
46
47 protected function recalculateCollisionBoxes() : array{
48 $shrinkSize = 1 / 16;
49 return [AxisAlignedBB::one()->contract($shrinkSize, 0, $shrinkSize)->trim(Facing::UP, $shrinkSize)];
50 }
51
52 public function getSupportType(int $facing) : SupportType{
53 return SupportType::NONE;
54 }
55
56 public function onEntityInside(Entity $entity) : bool{
57 $ev = new EntityDamageByBlockEvent($this, $entity, EntityDamageEvent::CAUSE_CONTACT, 1);
58 $entity->attack($ev);
59 return true;
60 }
61
62 private function canBeSupportedAt(Block $block) : bool{
63 $supportBlock = $block->getSide(Facing::DOWN);
64 if(!$supportBlock->hasSameTypeId($this) && !$supportBlock->hasTypeTag(BlockTypeTags::SAND)){
65 return false;
66 }
67 foreach(Facing::HORIZONTAL as $side){
68 if($block->getSide($side)->isSolid()){
69 return false;
70 }
71 }
72
73 return true;
74 }
75
76 public function ticksRandomly() : bool{
77 return true;
78 }
79
80 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;
93 }
94 }
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);
100 }
101 }
102 }
103}
getSupportType(int $facing)
Definition Cactus.php:52
onEntityInside(Entity $entity)
Definition Cactus.php:56