getNamedTag()->getByte("Unbreakable", 0) !== 0; } /** * Sets whether the item will take damage when used. * @param bool $value */ public function setUnbreakable(bool $value = true){ $this->setNamedTagEntry(new ByteTag("Unbreakable", $value ? 1 : 0)); } /** * Applies damage to the item. * @param int $amount * * @return bool if any damage was applied to the item */ public function applyDamage(int $amount) : bool{ if($this->isUnbreakable() or $this->isBroken()){ return false; } $amount -= $this->getUnbreakingDamageReduction($amount); $this->meta = min($this->meta + $amount, $this->getMaxDurability()); if($this->isBroken()){ $this->onBroken(); } return true; } protected function getUnbreakingDamageReduction(int $amount) : int{ if(($unbreakingLevel = $this->getEnchantmentLevel(Enchantment::UNBREAKING)) > 0){ $negated = 0; $chance = 1 / ($unbreakingLevel + 1); for($i = 0; $i < $amount; ++$i){ if(lcg_value() > $chance){ $negated++; } } return $negated; } return 0; } /** * Called when the item's damage exceeds its maximum durability. */ protected function onBroken() : void{ $this->pop(); } /** * Returns the maximum amount of damage this item can take before it breaks. * * @return int */ abstract public function getMaxDurability() : int; /** * Returns whether the item is broken. * @return bool */ public function isBroken() : bool{ return $this->meta >= $this->getMaxDurability(); } }