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