PocketMine-MP 5.21.2 git-a6534ecbbbcf369264567d27e5ed70f7f5be9816
Loading...
Searching...
No Matches
Durable.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\item;
25
28use function lcg_value;
29use function min;
30
31abstract class Durable extends Item{
32 protected int $damage = 0;
33 private bool $unbreakable = false;
34
38 public function isUnbreakable() : bool{
39 return $this->unbreakable;
40 }
41
47 public function setUnbreakable(bool $value = true) : self{
48 $this->unbreakable = $value;
49 return $this;
50 }
51
57 public function applyDamage(int $amount) : bool{
58 if($this->isUnbreakable() || $this->isBroken()){
59 return false;
60 }
61
62 $amount -= $this->getUnbreakingDamageReduction($amount);
63
64 $this->damage = min($this->damage + $amount, $this->getMaxDurability());
65 if($this->isBroken()){
66 $this->onBroken();
67 }
68
69 return true;
70 }
71
72 public function getDamage() : int{
73 return $this->damage;
74 }
75
76 public function setDamage(int $damage) : Item{
77 if($damage < 0 || $damage > $this->getMaxDurability()){
78 throw new \InvalidArgumentException("Damage must be in range 0 - " . $this->getMaxDurability());
79 }
80 $this->damage = $damage;
81 return $this;
82 }
83
84 protected function getUnbreakingDamageReduction(int $amount) : int{
85 if(($unbreakingLevel = $this->getEnchantmentLevel(VanillaEnchantments::UNBREAKING())) > 0){
86 $negated = 0;
87
88 $chance = 1 / ($unbreakingLevel + 1);
89 for($i = 0; $i < $amount; ++$i){
90 if(lcg_value() > $chance){
91 $negated++;
92 }
93 }
94
95 return $negated;
96 }
97
98 return 0;
99 }
100
104 protected function onBroken() : void{
105 $this->pop();
106 $this->setDamage(0); //the stack size may be greater than 1 if overstacked by a plugin
107 }
108
112 abstract public function getMaxDurability() : int;
113
117 public function isBroken() : bool{
118 return $this->damage >= $this->getMaxDurability() || $this->isNull();
119 }
120
121 protected function deserializeCompoundTag(CompoundTag $tag) : void{
122 parent::deserializeCompoundTag($tag);
123 $this->unbreakable = $tag->getByte("Unbreakable", 0) !== 0;
124
125 $damage = $tag->getInt("Damage", $this->damage);
126 if($damage !== $this->damage && $damage >= 0 && $damage <= $this->getMaxDurability()){
127 //TODO: out-of-bounds damage should be an error
128 $this->setDamage($damage);
129 }
130 }
131
132 protected function serializeCompoundTag(CompoundTag $tag) : void{
133 parent::serializeCompoundTag($tag);
134 $this->unbreakable ? $tag->setByte("Unbreakable", 1) : $tag->removeTag("Unbreakable");
135 $this->damage !== 0 ? $tag->setInt("Damage", $this->damage) : $tag->removeTag("Damage");
136 }
137}
deserializeCompoundTag(CompoundTag $tag)
Definition Durable.php:121
applyDamage(int $amount)
Definition Durable.php:57
setUnbreakable(bool $value=true)
Definition Durable.php:47
setByte(string $name, int $value)
setInt(string $name, int $value)
getEnchantmentLevel(Enchantment $enchantment)