PocketMine-MP 5.25.1 git-694aa17cc916a954b10fe12721c81b1dc73eecd5
Loading...
Searching...
No Matches
ChorusPlant.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\StaticSupportTrait;
32use function mt_rand;
33
34final class ChorusPlant extends Flowable{
35 use StaticSupportTrait;
36
41 protected array $connections = [];
42
43 protected function recalculateCollisionBoxes() : array{
44 $bb = AxisAlignedBB::one();
45 foreach(Facing::ALL as $facing){
46 if(!isset($this->connections[$facing])){
47 $bb->trim($facing, 2 / 16);
48 }
49 }
50
51 return [$bb];
52 }
53
54 public function readStateFromWorld() : Block{
55 parent::readStateFromWorld();
56
57 $this->collisionBoxes = null;
58
59 foreach(Facing::ALL as $facing){
60 $block = $this->getSide($facing);
61 if(match($block->getTypeId()){
62 BlockTypeIds::END_STONE, BlockTypeIds::CHORUS_FLOWER, $this->getTypeId() => true,
63 default => false
64 }){
65 $this->connections[$facing] = true;
66 }else{
67 unset($this->connections[$facing]);
68 }
69 }
70
71 return $this;
72 }
73
74 private function canBeSupportedBy(Block $block) : bool{
75 return $block->hasSameTypeId($this) || $block->getTypeId() === BlockTypeIds::END_STONE;
76 }
77
78 private function canBeSupportedAt(Block $block) : bool{
79 $position = $block->position;
80 $world = $position->getWorld();
81
82 $down = $world->getBlock($position->down());
83 $verticalAir = $down->getTypeId() === BlockTypeIds::AIR || $world->getBlock($position->up())->getTypeId() === BlockTypeIds::AIR;
84
85 foreach($position->sidesAroundAxis(Axis::Y) as $sidePosition){
86 $block = $world->getBlock($sidePosition);
87
88 if($block->getTypeId() === BlockTypeIds::CHORUS_PLANT){
89 if(!$verticalAir){
90 return false;
91 }
92
93 if($this->canBeSupportedBy($block->getSide(Facing::DOWN))){
94 return true;
95 }
96 }
97 }
98
99 return $this->canBeSupportedBy($down);
100 }
101
102 public function getDropsForCompatibleTool(Item $item) : array{
103 if(mt_rand(0, 1) === 1){
104 return [VanillaItems::CHORUS_FRUIT()];
105 }
106
107 return [];
108 }
109}