Skip to content

Instantly share code, notes, and snippets.

@Muqsit
Last active August 3, 2017 20:07
Show Gist options
  • Save Muqsit/0d7357b2cc557e969cb8f8a848f3e26e to your computer and use it in GitHub Desktop.
Save Muqsit/0d7357b2cc557e969cb8f8a848f3e26e to your computer and use it in GitHub Desktop.
Enchantment Protection Factor
<?php
//source: https://minecraft.gamepedia.com/Armor#Enchantments
//$damage = EntityDamageEvent::getFinalDamage();
const TYPE_HELMET = 0;
const TYPE_CHESTPLATE = 1;
const TYPE_LEGGINGS = 2;
const TYPE_BOOTS = 3;
const EPF_MODIFIERES = [
Enchantment::TYPE_ARMOR_PROTECTION => 1,
Enchantment::TYPE_ARMOR_FIRE_PROTECTION => 1,
Enchantment::TYPE_ARMOR_EXPLOSION_PROTECTION => 1,
Enchantment::TYPE_ARMOR_PROJECTILE_PROTECTION => 2,
Enchantment::TYPE_ARMOR_FALL_PROTECTION => 3
];
const EPF_CAUSES = [
Enchantment::TYPE_ARMOR_PROTECTION => [
EntityDamageEvent::CAUSE_CONTACT,
EntityDamageEvent::CAUSE_ENTITY_ATTACK,
EntityDamageEvent::CAUSE_PROJECTILE,
EntityDamageEvent::CAUSE_SUFFOCATION,
EntityDamageEvent::CAUSE_FALL,
EntityDamageEvent::CAUSE_FIRE,
EntityDamageEvent::CAUSE_FIRE_TICK,
EntityDamageEvent::CAUSE_LAVA,
EntityDamageEvent::CAUSE_DROWNING,
EntityDamageEvent::CAUSE_BLOCK_EXPLOSION,
EntityDamageEvent::CAUSE_ENTITY_EXPLOSION,
EntityDamageEvent::CAUSE_MAGIC
],
Enchantment::TYPE_ARMOR_FIRE_PROTECTION => [
EntityDamageEvent::CAUSE_FIRE,
EntityDamageEvent::CAUSE_FIRE_TICK,
EntityDamageEvent::CAUSE_LAVA
],
Enchantment::TYPE_ARMOR_EXPLOSION_PROTECTION => [
EntityDamageEvent::CAUSE_BLOCK_EXPLOSION,
EntityDamageEvent::CAUSE_ENTITY_EXPLOSION
],
Enchantment::TYPE_ARMOR_PROJECTILE_PROTECTION => [
EntityDamageEvent::CAUSE_PROJECTILE
],
Enchantment::TYPE_ARMOR_FALL_PROTECTION => [
EntityDamageEvent::CAUSE_FALL
]
];
const MODIFIER_EPF = 5;
public function protec(\pocketmine\Player $player, int $cause) : array{
/** @var Item[] $armor */
$armor = $player->getInventory()->getArmorContents();
$epf = [];
/** @var Item $item */
foreach($armor as $type => $item){
if($item->hasEnchantments()){
/** @var ListTag $en */
foreach($item->getNamedTag()->ench as $en){
if(isset(self::EPF_MODIFIERS[$en["id"]]) and in_array($cause, self::EPF_CAUSES)){//if its a protection* enchantment
$modifier = self::EPF_MODIFIERS[$en["id"]];
if(!isset($epf[$modifier])){
$epf[$modifier] = $modifier * $en["lvl"];
}else{
$epf[$modifier] += $modifier * $en["lvl"];
}
}
}
}
}
foreach ($epf as &$moddedProtection) {
if ($moddedProtection > 20) {
$moddedProtection = 20;
}
}
return $epf;
}
/**
* @param EntityDamageEvent $event
* @priority HIGH
* @ignoreCancelled true
*/
public function attac(EntityDamageEvent $event){
if(
$event->getCause() === EntityDamageEvent::CAUSE_ENTITY_ATTACK and
$event->getFinalDamage() < $event->getEntity()->getHealth() and
$event->getEntity() instanceof Player
){
$damage = $event->getFinalDamage();
$mods = $this->protec($event->getEntity());
if(!empty($mods)){
$cappedEPF = array_sum($mods);
$damage = $event->getFinalDamage() * (1 - $cappedEPF / 25);
$event->setDamage($damage, self::MODIFIER_EPF);
}
}
}
@Everdark316Ftw
Copy link

so ur going to buy a slot on svilex proxy to figure out how it works??

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment