package com.forgeessentials.afterlife;

import java.util.ArrayList;
import java.util.List;

import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraftforge.event.entity.player.PlayerDropsEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.event.world.BlockEvent.BreakEvent;

import com.forgeessentials.commons.selections.WorldPoint;
import com.forgeessentials.core.misc.Translator;
import com.forgeessentials.util.OutputHandler;
import com.forgeessentials.util.events.ServerEventHandler;

import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.TickEvent;

public class AfterlifeEventHandler extends ServerEventHandler
{

    public List<Grave> newGraves = new ArrayList<>();

    @SubscribeEvent
    public void playerDeathDropEvent(PlayerDropsEvent e)
    {
        Grave grave = Grave.createGrave(e.entityPlayer, e.drops);
        if (grave != null)
        {
            newGraves.add(grave);
            e.setCanceled(true);
        }
    }

    @SubscribeEvent
    public void serverTickEvent(TickEvent.ServerTickEvent e)
    {
        for (Grave grave : newGraves)
            grave.updateBlocks();
        newGraves.clear();
    }

    @SubscribeEvent
    public void playerInteractEvent(PlayerInteractEvent e)
    {
        if (e.entity.worldObj.isRemote)
            return;
        if (e.action == PlayerInteractEvent.Action.RIGHT_CLICK_AIR)
            return;

        WorldPoint point = new WorldPoint(e.entity.worldObj, e.x, e.y, e.z);
        Grave grave = Grave.graves.get(point);
        if (grave == null)
            return;

        // Block block = e.entity.worldObj.getBlock(e.x, e.y, e.z);
        // if (block != Blocks.skull && block != Blocks.chest && block != Blocks.fence)
        // return;

        grave.interact((EntityPlayerMP) e.entityPlayer);
        e.setCanceled(true);
    }

    @SubscribeEvent
    public void blockBreakEvent(BreakEvent e)
    {
        if (e.world.isRemote)
            return;

        WorldPoint point = new WorldPoint(e.world, e.x, e.y, e.z);
        Grave grave = Grave.graves.get(point);
        if (grave == null)
        {
            // Check for fence post
            point.setY(e.y + 1);
            grave = Grave.graves.get(point);
            if (grave == null || !grave.hasFencePost)
                return;
        }

        grave.update();
        if (grave.isProtected)
        {
            e.setCanceled(true);
            OutputHandler.chatError(e.getPlayer(), Translator.translate("You may not defile the grave of a player"));
            return;
        }
        grave.remove(true);
    }

}

@@ -263,24 +263,98 @@
 eractEvent;%0A
+import net.minecraftforge.event.entity.player.PlayerInteractEvent.Action;%0A
 import net.m
@@ -1701,32 +1701,158 @@
          return;
+%0A        else if (e.action == Action.LEFT_CLICK_BLOCK)%0A        %7B%0A            grave.remove(true);%0A            return;%0A        %7D
 %0A%0A        // Blo

Add ability to break a grave to drop all items. (should this be a config option?)