PocketMine-MP 5.33.2 git-919492bdcad8510eb6606439eb77e1c604f1d1ea
Loading...
Searching...
No Matches
CocoaBlock.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\HorizontalFacingOption;
31use pocketmine\block\utils\HorizontalFacingTrait;
32use pocketmine\block\utils\WoodType;
42use function mt_rand;
43
44class CocoaBlock extends Flowable implements Ageable, HorizontalFacing{
45 use HorizontalFacingTrait;
46 use AgeableTrait;
47
48 public const MAX_AGE = 2;
49
50 protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
51 $w->enum($this->facing);
52 $w->boundedIntAuto(0, self::MAX_AGE, $this->age);
53 }
54
55 protected function recalculateCollisionBoxes() : array{
56 $realFacing = $this->facing->toFacing();
57 return [
58 AxisAlignedBB::one()
59 ->squashedCopy(Facing::axis(Facing::rotateY($realFacing, true)), (6 - $this->age) / 16) //sides
60 ->trimmedCopy(Facing::DOWN, (7 - $this->age * 2) / 16)
61 ->trimmedCopy(Facing::UP, 0.25)
62 ->trimmedCopy(Facing::opposite($realFacing), 1 / 16) //gap between log and pod
63 ->trimmedCopy($realFacing, (11 - $this->age * 2) / 16) //outward face
64 ];
65 }
66
67 private function canAttachTo(Block $block) : bool{
68 return $block instanceof Wood && $block->getWoodType() === WoodType::JUNGLE;
69 }
70
71 public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, Facing $face, Vector3 $clickVector, ?Player $player = null) : bool{
72 if(($hzFacing = HorizontalFacingOption::tryFromFacing($face)) !== null && $this->canAttachTo($blockClicked)){
73 $this->facing = $hzFacing;
74 return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player);
75 }
76
77 return false;
78 }
79
80 public function onInteract(Item $item, Facing $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
81 if($item instanceof Fertilizer && $this->grow($player)){
82 $item->pop();
83
84 return true;
85 }
86
87 return false;
88 }
89
90 public function onNearbyBlockChange() : void{
91 if(!$this->canAttachTo($this->getSide(Facing::opposite($this->facing->toFacing())))){
92 $this->position->getWorld()->useBreakOn($this->position);
93 }
94 }
95
96 public function ticksRandomly() : bool{
97 return $this->age < self::MAX_AGE;
98 }
99
100 public function onRandomTick() : void{
101 if(mt_rand(1, 5) === 1){
102 $this->grow();
103 }
104 }
105
106 private function grow(?Player $player = null) : bool{
107 if($this->age < self::MAX_AGE){
108 $block = clone $this;
109 $block->age++;
110 return BlockEventHelper::grow($this, $block, $player);
111 }
112 return false;
113 }
114
115 public function getDropsForCompatibleTool(Item $item) : array{
116 return [
117 VanillaItems::COCOA_BEANS()->setCount($this->age === self::MAX_AGE ? mt_rand(2, 3) : 1)
118 ];
119 }
120
121 public function asItem() : Item{
122 return VanillaItems::COCOA_BEANS();
123 }
124}
place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, Facing $face, Vector3 $clickVector, ?Player $player=null)
describeBlockOnlyState(RuntimeDataDescriber $w)
getDropsForCompatibleTool(Item $item)
onInteract(Item $item, Facing $face, Vector3 $clickVector, ?Player $player=null, array &$returnedItems=[])
pop(int $count=1)
Definition Item.php:435
boundedIntAuto(int $min, int $max, int &$value)