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