package me.otisdiver.otisarena.event;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
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 me.otisdiver.otisarena.OtisArena;
import me.otisdiver.otisarena.game.GameState;
import me.otisdiver.otisarena.task.Respawn;

public class GameDeath extends EasyListener {
    
    private final String killMessage = ChatColor.GRAY + "%s" + ChatColor.GRAY + " killed " + ChatColor.GREEN + "%s" + ChatColor.GRAY + "!";
    
    /** GameDeath handles deaths and kills during games.
     * 
     * @param main instance of JavaPlugin
     */
    public GameDeath(OtisArena main) {
        super(main);
    }
    
    @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
    public void onDamage(EntityDamageEvent e) {
        // conditions (we only care about players during the game)
        if (!(e.getEntity() instanceof Player)) return;
        if (!GameState.getCurrent().equals(GameState.PLAYING)) return;
        
        Player victim = (Player) e.getEntity();
        
        // if the attack wasn't fatal, stop
        double health = victim.getHealth() - e.getFinalDamage();
        if (health > 0) return;
        
        // artificial death (override Minecraft)
        e.setCancelled(true);
        victim.setGameMode(GameMode.SPECTATOR);
        new Respawn(main, victim).runFuture(100);
    }
    
    @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
    public void onAttack(EntityDamageByEntityEvent e) {
        // conditions (we only care about players during the game)
        if (!(e.getDamager() instanceof Player) || !(e.getEntity() instanceof Player)) return;
        if (!GameState.getCurrent().equals(GameState.PLAYING)) return;
        
        // find the attacker and victim
        Player attacker = (Player) e.getDamager();
        Player victim = (Player) e.getEntity();
        
        // if the attack wasn't fatal, stop
        double health = victim.getHealth() - e.getFinalDamage();
        if (health > 0) return;
        
        // point!
        Bukkit.broadcastMessage(String.format(killMessage, attacker.getName(), victim.getName()));
        main.getGame().playerScore(attacker);
    }
    
}

@@ -115,16 +115,48 @@
 meMode;%0A
+import org.bukkit.entity.Arrow;%0A
 import o
@@ -360,24 +360,72 @@
 DamageEvent;
+%0Aimport org.bukkit.projectiles.ProjectileSource;
 %0A%0Aimport me.
@@ -1136,111 +1136,111 @@
 f (!
-(e.getEntity() instanceof Player)) return;%0A        if (!GameState.getCurrent().equals(GameState.PLAYING
+GameState.getCurrent().equals(GameState.PLAYING)) return;%0A        if (!(e.getEntity() instanceof Player
 )) r
@@ -1794,376 +1794,800 @@
  // 
-conditions (we only care about players during the game)%0A        if (!(e.getDamager() instanceof Player) %7C%7C !(e.getEntity() instanceof Player)) return;%0A        if (!GameState.getCurrent().equals(GameState.PLAYING)) return;%0A        %0A        // find the attacker and victim%0A        Player attacker = (Player) e.getDamager();%0A        Player victim = (Player) e.getEntity()
+if a game's not underway, stop%0A        if (!GameState.getCurrent().equals(GameState.PLAYING)) return;%0A        %0A        Player victim;%0A        Player attacker;%0A        %0A        // find the victim%0A        if (e.getEntity() instanceof Player) %7B%0A            victim = (Player) e.getEntity();%0A        %7D%0A        // victim not a player, stop%0A        else return;%0A        %0A        // find the attacker%0A        if (e.getDamager() instanceof Player) %7B%0A            attacker = (Player) e.getDamager();%0A        %7D%0A        else if (e.getDamager() instanceof Arrow) %7B%0A            ProjectileSource shooter = ((Arrow) e.getDamager()).getShooter();%0A            if (!(shooter instanceof Player)) return;%0A            attacker = (Player) shooter;%0A        %7D%0A        // attacker not a player, stop%0A        else return
 ;%0A  

update - track arrow kills needs improvement