PocketMine-MP 5.15.1 git-5ef247620a7c6301a849b54e5ef1009217729fc8
BaseBigDripleaf.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\HorizontalFacingTrait;
27use pocketmine\block\utils\SupportType;
35
36abstract class BaseBigDripleaf extends Transparent{
37 use HorizontalFacingTrait;
38
39 abstract protected function isHead() : bool;
40
41 private function canBeSupportedBy(Block $block, bool $head) : bool{
42 //TODO: Moss block
43 return
44 ($block instanceof BaseBigDripleaf && $block->isHead() === $head) ||
45 $block->getTypeId() === BlockTypeIds::CLAY ||
46 $block->hasTypeTag(BlockTypeTags::DIRT) ||
47 $block->hasTypeTag(BlockTypeTags::MUD);
48 }
49
50 public function onNearbyBlockChange() : void{
51 if(
52 (!$this->isHead() && !$this->getSide(Facing::UP) instanceof BaseBigDripleaf) ||
53 !$this->canBeSupportedBy($this->getSide(Facing::DOWN), false)
54 ){
55 $this->position->getWorld()->useBreakOn($this->position);
56 }
57 }
58
59 public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{
60 $block = $blockReplace->getSide(Facing::DOWN);
61 if(!$this->canBeSupportedBy($block, true)){
62 return false;
63 }
64 if($player !== null){
65 $this->facing = Facing::opposite($player->getHorizontalFacing());
66 }
67 if($block instanceof BaseBigDripleaf){
68 $this->facing = $block->facing;
69 $tx->addBlock($block->position, VanillaBlocks::BIG_DRIPLEAF_STEM()->setFacing($this->facing));
70 }
71 return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player);
72 }
73
74 public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
75 if($item instanceof Fertilizer && $this->grow($player)){
76 $item->pop();
77 return true;
78 }
79 return false;
80 }
81
82 private function seekToHead() : ?BaseBigDripleaf{
83 if($this->isHead()){
84 return $this;
85 }
86 $step = 1;
87 while(($next = $this->getSide(Facing::UP, $step)) instanceof BaseBigDripleaf){
88 if($next->isHead()){
89 return $next;
90 }
91 $step++;
92 }
93 return null;
94 }
95
96 private function grow(?Player $player) : bool{
97 $head = $this->seekToHead();
98 if($head === null){
99 return false;
100 }
101 $pos = $head->position;
102 $up = $pos->up();
103 $world = $pos->getWorld();
104 if(
105 !$world->isInWorld($up->getFloorX(), $up->getFloorY(), $up->getFloorZ()) ||
106 $world->getBlock($up)->getTypeId() !== BlockTypeIds::AIR
107 ){
108 return false;
109 }
110
111 $tx = new BlockTransaction($world);
112
113 $tx->addBlock($pos, VanillaBlocks::BIG_DRIPLEAF_STEM()->setFacing($head->facing));
114 $tx->addBlock($up, VanillaBlocks::BIG_DRIPLEAF_HEAD()->setFacing($head->facing));
115
116 $ev = new StructureGrowEvent($head, $tx, $player);
117 $ev->call();
118
119 if(!$ev->isCancelled()){
120 return $tx->apply();
121 }
122 return false;
123 }
124
125 public function getFlameEncouragement() : int{
126 return 15;
127 }
128
129 public function getFlammability() : int{
130 return 100;
131 }
132
133 public function getSupportType(int $facing) : SupportType{
134 return SupportType::NONE;
135 }
136}
place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player=null)
onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player=null, array &$returnedItems=[])
hasTypeTag(string $tag)
Definition: Block.php:212
getSide(int $side, int $step=1)
Definition: Block.php:768
pop(int $count=1)
Definition: Item.php:430
static opposite(int $direction)
Definition: Facing.php:108
addBlock(Vector3 $pos, Block $state)