package kernitus.plugin.OldCombatMechanics.module;

import java.util.Collection;
import java.util.EnumMap;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.FishHook;
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.event.entity.ProjectileHitEvent;

import com.google.common.base.Functions;
import com.google.common.collect.ImmutableMap;

import kernitus.plugin.OldCombatMechanics.OCMMain;

/**
 * Created by Rayzr522 on 6/27/16.
 */
public class ModuleFishingKnockback extends Module {
	public ModuleFishingKnockback(OCMMain plugin) {
		super(plugin, "old-fishing-knockback");
	}

	@EventHandler(priority = EventPriority.HIGHEST)
	public void onRodLand(ProjectileHitEvent e) {

		if (!isEnabled(e.getEntity().getWorld())) {
			return;
		}

		if (!(e.getEntityType() == EntityType.FISHING_HOOK)) {
			return;
		}

		Collection<Entity> entities = Bukkit.getWorld(e.getEntity().getLocation().getWorld().getName()).getNearbyEntities(e.getEntity().getLocation(), 0.25, 0.25, 0.25);

		for (Entity entity : entities) {

			if (!(entity instanceof Player)) {
				continue;
			}

			FishHook hook = (FishHook) e.getEntity();
			Player rodder = (Player) hook.getShooter();
			Player player = (Player) entity;

			if (player.getUniqueId() == rodder.getUniqueId()) {
				continue;
			}

			//player.damage(0.2);
			Bukkit.getPluginManager().callEvent(makeEvent(rodder, player));
			Location loc = player.getLocation().add(0, 0.5, 0);
			player.teleport(loc);
			player.setVelocity(loc.subtract(rodder.getLocation()).toVector().normalize().multiply(0.4));

			return;

		}

	}

	private EntityDamageByEntityEvent makeEvent(Player rodder, Player player) {

		return new EntityDamageByEntityEvent(rodder, player, EntityDamageEvent.DamageCause.ENTITY_ATTACK, new EnumMap(ImmutableMap.of(EntityDamageEvent.DamageModifier.BASE, Double.valueOf(0.2))), new EnumMap(ImmutableMap.of(EntityDamageEvent.DamageModifier.BASE, Functions.constant(Double.valueOf(0.2)))));

	}

}

@@ -366,24 +366,61 @@
 ntPriority;%0A
+import org.bukkit.event.HandlerList;%0A
 import org.b
@@ -565,16 +565,61 @@
 itEvent;
+%0Aimport org.bukkit.plugin.RegisteredListener;
 %0A%0Aimport
@@ -849,16 +849,17 @@
 odule %7B%0A
+%0A
 %09public 
@@ -1085,18 +1085,16 @@
 orld()))
- %7B
 %0A%09%09%09retu
@@ -1100,20 +1100,16 @@
 urn;
-%0A%09%09%7D
 %0A%0A%09%09if (
 !(e.
@@ -1104,17 +1104,16 @@
 %0A%0A%09%09if (
-!
 (e.getEn
@@ -1123,17 +1123,17 @@
 yType() 
-=
+!
 = Entity
@@ -1151,18 +1151,16 @@
 G_HOOK))
- %7B
 %0A%09%09%09retu
@@ -1162,20 +1162,16 @@
 %09return;
-%0A%09%09%7D
 %0A%0A%09%09Coll
@@ -1604,18 +1604,16 @@
 queId())
- %7B
 %0A%09%09%09%09con
@@ -1623,103 +1623,484 @@
 ue;%0A
-%09%09%09%7D%0A%0A%09%09%09//player.damage(0.2);%0A%09%09%09Bukkit.getPluginManager().callEvent(makeEvent(rodder, player)
+%0A%09%09%09EntityDamageByEntityEvent event = makeEvent(rodder, player);%0A%09%09%09Bukkit.getPluginManager().callEvent(event);%0A%0A%09%09%09if(event.isCancelled())%7B%0A%0A%09%09%09%09if(plugin.getConfig().getBoolean(%22debug.enabled%22))%7B%0A%09%09%09%09%09debug(%22You can't do that here!%22, rodder);%0A%09%09%09%09%09HandlerList hl = event.getHandlers();%0A%0A%09%09%09%09%09for(RegisteredListener rl : hl.getRegisteredListeners())%7B%0A%09%09%09%09%09%09debug(%22Plugin Listening: %22 + rl.getPlugin().getName(), rodder);%0A%09%09%09%09%09%7D%0A%0A%09%09%09%09%7D%0A%0A%09%09%09%09return; %0A%09%09%09%7D%0A%0A%09%09%09player.damage(0.2
 );%0A%09
@@ -2296,16 +2296,64 @@
 %09%7D%0A%0A%09%7D%0A%0A
+%09@SuppressWarnings(%7B %22rawtypes%22, %22unchecked%22 %7D)%0A
 %09private
@@ -2421,17 +2421,16 @@
 ayer) %7B%0A
-%0A
 %09%09return
@@ -2726,11 +2726,8 @@
 ));%0A
-%0A
 %09%7D%0A
-%0A%7D%0A
+%7D

Check for fishing rod damage event getting cancelled