package xyz.brassgoggledcoders.steamagerevolution.modules.mining;

import java.lang.ref.WeakReference;

import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.NonNullList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.util.FakePlayer;
import net.minecraftforge.common.util.FakePlayerFactory;
import net.minecraftforge.event.ForgeEventFactory;
import net.minecraftforge.event.world.BlockEvent;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemHandlerHelper;
import xyz.brassgoggledcoders.steamagerevolution.SteamAgeRevolution;
import xyz.brassgoggledcoders.steamagerevolution.modules.mining.drill.ControllerDrill;

public class MiningUtils {

	public static boolean allowedToBreak(IBlockState state, World world, BlockPos pos, EntityPlayer entityPlayer) {
		if (!state.getBlock().canEntityDestroy(state, world, pos, entityPlayer)) {
			return false;
		}
		BlockEvent.BreakEvent event = new BlockEvent.BreakEvent(world, pos, state, entityPlayer);
		MinecraftForge.EVENT_BUS.post(event);
		return !event.isCanceled();
	}

	//TODO Account for breaking speed
	public static void doMining(World world, BlockPos pos, IItemHandler itemHandler) {
		WeakReference<FakePlayer> fakePlayer = new WeakReference<FakePlayer>(
				FakePlayerFactory.get((WorldServer) world, ControllerDrill.profile));
		// Skip air, skip unbreakable blocks, skip tile entities and skip blocks that
		// are otherwise unharvestable
		IBlockState state = world.getBlockState(pos);
		if (!world.isAirBlock(pos) && state.getBlockHardness(world, pos) >= 0 && world.getTileEntity(pos) == null
				&& allowedToBreak(state, world, pos, fakePlayer.get())) {
			NonNullList<ItemStack> drops = NonNullList.create();
			state.getBlock().getDrops(drops, world, pos, state, 0);
			for (ItemStack drop : drops) {
				ItemHandlerHelper.insertItemStacked(itemHandler, drop, false);
			}
			world.destroyBlock(pos, false);
			ForgeEventFactory.fireBlockHarvesting(drops, world, pos, state, 0, 0, false, fakePlayer.get());
			SteamAgeRevolution.instance.getLogger().devInfo("Mining " + pos.toString());
		}
	}

}

@@ -175,27 +175,105 @@
 ity.
-player.EntityPlayer
+item.EntityItem;%0Aimport net.minecraft.entity.player.EntityPlayer;%0Aimport net.minecraft.init.Items
 ;%0Aim
@@ -346,16 +346,62 @@
 llList;%0A
+import net.minecraft.util.math.AxisAlignedBB;%0A
 import n
@@ -865,77 +865,8 @@
 er;%0A
-import xyz.brassgoggledcoders.steamagerevolution.SteamAgeRevolution;%0A
 impo
@@ -947,16 +947,103 @@
 erDrill;
+%0Aimport xyz.brassgoggledcoders.steamagerevolution.utils.inventory.IMachineHasInventory;
 %0A%0Apublic
@@ -1560,16 +1560,37 @@
 dler) %7B%0A
+%09%09//TODO Player tool%0A
 %09%09WeakRe
@@ -2065,154 +2065,509 @@
 %0A%09%09%09
-NonNullList%3CItemStack%3E drops = NonNullList.create();%0A%09%09%09state.getBlock().getDrops(drops, world, pos, state, 0
+%09state.getBlock().onBlockHarvested(world, pos, state, fakePlayer.get());%0A%09%09%09%09state.getBlock().harvestBlock(world, fakePlayer.get(), pos, state, world.getTileEntity(pos), new ItemStack(Items.IRON_PICKAXE));%0A%09%09%09%09NonNullList%3CItemStack%3E drops = NonNullList.create();%0A%09%09%09%09for(EntityItem item : world.getEntitiesWithinAABB(EntityItem.class, new AxisAlignedBB(pos).grow(1))) %7B%0A%09%09%09%09%09ItemStack stack = item.getItem().copy(
 );%0A%09%09%09
+%09
-for 
+%09if
 (Item
-Stack drop : drops
+HandlerHelper.insertItemStacked(itemHandler, stack, true).isEmpty()
 ) %7B%0A
+%09%09
 %09%09%09%09
@@ -2615,20 +2615,21 @@
 andler, 
-drop
+stack
 , false)
@@ -2633,18 +2633,73 @@
 se);%0A%09%09%09
-%7D%0A
+%09%09%09item.setDead();%0A%09%09%09%09%09%09drops.add(stack);%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A%09
 %09%09%09world
@@ -2725,16 +2725,17 @@
 false);%0A
+%09
 %09%09%09Forge
@@ -2829,88 +2829,8 @@
 ));%0A
-%09%09%09SteamAgeRevolution.instance.getLogger().devInfo(%22Mining %22 + pos.toString());%0A
 %09%09%7D%0A

Rewrite mining logic to handle drops correctly