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