mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-07 18:32:55 +00:00
Enchantment: Implemented Unbreaking
This commit is contained in:
@ -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
|
||||
|
Reference in New Issue
Block a user