PocketMine-MP 5.15.1 git-5ef247620a7c6301a849b54e5ef1009217729fc8
DoublePitcherCrop.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\AgeableTrait;
39
40final class DoublePitcherCrop extends DoublePlant{
41 use AgeableTrait {
42 describeBlockOnlyState as describeAge;
43 }
44
45 public const MAX_AGE = 1;
46
47 public function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
48 parent::describeBlockOnlyState($w);
49 $this->describeAge($w);
50 }
51
52 protected function recalculateCollisionBoxes() : array{
53 if($this->top){
54 return [];
55 }
56
57 //the pod exists only in the bottom half of the plant
58 return [
60 ->trim(Facing::UP, 11 / 16)
61 ->squash(Axis::X, 3 / 16)
62 ->squash(Axis::Z, 3 / 16)
63 ->extend(Facing::DOWN, 1 / 16) //presumably this is to correct for farmland being 15/16 of a block tall
64 ];
65 }
66
67 private function grow(?Player $player) : bool{
68 if($this->age >= self::MAX_AGE){
69 return false;
70 }
71
72 $bottom = $this->top ? $this->getSide(Facing::DOWN) : $this;
73 $top = $this->top ? $this : $this->getSide(Facing::UP);
74 if($top->getTypeId() !== BlockTypeIds::AIR && !$top->hasSameTypeId($this)){
75 return false;
76 }
77
78 $newState = (clone $this)->setAge($this->age + 1);
79
80 $tx = new BlockTransaction($this->position->getWorld());
81 $tx->addBlock($bottom->position, (clone $newState)->setTop(false));
82 $tx->addBlock($top->position, (clone $newState)->setTop(true));
83
84 $ev = new StructureGrowEvent($bottom, $tx, $player);
85 $ev->call();
86
87 return !$ev->isCancelled() && $tx->apply();
88
89 }
90
91 public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
92 if($item instanceof Fertilizer && $this->grow($player)){
93 $item->pop();
94 return true;
95 }
96
97 return false;
98 }
99
100 public function ticksRandomly() : bool{
101 return $this->age < self::MAX_AGE && !$this->top;
102 }
103
104 public function onRandomTick() : void{
105 //only the bottom half of the plant can grow randomly
106 if(CropGrowthHelper::canGrow($this) && !$this->top){
107 $this->grow(null);
108 }
109 }
110
111 public function getDropsForCompatibleTool(Item $item) : array{
112 return [
113 $this->age >= self::MAX_AGE ? VanillaBlocks::PITCHER_PLANT()->asItem() : VanillaItems::PITCHER_POD()
114 ];
115 }
116
117 public function asItem() : Item{
118 return VanillaItems::PITCHER_POD();
119 }
120}
describeBlockOnlyState(RuntimeDataDescriber $w)
onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player=null, array &$returnedItems=[])
pop(int $count=1)
Definition: Item.php:430