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