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