From 2971f652909a4ed21902912cb5c5a78febbcb08d Mon Sep 17 00:00:00 2001 From: Az928 Date: Mon, 23 Oct 2017 02:03:10 +0600 Subject: [PATCH] Implement protection --- .../handlers/Protection.php | 34 +++++++++++++++++++ .../handlers/VanillaEnchant.php | 5 +-- 2 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 src/VanillaEnchantments/handlers/Protection.php diff --git a/src/VanillaEnchantments/handlers/Protection.php b/src/VanillaEnchantments/handlers/Protection.php new file mode 100644 index 0000000..302ad09 --- /dev/null +++ b/src/VanillaEnchantments/handlers/Protection.php @@ -0,0 +1,34 @@ +getServer()->getPluginManager()->registerEvents($this, $core); + } + + public function onDamage(EntityDamageEvent $event): void{ + $player = $event->getEntity(); + $cause = $event->getCause(); + if($event->isCancelled() or $cause == $event::CAUSE_STARVATION or $cause == $event::CAUSE_MAGIC){ + return; + } + if($player instanceof Player){ + $level = $this->getEnchantmentLevelOfArmors($player, Enchantment::PROTECTION); + $base = $event->getDamage(); + $reduce = $this->getReducedDamage(Enchantment::PROTECTION, $base, $level); + if($reduce > 0){ + $event->setDamage($base - $reduce); + } + } + } +} diff --git a/src/VanillaEnchantments/handlers/VanillaEnchant.php b/src/VanillaEnchantments/handlers/VanillaEnchant.php index d177cf5..0028559 100644 --- a/src/VanillaEnchantments/handlers/VanillaEnchant.php +++ b/src/VanillaEnchantments/handlers/VanillaEnchant.php @@ -77,11 +77,12 @@ protected function getReducedDamage(Int $id, Int $base, Int $level): float{ $factor *= $level; $reduce = $base * $factor; break; - case Enchantment::PROJECTILE_PROTECTION: - $factor = (4 / 100); + case Enchantment::PROTECTION: + $factor = (2.5 / 100); $factor *= $level; $reduce = $base * $factor; break; + case Enchantment::PROJECTILE_PROTECTION: case Enchantment::BLAST_PROTECTION: $factor = (4 / 100); $factor *= $level;