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 me.otisdiver.otisarena.OtisArena;
import me.otisdiver.otisarena.task.Respawn;

public class GameDeath extends EasyListener {
    
    private final String deathMessage = 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 onAttack(EntityDamageByEntityEvent e) {
        
        // if either entity involved wasn't a player, stop
        if (!(e.getDamager() instanceof Player) || !(e.getEntity() instanceof Player)) {
            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;
        
        // artificial death (override Minecraft)
        e.setCancelled(true);
        Bukkit.broadcastMessage(String.format(deathMessage, attacker.getName(), victim.getName()));
        victim.setGameMode(GameMode.SPECTATOR);
        new Respawn(main, victim).runFuture(100);
        
        // point!
        main.getGame().playerScore(attacker);
    }
    
}

@@ -278,24 +278,74 @@
 EntityEvent;
+%0Aimport org.bukkit.event.entity.EntityDamageEvent;
 %0A%0Aimport me.
@@ -375,16 +375,62 @@
 sArena;%0A
+import me.otisdiver.otisarena.game.GameState;%0A
 import m
@@ -542,21 +542,20 @@
  String 
-death
+kill
 Message 
@@ -946,113 +946,831 @@
 d on
-Attack(EntityDamageByEntityEvent e) %7B%0A        %0A        // if either entity involved wasn't a player, stop
+Damage(EntityDamageEvent e) %7B%0A        // conditions (we only care about players during the game)%0A        if (!(e.getEntity() instanceof Player)) return;%0A        if (!GameState.getCurrent().equals(GameState.PLAYING)) return;%0A        %0A        Player victim = (Player) e.getEntity();%0A        %0A        // if the attack wasn't fatal, stop%0A        double health = victim.getHealth() - e.getFinalDamage();%0A        if (health %3E 0) return;%0A        %0A        // artificial death (override Minecraft)%0A        e.setCancelled(true);%0A        victim.setGameMode(GameMode.SPECTATOR);%0A        new Respawn(main, victim).runFuture(100);%0A    %7D%0A    %0A    @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)%0A    public void onAttack(EntityDamageByEntityEvent e) %7B%0A        // conditions (we only care about players during the game)
 %0A   
@@ -1853,17 +1853,23 @@
 layer)) 
-%7B
+return;
 %0A       
@@ -1873,29 +1873,70 @@
     
-    return;%0A        %7D
+if (!GameState.getCurrent().equals(GameState.PLAYING)) return;
 %0A   
@@ -2254,75 +2254,14 @@
  // 
-artificial death (override Minecraft)%0A        e.setCancelled(true);
+point!
 %0A   
@@ -2303,21 +2303,20 @@
 .format(
-death
+kill
 Message,
@@ -2360,133 +2360,8 @@
 ));%0A
-        victim.setGameMode(GameMode.SPECTATOR);%0A        new Respawn(main, victim).runFuture(100);%0A        %0A        // point!%0A
     

update - handle all deaths during the game artificially