package extracells.blocks;
import java.util.Random;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.entity.EntityLivingBase;
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.Icon;
import net.minecraft.util.MathHelper;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeDirection;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import extracells.Extracells;
import extracells.tileentity.TileEntityHardMEDrive;
public class BlockHardMEDrive extends RotatableColorBlock
{
@SideOnly(Side.CLIENT)
Icon frontIcon;
@SideOnly(Side.CLIENT)
Icon sideIcon;
@SideOnly(Side.CLIENT)
Icon bottomIcon;
@SideOnly(Side.CLIENT)
Icon topIcon;
public BlockHardMEDrive(int id)
{
super(id, Material.rock);
setCreativeTab(Extracells.ModTab);
setUnlocalizedName("block.hardmedrive");
setHardness(2.0F);
setResistance(1000000.0F);
}
@SideOnly(Side.CLIENT)
public Icon getIcon(int side, int metadata)
{
return giveIcon(side, 3);
}
@SideOnly(Side.CLIENT)
public Icon giveIcon(int side, int metadata)
{
return side == metadata ? frontIcon : side == 0 ? bottomIcon : side == 1 ? topIcon : sideIcon;
}
@Override
public ForgeDirection[] getValidRotations(World worldObj, int x, int y, int z)
{
return new ForgeDirection[]
{ ForgeDirection.WEST, ForgeDirection.EAST, ForgeDirection.NORTH, ForgeDirection.SOUTH };
}
@Override
@SideOnly(Side.CLIENT)
public Icon getBlockTexture(IBlockAccess blockAccess, int x, int y, int z, int side)
{
TileEntity tileentity = blockAccess.getBlockTileEntity(x, y, z);
int metadata = blockAccess.getBlockMetadata(x, y, z);
if (tileentity != null)
{
return giveIcon(side, metadata);
}
return null;
}
@SideOnly(Side.CLIENT)
public void registerIcons(IconRegister iconregister)
{
frontIcon = iconregister.registerIcon("extracells:hardmedrive.face");
sideIcon = iconregister.registerIcon("extracells:hardmedrive.side");
bottomIcon = iconregister.registerIcon("extracells:machine.bottom");
topIcon = iconregister.registerIcon("extracells:machine.top");
}
@Override
public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase player, ItemStack itemstack)
{
super.onBlockPlacedBy(world, x, y, z, player, itemstack);
int l = MathHelper.floor_double(player.rotationYaw * 4.0F / 360.0F + 0.5D) & 3;
if (!player.isSneaking())
{
if (l == 0)
{
world.setBlockMetadataWithNotify(x, y, z, 2, 2);
}
if (l == 1)
{
world.setBlockMetadataWithNotify(x, y, z, 5, 2);
}
if (l == 2)
{
world.setBlockMetadataWithNotify(x, y, z, 3, 2);
}
if (l == 3)
{
world.setBlockMetadataWithNotify(x, y, z, 4, 2);
}
} else
{
if (l == 0)
{
world.setBlockMetadataWithNotify(x, y, z, ForgeDirection.getOrientation(2).getOpposite().ordinal(), 2);
}
if (l == 1)
{
world.setBlockMetadataWithNotify(x, y, z, ForgeDirection.getOrientation(5).getOpposite().ordinal(), 2);
}
if (l == 2)
{
world.setBlockMetadataWithNotify(x, y, z, ForgeDirection.getOrientation(3).getOpposite().ordinal(), 2);
}
if (l == 3)
{
world.setBlockMetadataWithNotify(x, y, z, ForgeDirection.getOrientation(4).getOpposite().ordinal(), 2);
}
}
}
@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float offsetX, float offsetY, float offsetZ)
{
if (!world.isRemote)
{
if (world.getBlockTileEntity(x, y, z) == null || player.isSneaking())
{
return false;
}
player.openGui(Extracells.instance, 0, world, x, y, z);
}
return true;
}
@Override
public void breakBlock(World world, int x, int y, int z, int par5, int par6)
{
dropItems(world, x, y, z);
super.breakBlock(world, x, y, z, par5, par6);
}
private void dropItems(World world, int x, int y, int z)
{
Random rand = new Random();
TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
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, x + rx, y + ry, z + rz, new ItemStack(item.itemID, 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)
{
return new TileEntityHardMEDrive();
}
}