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";

    public static final String SUPRESSED_NODE = "remove";
    public static final String FORMAT_NODE = "message";

    /**
     * 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(getBaseConfig()+SUPRESSED_NODE)) {
                //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(getBaseConfig()+FORMAT_NODE));

                //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();
    }
}

@@ -729,115 +729,77 @@
    p
-ublic static final String SUPRESSED_NODE = %22remove%22;%0A    public static final String FORMAT_NODE = %22
+rivate final boolean m_suppressed;%0A    private final String m_
 message
-%22
 ;%0A%0A 
@@ -964,24 +964,223 @@
 r deaths%22);%0A
+        m_message = ConfigManager.getInstance().getConfig().getString(getBaseConfig()+%22message%22);%0A        m_suppressed = ConfigManager.getInstance().getConfig().getBoolean(getBaseConfig()+%22remove%22);%0A
     %7D%0A%0A    /
@@ -1421,90 +1421,20 @@
 if (
-ConfigManager.getInstance().getConfig().getBoolean(getBaseConfig()+SUPRESSED_NODE)
+m_suppressed
 ) %7B%0A
@@ -1963,86 +1963,17 @@
 &', 
-ConfigManager.getInstance().getConfig().getString(getBaseConfig()+FORMAT_NODE)
+m_message
 );%0A%0A

read format and supressed on construct