PocketMine-MP 5.16.1 git-585aee9386a787c95e73dd0a05ffca8329606b68
PitcherCrop.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;
29use pocketmine\block\utils\StaticSupportTrait;
40
41final class PitcherCrop extends Flowable{
42 use AgeableTrait;
43 use StaticSupportTrait;
44
45 public const MAX_AGE = 2;
46
47 private function canBeSupportedAt(Block $block) : bool{
48 return $block->getSide(Facing::DOWN)->getTypeId() === BlockTypeIds::FARMLAND;
49 }
50
51 protected function recalculateCollisionBoxes() : array{
52 $widthTrim = $this->age === 0 ? 5 : 3;
53 $heightTrim = $this->age === 0 ? 13 : 11;
54 return [
56 ->trim(Facing::UP, $heightTrim / 16)
57 ->squash(Axis::X, $widthTrim / 16)
58 ->squash(Axis::Z, $widthTrim / 16)
59 ->extend(Facing::DOWN, 1 / 16) //presumably this is to correct for farmland being 15/16 of a block tall
60 ];
61 }
62
63 private function grow(?Player $player) : bool{
64 if($this->age > self::MAX_AGE){
65 return false;
66 }
67
68 if($this->age === self::MAX_AGE){
69 $up = $this->getSide(Facing::UP);
70 if($up->getTypeId() !== BlockTypeIds::AIR){
71 return false;
72 }
73
74 $tx = new BlockTransaction($this->position->getWorld());
75 $tx->addBlock($this->position, VanillaBlocks::DOUBLE_PITCHER_CROP()->setTop(false));
76 $tx->addBlock($this->position->up(), VanillaBlocks::DOUBLE_PITCHER_CROP()->setTop(true));
77
78 $ev = new StructureGrowEvent($this, $tx, $player);
79 $ev->call();
80
81 return !$ev->isCancelled() && $tx->apply();
82 }
83
84 return BlockEventHelper::grow($this, (clone $this)->setAge($this->age + 1), $player);
85 }
86
87 public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
88 if($item instanceof Fertilizer && $this->grow($player)){
89 $item->pop();
90 return true;
91 }
92
93 return false;
94 }
95
96 public function ticksRandomly() : bool{
97 return true;
98 }
99
100 public function onRandomTick() : void{
101 if(CropGrowthHelper::canGrow($this)){
102 $this->grow(null);
103 }
104 }
105
106 public function asItem() : Item{
107 return VanillaItems::PITCHER_POD();
108 }
109}
getSide(int $side, int $step=1)
Definition: Block.php:768
onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player=null, array &$returnedItems=[])
Definition: PitcherCrop.php:87
pop(int $count=1)
Definition: Item.php:430