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