package com.harry9137.ct.block;
import com.harry9137.ct.CreepTech;
import com.harry9137.ct.reference.names;
import com.harry9137.ct.tileentity.TileEntityTechTable;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.world.World;
import java.util.Random;
public class BlockTechTable extends BlockCT implements ITileEntityProvider {
public BlockTechTable(){
super(Material.iron);
this.setUnlocalizedName(names.blocks.BlockTechTable);
}
@Override
public void breakBlock(World world, BlockPos pos, IBlockState state) {
dropItems(world, pos);
super.breakBlock(world,pos, state);
}
@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ)
{
if (playerIn.isSneaking()) {
return false;
}
playerIn.openGui(CreepTech.INSTANCE, 0, worldIn, pos.getX(), pos.getY(), pos.getZ());
return true;
}
private void dropItems(World world, BlockPos pos){
Random rand = new Random();
TileEntity tileEntity = world.getTileEntity(pos);
if (!(tileEntity instanceof IInventory)) {
return;
}
IInventory inventory = (IInventory) tileEntity;
for (int i = 0; i < inventory.getSizeInventory(); i++) {
ItemStack item = inventory.getStackInSlot(i);
if (item != null && item.stackSize > 0) {
float rx = rand.nextFloat() * 0.8F + 0.1F;
float ry = rand.nextFloat() * 0.8F + 0.1F;
float rz = rand.nextFloat() * 0.8F + 0.1F;
EntityItem entityItem = new EntityItem(world,
pos.getX() + rx, pos.getY() + ry, pos.getZ() + rz,
new ItemStack(item.getItem(), item.stackSize, item.getItemDamage()));
if (item.hasTagCompound()) {
entityItem.getEntityItem().setTagCompound((NBTTagCompound) item.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);
item.stackSize = 0;
}
}
}
@Override
public TileEntity createNewTileEntity(World world, int metadata)
{
return new TileEntityTechTable();
}
@Override
public boolean hasTileEntity(IBlockState state){
return true;
}
}