package kernitus.plugin.OldCombatMechanics.module;

import kernitus.plugin.OldCombatMechanics.OCMMain;
import kernitus.plugin.OldCombatMechanics.utilities.ToolDamage;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.ArrayList;
import java.util.List;

/**
 * A module to disable the sweep attack.
 */
public class ModuleSwordSweep extends Module {

    private BukkitRunnable task;
    private List<Location> sweepLocations = new ArrayList<>();
    private EntityDamageEvent.DamageCause sweepDamageCause;

    public ModuleSwordSweep(OCMMain plugin) {
        super(plugin, "disable-sword-sweep");

        try {
            // Will be available from some 1.11 version onwards
            sweepDamageCause = EntityDamageEvent.DamageCause.valueOf("ENTITY_SWEEP_ATTACK");
        } catch (IllegalArgumentException e) {
            sweepDamageCause = null;
        }
    }

    @Override
    public void reload() {
        // we didn't set anything up in the first place
        if (sweepDamageCause != null) {
            return;
        }

        if (task != null) {
            task.cancel();
        }

        task = new BukkitRunnable() {
            @Override
            public void run() {
                sweepLocations.clear();
            }
        };
        task.runTaskTimer(plugin, 0, 1);
    }

    @EventHandler(priority = EventPriority.HIGHEST)
    public void onEntityDamaged(EntityDamageByEntityEvent e) {
        World world = e.getDamager().getWorld();

        if (!isEnabled(world)) return;

        if (!(e.getDamager() instanceof Player)) return;

        if (sweepDamageCause != null) {
            if (e.getCause() == sweepDamageCause) {
                e.setCancelled(true);
            }
            // sweep attack detected or not, we do not need to fall back to the guessing implementation
            return;
        }

        Player p = (Player) e.getDamager();
        ItemStack weapon = p.getInventory().getItemInMainHand();

        if (isHoldingSword(weapon.getType()))
            onSwordAttack(e, p, weapon);
    }

    private void onSwordAttack(EntityDamageByEntityEvent e, Player p, ItemStack weapon) {
        //Disable sword sweep
        Location location = p.getLocation(); // ATTACKER

        int level = 0;

        try { //In a try catch for servers that haven't updated
            level = weapon.getEnchantmentLevel(Enchantment.SWEEPING_EDGE);
        } catch (NoSuchFieldError ignored) {
        }

        float damage = ToolDamage.getDamage(weapon.getType()) * level / (level + 1) + 1;

        if (e.getDamage() == damage) {
            // Possibly a sword-sweep attack
            if (sweepLocations.contains(location)) {
                debug("Cancelling sweep...", p);
                e.setCancelled(true);
            }
        } else {
            sweepLocations.add(location);
        }

        ModuleOldToolDamage.onAttack(e);
    }

    private boolean isHoldingSword(Material mat) {
        return mat.toString().endsWith("_SWORD");
    }

}
@@ -936,17 +936,16 @@
  plugin)
- 
 %7B%0A      
@@ -996,17 +996,16 @@
      try
- 
 %7B%0A      
@@ -1170,17 +1170,16 @@
  %7D catch
- 
 (Illegal
@@ -1198,17 +1198,16 @@
 ption e)
- 
 %7B%0A      
@@ -1292,17 +1292,16 @@
 reload()
- 
 %7B%0A      
@@ -1352,33 +1352,32 @@
 place%0A        if
- 
 (sweepDamageCaus
@@ -1378,33 +1378,32 @@
 geCause != null)
- 
 %7B%0A            re
@@ -1429,17 +1429,16 @@
       if
- 
 (task !=
@@ -1435,33 +1435,32 @@
 if(task != null)
- 
 %7B%0A            ta
@@ -1572,17 +1572,16 @@
 id run()
- 
 %7B%0A      
@@ -1799,17 +1799,16 @@
 Event e)
- 
 %7B%0A      
@@ -1861,17 +1861,16 @@
       if
- 
 (!isEnab
@@ -1900,17 +1900,16 @@
       if
- 
 (!(e.get
@@ -1953,25 +1953,24 @@
 %0A%0A        if
- 
 (sweepDamage
@@ -1983,17 +1983,16 @@
 != null)
- 
 %7B%0A      
@@ -1995,25 +1995,24 @@
           if
- 
 (e.getCause(
@@ -2033,17 +2033,16 @@
 geCause)
- 
 %7B%0A      
@@ -2342,17 +2342,16 @@
       if
- 
 (isHoldi
@@ -2512,17 +2512,16 @@
  weapon)
- 
 %7B%0A      
@@ -2637,17 +2637,16 @@
      try
- 
 %7B //In a
@@ -2779,17 +2779,16 @@
  %7D catch
- 
 (NoSuchF
@@ -2805,17 +2805,16 @@
 ignored)
- 
 %7B%0A      
@@ -2918,17 +2918,16 @@
       if
- 
 (e.getDa
@@ -2943,17 +2943,16 @@
  damage)
- 
 %7B%0A      
@@ -3004,17 +3004,16 @@
       if
- 
 (sweepLo
@@ -3039,17 +3039,16 @@
 cation))
- 
 %7B%0A      
@@ -3308,17 +3308,16 @@
 ial mat)
- 
 %7B%0A      

Reformat according to new formatter profile