package com.ekstemicraft.plugin.echorses;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.entity.AnimalTamer;
import org.bukkit.entity.Arrow;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Horse;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import org.bukkit.event.entity.EntityTameEvent;
import org.bukkit.event.entity.ProjectileHitEvent;
import org.bukkit.event.vehicle.VehicleEnterEvent;

public class ECHorsesListeners implements Listener {
		
@EventHandler
	public void horseEnter(VehicleEnterEvent event){
	
	if(event.getVehicle() instanceof Horse){

      EntityType entered = (EntityType)event.getEntered().getType();
      if(entered == EntityType.PLAYER){ //Playercheck
    	  
    	  Player p = (Player)event.getEntered();
    	  String playername = p.getName();
    	  @SuppressWarnings("unused")
		Server server = p.getServer(); //Not used for now
          Horse h = (Horse)event.getVehicle();
          AnimalTamer owner = h.getOwner();
          if(!(owner == null)){
        	  
        	  if(p.isOp() && p.hasPermission("echorse.override")){ //Overriding the protection if player is OP
        		  return;
        	  }
        	  if(h.getOwner().getName() == p.getName()){ //Checking if entering player is the owner
        	  return;
        	  }
        	  else {
        		  p.sendMessage(ChatColor.AQUA + "[ECHorses]" + ChatColor.RED + " This horse is owned by " + h.getOwner().getName());
        		  Bukkit.getLogger().info("[ECHorses] " + playername + "tried entering someone elses horse");
        		  event.setCancelled(true);
        		  return;
        	  }
          } 
       p.sendMessage(ChatColor.AQUA + "[ECHorses]" + ChatColor.GREEN + " This horse is untamed!"); 
      }	
		
	}
}
@EventHandler
public void horseTameEvent(EntityTameEvent event){
     
	 if(event.getEntityType() == EntityType.HORSE){
		 Player p = (Player)event.getOwner();
		 p.sendMessage(ChatColor.AQUA + "[ECHorses]" + ChatColor.GREEN + " You have succesfully protected this horse!");
		 
	 }	
}
@EventHandler
public void horseDamageByEntity(EntityDamageByEntityEvent event){
	if(event.getEntityType() == EntityType.HORSE){
		Player p = (Player)event.getDamager();
		Horse h = (Horse)event.getEntity();				
		if(event.getDamager().getType() == EntityType.PLAYER){ //Playercheck

			if(p.isOp() || p.hasPermission("echorse.override") || h.getOwner() == null){ //Op & permission check & if horse isnt tamed.
				return;
			}
			 if(!(h.getOwner().getName() == p.getName())){ //If its not the horse owner, cancel the event
				 event.setCancelled(true);
				 p.sendMessage(ChatColor.AQUA + "[ECHorses]" + ChatColor.RED + " You dont have permission to hurt " + h.getOwner().getName() + "s horse!" );
			 }
		}		
	return;	
	}	
}
@EventHandler
public void horseDamageByProjectile(EntityDamageEvent event){
	if (event instanceof EntityDamageByEntityEvent){
	if(((EntityDamageByEntityEvent) event).getDamager() instanceof Arrow && event.getEntity() instanceof Horse){
		Arrow a = (Arrow)((EntityDamageByEntityEvent) event).getDamager();
		if(a.getShooter() instanceof Player){
			Player p = (Player)a.getShooter();
			Horse h = (Horse)event.getEntity();
			
		}
		
	}
  }
	
}





}
@@ -2557,89 +2557,392 @@
 %0D%0A%09%09
-Player p = (Player)event.getDamager();%0D%0A%09%09Horse h = (Horse)
+Horse h = (Horse)event.getEntity();%0D%0A%09%09%0D%0A%09%09//Handle arrow shots from players%0D%0A%09%09if(event.getDamager().getType() == EntityType.ARROW)%7B //Arrowcheck%0D%0A%09%09%09Arrow a = (Arrow)event.getDamager();%0D%0A%09%09%09if(a.getShooter() instanceof Player)%7B%0D%0A%09%09%09%09if(h.getOwner() == null)%7B%0D%0A%09%09%09%09%09return;%0D%0A%09%09%09%09%7D%0D%0A%09%09%09%09
 event.
-getEntity();%09%09%09%09
+setCancelled(true);%0D%0A%09%09%09%09return;%0D%0A%09%09%09%7D%0D%0A%09%09%7D%0D%0A%09%09%0D%0A%09%09//Handle melee attack direct from player
 %0D%0A%09%09
@@ -3011,18 +3011,74 @@
 rcheck%0D%0A
+%09%09%09Player p = (Player)event.getDamager();%0D%0A             
 %0D%0A
-
 %09%09%09if(p.
@@ -3492,16 +3492,30 @@
 e!%22 );%0D%0A
+%09%09%09%09 return;%0D%0A
 %09%09%09 %7D%0D%0A%09
@@ -3543,465 +3543,8 @@
 %0A%7D%0D%0A
-@EventHandler%0D%0Apublic void horseDamageByProjectile(EntityDamageEvent event)%7B%0D%0A%09if (event instanceof EntityDamageByEntityEvent)%7B%0D%0A%09if(((EntityDamageByEntityEvent) event).getDamager() instanceof Arrow && event.getEntity() instanceof Horse)%7B%0D%0A%09%09Arrow a = (Arrow)((EntityDamageByEntityEvent) event).getDamager();%0D%0A%09%09if(a.getShooter() instanceof Player)%7B%0D%0A%09%09%09Player p = (Player)a.getShooter();%0D%0A%09%09%09Horse h = (Horse)event.getEntity();%0D%0A%09%09%09%0D%0A%09%09%7D%0D%0A%09%09%0D%0A%09%7D%0D%0A  %7D%0D%0A%09%0D%0A%7D
 %0D%0A%0D%0A

Add protection from player shot arrows