PocketMine-MP 5.33.2 git-919492bdcad8510eb6606439eb77e1c604f1d1ea
Loading...
Searching...
No Matches
Vine.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\HorizontalFacingOption;
34use function array_intersect_key;
35use function count;
36use function spl_object_id;
37
38class Vine extends Flowable{
39
41 protected array $faces = [];
42
43 protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
44 $w->enumSet($this->faces, HorizontalFacingOption::cases());
45 }
46
48 public function getFaces() : array{ return $this->faces; }
49
50 public function hasFace(HorizontalFacingOption $face) : bool{
51 return isset($this->faces[spl_object_id($face)]);
52 }
53
58 public function setFaces(array $faces) : self{
59 $uniqueFaces = [];
60 foreach($faces as $face){
61 if(!$face instanceof HorizontalFacingOption){
62 throw new \InvalidArgumentException("Expected array of HorizontalFacingOption");
63 }
64 $uniqueFaces[spl_object_id($face)] = $face;
65 }
66 $this->faces = $uniqueFaces;
67 return $this;
68 }
69
71 public function setFace(HorizontalFacingOption $face, bool $value) : self{
72 if($value){
73 $this->faces[spl_object_id($face)] = $face;
74 }else{
75 unset($this->faces[spl_object_id($face)]);
76 }
77 return $this;
78 }
79
80 public function hasEntityCollision() : bool{
81 return true;
82 }
83
84 public function canClimb() : bool{
85 return true;
86 }
87
88 public function canBeReplaced() : bool{
89 return true;
90 }
91
92 public function onEntityInside(Entity $entity) : bool{
93 $entity->resetFallDistance();
94 return true;
95 }
96
97 protected function recalculateCollisionBoxes() : array{
98 return [];
99 }
100
101 public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, Facing $face, Vector3 $clickVector, ?Player $player = null) : bool{
102 $oppositeFace = Facing::opposite($face);
103 $hzFacing = HorizontalFacingOption::tryFromFacing($oppositeFace);
104 if($hzFacing === null || !$blockReplace->getSide($oppositeFace)->isFullCube()){
105 return false;
106 }
107
108 $this->faces = $blockReplace instanceof Vine ? $blockReplace->faces : [];
109 $this->faces[spl_object_id($hzFacing)] = $hzFacing;
110
111 return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player);
112 }
113
114 public function onNearbyBlockChange() : void{
115 $changed = false;
116
117 $up = $this->getSide(Facing::UP);
118 //check which faces have corresponding vines in the block above
119 $supportedFaces = $up instanceof Vine ? array_intersect_key($this->faces, $up->faces) : [];
120
121 foreach($this->faces as $face){
122 if(!isset($supportedFaces[spl_object_id($face)]) && !$this->getSide($face->toFacing())->isSolid()){
123 unset($this->faces[spl_object_id($face)]);
124 $changed = true;
125 }
126 }
127
128 if($changed){
129 $world = $this->position->getWorld();
130 if(count($this->faces) === 0){
131 $world->useBreakOn($this->position);
132 }else{
133 $world->setBlock($this->position, $this);
134 }
135 }
136 }
137
138 public function ticksRandomly() : bool{
139 return true;
140 }
141
142 public function onRandomTick() : void{
143 //TODO: vine growth
144 }
145
146 public function getDrops(Item $item) : array{
147 if(($item->getBlockToolType() & BlockToolType::SHEARS) !== 0){
148 return $this->getDropsForCompatibleTool($item);
149 }
150
151 return [];
152 }
153
154 public function getFlameEncouragement() : int{
155 return 15;
156 }
157
158 public function getFlammability() : int{
159 return 100;
160 }
161}
getSide(Facing $side, int $step=1)
Definition Block.php:773
setFace(HorizontalFacingOption $face, bool $value)
Definition Vine.php:71
describeBlockOnlyState(RuntimeDataDescriber $w)
Definition Vine.php:43
place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, Facing $face, Vector3 $clickVector, ?Player $player=null)
Definition Vine.php:101
onEntityInside(Entity $entity)
Definition Vine.php:92
setFaces(array $faces)
Definition Vine.php:58