package uk.co.eluinhost.ultrahardcore.features.deathmessages;

import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.event.EventHandler;
import org.bukkit.event.entity.PlayerDeathEvent;

import uk.co.eluinhost.configuration.ConfigManager;
import uk.co.eluinhost.ultrahardcore.features.UHCFeature;


/**
 * DeathMessagesFeature
 * Handles changes to death messages on death
 *
 * @author ghowden
 */
public class DeathMessagesFeature extends UHCFeature {

    public static final String BASE_MESSAGES = BASE_PERMISSION + "death_messages.";
    public static final String DEATH_MESSAGE_SUPPRESSED = BASE_MESSAGES + "remove";
    public static final String DEATH_MESSAGE_AFFIXES = BASE_MESSAGES + "affixes";

    /**
     * Change the format of death messages
     */
    public DeathMessagesFeature() {
        super("DeathMessages","Adds a prefix/suffix to all player deaths");
    }

    /**
     * Whenver a player dies
     * @param pde the death event
     */
    @EventHandler
    public void onPlayerDeath(PlayerDeathEvent pde) {
        if (isEnabled()) {
            //if death message suppression is on
            if (ConfigManager.getInstance().getConfig().getBoolean(ConfigNodes.DEATH_MESSAGES_SUPPRESSED)) {
                //and the players death messages are suppressed
                if (pde.getEntity().hasPermission(DEATH_MESSAGE_SUPPRESSED)) {
                    //set to nothing
                    pde.setDeathMessage("");
                }
                return;
            }
            //if there is an affix for the player
            if (pde.getEntity().hasPermission(DEATH_MESSAGE_AFFIXES)) {
                //grab format from config file
                String format = ChatColor.translateAlternateColorCodes('&', ConfigManager.getInstance().getConfig().getString(ConfigNodes.DEATH_MESSAGES_FORMAT));

                //replace vars
                format = format.replaceAll("%message", pde.getDeathMessage());
                format = format.replaceAll("%player", pde.getEntity().getName());
                Location loc = pde.getEntity().getLocation();
                format = format.replaceAll("%coords", locationString(loc));

                //set the new message
                pde.setDeathMessage(format);
            }
        }
    }

    /**
     * Returns string in the format x:X y:Y z:Z
     * @param loc Location
     * @return String
     */
    private static String locationString(Location loc) {
        return "x:" + loc.getBlockX() + " y:" + loc.getBlockY() + " z:" + loc.getBlockZ();
    }
}

@@ -720,24 +720,139 @@
 ixes%22;%0A%0A
+    public static final String SUPRESSED_NODE = %22remove%22;%0A    public static final String FORMAT_NODE = %22message%22;%0A%0A
     /**%0A
      * C
@@ -839,24 +839,24 @@
 %22;%0A%0A    /**%0A
-
      * Chang
@@ -1311,45 +1311,38 @@
 ean(
-ConfigNodes.DEATH_MESSAGES_
+getBaseConfig()+
 SUP
-P
 RESSED
+_NODE
 )) %7B
@@ -1922,41 +1922,35 @@
 ing(
-ConfigNodes.DEATH_MESSAGES_
+getBaseConfig()+
 FORMAT
+_NODE
 ));%0A

update deathmessages