PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
Loading...
Searching...
No Matches
BlockBreakInfo.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
28use function get_class;
29
35 public const COMPATIBLE_TOOL_MULTIPLIER = 1.5;
40 public const INCOMPATIBLE_TOOL_MULTIPLIER = 5.0;
41
42 private float $blastResistance;
43
47 public function __construct(
48 private float $hardness,
49 private int $toolType = BlockToolType::NONE,
50 private int $toolHarvestLevel = 0,
51 ?float $blastResistance = null
52 ){
53 $this->blastResistance = $blastResistance ?? $hardness * 5;
54 }
55
56 public static function tier(float $hardness, int $toolType, ToolTier $toolTier, ?float $blastResistance = null) : self{
57 return new self($hardness, $toolType, $toolTier->getHarvestLevel(), $blastResistance);
58 }
59
60 public static function pickaxe(float $hardness, ?ToolTier $toolTier = null, ?float $blastResistance = null) : self{
61 return new self($hardness, BlockToolType::PICKAXE, $toolTier?->getHarvestLevel() ?? 0, $blastResistance);
62 }
63
64 public static function shovel(float $hardness, ?ToolTier $toolTier = null, ?float $blastResistance = null) : self{
65 return new self($hardness, BlockToolType::SHOVEL, $toolTier?->getHarvestLevel() ?? 0, $blastResistance);
66 }
67
68 public static function axe(float $hardness, ?ToolTier $toolTier = null, ?float $blastResistance = null) : self{
69 return new self($hardness, BlockToolType::AXE, $toolTier?->getHarvestLevel() ?? 0, $blastResistance);
70 }
71
72 public static function instant(int $toolType = BlockToolType::NONE, int $toolHarvestLevel = 0) : self{
73 return new self(0.0, $toolType, $toolHarvestLevel, 0.0);
74 }
75
76 public static function indestructible(float $blastResistance = 18000000.0) : self{
77 return new self(-1.0, BlockToolType::NONE, 0, $blastResistance);
78 }
79
83 public function getHardness() : float{
84 return $this->hardness;
85 }
86
90 public function isBreakable() : bool{
91 return $this->hardness >= 0;
92 }
93
97 public function breaksInstantly() : bool{
98 return $this->hardness == 0.0;
99 }
100
104 public function getBlastResistance() : float{
105 return $this->blastResistance;
106 }
107
108 public function getToolType() : int{
109 return $this->toolType;
110 }
111
122 public function getToolHarvestLevel() : int{
123 return $this->toolHarvestLevel;
124 }
125
133 public function isToolCompatible(Item $tool) : bool{
134 if($this->hardness < 0){
135 return false;
136 }
137
138 return $this->toolType === BlockToolType::NONE || $this->toolHarvestLevel === 0 || (
139 ($this->toolType & $tool->getBlockToolType()) !== 0 && $tool->getBlockToolHarvestLevel() >= $this->toolHarvestLevel);
140 }
141
147 public function getBreakTime(Item $item) : float{
148 $base = $this->hardness;
149 if($this->isToolCompatible($item)){
150 $base *= self::COMPATIBLE_TOOL_MULTIPLIER;
151 }else{
152 $base *= self::INCOMPATIBLE_TOOL_MULTIPLIER;
153 }
154
155 $efficiency = $item->getMiningEfficiency(($this->toolType & $item->getBlockToolType()) !== 0);
156 if($efficiency <= 0){
157 throw new \InvalidArgumentException(get_class($item) . " has invalid mining efficiency: expected >= 0, got $efficiency");
158 }
159
160 $base /= $efficiency;
161
162 return $base;
163 }
164}
__construct(private float $hardness, private int $toolType=BlockToolType::NONE, private int $toolHarvestLevel=0, ?float $blastResistance=null)