PocketMine-MP 5.15.1 git-5ef247620a7c6301a849b54e5ef1009217729fc8
BambooSapling.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;
29use pocketmine\item\Bamboo as ItemBamboo;
37
38final class BambooSapling extends Flowable{
39 use StaticSupportTrait;
40
41 private bool $ready = false;
42
43 protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
44 $w->bool($this->ready);
45 }
46
47 public function isReady() : bool{ return $this->ready; }
48
50 public function setReady(bool $ready) : self{
51 $this->ready = $ready;
52 return $this;
53 }
54
55 private function canBeSupportedAt(Block $block) : bool{
56 $supportBlock = $block->getSide(Facing::DOWN);
57 return
58 $supportBlock->getTypeId() === BlockTypeIds::GRAVEL ||
59 $supportBlock->hasTypeTag(BlockTypeTags::DIRT) ||
60 $supportBlock->hasTypeTag(BlockTypeTags::MUD) ||
61 $supportBlock->hasTypeTag(BlockTypeTags::SAND);
62 }
63
64 public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
65 if($item instanceof Fertilizer || $item instanceof ItemBamboo){
66 if($this->grow($player)){
67 $item->pop();
68 return true;
69 }
70 }
71 return false;
72 }
73
74 private function grow(?Player $player) : bool{
75 $world = $this->position->getWorld();
76 if(!$world->getBlock($this->position->up())->canBeReplaced()){
77 return false;
78 }
79
80 $tx = new BlockTransaction($world);
81 $bamboo = VanillaBlocks::BAMBOO();
82 $tx->addBlock($this->position, $bamboo)
83 ->addBlock($this->position->up(), (clone $bamboo)->setLeafSize(Bamboo::SMALL_LEAVES));
84
85 $ev = new StructureGrowEvent($this, $tx, $player);
86 $ev->call();
87 if($ev->isCancelled()){
88 return false;
89 }
90
91 return $tx->apply();
92 }
93
94 public function ticksRandomly() : bool{
95 return true;
96 }
97
98 public function onRandomTick() : void{
99 $world = $this->position->getWorld();
100 if($this->ready){
101 $this->ready = false;
102 if($world->getFullLight($this->position) < 9 || !$this->grow(null)){
103 $world->setBlock($this->position, $this);
104 }
105 }elseif($world->getBlock($this->position->up())->canBeReplaced()){
106 $this->ready = true;
107 $world->setBlock($this->position, $this);
108 }
109 }
110
111 public function asItem() : Item{
112 return VanillaItems::BAMBOO();
113 }
114}
describeBlockOnlyState(RuntimeDataDescriber $w)
onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player=null, array &$returnedItems=[])
pop(int $count=1)
Definition: Item.php:430