PocketMine-MP 5.33.2 git-919492bdcad8510eb6606439eb77e1c604f1d1ea
Loading...
Searching...
No Matches
SmallDripleaf.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\HorizontalFacingOption;
28use pocketmine\block\utils\HorizontalFacingTrait;
29use pocketmine\block\utils\SupportType;
39use function mt_rand;
40
42 use HorizontalFacingTrait;
43
44 protected bool $top = false;
45
46 public function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
47 $w->enum($this->facing);
48 $w->bool($this->top);
49 }
50
51 public function isTop() : bool{
52 return $this->top;
53 }
54
56 public function setTop(bool $top) : self{
57 $this->top = $top;
58 return $this;
59 }
60
61 private function canBeSupportedBy(Block $block) : bool{
62 //TODO: Moss
63 //TODO: Small Dripleaf also can be placed on dirt, coarse dirt, farmland, grass blocks,
64 // podzol, rooted dirt, mycelium, and mud if these blocks are underwater (needs waterlogging)
65 return $block->getTypeId() === BlockTypeIds::CLAY;
66 }
67
68 public function onNearbyBlockChange() : void{
69 if(!$this->top && !$this->canBeSupportedBy($this->getSide(Facing::DOWN))){
70 $this->position->getWorld()->useBreakOn($this->position);
71 return;
72 }
73 $face = $this->top ? Facing::DOWN : Facing::UP;
74 if(!$this->getSide($face)->hasSameTypeId($this)){
75 $this->position->getWorld()->useBreakOn($this->position);
76 }
77 }
78
79 public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, Facing $face, Vector3 $clickVector, ?Player $player = null) : bool{
80 $block = $blockReplace->getSide(Facing::UP);
81 if($block->getTypeId() !== BlockTypeIds::AIR || !$this->canBeSupportedBy($blockReplace->getSide(Facing::DOWN))){
82 return false;
83 }
84 if($player !== null){
85 $this->facing = HorizontalFacingOption::fromFacing(Facing::opposite($player->getHorizontalFacing()));
86 }
87
88 $tx->addBlock($block->position, VanillaBlocks::SMALL_DRIPLEAF()
89 ->setFacing($this->facing)
90 ->setTop(true)
91 );
92 return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player);
93 }
94
95 public function onInteract(Item $item, Facing $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
96 if($item instanceof Fertilizer && $this->grow($player)){
97 $item->pop();
98 return true;
99 }
100 return false;
101 }
102
103 private function canGrowTo(Position $pos) : bool{
104 $world = $pos->getWorld();
105 if(!$world->isInWorld($pos->getFloorX(), $pos->getFloorY(), $pos->getFloorZ())){
106 return false;
107 }
108 $block = $world->getBlock($pos);
109 return $block->hasSameTypeId($this) || $block->getTypeId() === BlockTypeIds::AIR;
110 }
111
112 private function grow(?Player $player) : bool{
113 $bottomBlock = $this->top ? $this->getSide(Facing::DOWN) : $this;
114 if(!$this->hasSameTypeId($bottomBlock)){
115 return false;
116 }
117 $world = $this->position->getWorld();
118 $tx = new BlockTransaction($world);
119 $height = mt_rand(2, 5);
120 $grown = 0;
121 for($i = 0; $i < $height; $i++){
122 $pos = $bottomBlock->getSide(Facing::UP, $i)->position;
123 if(!$this->canGrowTo($pos)){
124 break;
125 }
126 $block = ++$grown < $height && $this->canGrowTo($pos->getSide(Facing::UP)) ?
127 VanillaBlocks::BIG_DRIPLEAF_STEM() :
128 VanillaBlocks::BIG_DRIPLEAF_HEAD();
129 $tx->addBlock($pos, $block->setFacing($this->facing));
130 }
131 if($grown > 1){
132 $ev = new StructureGrowEvent($bottomBlock, $tx, $player);
133 $ev->call();
134 if(!$ev->isCancelled()){
135 return $tx->apply();
136 }
137 }
138
139 return false;
140 }
141
142 public function getAffectedBlocks() : array{
143 $other = $this->getSide($this->top ? Facing::DOWN : Facing::UP);
144 if($other->hasSameTypeId($this)){
145 return [$this, $other];
146 }
147 return parent::getAffectedBlocks();
148 }
149
150 public function getDropsForCompatibleTool(Item $item) : array{
151 if(!$this->top){
152 return [$this->asItem()];
153 }
154 return [];
155 }
156
157 public function getFlameEncouragement() : int{
158 return 15;
159 }
160
161 public function getFlammability() : int{
162 return 100;
163 }
164
165 public function getSupportType(Facing $facing) : SupportType{
166 return SupportType::NONE;
167 }
168
169 protected function recalculateCollisionBoxes() : array{
170 return [];
171 }
172}
getSide(Facing $side, int $step=1)
Definition Block.php:773
describeBlockOnlyState(RuntimeDataDescriber $w)
onInteract(Item $item, Facing $face, Vector3 $clickVector, ?Player $player=null, array &$returnedItems=[])
place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, Facing $face, Vector3 $clickVector, ?Player $player=null)
pop(int $count=1)
Definition Item.php:435
addBlock(Vector3 $pos, Block $state)
getSide(Facing $side, int $step=1)
Definition Position.php:86