From 65bd20379126a4b0b69f3d3c86a0f0e0c86d93d8 Mon Sep 17 00:00:00 2001 From: Gig Date: Sun, 7 Jun 2015 00:00:15 +0100 Subject: [PATCH] All machines will drop Inventory after they Implement IInventory --- .../techreborn/blocks/BlockMachineBase.java | 53 ++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/src/main/java/techreborn/blocks/BlockMachineBase.java b/src/main/java/techreborn/blocks/BlockMachineBase.java index a02ecb9a4..190edbed6 100644 --- a/src/main/java/techreborn/blocks/BlockMachineBase.java +++ b/src/main/java/techreborn/blocks/BlockMachineBase.java @@ -1,12 +1,16 @@ package techreborn.blocks; +import java.util.Random; + import net.minecraft.block.Block; import net.minecraft.block.BlockContainer; import net.minecraft.block.material.Material; import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.EnumCreatureType; -import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.item.EntityItem; +import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.MathHelper; import net.minecraft.world.World; @@ -102,4 +106,51 @@ public class BlockMachineBase extends BlockContainer { { return false; } + + @Override + public void breakBlock(World world, int x, int y, int z, Block block, int meta) + { + dropInventory(world, x, y, z); + super.breakBlock(world, x, y, z, block, meta); + } + + protected void dropInventory(World world, int x, int y, int z) + { + TileEntity tileEntity = world.getTileEntity(x, y, z); + + if (!(tileEntity instanceof IInventory)) + { + return; + } + + IInventory inventory = (IInventory) tileEntity; + + for (int i = 0; i < inventory.getSizeInventory(); i++) + { + ItemStack itemStack = inventory.getStackInSlot(i); + + if (itemStack != null && itemStack.stackSize > 0) + { + Random rand = new Random(); + + float dX = rand.nextFloat() * 0.8F + 0.1F; + float dY = rand.nextFloat() * 0.8F + 0.1F; + float dZ = rand.nextFloat() * 0.8F + 0.1F; + + EntityItem entityItem = new EntityItem(world, x + dX, y + dY, z + dZ, itemStack.copy()); + + if (itemStack.hasTagCompound()) + { + entityItem.getEntityItem().setTagCompound((NBTTagCompound) itemStack.getTagCompound().copy()); + } + + float factor = 0.05F; + entityItem.motionX = rand.nextGaussian() * factor; + entityItem.motionY = rand.nextGaussian() * factor + 0.2F; + entityItem.motionZ = rand.nextGaussian() * factor; + world.spawnEntityInWorld(entityItem); + itemStack.stackSize = 0; + } + } + } }