PocketMine-MP 5.32.2 git-237b304ef9858756b018e44e8f298093f66f823b
Loading...
Searching...
No Matches
NetherVines.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;
39use function min;
40use function mt_rand;
41
45class NetherVines extends Flowable implements Ageable{
46 use AgeableTrait;
47 use StaticSupportTrait;
48
49 public const MAX_AGE = 25;
50
52 private int $growthFace;
53
54 public function __construct(BlockIdentifier $idInfo, string $name, BlockTypeInfo $typeInfo, int $growthFace){
55 $this->growthFace = $growthFace;
56 parent::__construct($idInfo, $name, $typeInfo);
57 }
58
59 public function isAffectedBySilkTouch() : bool{
60 return true;
61 }
62
63 public function canClimb() : bool{
64 return true;
65 }
66
67 private function canBeSupportedAt(Block $block) : bool{
68 $supportBlock = $block->getSide(Facing::opposite($this->growthFace));
69 return $supportBlock->getSupportType($this->growthFace)->hasCenterSupport() || $supportBlock->hasSameTypeId($this);
70 }
71
75 private function seekToTip() : NetherVines{
76 $top = $this;
77 while(($next = $top->getSide($this->growthFace)) instanceof NetherVines && $next->hasSameTypeId($this)){
78 $top = $next;
79 }
80 return $top;
81 }
82
83 public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{
84 $this->age = mt_rand(0, self::MAX_AGE - 1);
85 return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player);
86 }
87
88 public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
89 if($item instanceof Fertilizer){
90 if($this->grow($player, mt_rand(1, 5))){
91 $item->pop();
92 }
93 return true;
94 }
95 return false;
96 }
97
98 public function ticksRandomly() : bool{
99 return $this->age < self::MAX_AGE;
100 }
101
102 public function onRandomTick() : void{
103 if($this->age < self::MAX_AGE && mt_rand(1, 10) === 1){
104 if($this->getSide($this->growthFace)->canBeReplaced()){
105 $this->grow(null);
106 }
107 }
108 }
109
110 private function grow(?Player $player, int $growthAmount = 1) : bool{
111 $top = $this->seekToTip();
112 $age = $top->age;
113 $pos = $top->position;
114 $world = $pos->getWorld();
115 $changedBlocks = 0;
116
117 $tx = new BlockTransaction($world);
118
119 for($i = 1; $i <= $growthAmount; $i++){
120 $growthPos = $pos->getSide($this->growthFace, $i);
121 if(!$world->isInWorld($growthPos->getFloorX(), $growthPos->getFloorY(), $growthPos->getFloorZ()) || !$world->getBlock($growthPos)->canBeReplaced()){
122 break;
123 }
124 $tx->addBlock($growthPos, (clone $top)->setAge(min(++$age, self::MAX_AGE)));
125 $changedBlocks++;
126 }
127
128 if($changedBlocks > 0){
129 $ev = new StructureGrowEvent($top, $tx, $player);
130 $ev->call();
131
132 if($ev->isCancelled()){
133 return false;
134 }
135
136 return $tx->apply();
137 }
138
139 return false;
140 }
141
142 public function hasEntityCollision() : bool{
143 return true;
144 }
145
146 public function onEntityInside(Entity $entity) : bool{
147 $entity->resetFallDistance();
148 return false;
149 }
150
151 protected function recalculateCollisionBoxes() : array{
152 return [];
153 }
154
155 public function getDropsForCompatibleTool(Item $item) : array{
156 if(($item->getBlockToolType() & BlockToolType::SHEARS) !== 0 || FortuneDropHelper::bonusChanceFixed($item, 1 / 3, 2 / 9)){
157 return [$this->asItem()];
158 }
159 return [];
160 }
161
162 public function getSupportType(int $facing) : SupportType{
163 return SupportType::NONE;
164 }
165}
getSupportType(int $facing)
Definition Block.php:948
place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player=null)
onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player=null, array &$returnedItems=[])
pop(int $count=1)
Definition Item.php:433