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 onPlayerInteract(PlayerInteractEvent event) {
        if (ModConfig.ALLOW_BREAKING_WITHOUT_TOOL) return;
        if (event.entityPlayer.capabilities.isCreativeMode) return;
        if (event.action == PlayerInteractEvent.Action.LEFT_CLICK_BLOCK) {
            Block tryingToHarvest = event.world.getBlock(event.x, event.y, event.z); 
            int blockMeta = event.world.getBlockMetadata(event.x, event.y, event.z);
            if (!tryHarvest(tryingToHarvest, blockMeta, event.entityPlayer, false)) {
                event.setCanceled(true);
            }
        }
    }

    @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, true)) {
            event.setCanceled(true);
        }
    }
    
    
    /*
     * Helper methods for block harvest/break prevention stuff
     * 
     */

    private boolean tryHarvest(Block block, int blockMeta, EntityPlayer entityPlayer, boolean silent) {
        if (block.getMaterial() == Material.wood)
            return letPlayerUseTool(block, blockMeta, entityPlayer, "axe", silent);
        if (block.getMaterial() == Material.rock)
            return letPlayerUseTool(block, blockMeta, entityPlayer, "pickaxe", silent);
        // 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, boolean silent) {
        if (entityPlayer.getHeldItem() != null) {
            silent = true;
            if ((block.canHarvestBlock(entityPlayer, blockMeta)) && (entityPlayer.getHeldItem().getItem().getHarvestLevel(entityPlayer.getHeldItem(), toolClass) != -1))
                    return true;
        }
        if (!silent) {
            entityPlayer.attackEntityFrom(DamageSource.generic, 0.1f);
            entityPlayer.addChatMessage(new ChatComponentText("Ouch! It seems I need the right tool for the job..."));
        }
        return false;
    }
}

@@ -320,16 +320,53 @@
 Player;%0A
+import net.minecraft.item.ItemStack;%0A
 import n
@@ -548,16 +548,65 @@
 kSpeed;%0A
+import net.minecraftforge.oredict.OreDictionary;%0A
 import n
@@ -1231,15 +1231,8 @@
 ayer
-, false
 )) %7B
@@ -1768,14 +1768,8 @@
 ayer
-, true
 )) %7B
@@ -2003,32 +2003,16 @@
 tyPlayer
-, boolean silent
 ) %7B%0A    
@@ -2056,17 +2056,176 @@
 al.wood)
+ %7B
 %0A
+            for (int oreId : OreDictionary.getOreIDs(new ItemStack(block))) %7B%0A                if (OreDictionary.getOreName(oreId).equals(%22logWood%22))%0A        
         
@@ -2285,35 +2285,51 @@
 layer, %22axe%22
-, silent
 );%0A
+            %7D%0A        %7D%0A
         if (
@@ -2365,17 +2365,174 @@
 al.rock)
+ %7B
 %0A
+            for (int oreId : OreDictionary.getOreIDs(new ItemStack(block))) %7B%0A                if (OreDictionary.getOreName(oreId).equals(%22stone%22))%0A        
         
@@ -2604,18 +2604,34 @@
 axe%22
-, silent);
+);%0A            %7D%0A        %7D
 %0A   
@@ -2859,24 +2859,8 @@
 lass
-, boolean silent
 ) %7B%0A
@@ -2913,35 +2913,8 @@
 ) %7B%0A
-            silent = true;%0A
     
@@ -3125,231 +3125,8 @@
   %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
     

Add ore dictionary checks to harvest restrictions