package net.minecraftplus.mcp_ice_box;
import java.util.Random;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.BlockPistonBase;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyDirection;
import net.minecraft.block.state.BlockState;
import net.minecraft.block.state.IBlockState;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.InventoryHelper;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftplus._api.dictionary.Sounds;
public class BlockIceBox extends BlockContainer
{
public static final PropertyDirection FACING = PropertyDirection.create("facing");
protected BlockIceBox()
{
super(Material.rock);
this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH));
this.setCreativeTab(CreativeTabs.tabDecorations);
this.setTickRandomly(true);
}
@Override
public void randomTick(World worldIn, BlockPos pos, IBlockState state, Random random)
{
if (!worldIn.isRemote)
{
TileEntity tileentity = worldIn.getTileEntity(pos);
if (tileentity instanceof TileEntityIceBox)
{
TileEntityIceBox iceBox = (TileEntityIceBox) tileentity;
TileEntityIceBox.consumeIceInInventory(worldIn, iceBox);
}
}
worldIn.playSoundEffect((double)pos.getX() + 0.5D, (double)pos.getY() + 0.5D, (double)pos.getZ() + 0.5D, Sounds.RANDOM_FIZZ, 0.05F, 0.6F);
super.randomTick(worldIn, pos, state, random);
}
@Override
public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state)
{
super.onBlockAdded(worldIn, pos, state);
this.setDefaultDirection(worldIn, pos, state);
}
private void setDefaultDirection(World worldIn, BlockPos pos, IBlockState state)
{
if (!worldIn.isRemote)
{
EnumFacing enumfacing = (EnumFacing)state.getValue(FACING);
boolean flag = worldIn.getBlockState(pos.north()).getBlock().isFullBlock();
boolean flag1 = worldIn.getBlockState(pos.south()).getBlock().isFullBlock();
if (enumfacing == EnumFacing.NORTH && flag && !flag1)
{
enumfacing = EnumFacing.SOUTH;
}
else if (enumfacing == EnumFacing.SOUTH && flag1 && !flag)
{
enumfacing = EnumFacing.NORTH;
}
else
{
boolean flag2 = worldIn.getBlockState(pos.west()).getBlock().isFullBlock();
boolean flag3 = worldIn.getBlockState(pos.east()).getBlock().isFullBlock();
if (enumfacing == EnumFacing.WEST && flag2 && !flag3)
{
enumfacing = EnumFacing.EAST;
}
else if (enumfacing == EnumFacing.EAST && flag3 && !flag2)
{
enumfacing = EnumFacing.WEST;
}
}
worldIn.setBlockState(pos, state.withProperty(FACING, enumfacing), 2);
}
}
@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ)
{
TileEntity tileentity = worldIn.getTileEntity(pos);
if (tileentity instanceof TileEntityIceBox)
{
playerIn.openGui(_Ice_Box.INSTANCE, 0, worldIn, pos.getX(), pos.getY(), pos.getZ());
if (TileEntityIceBox.containsIceInInventory((TileEntityIceBox) tileentity))
{
TileEntityIceBox.freezeInventory(playerIn.worldObj, (TileEntityIceBox) tileentity);
}
}
return true;
}
@Override
public TileEntity createNewTileEntity(World worldIn, int meta)
{
return new TileEntityIceBox();
}
@Override
public IBlockState onBlockPlaced(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
{
return this.getDefaultState().withProperty(FACING, BlockPistonBase.getFacingFromEntity(worldIn, pos, placer));
}
@Override
public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack)
{
worldIn.setBlockState(pos, state.withProperty(FACING, BlockPistonBase.getFacingFromEntity(worldIn, pos, placer)), 2);
if (stack.hasDisplayName())
{
TileEntity tileentity = worldIn.getTileEntity(pos);
if (tileentity instanceof TileEntityIceBox)
{
((TileEntityIceBox)tileentity).setCustomName(stack.getDisplayName());
}
}
}
@Override
public void breakBlock(World worldIn, BlockPos pos, IBlockState state)
{
TileEntity tileentity = worldIn.getTileEntity(pos);
if (tileentity instanceof TileEntityIceBox)
{
InventoryHelper.dropInventoryItems(worldIn, pos, (TileEntityIceBox)tileentity);
worldIn.updateComparatorOutputLevel(pos, this);
}
super.breakBlock(worldIn, pos, state);
}
/**
* Get the facing of a dispenser with the given metadata
*/
public static EnumFacing getFacing(int meta)
{
return EnumFacing.getFront(meta & 7);
}
@Override
public boolean hasComparatorInputOverride()
{
return true;
}
@Override
public int getComparatorInputOverride(World worldIn, BlockPos pos)
{
return Container.calcRedstone(worldIn.getTileEntity(pos));
}
@Override
public int getRenderType()
{
return 3;
}
@SideOnly(Side.CLIENT)
@Override
public IBlockState getStateForEntityRender(IBlockState state)
{
return this.getDefaultState().withProperty(FACING, EnumFacing.SOUTH);
}
@Override
public IBlockState getStateFromMeta(int meta)
{
return this.getDefaultState().withProperty(FACING, getFacing(meta));
}
@Override
public int getMetaFromState(IBlockState state)
{
byte b0 = 0;
int i = b0 | ((EnumFacing)state.getValue(FACING)).getIndex();
return i;
}
@Override
protected BlockState createBlockState()
{
return new BlockState(this, new IProperty[] {FACING});
}
}