PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
Loading...
Searching...
No Matches
CaveVines.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;
40use function mt_rand;
41
42class CaveVines extends Flowable{
43 use AgeableTrait;
44 use StaticSupportTrait;
45
46 public const MAX_AGE = 25;
47
48 protected bool $berries = false;
49 protected bool $head = false;
50
51 protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
52 $w->boundedIntAuto(0, self::MAX_AGE, $this->age);
53 $w->bool($this->berries);
54 $w->bool($this->head);
55 }
56
57 public function hasBerries() : bool{ return $this->berries; }
58
60 public function setBerries(bool $berries) : self{
61 $this->berries = $berries;
62 return $this;
63 }
64
65 public function isHead() : bool{ return $this->head; }
66
68 public function setHead(bool $head) : self{
69 $this->head = $head;
70 return $this;
71 }
72
73 public function canClimb() : bool{
74 return true;
75 }
76
77 public function getLightLevel() : int{
78 return $this->berries ? 14 : 0;
79 }
80
81 private function canBeSupportedAt(Block $block) : bool{
82 $supportBlock = $block->getSide(Facing::UP);
83 return $supportBlock->getSupportType(Facing::DOWN) === SupportType::FULL || $supportBlock->hasSameTypeId($this);
84 }
85
86 public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{
87 $this->age = mt_rand(0, self::MAX_AGE);
88 return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player);
89 }
90
91 public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
92 if($this->berries){
93 $this->position->getWorld()->dropItem($this->position, $this->asItem());
94 $this->position->getWorld()->addSound($this->position, new GlowBerriesPickSound());
95
96 $this->position->getWorld()->setBlock($this->position, $this->setBerries(false));
97 return true;
98 }
99 if($item instanceof Fertilizer){
100 $newState = (clone $this)
101 ->setBerries(true)
102 ->setHead(!$this->getSide(Facing::DOWN)->hasSameTypeId($this));
103 if(BlockEventHelper::grow($this, $newState, $player)){
104 $item->pop();
105 }
106 return true;
107 }
108 return false;
109 }
110
111 public function onRandomTick() : void{
112 $head = !$this->getSide(Facing::DOWN)->hasSameTypeId($this);
113 if($head !== $this->head){
114 $this->position->getWorld()->setBlock($this->position, $this->setHead($head));
115 }
116
117 if($this->age < self::MAX_AGE && mt_rand(1, 10) === 1){
118 $growthPos = $this->position->getSide(Facing::DOWN);
119 $world = $growthPos->getWorld();
120 if($world->isInWorld($growthPos->getFloorX(), $growthPos->getFloorY(), $growthPos->getFloorZ())){
121 $block = $world->getBlock($growthPos);
122 if($block->getTypeId() === BlockTypeIds::AIR){
123 $newState = VanillaBlocks::CAVE_VINES()
124 ->setAge($this->age + 1)
125 ->setBerries(mt_rand(1, 9) === 1);
126 BlockEventHelper::grow($block, $newState, null);
127 }
128 }
129 }
130 }
131
132 public function ticksRandomly() : bool{
133 return true;
134 }
135
136 protected function recalculateCollisionBoxes() : array{
137 return [];
138 }
139
140 public function hasEntityCollision() : bool{
141 return true;
142 }
143
144 public function onEntityInside(Entity $entity) : bool{
145 $entity->resetFallDistance();
146 return false;
147 }
148
149 public function getDropsForCompatibleTool(Item $item) : array{
150 return $this->berries ? [$this->asItem()] : [];
151 }
152
153 public function isAffectedBySilkTouch() : bool{
154 return true;
155 }
156
157 public function asItem() : Item{
158 return VanillaItems::GLOW_BERRIES();
159 }
160
161 public function getSupportType(int $facing) : SupportType{
162 return SupportType::NONE;
163 }
164}
getSupportType(int $facing)
Definition Block.php:943
onEntityInside(Entity $entity)
describeBlockOnlyState(RuntimeDataDescriber $w)
Definition CaveVines.php:51
onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player=null, array &$returnedItems=[])
Definition CaveVines.php:91
setBerries(bool $berries)
Definition CaveVines.php:60
getDropsForCompatibleTool(Item $item)
place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player=null)
Definition CaveVines.php:86