package me.km.blocks; import java.util.Random; import me.km.KajetansMod; import net.minecraft.block.Block; import net.minecraft.block.BlockDirectional; import net.minecraft.block.SoundType; import net.minecraft.block.material.Material; import net.minecraft.block.properties.IProperty; import net.minecraft.block.state.BlockFaceShape; import net.minecraft.block.state.BlockStateContainer; import net.minecraft.block.state.IBlockState; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.entity.EntityLivingBase; import net.minecraft.item.Item; import net.minecraft.util.BlockRenderLayer; import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumParticleTypes; import net.minecraft.util.Mirror; import net.minecraft.util.Rotation; import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.BlockPos; import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; public class BlockLantern extends BlockDirectional implements IBlockBase { protected static final AxisAlignedBB HANGING_AABB = new AxisAlignedBB(0.1875d, 0.125d, 0.1875d, 0.8125d, 1, 0.875d); protected static final AxisAlignedBB STANDING_AABB = new AxisAlignedBB(0.1875d, 0, 0.1875d, 0.8125d, 0.75d, 0.875d); protected static final AxisAlignedBB TORCH_NORTH_AABB = new AxisAlignedBB(0.1875d, 0, 0.375d, 0.8125d, 1, 1); protected static final AxisAlignedBB TORCH_SOUTH_AABB = new AxisAlignedBB(0.1875d, 0, 0, 0.8125d, 1, 0.625d); protected static final AxisAlignedBB TORCH_WEST_AABB = new AxisAlignedBB(0.375d, 0, 0.1875d, 1, 1, 0.8125d); protected static final AxisAlignedBB TORCH_EAST_AABB = new AxisAlignedBB(0, 0, 0.1875d, 0.625d, 1, 0.8125d); protected String name; public BlockLantern(String name, String local) { super(Material.IRON); this.name = name; super.setHardness(0.0F); super.setLightLevel(0.9375F); super.setSoundType(SoundType.METAL); this.setRegistryName(name); super.setUnlocalizedName(local); super.setCreativeTab(CreativeTabs.DECORATIONS); this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.UP)); super.setTickRandomly(true); } @Override public void registerItemModel(Item itemBlock) { KajetansMod.proxy.registerItemRenderer(itemBlock, 0, name); } @Override public BlockLantern setSoundType(SoundType sound) { super.setSoundType(sound); return this; } @Override public AxisAlignedBB getCollisionBoundingBox(IBlockState state, IBlockAccess w, BlockPos pos) { return NULL_AABB; } @Override public boolean isOpaqueCube(IBlockState state) { return false; } @Override public boolean isFullCube(IBlockState state) { return false; } @Override public boolean canPlaceBlockOnSide(World w, BlockPos pos, EnumFacing side) { return canPlaceBlock(w, pos, side); } @Override public boolean canPlaceBlockAt(World worldIn, BlockPos pos) { for(EnumFacing enumfacing : EnumFacing.values()) { if(canPlaceBlock(worldIn, pos, enumfacing)) { return true; } } return false; } protected static boolean canPlaceBlock(World w, BlockPos pos, EnumFacing direction) { BlockPos blockpos = pos.offset(direction.getOpposite()); IBlockState state = w.getBlockState(blockpos); Block block = state.getBlock(); if(direction == EnumFacing.UP) { return block.canPlaceTorchOnTop(state, w, pos); } else { return !isExceptBlockForAttachWithPiston(block) && state.getBlockFaceShape(w, blockpos, direction) == BlockFaceShape.SOLID; } } @Override public IBlockState getStateForPlacement(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer) { return canPlaceBlock(worldIn, pos, facing) ? this.getDefaultState().withProperty(FACING, facing) : this.getDefaultState().withProperty(FACING, EnumFacing.DOWN); } @Override public void neighborChanged(IBlockState state, World worldIn, BlockPos pos, Block blockIn, BlockPos fromPos) { if(this.checkForDrop(worldIn, pos, state) && !canPlaceBlock(worldIn, pos, state.getValue(FACING))) { this.dropBlockAsItem(worldIn, pos, state, 0); worldIn.setBlockToAir(pos); } } private boolean checkForDrop(World worldIn, BlockPos pos, IBlockState state) { if(this.canPlaceBlockAt(worldIn, pos)) { return true; } else { this.dropBlockAsItem(worldIn, pos, state, 0); worldIn.setBlockToAir(pos); return false; } } @Override public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos) { EnumFacing enumfacing = (EnumFacing) state.getValue(FACING); switch(enumfacing) { case EAST: return TORCH_EAST_AABB; case WEST: return TORCH_WEST_AABB; case SOUTH: return TORCH_SOUTH_AABB; case NORTH: default: return TORCH_NORTH_AABB; case UP: return STANDING_AABB; case DOWN: return HANGING_AABB; } } @Override public IBlockState getStateFromMeta(int meta) { EnumFacing enumfacing; switch(meta & 7) { case 0: enumfacing = EnumFacing.DOWN; break; case 1: enumfacing = EnumFacing.EAST; break; case 2: enumfacing = EnumFacing.WEST; break; case 3: enumfacing = EnumFacing.SOUTH; break; case 4: enumfacing = EnumFacing.NORTH; break; case 5: default: enumfacing = EnumFacing.UP; } return this.getDefaultState().withProperty(FACING, enumfacing); } @Override public int getMetaFromState(IBlockState state) { switch(state.getValue(FACING)) { case EAST: return 1; case WEST: return 2; case SOUTH: return 3; case NORTH: return 4; case UP: default: return 5; case DOWN: return 0; } } @Override public IBlockState withRotation(IBlockState state, Rotation rot) { return state.withProperty(FACING, rot.rotate(state.getValue(FACING))); } @Override public IBlockState withMirror(IBlockState state, Mirror mirrorIn) { return state.withRotation(mirrorIn.toRotation(state.getValue(FACING))); } @Override protected BlockStateContainer createBlockState() { return new BlockStateContainer(this, new IProperty[] {FACING}); } @Override public BlockFaceShape getBlockFaceShape(IBlockAccess p_193383_1_, IBlockState p_193383_2_, BlockPos p_193383_3_, EnumFacing p_193383_4_) { return BlockFaceShape.UNDEFINED; } @SideOnly(Side.CLIENT) @Override public void randomDisplayTick(IBlockState state, World w, BlockPos pos, Random rand) { EnumFacing face = state.getValue(FACING); double x = pos.getX() + 0.5D; double y = pos.getY() + 0.7D; double z = pos.getZ() + 0.5D; switch(face) { case EAST: case WEST: case SOUTH: case NORTH: EnumFacing oface = face.getOpposite(); x += 0.1875d * oface.getFrontOffsetX(); z += 0.1875d * oface.getFrontOffsetZ(); break; case DOWN: break; case UP: y -= 0.125d; break; } w.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, x, y , z, 0, 0, 0); w.spawnParticle(EnumParticleTypes.FLAME, x, y, z, 0, 0, 0); } @SideOnly(Side.CLIENT) @Override public BlockRenderLayer getBlockLayer() { return BlockRenderLayer.TRANSLUCENT; } }