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.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;

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

/**
 * Created by Rayzr522 on 25/06/16.
 */
public class ModuleSwordSweep extends Module {

    private BukkitRunnable task;
    private List<Location> sweepLocations = new ArrayList<>();

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

    @Override
    public void reload(){
        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;

        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");
    }

}
@@ -449,24 +449,74 @@
 ntityEvent;%0A
+import org.bukkit.event.entity.EntityDamageEvent;%0A
 import org.b
@@ -650,39 +650,44 @@
 %0A * 
-Created by Rayzr522 on 25/06/16
+A module to disable the sweep attack
 .%0A *
@@ -831,16 +831,76 @@
 ist%3C%3E();
+%0A    private EntityDamageEvent.DamageCause sweepDamageCause;
 %0A%0A    pu
@@ -936,16 +936,17 @@
  plugin)
+ 
 %7B%0A      
@@ -984,16 +984,282 @@
 sweep%22);
+%0A%0A        try %7B%0A            // Will be available from some 1.11 version onwards%0A            sweepDamageCause = EntityDamageEvent.DamageCause.valueOf(%22ENTITY_SWEEP_ATTACK%22);%0A        %7D catch (IllegalArgumentException e) %7B%0A            sweepDamageCause = null;%0A        %7D
 %0A    %7D%0A%0A
@@ -1296,16 +1296,17 @@
 reload()
+ 
 %7B%0A      
@@ -1307,18 +1307,146 @@
         
-if
+// we didn't set anything up in the first place%0A        if (sweepDamageCause != null) %7B%0A            return;%0A        %7D%0A%0A        if 
 (task !=
@@ -1451,16 +1451,17 @@
 != null)
+ 
 %7B%0A      
@@ -1581,16 +1581,17 @@
 id run()
+ 
 %7B%0A      
@@ -1809,16 +1809,17 @@
 Event e)
+ 
 %7B%0A      
@@ -1872,16 +1872,17 @@
       if
+ 
 (!isEnab
@@ -1912,16 +1912,17 @@
       if
+ 
 (!(e.get
@@ -1952,32 +1952,311 @@
 ayer)) return;%0A%0A
+        if (sweepDamageCause != null) %7B%0A            if (e.getCause() == sweepDamageCause) %7B%0A                e.setCancelled(true);%0A            %7D%0A            // sweep attack detected or not, we do not need to fall back to the guessing implementation%0A            return;%0A        %7D%0A%0A
         Player p
@@ -2359,16 +2359,17 @@
       if
+ 
 (isHoldi
@@ -2530,16 +2530,17 @@
  weapon)
+ 
 %7B%0A      
@@ -2656,16 +2656,17 @@
      try
+ 
 %7B //In a
@@ -2799,16 +2799,17 @@
  %7D catch
+ 
 (NoSuchF
@@ -2826,16 +2826,17 @@
 ignored)
+ 
 %7B%0A      
@@ -2940,16 +2940,17 @@
       if
+ 
 (e.getDa
@@ -2966,16 +2966,17 @@
  damage)
+ 
 %7B%0A      
@@ -3028,16 +3028,17 @@
       if
+ 
 (sweepLo
@@ -3064,16 +3064,17 @@
 cation))
+ 
 %7B%0A      
@@ -3334,16 +3334,17 @@
 ial mat)
+ 
 %7B%0A      

Make sweep handling on newwer version more robust