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