PocketMine-MP 5.21.2 git-b2aa6396c3cc2cafdd815eacc360e1ad89599899
Loading...
Searching...
No Matches
FlowerPot.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\tile\FlowerPot as TileFlowerPot;
27use pocketmine\block\utils\StaticSupportTrait;
33use function assert;
34
35class FlowerPot extends Flowable{
36 use StaticSupportTrait;
37
38 protected ?Block $plant = null;
39
40 public function readStateFromWorld() : Block{
41 parent::readStateFromWorld();
42 $tile = $this->position->getWorld()->getTile($this->position);
43 if($tile instanceof TileFlowerPot){
44 $this->setPlant($tile->getPlant());
45 }else{
46 $this->setPlant(null);
47 }
48
49 return $this;
50 }
51
52 public function writeStateToWorld() : void{
53 parent::writeStateToWorld();
54
55 $tile = $this->position->getWorld()->getTile($this->position);
56 assert($tile instanceof TileFlowerPot);
57 $tile->setPlant($this->plant);
58 }
59
60 public function getPlant() : ?Block{
61 return $this->plant;
62 }
63
65 public function setPlant(?Block $plant) : self{
66 if($plant === null || $plant instanceof Air){
67 $this->plant = null;
68 }else{
69 $this->plant = clone $plant;
70 }
71 return $this;
72 }
73
74 public function canAddPlant(Block $block) : bool{
75 if($this->plant !== null){
76 return false;
77 }
78
79 return $this->isValidPlant($block);
80 }
81
82 private function isValidPlant(Block $block) : bool{
83 return $block->hasTypeTag(BlockTypeTags::POTTABLE_PLANTS);
84 }
85
89 protected function recalculateCollisionBoxes() : array{
90 return [AxisAlignedBB::one()->contract(3 / 16, 0, 3 / 16)->trim(Facing::UP, 5 / 8)];
91 }
92
93 private function canBeSupportedAt(Block $block) : bool{
94 return $block->getAdjacentSupportType(Facing::DOWN)->hasCenterSupport();
95 }
96
97 public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
98 $world = $this->position->getWorld();
99 $plant = $item->getBlock();
100 if($this->plant !== null){
101 if($this->isValidPlant($plant)){
102 //for some reason, vanilla doesn't remove the contents of the pot if the held item is plantable
103 //and will also cause a new plant to be placed if clicking on the side
104 return false;
105 }
106
107 $removedItems = [$this->plant->asItem()];
108 if($player !== null){
109 //this one just has to be a weirdo :(
110 //this is the only block that directly adds items to the player inventory instead of just dropping items
111 $removedItems = $player->getInventory()->addItem(...$removedItems);
112 }
113 foreach($removedItems as $drops){
114 $world->dropItem($this->position->add(0.5, 0.5, 0.5), $drops);
115 }
116
117 $this->setPlant(null);
118 $world->setBlock($this->position, $this);
119 return true;
120 }elseif($this->isValidPlant($plant)){
121 $this->setPlant($plant);
122 $item->pop();
123 $world->setBlock($this->position, $this);
124
125 return true;
126 }
127
128 return false;
129 }
130
131 public function getDropsForCompatibleTool(Item $item) : array{
132 $items = parent::getDropsForCompatibleTool($item);
133 if($this->plant !== null){
134 $items[] = $this->plant->asItem();
135 }
136
137 return $items;
138 }
139
140 public function getPickedItem(bool $addUserData = false) : Item{
141 return $this->plant !== null ? $this->plant->asItem() : parent::getPickedItem($addUserData);
142 }
143}
getDropsForCompatibleTool(Item $item)
getPickedItem(bool $addUserData=false)
setPlant(?Block $plant)
Definition FlowerPot.php:65
onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player=null, array &$returnedItems=[])
Definition FlowerPot.php:97
getBlock(?int $clickedFace=null)
Definition Item.php:491
pop(int $count=1)
Definition Item.php:430