PocketMine-MP 5.15.1 git-5ef247620a7c6301a849b54e5ef1009217729fc8
Sapling.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\SaplingType;
27use pocketmine\block\utils\StaticSupportTrait;
37use function mt_rand;
38
39class Sapling extends Flowable{
40 use StaticSupportTrait;
41
42 protected bool $ready = false;
43
44 private SaplingType $saplingType;
45
46 public function __construct(BlockIdentifier $idInfo, string $name, BlockTypeInfo $typeInfo, SaplingType $saplingType){
47 parent::__construct($idInfo, $name, $typeInfo);
48 $this->saplingType = $saplingType;
49 }
50
51 protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
52 $w->bool($this->ready);
53 }
54
55 public function isReady() : bool{ return $this->ready; }
56
58 public function setReady(bool $ready) : self{
59 $this->ready = $ready;
60 return $this;
61 }
62
63 private function canBeSupportedAt(Block $block) : bool{
64 $supportBlock = $block->getSide(Facing::DOWN);
65 return $supportBlock->hasTypeTag(BlockTypeTags::DIRT) || $supportBlock->hasTypeTag(BlockTypeTags::MUD);
66 }
67
68 public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
69 if($item instanceof Fertilizer && $this->grow($player)){
70 $item->pop();
71
72 return true;
73 }
74
75 return false;
76 }
77
78 public function ticksRandomly() : bool{
79 return true;
80 }
81
82 public function onRandomTick() : void{
83 $world = $this->position->getWorld();
84 if($world->getFullLightAt($this->position->getFloorX(), $this->position->getFloorY(), $this->position->getFloorZ()) >= 8 && mt_rand(1, 7) === 1){
85 if($this->ready){
86 $this->grow(null);
87 }else{
88 $this->ready = true;
89 $world->setBlock($this->position, $this);
90 }
91 }
92 }
93
94 private function grow(?Player $player) : bool{
95 $random = new Random(mt_rand());
96 $tree = TreeFactory::get($random, $this->saplingType->getTreeType());
97 $transaction = $tree?->getBlockTransaction($this->position->getWorld(), $this->position->getFloorX(), $this->position->getFloorY(), $this->position->getFloorZ(), $random);
98 if($transaction === null){
99 return false;
100 }
101
102 $ev = new StructureGrowEvent($this, $transaction, $player);
103 $ev->call();
104 if(!$ev->isCancelled()){
105 return $transaction->apply();
106 }
107 return false;
108 }
109
110 public function getFuelTime() : int{
111 return 100;
112 }
113}
hasTypeTag(string $tag)
Definition: Block.php:212
onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player=null, array &$returnedItems=[])
Definition: Sapling.php:68
setReady(bool $ready)
Definition: Sapling.php:58
describeBlockOnlyState(RuntimeDataDescriber $w)
Definition: Sapling.php:51
pop(int $count=1)
Definition: Item.php:430