ActuallyAdditions/src/main/java/ellpeck/someprettytechystuff/blocks/BlockContainerBase.java

48 lines
2 KiB
Java
Raw Normal View History

2015-01-05 22:14:01 +01:00
package ellpeck.someprettytechystuff.blocks;
2014-12-20 21:34:07 +01:00
2015-01-05 22:14:01 +01:00
import ellpeck.someprettytechystuff.tile.TileEntityInventoryBase;
2014-12-20 21:34:07 +01:00
import net.minecraft.block.Block;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import java.util.Random;
public abstract class BlockContainerBase extends BlockContainer{
public BlockContainerBase(Material mat) {
super(mat);
}
public TileEntityInventoryBase dropInventory(World world, int x, int y, int z) {
TileEntityInventoryBase tileEntity = (TileEntityInventoryBase) world.getTileEntity(x, y, z);
for (int i = 0; i < tileEntity.getSizeInventory(); i++){
2014-12-20 21:34:07 +01:00
ItemStack itemStack = tileEntity.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;
}
}
return tileEntity;
}
public void breakBlock(World world, int x, int y, int z, Block block, int meta){
if(world.getTileEntity(x, y, z) instanceof TileEntityInventoryBase) this.dropInventory(world, x, y, z);
2014-12-20 21:34:07 +01:00
super.breakBlock(world, x, y, z, block, meta);
}
}