PocketMine-MP 5.32.2 git-237b304ef9858756b018e44e8f298093f66f823b
Loading...
Searching...
No Matches
SweetBerryBush.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\AgeableTrait;
30use pocketmine\block\utils\StaticSupportTrait;
41use function mt_rand;
42
43class SweetBerryBush extends Flowable implements Ageable{
44 use AgeableTrait;
45 use StaticSupportTrait;
46
47 public const STAGE_SAPLING = 0;
48 public const STAGE_BUSH_NO_BERRIES = 1;
49 public const STAGE_BUSH_SOME_BERRIES = 2;
50 public const STAGE_MATURE = 3;
51 public const MAX_AGE = self::STAGE_MATURE;
52
53 public function getBerryDropAmount() : int{
54 if($this->age === self::STAGE_MATURE){
55 return mt_rand(2, 3);
56 }elseif($this->age >= self::STAGE_BUSH_SOME_BERRIES){
57 return mt_rand(1, 2);
58 }
59 return 0;
60 }
61
62 private function canBeSupportedAt(Block $block) : bool{
63 $supportBlock = $block->getSide(Facing::DOWN);
64 return $supportBlock->getTypeId() !== BlockTypeIds::FARMLAND && //bedrock-specific thing (bug?)
65 ($supportBlock->hasTypeTag(BlockTypeTags::DIRT) || $supportBlock->hasTypeTag(BlockTypeTags::MUD));
66 }
67
68 public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
69 $world = $this->position->getWorld();
70 if($this->age < self::STAGE_MATURE && $item instanceof Fertilizer){
71 $block = clone $this;
72 $block->age++;
73 if(BlockEventHelper::grow($this, $block, $player)){
74 $item->pop();
75 }
76 }elseif(($dropAmount = $this->getBerryDropAmount()) > 0){
77 $world->setBlock($this->position, $this->setAge(self::STAGE_BUSH_NO_BERRIES));
78 $world->dropItem($this->position, $this->asItem()->setCount($dropAmount));
79 $world->addSound($this->position, new SweetBerriesPickSound());
80 }
81
82 return true;
83 }
84
85 public function asItem() : Item{
86 return VanillaItems::SWEET_BERRIES();
87 }
88
89 public function getDropsForCompatibleTool(Item $item) : array{
90 $count = match($this->age){
91 self::STAGE_MATURE => FortuneDropHelper::discrete($item, 2, 3),
92 self::STAGE_BUSH_SOME_BERRIES => FortuneDropHelper::discrete($item, 1, 2),
93 default => 0
94 };
95 return [
96 $this->asItem()->setCount($count)
97 ];
98 }
99
100 public function ticksRandomly() : bool{
101 return $this->age < self::STAGE_MATURE;
102 }
103
104 public function onRandomTick() : void{
105 if($this->age < self::STAGE_MATURE && mt_rand(0, 2) === 1){
106 $block = clone $this;
107 ++$block->age;
108 BlockEventHelper::grow($this, $block, null);
109 }
110 }
111
112 public function hasEntityCollision() : bool{
113 return true;
114 }
115
116 public function onEntityInside(Entity $entity) : bool{
117 if($this->age >= self::STAGE_BUSH_NO_BERRIES && $entity instanceof Living){
118 $entity->resetFallDistance();
119
120 //TODO: in MCPE, this only triggers if moving while inside the bush block - we don't have the system to deal
121 //with that reliably right now
122 $entity->attack(new EntityDamageByBlockEvent($this, $entity, EntityDamageByBlockEvent::CAUSE_CONTACT, 1));
123 }
124 return true;
125 }
126}
getSide(int $side, int $step=1)
Definition Block.php:773
onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player=null, array &$returnedItems=[])
pop(int $count=1)
Definition Item.php:433