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