PocketMine-MP 5.21.2 git-b2aa6396c3cc2cafdd815eacc360e1ad89599899
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
26use pocketmine\block\utils\AgeableTrait;
28use pocketmine\block\utils\StaticSupportTrait;
29use pocketmine\block\utils\SupportType;
35
36class Cactus extends Transparent{
37 use AgeableTrait;
38 use StaticSupportTrait;
39
40 public const MAX_AGE = 15;
41
42 public function hasEntityCollision() : bool{
43 return true;
44 }
45
49 protected function recalculateCollisionBoxes() : array{
50 $shrinkSize = 1 / 16;
51 return [AxisAlignedBB::one()->contract($shrinkSize, 0, $shrinkSize)->trim(Facing::UP, $shrinkSize)];
52 }
53
54 public function getSupportType(int $facing) : SupportType{
55 return SupportType::NONE;
56 }
57
58 public function onEntityInside(Entity $entity) : bool{
59 $ev = new EntityDamageByBlockEvent($this, $entity, EntityDamageEvent::CAUSE_CONTACT, 1);
60 $entity->attack($ev);
61 return true;
62 }
63
64 private function canBeSupportedAt(Block $block) : bool{
65 $supportBlock = $block->getSide(Facing::DOWN);
66 if(!$supportBlock->hasSameTypeId($this) && !$supportBlock->hasTypeTag(BlockTypeTags::SAND)){
67 return false;
68 }
69 foreach(Facing::HORIZONTAL as $side){
70 if($block->getSide($side)->isSolid()){
71 return false;
72 }
73 }
74
75 return true;
76 }
77
78 public function ticksRandomly() : bool{
79 return true;
80 }
81
82 public function onRandomTick() : void{
83 if(!$this->getSide(Facing::DOWN)->hasSameTypeId($this)){
84 $world = $this->position->getWorld();
85 if($this->age === self::MAX_AGE){
86 for($y = 1; $y < 3; ++$y){
87 if(!$world->isInWorld($this->position->x, $this->position->y + $y, $this->position->z)){
88 break;
89 }
90 $b = $world->getBlockAt($this->position->x, $this->position->y + $y, $this->position->z);
91 if($b->getTypeId() === BlockTypeIds::AIR){
92 BlockEventHelper::grow($b, VanillaBlocks::CACTUS(), null);
93 }else{
94 break;
95 }
96 }
97 $this->age = 0;
98 $world->setBlock($this->position, $this, update: false);
99 }else{
100 ++$this->age;
101 $world->setBlock($this->position, $this, update: false);
102 }
103 }
104 }
105}
getSupportType(int $facing)
Definition Cactus.php:54
onEntityInside(Entity $entity)
Definition Cactus.php:58