From 3a6724b6f440f824a19a543d0bb4fb910e537a13 Mon Sep 17 00:00:00 2001
From: modmuss50 <modmuss50@gmail.com>
Date: Sat, 27 May 2017 01:49:07 +0100
Subject: [PATCH] Advanced diamond drill is now 3x3

---
 .../items/tools/ItemAdvancedDrill.java        | 75 +++++++++++++++++++
 1 file changed, 75 insertions(+)

diff --git a/src/main/java/techreborn/items/tools/ItemAdvancedDrill.java b/src/main/java/techreborn/items/tools/ItemAdvancedDrill.java
index 1800d56e3..dace54618 100644
--- a/src/main/java/techreborn/items/tools/ItemAdvancedDrill.java
+++ b/src/main/java/techreborn/items/tools/ItemAdvancedDrill.java
@@ -24,18 +24,30 @@
 
 package techreborn.items.tools;
 
+import net.minecraft.block.Block;
 import net.minecraft.block.state.IBlockState;
 import net.minecraft.creativetab.CreativeTabs;
+import net.minecraft.entity.EntityLivingBase;
+import net.minecraft.entity.item.EntityItem;
+import net.minecraft.entity.player.EntityPlayer;
 import net.minecraft.init.Items;
 import net.minecraft.item.Item;
 import net.minecraft.item.ItemStack;
+import net.minecraft.util.EnumFacing;
 import net.minecraft.util.NonNullList;
+import net.minecraft.util.math.BlockPos;
+import net.minecraft.world.World;
+import net.minecraftforge.common.MinecraftForge;
+import net.minecraftforge.event.world.BlockEvent;
 import net.minecraftforge.fml.relauncher.Side;
 import net.minecraftforge.fml.relauncher.SideOnly;
 import reborncore.common.powerSystem.PoweredItem;
 import techreborn.config.ConfigTechReborn;
 import techreborn.init.ModItems;
 
+import java.util.ArrayList;
+import java.util.List;
+
 public class ItemAdvancedDrill extends ItemDrill {
 
 	public ItemAdvancedDrill() {
@@ -62,4 +74,67 @@ public class ItemAdvancedDrill extends ItemDrill {
 		return Items.DIAMOND_PICKAXE.canHarvestBlock(blockIn) || Items.DIAMOND_SHOVEL.canHarvestBlock(blockIn);
 	}
 
+	@Override
+	public boolean onBlockDestroyed(ItemStack stack, World worldIn, IBlockState blockIn, BlockPos pos, EntityLivingBase entityLiving) {
+		//TODO raytrace
+		EnumFacing enumfacing = entityLiving.getHorizontalFacing().getOpposite();
+		if (entityLiving.rotationPitch < -50) {
+			enumfacing = EnumFacing.DOWN;
+		} else if (entityLiving.rotationPitch > 50) {
+			enumfacing = EnumFacing.UP;
+		}
+		if (enumfacing == EnumFacing.SOUTH || enumfacing == EnumFacing.NORTH) {
+			for (int i = -1; i < 2; i++) {
+				for (int j = -1; j < 2; j++) {
+					breakBlock(pos.add(i, j, 0), stack, worldIn, entityLiving, pos);
+				}
+			}
+		} else if (enumfacing == EnumFacing.EAST || enumfacing == EnumFacing.WEST) {
+			for (int i = -1; i < 2; i++) {
+				for (int j = -1; j < 2; j++) {
+					breakBlock(pos.add(0, j, i), stack, worldIn, entityLiving, pos);
+				}
+			}
+		} else if (enumfacing == EnumFacing.DOWN || enumfacing == EnumFacing.UP) {
+			for (int i = -1; i < 2; i++) {
+				for (int j = -1; j < 2; j++) {
+					breakBlock(pos.add(j, 0, i), stack, worldIn, entityLiving, pos);
+				}
+			}
+		}
+
+		return super.onBlockDestroyed(stack, worldIn, blockIn, pos, entityLiving);
+	}
+
+	public void breakBlock(BlockPos pos, ItemStack stack, World world, EntityLivingBase entityLiving, BlockPos oldPos) {
+		if (oldPos == pos) {
+			return;
+		}
+		if (!PoweredItem.canUseEnergy(cost, stack)) {
+			return;
+		}
+		IBlockState blockState = world.getBlockState(pos);
+		Block block = blockState.getBlock();
+		List<ItemStack> stuff = block.getDrops(world, pos, blockState, 0);
+		List<ItemStack> dropList = new ArrayList<>();
+		BlockEvent.HarvestDropsEvent event = new BlockEvent.HarvestDropsEvent(world, pos, blockState, 0, 1, dropList, (EntityPlayer) entityLiving, false);
+		MinecraftForge.EVENT_BUS.post(event);
+		for (ItemStack drop : dropList) {
+			if (!drop.isEmpty() && drop.getCount() > 0) {
+				stuff.add(drop);
+			}
+		}
+		for (ItemStack drop : stuff) {
+			if (world.isRemote) {
+				continue;
+			}
+			final EntityItem entityitem = new EntityItem(world, oldPos.getX(), oldPos.getY(), oldPos.getZ(), drop);
+			entityitem.motionX = (oldPos.getX() - oldPos.getX()) / 10.0f;
+			entityitem.motionY = 0.15000000596046448;
+			entityitem.motionZ = (oldPos.getZ() - oldPos.getZ()) / 10.0f;
+			world.spawnEntity(entityitem);
+		}
+		PoweredItem.useEnergy(cost, stack);
+		world.setBlockToAir(pos);
+	}
 }