PocketMine-MP 5.39.3 git-21ae710729750cd637333d673bbbbbc598fc659e
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 private bool $explosionHarvestable;
44
49 public function __construct(
50 private float $hardness,
51 private int $toolType = BlockToolType::NONE,
52 private int $toolHarvestLevel = 0,
53 ?float $blastResistance = null,
54 ?bool $explosionHarvestable = null,
55 ){
56 $this->blastResistance = $blastResistance ?? $hardness * 5;
57 $this->explosionHarvestable = $explosionHarvestable ?? ($this->toolType & BlockToolType::PICKAXE) !== 0;
58 }
59
60 public static function tier(float $hardness, int $toolType, ToolTier $toolTier, ?float $blastResistance = null) : self{
61 return new self($hardness, $toolType, $toolTier->getHarvestLevel(), $blastResistance);
62 }
63
64 public static function pickaxe(float $hardness, ?ToolTier $toolTier = null, ?float $blastResistance = null) : self{
65 return new self($hardness, BlockToolType::PICKAXE, $toolTier?->getHarvestLevel() ?? 0, $blastResistance);
66 }
67
68 public static function shovel(float $hardness, ?ToolTier $toolTier = null, ?float $blastResistance = null) : self{
69 return new self($hardness, BlockToolType::SHOVEL, $toolTier?->getHarvestLevel() ?? 0, $blastResistance);
70 }
71
72 public static function axe(float $hardness, ?ToolTier $toolTier = null, ?float $blastResistance = null) : self{
73 return new self($hardness, BlockToolType::AXE, $toolTier?->getHarvestLevel() ?? 0, $blastResistance);
74 }
75
76 public static function instant(int $toolType = BlockToolType::NONE, int $toolHarvestLevel = 0) : self{
77 return new self(0.0, $toolType, $toolHarvestLevel, 0.0);
78 }
79
80 public static function indestructible(float $blastResistance = 18000003.75) : self{
81 return new self(-1.0, BlockToolType::NONE, 0, $blastResistance);
82 }
83
87 public function getHardness() : float{
88 return $this->hardness;
89 }
90
94 public function isBreakable() : bool{
95 return $this->hardness >= 0;
96 }
97
101 public function breaksInstantly() : bool{
102 return $this->hardness === 0.0;
103 }
104
108 public function getBlastResistance() : float{
109 return $this->blastResistance;
110 }
111
112 public function getToolType() : int{
113 return $this->toolType;
114 }
115
126 public function getToolHarvestLevel() : int{
127 return $this->toolHarvestLevel;
128 }
129
137 public function isToolCompatible(Item $tool) : bool{
138 if($this->hardness < 0){
139 return false;
140 }
141
142 return $this->toolType === BlockToolType::NONE || $this->toolHarvestLevel === 0 || (
143 ($this->toolType & $tool->getBlockToolType()) !== 0 && $tool->getBlockToolHarvestLevel() >= $this->toolHarvestLevel);
144 }
145
151 public function getBreakTime(Item $item) : float{
152 $base = $this->hardness;
153 if($this->isToolCompatible($item)){
154 $base *= self::COMPATIBLE_TOOL_MULTIPLIER;
155 }else{
156 $base *= self::INCOMPATIBLE_TOOL_MULTIPLIER;
157 }
158
159 $efficiency = $item->getMiningEfficiency(($this->toolType & $item->getBlockToolType()) !== 0);
160 if($efficiency <= 0){
161 throw new \InvalidArgumentException(get_class($item) . " must have a positive mining efficiency, but got $efficiency");
162 }
163
164 $base /= $efficiency;
165
166 return $base;
167 }
168
176 public function isExplosionHarvestable() : bool{ return $this->explosionHarvestable; }
177}
__construct(private float $hardness, private int $toolType=BlockToolType::NONE, private int $toolHarvestLevel=0, ?float $blastResistance=null, ?bool $explosionHarvestable=null,)