PocketMine-MP 5.30.2 git-98f04176111e5ecab5e8814ffc69d992bfb64939
Loading...
Searching...
No Matches
Stem.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
33use function array_rand;
34
35abstract class Stem extends Crops{
36 protected int $facing = Facing::UP;
37
38 protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
39 parent::describeBlockOnlyState($w);
40 $w->facingExcept($this->facing, Facing::DOWN);
41 }
42
43 public function getFacing() : int{ return $this->facing; }
44
46 public function setFacing(int $facing) : self{
47 if($facing === Facing::DOWN){
48 throw new \InvalidArgumentException("DOWN is not a valid facing for this block");
49 }
50 $this->facing = $facing;
51 return $this;
52 }
53
54 abstract protected function getPlant() : Block;
55
56 public function onNearbyBlockChange() : void{
57 if($this->facing !== Facing::UP && !$this->getSide($this->facing)->hasSameTypeId($this->getPlant())){
58 $this->position->getWorld()->setBlock($this->position, $this->setFacing(Facing::UP));
59 }
60 parent::onNearbyBlockChange();
61 }
62
63 public function ticksRandomly() : bool{
64 return $this->age < self::MAX_AGE || $this->facing === Facing::UP;
65 }
66
67 public function onRandomTick() : void{
68 if($this->facing === Facing::UP && CropGrowthHelper::canGrow($this)){
69 $world = $this->position->getWorld();
70 if($this->age < self::MAX_AGE){
71 $block = clone $this;
72 ++$block->age;
73 BlockEventHelper::grow($this, $block, null);
74 }else{
75 $grow = $this->getPlant();
76 foreach(Facing::HORIZONTAL as $side){
77 if($this->getSide($side)->hasSameTypeId($grow)){
78 return;
79 }
80 }
81
82 $facing = Facing::HORIZONTAL[array_rand(Facing::HORIZONTAL)];
83 $side = $this->getSide($facing);
84 if($side->getTypeId() === BlockTypeIds::AIR && $side->getSide(Facing::DOWN)->hasTypeTag(BlockTypeTags::DIRT)){
85 if(BlockEventHelper::grow($side, $grow, null)){
86 $this->position->getWorld()->setBlock($this->position, $this->setFacing($facing));
87 }
88 }
89 }
90 }
91 }
92
93 public function getDropsForCompatibleTool(Item $item) : array{
94 //TODO: bit annoying we have to pass an Item instance here
95 //this should not be affected by Fortune, but still follows a binomial distribution
96 return [
97 $this->asItem()->setCount(FortuneDropHelper::binomial(VanillaItems::AIR(), 0, chance: ($this->age + 1) / 15))
98 ];
99 }
100}
setFacing(int $facing)
Definition Stem.php:46
describeBlockOnlyState(RuntimeDataDescriber $w)
Definition Stem.php:38
getDropsForCompatibleTool(Item $item)
Definition Stem.php:93