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