PocketMine-MP 5.32.2 git-237b304ef9858756b018e44e8f298093f66f823b
Loading...
Searching...
No Matches
GlowLichen.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\MultiAnySupportTrait;
29use pocketmine\block\utils\SupportType;
36use function count;
37use function shuffle;
38
39class GlowLichen extends Transparent implements MultiFacing{
40 use MultiAnySupportTrait;
41
42 public function getLightLevel() : int{
43 return 7;
44 }
45
46 public function isSolid() : bool{
47 return false;
48 }
49
50 protected function recalculateCollisionBoxes() : array{
51 return [];
52 }
53
54 public function getSupportType(int $facing) : SupportType{
55 return SupportType::NONE;
56 }
57
58 public function canBeReplaced() : bool{
59 return true;
60 }
61
65 protected function getInitialPlaceFaces(Block $blockReplace) : array{
66 return $blockReplace instanceof GlowLichen ? $blockReplace->faces : [];
67 }
68
69 private function getSpreadBlock(Block $replace, int $spreadFace) : ?Block{
70 if($replace instanceof self && $replace->hasSameTypeId($this)){
71 if($replace->hasFace($spreadFace)){
72 return null;
73 }
74 $result = $replace;
75 }elseif($replace->getTypeId() === BlockTypeIds::AIR){
76 $result = VanillaBlocks::GLOW_LICHEN();
77 }else{
78 //TODO: if this is a water block, generate a waterlogged block
79 return null;
80 }
81 return $result->setFace($spreadFace, true);
82 }
83
84 private function spread(World $world, Vector3 $replacePos, int $spreadFace) : bool{
85 $supportBlock = $world->getBlock($replacePos->getSide($spreadFace));
86 $supportFace = Facing::opposite($spreadFace);
87
88 if($supportBlock->getSupportType($supportFace) !== SupportType::FULL){
89 return false;
90 }
91
92 $replacedBlock = $supportBlock->getSide($supportFace);
93 $replacementBlock = $this->getSpreadBlock($replacedBlock, Facing::opposite($supportFace));
94 if($replacementBlock === null){
95 return false;
96 }
97
98 return BlockEventHelper::spread($replacedBlock, $replacementBlock, $this);
99 }
100
104 private static function getShuffledSpreadFaces(int $sourceFace) : \Generator{
105 $skipAxis = Facing::axis($sourceFace);
106
107 $faces = Facing::ALL;
108 shuffle($faces);
109 foreach($faces as $spreadFace){
110 if(Facing::axis($spreadFace) !== $skipAxis){
111 yield $spreadFace;
112 }
113 }
114 }
115
116 private function spreadAroundSupport(int $sourceFace) : bool{
117 $world = $this->position->getWorld();
118
119 $supportPos = $this->position->getSide($sourceFace);
120 foreach(self::getShuffledSpreadFaces($sourceFace) as $spreadFace){
121 $replacePos = $supportPos->getSide($spreadFace);
122 if($this->spread($world, $replacePos, Facing::opposite($spreadFace))){
123 return true;
124 }
125 }
126
127 return false;
128 }
129
130 private function spreadAdjacentToSupport(int $sourceFace) : bool{
131 $world = $this->position->getWorld();
132
133 foreach(self::getShuffledSpreadFaces($sourceFace) as $spreadFace){
134 $replacePos = $this->position->getSide($spreadFace);
135 if($this->spread($world, $replacePos, $sourceFace)){
136 return true;
137 }
138 }
139 return false;
140 }
141
142 private function spreadWithinSelf(int $sourceFace) : bool{
143 foreach(self::getShuffledSpreadFaces($sourceFace) as $spreadFace){
144 if(!$this->hasFace($spreadFace) && $this->spread($this->position->getWorld(), $this->position, $spreadFace)){
145 return true;
146 }
147 }
148
149 return false;
150 }
151
152 public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
153 if($item instanceof Fertilizer && count($this->faces) > 0){
154 $shuffledFaces = $this->faces;
155 shuffle($shuffledFaces);
156
157 $spreadMethods = [
158 $this->spreadAroundSupport(...),
159 $this->spreadAdjacentToSupport(...),
160 $this->spreadWithinSelf(...),
161 ];
162 shuffle($spreadMethods);
163
164 foreach($shuffledFaces as $sourceFace){
165 foreach($spreadMethods as $spreadMethod){
166 if($spreadMethod($sourceFace)){
167 $item->pop();
168 break 2;
169 }
170 }
171 }
172
173 return true;
174 }
175 return false;
176 }
177
178 public function getDrops(Item $item) : array{
179 if(($item->getBlockToolType() & BlockToolType::SHEARS) !== 0){
180 return $this->getDropsForCompatibleTool($item);
181 }
182
183 return [];
184 }
185
186 public function getFlameEncouragement() : int{
187 return 15;
188 }
189
190 public function getFlammability() : int{
191 return 100;
192 }
193}
getInitialPlaceFaces(Block $blockReplace)
onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player=null, array &$returnedItems=[])
pop(int $count=1)
Definition Item.php:433