package com.gmail.nossr50.skills.axes;

import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.inventory.EntityEquipment;
import org.bukkit.inventory.ItemStack;

import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.mods.ModChecks;
import com.gmail.nossr50.util.ItemChecks;
import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.Permissions;

public class ImpactEventHandler {
    private AxeManager manager;
    private Player player;
    private EntityDamageByEntityEvent event;
    private short durabilityDamage = 1;
    private EntityEquipment entityEquipment;
    protected LivingEntity defender;
    boolean impactApplied;

    public ImpactEventHandler(AxeManager manager, EntityDamageByEntityEvent event, LivingEntity defender) {
        this.manager = manager;
        this.player = manager.getMcMMOPlayer().getPlayer();
        this.event = event;
        this.defender = defender;
        this.entityEquipment = defender.getEquipment();
    }

    protected boolean applyImpact() {
        // Every 50 Skill Levels you gain 1 durability damage (default values)
        durabilityDamage += (short) (manager.getSkillLevel() / Axes.impactIncreaseLevel);
        // getArmorContents.length can't be used because it's always equal to 4 (no armor = air block)
        boolean hasArmor = false;

        for (ItemStack itemStack : entityEquipment.getArmorContents()) {
            if (ItemChecks.isArmor(itemStack)) {
                hasArmor = true;

                if (Misc.getRandom().nextInt(100) < 25) {
                    damageArmor(itemStack);
                }
            }
        }

        return hasArmor;
    }

    private void damageArmor(ItemStack armor) {
        // Modifier simulate the durability enchantment behavior
        float modifier = 1;

        if (armor.containsEnchantment(Enchantment.DURABILITY)) {
            modifier /= armor.getEnchantmentLevel(Enchantment.DURABILITY) + 1;
        }

        float modifiedDurabilityDamage = durabilityDamage * modifier;
        short maxDurabilityDamage = ModChecks.isCustomArmor(armor) ? ModChecks.getArmorFromItemStack(armor).getDurability() : armor.getType().getMaxDurability();
        maxDurabilityDamage *= Axes.impactMaxDurabilityDamageModifier;

        if (modifiedDurabilityDamage > maxDurabilityDamage) {
            modifiedDurabilityDamage = maxDurabilityDamage;
        }

        armor.setDurability((short) (modifiedDurabilityDamage + armor.getDurability()));
    }

    protected void applyGreaterImpact() {
        if (!Permissions.greaterImpact(player)) {
            return;
        }

        if (Misc.getRandom().nextInt(manager.getActivationChance()) <= Axes.greaterImpactChance) {
            handleGreaterImpactEffect();
            sendAbilityMessge();
        }
    }

    private void handleGreaterImpactEffect() {
        event.setDamage(event.getDamage() + Axes.greaterImpactBonusDamage);
        defender.setVelocity(player.getLocation().getDirection().normalize().multiply(Axes.greaterImpactKnockbackMultiplier));
    }

    private void sendAbilityMessge() {
        player.sendMessage(LocaleLoader.getString("Axes.Combat.GI.Proc"));

        if (defender instanceof Player) {
            ((Player) defender).sendMessage(LocaleLoader.getString("Axes.Combat.GI.Struck"));
        }
    }
}

@@ -31,24 +31,53 @@
 ls.axes;%0D%0A%0D%0A
+import org.bukkit.Location;%0D%0A
 import org.b
@@ -542,16 +542,54 @@
 ssions;%0D
+%0Aimport com.gmail.nossr50.util.Users;%0D
 %0A%0D%0Apubli
@@ -3204,24 +3204,199 @@
 usDamage);%0D%0A
+%0D%0A        Location location = defender.getEyeLocation();%0D%0A        defender.getWorld().createExplosion(location.getX(), location.getY(), location.getZ(), 0F, false, false);%0D%0A%0D%0A
         defe
@@ -3556,24 +3556,105 @@
 Messge() %7B%0D%0A
+        if (manager.getMcMMOPlayer().getProfile().useChatNotifications()) %7B%0D%0A    
         play
@@ -3716,16 +3716,27 @@
 roc%22));%0D
+%0A        %7D%0D
 %0A%0D%0A     
@@ -3789,27 +3789,175 @@
     
-((Player) defend
+Player defendingPlayer = (Player) defender;%0D%0A%0D%0A            if (Users.getPlayer(defendingPlayer).getProfile().useChatNotifications()) %7B%0D%0A                defendingPlay
 er
-)
 .sen
@@ -4012,24 +4012,39 @@
 Struck%22));%0D%0A
+            %7D%0D%0A
         %7D%0D%0A 

Add explosion effect for Greater Impact.