package micdoodle8.mods.galacticraft.core.blocks;
import micdoodle8.mods.galacticraft.api.block.IOxygenReliantBlock;
import micdoodle8.mods.galacticraft.api.world.IGalacticraftWorldProvider;
import micdoodle8.mods.galacticraft.core.GCBlocks;
import micdoodle8.mods.galacticraft.core.GalacticraftCore;
import micdoodle8.mods.galacticraft.core.util.OxygenUtil;
import net.minecraft.block.Block;
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.item.ItemStack;
import net.minecraft.util.*;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class BlockUnlitTorch extends BlockTorchBase implements IOxygenReliantBlock
{
public static final PropertyDirection FACING = PropertyDirection.create("facing", facing -> facing != EnumFacing.DOWN);
public boolean lit;
public Block litVersion;
public Block unlitVersion;
public Block fallback;
public BlockUnlitTorch(boolean lit, String assetName)
{
super(Material.circuits);
this.setTickRandomly(true);
this.lit = lit;
this.setLightLevel(lit ? 0.9375F : 0.2F);
this.setHardness(0.0F);
this.setStepSound(Block.soundTypeWood);
this.setUnlocalizedName(assetName);
this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.UP));
}
public static void register(BlockUnlitTorch unlittorch, BlockUnlitTorch littorch, Block vanillatorch)
{
littorch.litVersion = littorch;
littorch.unlitVersion = unlittorch;
littorch.fallback = vanillatorch;
unlittorch.litVersion = littorch;
unlittorch.unlitVersion = unlittorch;
unlittorch.fallback = vanillatorch;
GalacticraftCore.handler.registerTorchType(littorch, vanillatorch);
}
@Override
public AxisAlignedBB getCollisionBoundingBox(World worldIn, BlockPos pos, IBlockState state)
{
return null;
}
@Override
public boolean isOpaqueCube()
{
return false;
}
@Override
public boolean isFullCube()
{
return false;
}
@Override
public boolean canPlaceBlockAt(World worldIn, BlockPos pos)
{
for (EnumFacing enumfacing : FACING.getAllowedValues())
{
if (this.canPlaceAt(worldIn, pos, enumfacing))
{
return true;
}
}
return false;
}
@Override
public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand)
{
if (state.getBlock().getMetaFromState(state) == 0)
{
this.onBlockAdded(worldIn, pos, state);
}
else
{
this.checkOxygen(worldIn, pos, state);
}
}
@Override
public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state)
{
if (this.checkForDrop(worldIn, pos, state))
{
this.checkOxygen(worldIn, pos, state);
}
}
@Override
public void onNeighborBlockChange(World worldIn, BlockPos pos, IBlockState state, Block neighborBlock)
{
this.onNeighborChangeInternal(worldIn, pos, state);
}
protected boolean onNeighborChangeInternal(World worldIn, BlockPos pos, IBlockState state)
{
if (!this.checkForDrop(worldIn, pos, state))
{
return true;
}
else
{
EnumFacing enumfacing = (EnumFacing) state.getValue(FACING);
EnumFacing.Axis enumfacingAxis = enumfacing.getAxis();
EnumFacing enumfacing1 = enumfacing.getOpposite();
boolean flag = false;
if (enumfacingAxis.isHorizontal() && !worldIn.isSideSolid(pos.offset(enumfacing1), enumfacing, true))
{
flag = true;
}
else if (enumfacingAxis.isVertical() && !this.canPlaceOn(worldIn, pos.offset(enumfacing1)))
{
flag = true;
}
if (flag)
{
this.dropBlockAsItem(worldIn, pos, state, 0);
worldIn.setBlockToAir(pos);
return true;
}
else
{
this.checkOxygen(worldIn, pos, state);
return false;
}
}
}
private void checkOxygen(World world, BlockPos pos, IBlockState state)
{
if (world.provider instanceof IGalacticraftWorldProvider)
{
if (OxygenUtil.checkTorchHasOxygen(world, pos))
{
this.onOxygenAdded(world, pos, state);
}
else
{
this.onOxygenRemoved(world, pos, state);
}
}
else
{
EnumFacing enumfacing = state.getValue(FACING);
world.setBlockState(pos, this.fallback.getDefaultState().withProperty(FACING, enumfacing), 2);
}
}
@Override
public MovingObjectPosition collisionRayTrace(World worldIn, BlockPos pos, Vec3 start, Vec3 end)
{
EnumFacing enumfacing = worldIn.getBlockState(pos).getValue(FACING);
float f = 0.15F;
if (enumfacing == EnumFacing.EAST)
{
this.setBlockBounds(0.0F, 0.2F, 0.5F - f, f * 2.0F, 0.8F, 0.5F + f);
}
else if (enumfacing == EnumFacing.WEST)
{
this.setBlockBounds(1.0F - f * 2.0F, 0.2F, 0.5F - f, 1.0F, 0.8F, 0.5F + f);
}
else if (enumfacing == EnumFacing.SOUTH)
{
this.setBlockBounds(0.5F - f, 0.2F, 0.0F, 0.5F + f, 0.8F, f * 2.0F);
}
else if (enumfacing == EnumFacing.NORTH)
{
this.setBlockBounds(0.5F - f, 0.2F, 1.0F - f * 2.0F, 0.5F + f, 0.8F, 1.0F);
}
else
{
f = 0.1F;
this.setBlockBounds(0.5F - f, 0.0F, 0.5F - f, 0.5F + f, 0.6F, 0.5F + f);
}
return super.collisionRayTrace(worldIn, pos, start, end);
}
@Override
@SideOnly(Side.CLIENT)
public void randomDisplayTick(World worldIn, BlockPos pos, IBlockState state, Random par5Random)
{
EnumFacing enumfacing = state.getValue(FACING);
double d0 = (double) pos.getX() + 0.5D;
double d1 = (double) pos.getY() + 0.7D;
double d2 = (double) pos.getZ() + 0.5D;
double d3 = 0.22D;
double d4 = 0.27D;
if (enumfacing.getAxis().isHorizontal())
{
EnumFacing enumfacing1 = enumfacing.getOpposite();
worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 + d4 * (double) enumfacing1.getFrontOffsetX(), d1 + d3, d2 + d4 * (double) enumfacing1.getFrontOffsetZ(), 0.0D, 0.0D, 0.0D, new int[0]);
if (this == GCBlocks.unlitTorchLit)
{
worldIn.spawnParticle(EnumParticleTypes.FLAME, d0 + d4 * (double) enumfacing1.getFrontOffsetX(), d1 + d3, d2 + d4 * (double) enumfacing1.getFrontOffsetZ(), 0.0D, 0.0D, 0.0D, new int[0]);
}
}
else
{
worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0, d1, d2, 0.0D, 0.0D, 0.0D, new int[0]);
if (this == GCBlocks.unlitTorchLit)
{
worldIn.spawnParticle(EnumParticleTypes.FLAME, d0, d1, d2, 0.0D, 0.0D, 0.0D, new int[0]);
}
}
}
@Override
public void onOxygenRemoved(World world, BlockPos pos, IBlockState state)
{
if (world.provider instanceof IGalacticraftWorldProvider)
{
EnumFacing enumfacing = state.getValue(FACING);
world.setBlockState(pos, this.unlitVersion.getDefaultState().withProperty(FACING, enumfacing), 2);
}
}
@Override
public void onOxygenAdded(World world, BlockPos pos, IBlockState state)
{
if (world.provider instanceof IGalacticraftWorldProvider)
{
EnumFacing enumfacing = state.getValue(FACING);
world.setBlockState(pos, this.litVersion.getDefaultState().withProperty(FACING, enumfacing), 2);
}
}
@Override
public List<ItemStack> getDrops(IBlockAccess world, BlockPos pos, IBlockState state, int fortune)
{
ArrayList<ItemStack> ret = new ArrayList<ItemStack>();
ret.add(new ItemStack(this.litVersion));
return ret;
}
@Override
protected BlockState createBlockState()
{
return new BlockState(this, new IProperty[] { FACING });
}
@Override
@SideOnly(Side.CLIENT)
public EnumWorldBlockLayer getBlockLayer()
{
return EnumWorldBlockLayer.CUTOUT;
}
}