Implemented armor durability

This commit is contained in:
Dylan K. Taylor 2018-03-09 13:21:05 +00:00
parent 74cff89df3
commit e06b78b0ee
22 changed files with 123 additions and 1 deletions

View File

@ -44,6 +44,7 @@ use pocketmine\nbt\tag\FloatTag;
use pocketmine\nbt\tag\IntTag;
use pocketmine\nbt\tag\ListTag;
use pocketmine\network\mcpe\protocol\EntityEventPacket;
use pocketmine\network\mcpe\protocol\LevelSoundEventPacket;
use pocketmine\network\mcpe\protocol\MobEffectPacket;
use pocketmine\Player;
use pocketmine\utils\Binary;
@ -466,6 +467,29 @@ abstract class Living extends Entity implements Damageable{
*/
protected function applyPostDamageEffects(EntityDamageEvent $source) : void{
$this->setAbsorption(max(0, $this->getAbsorption() + $source->getDamage(EntityDamageEvent::MODIFIER_ABSORPTION)));
$this->damageArmor($source->getDamage(EntityDamageEvent::MODIFIER_BASE));
}
/**
* Damages the worn armour according to the amount of damage given. Each 4 points (rounded down) deals 1 damage
* point to each armour piece, but never less than 1 total.
*
* @param float $damage
*/
public function damageArmor(float $damage) : void{
$durabilityRemoved = (int) max(floor($damage / 4), 1);
$armor = $this->armorInventory->getContents(true);
foreach($armor as $item){
if($item instanceof Armor){
$item->applyDamage($durabilityRemoved);
if($item->isBroken()){
$this->level->broadcastLevelSoundEvent($this, LevelSoundEventPacket::SOUND_BREAK);
}
}
}
$this->armorInventory->setContents($armor);
}
public function attack(EntityDamageEvent $source){

View File

@ -25,12 +25,13 @@ declare(strict_types=1);
namespace pocketmine\item;
use pocketmine\event\entity\EntityDamageEvent;
use pocketmine\item\enchantment\Enchantment;
use pocketmine\item\enchantment\ProtectionEnchantment;
use pocketmine\nbt\tag\IntTag;
use pocketmine\utils\Binary;
use pocketmine\utils\Color;
abstract class Armor extends Item{
abstract class Armor extends Durable{
public const TAG_CUSTOM_COLOR = "customColor"; //TAG_Int
@ -78,4 +79,21 @@ abstract class Armor extends Item{
return $epf;
}
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(mt_rand(1, 100) > 60 and lcg_value() > $chance){ //unbreaking only applies to armor 40% of the time at best
$negated++;
}
}
return $negated;
}
return 0;
}
}

View File

@ -32,4 +32,8 @@ class ChainBoots extends Armor{
public function getDefensePoints() : int{
return 1;
}
public function getMaxDurability() : int{
return 196;
}
}

View File

@ -32,4 +32,8 @@ class ChainChestplate extends Armor{
public function getDefensePoints() : int{
return 5;
}
public function getMaxDurability() : int{
return 241;
}
}

View File

@ -32,4 +32,8 @@ class ChainHelmet extends Armor{
public function getDefensePoints() : int{
return 2;
}
public function getMaxDurability() : int{
return 166;
}
}

View File

@ -32,4 +32,8 @@ class ChainLeggings extends Armor{
public function getDefensePoints() : int{
return 4;
}
public function getMaxDurability() : int{
return 226;
}
}

View File

@ -32,4 +32,8 @@ class DiamondBoots extends Armor{
public function getDefensePoints() : int{
return 3;
}
public function getMaxDurability() : int{
return 430;
}
}

View File

@ -32,4 +32,8 @@ class DiamondChestplate extends Armor{
public function getDefensePoints() : int{
return 8;
}
public function getMaxDurability() : int{
return 529;
}
}

View File

@ -32,4 +32,8 @@ class DiamondHelmet extends Armor{
public function getDefensePoints() : int{
return 3;
}
public function getMaxDurability() : int{
return 364;
}
}

View File

@ -32,4 +32,8 @@ class DiamondLeggings extends Armor{
public function getDefensePoints() : int{
return 6;
}
public function getMaxDurability() : int{
return 496;
}
}

View File

@ -32,4 +32,8 @@ class GoldBoots extends Armor{
public function getDefensePoints() : int{
return 1;
}
public function getMaxDurability() : int{
return 92;
}
}

View File

@ -32,4 +32,8 @@ class GoldChestplate extends Armor{
public function getDefensePoints() : int{
return 5;
}
public function getMaxDurability() : int{
return 113;
}
}

View File

@ -32,4 +32,8 @@ class GoldHelmet extends Armor{
public function getDefensePoints() : int{
return 2;
}
public function getMaxDurability() : int{
return 78;
}
}

View File

@ -32,4 +32,8 @@ class GoldLeggings extends Armor{
public function getDefensePoints() : int{
return 3;
}
public function getMaxDurability() : int{
return 106;
}
}

View File

@ -32,4 +32,8 @@ class IronBoots extends Armor{
public function getDefensePoints() : int{
return 2;
}
public function getMaxDurability() : int{
return 196;
}
}

View File

@ -32,4 +32,8 @@ class IronChestplate extends Armor{
public function getDefensePoints() : int{
return 6;
}
public function getMaxDurability() : int{
return 241;
}
}

View File

@ -32,4 +32,8 @@ class IronHelmet extends Armor{
public function getDefensePoints() : int{
return 2;
}
public function getMaxDurability() : int{
return 166;
}
}

View File

@ -32,4 +32,8 @@ class IronLeggings extends Armor{
public function getDefensePoints() : int{
return 5;
}
public function getMaxDurability() : int{
return 226;
}
}

View File

@ -32,4 +32,8 @@ class LeatherBoots extends Armor{
public function getDefensePoints() : int{
return 1;
}
public function getMaxDurability() : int{
return 66;
}
}

View File

@ -32,4 +32,8 @@ class LeatherCap extends Armor{
public function getDefensePoints() : int{
return 1;
}
public function getMaxDurability() : int{
return 56;
}
}

View File

@ -32,4 +32,8 @@ class LeatherPants extends Armor{
public function getDefensePoints() : int{
return 2;
}
public function getMaxDurability() : int{
return 76;
}
}

View File

@ -32,4 +32,8 @@ class LeatherTunic extends Armor{
public function getDefensePoints() : int{
return 3;
}
public function getMaxDurability() : int{
return 81;
}
}