package com.miningmark48.pearcelmod.block;
import com.miningmark48.pearcelmod.init.ModBlocks;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.BlockHorizontal;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyDirection;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.SoundEvents;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.InventoryHelper;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.stats.StatList;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityFurnace;
import net.minecraft.util.*;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import javax.annotation.Nullable;
import java.util.Random;
public class BlockPearcelFurnace extends BlockContainer {
public static final PropertyDirection FACING = BlockHorizontal.FACING;
private final boolean isBurning;
private static boolean keepInventory;
public BlockPearcelFurnace(boolean isBurning)
{
super(Material.ROCK);
this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH));
this.isBurning = isBurning;
}
/**
* Get the Item that this Block should drop when harvested.
*/
@Nullable
public Item getItemDropped(IBlockState state, Random rand, int fortune)
{
return Item.getItemFromBlock(ModBlocks.pearcel_furnace);
}
public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state)
{
this.setDefaultFacing(worldIn, pos, state);
}
private void setDefaultFacing(World worldIn, BlockPos pos, IBlockState state)
{
if (!worldIn.isRemote)
{
IBlockState iblockstate = worldIn.getBlockState(pos.north());
IBlockState iblockstate1 = worldIn.getBlockState(pos.south());
IBlockState iblockstate2 = worldIn.getBlockState(pos.west());
IBlockState iblockstate3 = worldIn.getBlockState(pos.east());
EnumFacing enumfacing = (EnumFacing)state.getValue(FACING);
if (enumfacing == EnumFacing.NORTH && iblockstate.isFullBlock() && !iblockstate1.isFullBlock())
{
enumfacing = EnumFacing.SOUTH;
}
else if (enumfacing == EnumFacing.SOUTH && iblockstate1.isFullBlock() && !iblockstate.isFullBlock())
{
enumfacing = EnumFacing.NORTH;
}
else if (enumfacing == EnumFacing.WEST && iblockstate2.isFullBlock() && !iblockstate3.isFullBlock())
{
enumfacing = EnumFacing.EAST;
}
else if (enumfacing == EnumFacing.EAST && iblockstate3.isFullBlock() && !iblockstate2.isFullBlock())
{
enumfacing = EnumFacing.WEST;
}
worldIn.setBlockState(pos, state.withProperty(FACING, enumfacing), 2);
}
}
@SideOnly(Side.CLIENT)
@SuppressWarnings("incomplete-switch")
public void randomDisplayTick(IBlockState stateIn, World worldIn, BlockPos pos, Random rand)
{
if (this.isBurning)
{
EnumFacing enumfacing = (EnumFacing)stateIn.getValue(FACING);
double d0 = (double)pos.getX() + 0.5D;
double d1 = (double)pos.getY() + rand.nextDouble() * 6.0D / 16.0D;
double d2 = (double)pos.getZ() + 0.5D;
double d3 = 0.52D;
double d4 = rand.nextDouble() * 0.6D - 0.3D;
if (rand.nextDouble() < 0.1D)
{
worldIn.playSound((double)pos.getX() + 0.5D, (double)pos.getY(), (double)pos.getZ() + 0.5D, SoundEvents.BLOCK_FURNACE_FIRE_CRACKLE, SoundCategory.BLOCKS, 1.0F, 1.0F, false);
}
switch (enumfacing)
{
case WEST:
worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 - d3, d1, d2 + d4, 0.0D, 0.0D, 0.0D, new int[0]);
worldIn.spawnParticle(EnumParticleTypes.FLAME, d0 - d3, d1, d2 + d4, 0.0D, 0.0D, 0.0D, new int[0]);
break;
case EAST:
worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 + d3, d1, d2 + d4, 0.0D, 0.0D, 0.0D, new int[0]);
worldIn.spawnParticle(EnumParticleTypes.FLAME, d0 + d3, d1, d2 + d4, 0.0D, 0.0D, 0.0D, new int[0]);
break;
case NORTH:
worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 + d4, d1, d2 - d3, 0.0D, 0.0D, 0.0D, new int[0]);
worldIn.spawnParticle(EnumParticleTypes.FLAME, d0 + d4, d1, d2 - d3, 0.0D, 0.0D, 0.0D, new int[0]);
break;
case SOUTH:
worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 + d4, d1, d2 + d3, 0.0D, 0.0D, 0.0D, new int[0]);
worldIn.spawnParticle(EnumParticleTypes.FLAME, d0 + d4, d1, d2 + d3, 0.0D, 0.0D, 0.0D, new int[0]);
}
}
}
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, @Nullable ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ)
{
if (worldIn.isRemote)
{
return true;
}
else
{
TileEntity tileentity = worldIn.getTileEntity(pos);
if (tileentity instanceof TileEntityFurnace)
{
playerIn.displayGUIChest((TileEntityFurnace)tileentity);
playerIn.addStat(StatList.FURNACE_INTERACTION);
}
return true;
}
}
public static void setState(boolean active, World worldIn, BlockPos pos)
{
IBlockState iblockstate = worldIn.getBlockState(pos);
TileEntity tileentity = worldIn.getTileEntity(pos);
keepInventory = true;
if (active)
{
worldIn.setBlockState(pos, ModBlocks.pearcel_furnace_lit.getDefaultState().withProperty(FACING, iblockstate.getValue(FACING)), 3);
worldIn.setBlockState(pos, ModBlocks.pearcel_furnace_lit.getDefaultState().withProperty(FACING, iblockstate.getValue(FACING)), 3);
}
else
{
worldIn.setBlockState(pos, ModBlocks.pearcel_furnace.getDefaultState().withProperty(FACING, iblockstate.getValue(FACING)), 3);
worldIn.setBlockState(pos, ModBlocks.pearcel_furnace.getDefaultState().withProperty(FACING, iblockstate.getValue(FACING)), 3);
}
keepInventory = false;
if (tileentity != null)
{
tileentity.validate();
worldIn.setTileEntity(pos, tileentity);
}
}
public TileEntity createNewTileEntity(World worldIn, int meta)
{
return new TileEntityFurnace();
}
/**
* Called by ItemBlocks just before a block is actually set in the world, to allow for adjustments to the
* IBlockstate
*/
public IBlockState onBlockPlaced(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
{
return this.getDefaultState().withProperty(FACING, placer.getHorizontalFacing().getOpposite());
}
/**
* Called by ItemBlocks after a block is set in the world, to allow post-place logic
*/
public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack)
{
worldIn.setBlockState(pos, state.withProperty(FACING, placer.getHorizontalFacing().getOpposite()), 2);
if (stack.hasDisplayName())
{
TileEntity tileentity = worldIn.getTileEntity(pos);
if (tileentity instanceof TileEntityFurnace)
{
((TileEntityFurnace)tileentity).setCustomInventoryName(stack.getDisplayName());
}
}
}
public void breakBlock(World worldIn, BlockPos pos, IBlockState state)
{
if (!keepInventory)
{
TileEntity tileentity = worldIn.getTileEntity(pos);
if (tileentity instanceof TileEntityFurnace)
{
InventoryHelper.dropInventoryItems(worldIn, pos, (TileEntityFurnace)tileentity);
worldIn.updateComparatorOutputLevel(pos, this);
}
}
super.breakBlock(worldIn, pos, state);
}
public boolean hasComparatorInputOverride(IBlockState state)
{
return true;
}
public int getComparatorInputOverride(IBlockState blockState, World worldIn, BlockPos pos)
{
return Container.calcRedstone(worldIn.getTileEntity(pos));
}
public ItemStack getItem(World worldIn, BlockPos pos, IBlockState state)
{
return new ItemStack(ModBlocks.pearcel_furnace);
}
/**
* The type of render function called. 3 for standard block models, 2 for TESR's, 1 for liquids, -1 is no render
*/
public EnumBlockRenderType getRenderType(IBlockState state)
{
return EnumBlockRenderType.MODEL;
}
/**
* Convert the given metadata into a BlockState for this Block
*/
public IBlockState getStateFromMeta(int meta)
{
EnumFacing enumfacing = EnumFacing.getFront(meta);
if (enumfacing.getAxis() == EnumFacing.Axis.Y)
{
enumfacing = EnumFacing.NORTH;
}
return this.getDefaultState().withProperty(FACING, enumfacing);
}
/**
* Convert the BlockState into the correct metadata value
*/
public int getMetaFromState(IBlockState state)
{
return ((EnumFacing)state.getValue(FACING)).getIndex();
}
/**
* Returns the blockstate with the given rotation from the passed blockstate. If inapplicable, returns the passed
* blockstate.
*/
public IBlockState withRotation(IBlockState state, Rotation rot)
{
return state.withProperty(FACING, rot.rotate((EnumFacing)state.getValue(FACING)));
}
/**
* Returns the blockstate with the given mirror of the passed blockstate. If inapplicable, returns the passed
* blockstate.
*/
public IBlockState withMirror(IBlockState state, Mirror mirrorIn)
{
return state.withRotation(mirrorIn.toRotation((EnumFacing)state.getValue(FACING)));
}
protected BlockStateContainer createBlockState()
{
return new BlockStateContainer(this, new IProperty[] {FACING});
}
}