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