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