PocketMine-MP 5.16.1 git-585aee9386a787c95e73dd0a05ffca8329606b68
block/Bamboo.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\StaticSupportTrait;
27use pocketmine\block\utils\SupportType;
30use pocketmine\item\Bamboo as ItemBamboo;
39use function count;
40use function gmp_add;
41use function gmp_and;
42use function gmp_intval;
43use function gmp_mul;
44use function gmp_xor;
45use function min;
46use function mt_rand;
47use const PHP_INT_MAX;
48
49class Bamboo extends Transparent{
50 use StaticSupportTrait;
51
52 public const NO_LEAVES = 0;
53 public const SMALL_LEAVES = 1;
54 public const LARGE_LEAVES = 2;
55
56 protected bool $thick = false; //age in PC, but this is 0/1
57 protected bool $ready = false;
58 protected int $leafSize = self::NO_LEAVES;
59
60 protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
61 $w->boundedIntAuto(self::NO_LEAVES, self::LARGE_LEAVES, $this->leafSize);
62 $w->bool($this->thick);
63 $w->bool($this->ready);
64 }
65
66 public function isThick() : bool{ return $this->thick; }
67
69 public function setThick(bool $thick) : self{
70 $this->thick = $thick;
71 return $this;
72 }
73
74 public function isReady() : bool{ return $this->ready; }
75
77 public function setReady(bool $ready) : self{
78 $this->ready = $ready;
79 return $this;
80 }
81
82 public function getLeafSize() : int{ return $this->leafSize; }
83
85 public function setLeafSize(int $leafSize) : self{
86 $this->leafSize = $leafSize;
87 return $this;
88 }
89
93 protected function recalculateCollisionBoxes() : array{
94 //this places the BB at the northwest corner, not the center
95 $inset = 1 - (($this->thick ? 3 : 2) / 16);
96 return [AxisAlignedBB::one()->trim(Facing::SOUTH, $inset)->trim(Facing::EAST, $inset)];
97 }
98
99 public function getSupportType(int $facing) : SupportType{
100 return SupportType::NONE;
101 }
102
103 private static function getOffsetSeed(int $x, int $y, int $z) : int{
104 $p1 = gmp_mul($z, 0x6ebfff5);
105 $p2 = gmp_mul($x, 0x2fc20f);
106 $p3 = $y;
107
108 $xord = gmp_xor(gmp_xor($p1, $p2), $p3);
109
110 $fullResult = gmp_mul(gmp_add(gmp_mul($xord, 0x285b825), 0xb), $xord);
111 return gmp_intval(gmp_and($fullResult, 0xffffffff));
112 }
113
114 private static function getMaxHeight(int $x, int $z) : int{
115 return 12 + (self::getOffsetSeed($x, 0, $z) % 5);
116 }
117
118 public function getModelPositionOffset() : ?Vector3{
119 $seed = self::getOffsetSeed($this->position->getFloorX(), 0, $this->position->getFloorZ());
120 $retX = (($seed % 12) + 1) / 16;
121 $retZ = ((($seed >> 8) % 12) + 1) / 16;
122 return new Vector3($retX, 0, $retZ);
123 }
124
125 private function canBeSupportedAt(Block $block) : bool{
126 $supportBlock = $block->getSide(Facing::DOWN);
127 return
128 $supportBlock->hasSameTypeId($this) ||
129 $supportBlock->getTypeId() === BlockTypeIds::GRAVEL ||
130 $supportBlock->hasTypeTag(BlockTypeTags::DIRT) ||
131 $supportBlock->hasTypeTag(BlockTypeTags::MUD) ||
132 $supportBlock->hasTypeTag(BlockTypeTags::SAND);
133 }
134
135 private function seekToTop() : Bamboo{
136 $world = $this->position->getWorld();
137 $top = $this;
138 while(($next = $world->getBlock($top->position->up())) instanceof Bamboo && $next->hasSameTypeId($this)){
139 $top = $next;
140 }
141 return $top;
142 }
143
144 public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
145 if($item instanceof Fertilizer){
146 $top = $this->seekToTop();
147 if($top->grow(self::getMaxHeight($top->position->getFloorX(), $top->position->getFloorZ()), mt_rand(1, 2), $player)){
148 $item->pop();
149 return true;
150 }
151 }elseif($item instanceof ItemBamboo){
152 if($this->seekToTop()->grow(PHP_INT_MAX, 1, $player)){
153 $item->pop();
154 return true;
155 }
156 }
157 return false;
158 }
159
160 private function grow(int $maxHeight, int $growAmount, ?Player $player) : bool{
161 $world = $this->position->getWorld();
162 if(!$world->getBlock($this->position->up())->canBeReplaced()){
163 return false;
164 }
165
166 $height = 1;
167 while($world->getBlock($this->position->subtract(0, $height, 0))->hasSameTypeId($this)){
168 if(++$height >= $maxHeight){
169 return false;
170 }
171 }
172
173 $newHeight = $height + $growAmount;
174
175 $stemBlock = (clone $this)->setReady(false)->setLeafSize(self::NO_LEAVES);
176 if($newHeight >= 4 && !$stemBlock->thick){ //don't change it to false if height is less, because it might have been chopped
177 $stemBlock = $stemBlock->setThick(true);
178 }
179 $smallLeavesBlock = (clone $stemBlock)->setLeafSize(self::SMALL_LEAVES);
180 $bigLeavesBlock = (clone $stemBlock)->setLeafSize(self::LARGE_LEAVES);
181
182 $newBlocks = [];
183 if($newHeight === 2){
184 $newBlocks[] = $smallLeavesBlock;
185 }elseif($newHeight === 3){
186 $newBlocks[] = $smallLeavesBlock;
187 $newBlocks[] = $smallLeavesBlock;
188 }elseif($newHeight === 4){
189 $newBlocks[] = $bigLeavesBlock;
190 $newBlocks[] = $smallLeavesBlock;
191 $newBlocks[] = $stemBlock;
192 $newBlocks[] = $stemBlock;
193 }elseif($newHeight > 4){
194 $newBlocks[] = $bigLeavesBlock;
195 $newBlocks[] = $bigLeavesBlock;
196 $newBlocks[] = $smallLeavesBlock;
197 for($i = 0, $max = min($growAmount, $newHeight - count($newBlocks)); $i < $max; ++$i){
198 $newBlocks[] = $stemBlock; //to replace the bottom blocks that currently have leaves
199 }
200 }
201
202 $tx = new BlockTransaction($world);
203 foreach($newBlocks as $idx => $newBlock){
204 $tx->addBlock($this->position->subtract(0, $idx - $growAmount, 0), $newBlock);
205 }
206
207 $ev = new StructureGrowEvent($this, $tx, $player);
208 $ev->call();
209 if($ev->isCancelled()){
210 return false;
211 }
212
213 return $tx->apply();
214 }
215
216 public function ticksRandomly() : bool{
217 return true;
218 }
219
220 public function onRandomTick() : void{
221 $world = $this->position->getWorld();
222 if($this->ready){
223 $this->ready = false;
224 if($world->getFullLight($this->position) < 9 || !$this->grow(self::getMaxHeight($this->position->getFloorX(), $this->position->getFloorZ()), 1, null)){
225 $world->setBlock($this->position, $this);
226 }
227 }elseif($world->getBlock($this->position->up())->canBeReplaced()){
228 $this->ready = true;
229 $world->setBlock($this->position, $this);
230 }
231 }
232
233 public function asItem() : Item{
234 return VanillaItems::BAMBOO();
235 }
236}
describeBlockOnlyState(RuntimeDataDescriber $w)
onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player=null, array &$returnedItems=[])
setLeafSize(int $leafSize)
getSupportType(int $facing)
hasSameTypeId(Block $other)
Definition: Block.php:184
pop(int $count=1)
Definition: Item.php:430