PocketMine-MP 5.33.2 git-09cc76ae2b49f1fe3ab0253e6e987fb82bd0a08f
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
65 protected function canBeSupportedBy(Block $block) : bool{
66 return $block->getTypeId() !== BlockTypeIds::FARMLAND && //bedrock-specific thing (bug?)
67 ($block->hasTypeTag(BlockTypeTags::DIRT) || $block->hasTypeTag(BlockTypeTags::MUD));
68 }
69
70 private function canBeSupportedAt(Block $block) : bool{
71 $supportBlock = $block->getSide(Facing::DOWN);
72 return $this->canBeSupportedBy($supportBlock);
73 }
74
75 public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
76 $world = $this->position->getWorld();
77 if($this->age < self::STAGE_MATURE && $item instanceof Fertilizer){
78 $block = clone $this;
79 $block->age++;
80 if(BlockEventHelper::grow($this, $block, $player)){
81 $item->pop();
82 }
83 }elseif(($dropAmount = $this->getBerryDropAmount()) > 0){
84 $world->setBlock($this->position, $this->setAge(self::STAGE_BUSH_NO_BERRIES));
85 $world->dropItem($this->position, $this->asItem()->setCount($dropAmount));
86 $world->addSound($this->position, new SweetBerriesPickSound());
87 }
88
89 return true;
90 }
91
92 public function asItem() : Item{
93 return VanillaItems::SWEET_BERRIES();
94 }
95
96 public function getDropsForCompatibleTool(Item $item) : array{
97 $count = match($this->age){
98 self::STAGE_MATURE => FortuneDropHelper::discrete($item, 2, 3),
99 self::STAGE_BUSH_SOME_BERRIES => FortuneDropHelper::discrete($item, 1, 2),
100 default => 0
101 };
102 return [
103 $this->asItem()->setCount($count)
104 ];
105 }
106
107 public function ticksRandomly() : bool{
108 return $this->age < self::STAGE_MATURE;
109 }
110
111 public function onRandomTick() : void{
112 if($this->age < self::STAGE_MATURE && mt_rand(0, 2) === 1){
113 $block = clone $this;
114 ++$block->age;
115 BlockEventHelper::grow($this, $block, null);
116 }
117 }
118
119 public function hasEntityCollision() : bool{
120 return true;
121 }
122
123 public function onEntityInside(Entity $entity) : bool{
124 if($this->age >= self::STAGE_BUSH_NO_BERRIES && $entity instanceof Living){
125 $entity->resetFallDistance();
126
127 //TODO: in MCPE, this only triggers if moving while inside the bush block - we don't have the system to deal
128 //with that reliably right now
129 $entity->attack(new EntityDamageByBlockEvent($this, $entity, EntityDamageByBlockEvent::CAUSE_CONTACT, 1));
130 }
131 return true;
132 }
133}
hasTypeTag(string $tag)
Definition Block.php:215
onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player=null, array &$returnedItems=[])
pop(int $count=1)
Definition Item.php:431