Enchantment: Implemented Unbreaking

This commit is contained in:
Dylan K. Taylor
2018-01-13 19:34:45 +00:00
parent 45b02d92d4
commit 94feecd44b
2 changed files with 21 additions and 1 deletions

View File

@ -23,6 +23,7 @@ declare(strict_types=1);
namespace pocketmine\item;
use pocketmine\item\enchantment\Enchantment;
use pocketmine\nbt\tag\ByteTag;
abstract class Durable extends Item{
@ -54,7 +55,7 @@ abstract class Durable extends Item{
return false;
}
//TODO: Unbreaking enchantment
$amount -= $this->getUnbreakingDamageReduction($amount);
$this->meta += $amount;
if($this->isBroken()){
@ -64,6 +65,23 @@ abstract class Durable extends Item{
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;
}
/**
* Returns whether the item is broken.
* @return bool