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