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