PocketMine-MP 5.16.1 git-585aee9386a787c95e73dd0a05ffca8329606b68
Sugarcane.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;
28use pocketmine\block\utils\StaticSupportTrait;
36
37class Sugarcane extends Flowable{
38 use AgeableTrait;
39 use StaticSupportTrait;
40
41 public const MAX_AGE = 15;
42
43 private function seekToBottom() : Position{
44 $world = $this->position->getWorld();
45 $bottom = $this->position;
46 while(($next = $world->getBlock($bottom->down()))->hasSameTypeId($this)){
47 $bottom = $next->position;
48 }
49 return $bottom;
50 }
51
52 private function grow(Position $pos, ?Player $player = null) : bool{
53 $grew = false;
54 $world = $pos->getWorld();
55 for($y = 1; $y < 3; ++$y){
56 if(!$world->isInWorld($pos->x, $pos->y + $y, $pos->z)){
57 break;
58 }
59 $b = $world->getBlockAt($pos->x, $pos->y + $y, $pos->z);
60 if($b->getTypeId() === BlockTypeIds::AIR){
61 if(BlockEventHelper::grow($b, VanillaBlocks::SUGARCANE(), $player)){
62 $grew = true;
63 }else{
64 break;
65 }
66 }elseif(!$b->hasSameTypeId($this)){
67 break;
68 }
69 }
70 $this->age = 0;
71 $world->setBlock($pos, $this);
72 return $grew;
73 }
74
75 public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
76 if($item instanceof Fertilizer){
77 if($this->grow($this->seekToBottom(), $player)){
78 $item->pop();
79 }
80
81 return true;
82 }
83
84 return false;
85 }
86
87 private function canBeSupportedAt(Block $block) : bool{
88 $supportBlock = $block->getSide(Facing::DOWN);
89 return $supportBlock->hasSameTypeId($this) ||
90 $supportBlock->hasTypeTag(BlockTypeTags::MUD) ||
91 $supportBlock->hasTypeTag(BlockTypeTags::DIRT) ||
92 $supportBlock->hasTypeTag(BlockTypeTags::SAND);
93 }
94
95 public function ticksRandomly() : bool{
96 return true;
97 }
98
99 public function onRandomTick() : void{
100 if(!$this->getSide(Facing::DOWN)->hasSameTypeId($this)){
101 if($this->age === self::MAX_AGE){
102 $this->grow($this->position);
103 }else{
104 ++$this->age;
105 $this->position->getWorld()->setBlock($this->position, $this);
106 }
107 }
108 }
109
110 public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{
111 $down = $blockReplace->getSide(Facing::DOWN);
112 if($down->hasSameTypeId($this)){
113 return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player);
114 }
115
116 //support criteria are checked by FixedSupportTrait, but this part applies to placement only
117 foreach(Facing::HORIZONTAL as $side){
118 $sideBlock = $down->getSide($side);
119 if($sideBlock instanceof Water || $sideBlock instanceof FrostedIce){
120 return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player);
121 }
122 }
123
124 return false;
125 }
126}
onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player=null, array &$returnedItems=[])
Definition: Sugarcane.php:75
place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player=null)
Definition: Sugarcane.php:110
pop(int $count=1)
Definition: Item.php:430