package com.cosmicdan.reignadditionals.events;

import com.cosmicdan.reignadditionals.Main;
import com.cosmicdan.reignadditionals.ModConfig;

import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemTool;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.DamageSource;
import net.minecraftforge.event.entity.player.PlayerEvent.BreakSpeed;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;

public class PlayerEvents {
    @SubscribeEvent
    public void onBreakSpeed(BreakSpeed event) {
        if (ModConfig.ALLOW_BREAKING_WITHOUT_TOOL) return;
        if (event.entityPlayer.capabilities.isCreativeMode) return;
        Block tryingToHarvest = event.entityPlayer.worldObj.getBlock(event.x, event.y, event.z);
        int blockMeta = event.entityPlayer.worldObj.getBlockMetadata(event.x, event.y, event.z);
        if (!tryHarvest(tryingToHarvest, blockMeta, event.entityPlayer)) {
            event.newSpeed = 0.00001f;
        }
    }
    
    
    /*
     * Helper methods for block harvest/break prevention stuff
     * 
     */

    private boolean tryHarvest(Block block, int blockMeta, EntityPlayer entityPlayer) {
        if (block.getMaterial() == Material.wood)
            return letPlayerUseTool(block, blockMeta, entityPlayer, "axe");
        if (block.getMaterial() == Material.rock)
            return letPlayerUseTool(block, blockMeta, entityPlayer, "pickaxe");
        // allow the player to punch-break/harvest anything that isn't explicitly prevented
        return true;
    }

    private boolean letPlayerUseTool(Block block, int blockMeta, EntityPlayer entityPlayer, String toolClass) {
        if (entityPlayer.getHeldItem() != null)
            if ((block.canHarvestBlock(entityPlayer, blockMeta)) && (entityPlayer.getHeldItem().getItem().getHarvestLevel(entityPlayer.getHeldItem(), toolClass) != -1))
                    return true;
        return false;
    }
}

@@ -607,16 +607,634 @@
 vents %7B%0A
+    %0A    @SubscribeEvent%0A    public void onPlayerInteract(PlayerInteractEvent event) %7B%0A        if (ModConfig.ALLOW_BREAKING_WITHOUT_TOOL) return;%0A        if (event.entityPlayer.capabilities.isCreativeMode) return;%0A        if (event.action == PlayerInteractEvent.Action.LEFT_CLICK_BLOCK) %7B%0A            Block tryingToHarvest = event.world.getBlock(event.x, event.y, event.z); %0A            int blockMeta = event.world.getBlockMetadata(event.x, event.y, event.z);%0A            if (!tryHarvest(tryingToHarvest, blockMeta, event.entityPlayer, false)) %7B%0A                event.setCanceled(true);%0A            %7D%0A        %7D%0A    %7D%0A%0A
     @Sub
@@ -1685,16 +1685,22 @@
 tyPlayer
+, true
 )) %7B%0A   
@@ -1718,27 +1718,25 @@
 ent.
-newSpeed = 0.00001f
+setCanceled(true)
 ;%0A  
@@ -1930,16 +1930,32 @@
 tyPlayer
+, boolean silent
 ) %7B%0A    
@@ -2069,24 +2069,32 @@
 layer, %22axe%22
+, silent
 );%0A        i
@@ -2211,16 +2211,24 @@
 pickaxe%22
+, silent
 );%0A     
@@ -2450,16 +2450,32 @@
 oolClass
+, boolean silent
 ) %7B%0A    
@@ -2517,16 +2517,45 @@
 != null)
+ %7B%0A            silent = true;
 %0A       
@@ -2745,24 +2745,257 @@
 eturn true;%0A
+        %7D%0A        if (!silent) %7B%0A            entityPlayer.attackEntityFrom(DamageSource.generic, 0.1f);%0A            entityPlayer.addChatMessage(new ChatComponentText(%22Ouch! It seems I need the right tool for the job...%22));%0A        %7D%0A
         retu

Revert "Compatibility fix for other mods that depend on hand being able to harvest wood (e.g. Storage Drawers)"