32 protected int $damage = 0;
33 private bool $unbreakable =
false;
39 return $this->unbreakable;
48 $this->unbreakable = $value;
58 if($this->isUnbreakable() || $this->isBroken()){
62 $amount -= $this->getUnbreakingDamageReduction($amount);
64 $this->damage = min($this->damage + $amount, $this->getMaxDurability());
65 if($this->isBroken()){
72 public function getDamage() : int{
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());
80 $this->damage = $damage;
84 protected function getUnbreakingDamageReduction(
int $amount) : int{
88 $chance = 1 / ($unbreakingLevel + 1);
89 for($i = 0; $i < $amount; ++$i){
90 if(lcg_value() > $chance){
118 return $this->damage >= $this->getMaxDurability() || $this->isNull();
122 parent::deserializeCompoundTag($tag);
123 $this->unbreakable = $tag->getByte(
"Unbreakable", 0) !== 0;
125 $damage = $tag->getInt(
"Damage", $this->damage);
126 if($damage !== $this->damage && $damage >= 0 && $damage <= $this->getMaxDurability()){
128 $this->setDamage($damage);
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");