package WayofTime.bloodmagic.block.base; import java.util.List; import javax.annotation.Nullable; import net.minecraft.block.BlockHorizontal; import net.minecraft.block.BlockStairs; import net.minecraft.block.BlockStairs.EnumHalf; import net.minecraft.block.BlockStairs.EnumShape; 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.Entity; import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.util.EnumFacing; 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.util.math.RayTraceResult; import net.minecraft.util.math.Vec3d; import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; import net.minecraftforge.common.property.ExtendedBlockState; import net.minecraftforge.common.property.IUnlistedProperty; import com.google.common.collect.Lists; public class BlockStringStairs extends BlockString { public static final PropertyDirection FACING = BlockHorizontal.FACING; protected static final AxisAlignedBB AABB_SLAB_TOP = new AxisAlignedBB(0.0D, 0.5D, 0.0D, 1.0D, 1.0D, 1.0D); protected static final AxisAlignedBB AABB_QTR_TOP_WEST = new AxisAlignedBB(0.0D, 0.5D, 0.0D, 0.5D, 1.0D, 1.0D); protected static final AxisAlignedBB AABB_QTR_TOP_EAST = new AxisAlignedBB(0.5D, 0.5D, 0.0D, 1.0D, 1.0D, 1.0D); protected static final AxisAlignedBB AABB_QTR_TOP_NORTH = new AxisAlignedBB(0.0D, 0.5D, 0.0D, 1.0D, 1.0D, 0.5D); protected static final AxisAlignedBB AABB_QTR_TOP_SOUTH = new AxisAlignedBB(0.0D, 0.5D, 0.5D, 1.0D, 1.0D, 1.0D); protected static final AxisAlignedBB AABB_OCT_TOP_NW = new AxisAlignedBB(0.0D, 0.5D, 0.0D, 0.5D, 1.0D, 0.5D); protected static final AxisAlignedBB AABB_OCT_TOP_NE = new AxisAlignedBB(0.5D, 0.5D, 0.0D, 1.0D, 1.0D, 0.5D); protected static final AxisAlignedBB AABB_OCT_TOP_SW = new AxisAlignedBB(0.0D, 0.5D, 0.5D, 0.5D, 1.0D, 1.0D); protected static final AxisAlignedBB AABB_OCT_TOP_SE = new AxisAlignedBB(0.5D, 0.5D, 0.5D, 1.0D, 1.0D, 1.0D); protected static final AxisAlignedBB AABB_SLAB_BOTTOM = new AxisAlignedBB(0.0D, 0.0D, 0.0D, 1.0D, 0.5D, 1.0D); protected static final AxisAlignedBB AABB_QTR_BOT_WEST = new AxisAlignedBB(0.0D, 0.0D, 0.0D, 0.5D, 0.5D, 1.0D); protected static final AxisAlignedBB AABB_QTR_BOT_EAST = new AxisAlignedBB(0.5D, 0.0D, 0.0D, 1.0D, 0.5D, 1.0D); protected static final AxisAlignedBB AABB_QTR_BOT_NORTH = new AxisAlignedBB(0.0D, 0.0D, 0.0D, 1.0D, 0.5D, 0.5D); protected static final AxisAlignedBB AABB_QTR_BOT_SOUTH = new AxisAlignedBB(0.0D, 0.0D, 0.5D, 1.0D, 0.5D, 1.0D); protected static final AxisAlignedBB AABB_OCT_BOT_NW = new AxisAlignedBB(0.0D, 0.0D, 0.0D, 0.5D, 0.5D, 0.5D); protected static final AxisAlignedBB AABB_OCT_BOT_NE = new AxisAlignedBB(0.5D, 0.0D, 0.0D, 1.0D, 0.5D, 0.5D); protected static final AxisAlignedBB AABB_OCT_BOT_SW = new AxisAlignedBB(0.0D, 0.0D, 0.5D, 0.5D, 0.5D, 1.0D); protected static final AxisAlignedBB AABB_OCT_BOT_SE = new AxisAlignedBB(0.5D, 0.0D, 0.5D, 1.0D, 0.5D, 1.0D); public BlockStringStairs(Material material, String[] values, String propName) { super(material, values, propName); } public BlockStringStairs(Material material, String[] values) { this(material, values, "type"); } @Override public void addCollisionBoxToList(IBlockState state, World worldIn, BlockPos pos, AxisAlignedBB entityBox, List collidingBoxes, @Nullable Entity entityIn) { state = this.getActualState(state, worldIn, pos); for (AxisAlignedBB axisalignedbb : getCollisionBoxList(state)) { addCollisionBoxToList(pos, entityBox, collidingBoxes, axisalignedbb); } } private static List getCollisionBoxList(IBlockState bstate) { List list = Lists.newArrayList(); boolean flag = bstate.getValue(BlockStairs.HALF) == BlockStairs.EnumHalf.TOP; list.add(flag ? AABB_SLAB_TOP : AABB_SLAB_BOTTOM); BlockStairs.EnumShape blockstairs$enumshape = (BlockStairs.EnumShape) bstate.getValue(BlockStairs.SHAPE); if (blockstairs$enumshape == BlockStairs.EnumShape.STRAIGHT || blockstairs$enumshape == BlockStairs.EnumShape.INNER_LEFT || blockstairs$enumshape == BlockStairs.EnumShape.INNER_RIGHT) { list.add(getCollQuarterBlock(bstate)); } if (blockstairs$enumshape != BlockStairs.EnumShape.STRAIGHT) { list.add(getCollEighthBlock(bstate)); } return list; } private static AxisAlignedBB getCollQuarterBlock(IBlockState bstate) { boolean flag = bstate.getValue(BlockStairs.HALF) == BlockStairs.EnumHalf.TOP; switch ((EnumFacing) bstate.getValue(FACING)) { case NORTH: default: return flag ? AABB_QTR_BOT_NORTH : AABB_QTR_TOP_NORTH; case SOUTH: return flag ? AABB_QTR_BOT_SOUTH : AABB_QTR_TOP_SOUTH; case WEST: return flag ? AABB_QTR_BOT_WEST : AABB_QTR_TOP_WEST; case EAST: return flag ? AABB_QTR_BOT_EAST : AABB_QTR_TOP_EAST; } } private static AxisAlignedBB getCollEighthBlock(IBlockState bstate) { EnumFacing enumfacing = (EnumFacing) bstate.getValue(FACING); EnumFacing enumfacing1; switch ((BlockStairs.EnumShape) bstate.getValue(BlockStairs.SHAPE)) { case OUTER_LEFT: default: enumfacing1 = enumfacing; break; case OUTER_RIGHT: enumfacing1 = enumfacing.rotateY(); break; case INNER_RIGHT: enumfacing1 = enumfacing.getOpposite(); break; case INNER_LEFT: enumfacing1 = enumfacing.rotateYCCW(); } boolean flag = bstate.getValue(BlockStairs.HALF) == BlockStairs.EnumHalf.TOP; switch (enumfacing1) { case NORTH: default: return flag ? AABB_OCT_BOT_NW : AABB_OCT_TOP_NW; case SOUTH: return flag ? AABB_OCT_BOT_SE : AABB_OCT_TOP_SE; case WEST: return flag ? AABB_OCT_BOT_SW : AABB_OCT_TOP_SW; case EAST: return flag ? AABB_OCT_BOT_NE : AABB_OCT_TOP_NE; } } @Override public boolean isOpaqueCube(IBlockState state) { return false; } @Override public boolean isFullCube(IBlockState state) { return false; } @Override public IBlockState onBlockPlaced(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer) { IBlockState iblockstate = super.onBlockPlaced(worldIn, pos, facing, hitX, hitY, hitZ, meta, placer); iblockstate = iblockstate.withProperty(FACING, placer.getHorizontalFacing()).withProperty(BlockStairs.SHAPE, BlockStairs.EnumShape.STRAIGHT); return facing != EnumFacing.DOWN && (facing == EnumFacing.UP || (double) hitY <= 0.5D) ? iblockstate.withProperty(BlockStairs.HALF, BlockStairs.EnumHalf.BOTTOM) : iblockstate.withProperty(BlockStairs.HALF, BlockStairs.EnumHalf.TOP); } @Override public RayTraceResult collisionRayTrace(IBlockState blockState, World worldIn, BlockPos pos, Vec3d start, Vec3d end) { List list = Lists.newArrayList(); for (AxisAlignedBB axisalignedbb : getCollisionBoxList(this.getActualState(blockState, worldIn, pos))) { list.add(this.rayTrace(pos, start, end, axisalignedbb)); } RayTraceResult raytraceresult1 = null; double d1 = 0.0D; for (RayTraceResult raytraceresult : list) { if (raytraceresult != null) { double d0 = raytraceresult.hitVec.squareDistanceTo(end); if (d0 > d1) { raytraceresult1 = raytraceresult; d1 = d0; } } } return raytraceresult1; } // Meta looks like: {1|11|1} = {HALF|FACING|TYPE} @Override public IBlockState getStateFromMeta(int meta) { IBlockState iblockstate = getBlockState().getBaseState().withProperty(BlockStairs.HALF, (meta & 8) > 0 ? BlockStairs.EnumHalf.TOP : BlockStairs.EnumHalf.BOTTOM); iblockstate = iblockstate.withProperty(FACING, EnumFacing.getFront(5 - (meta & 6) / 2)).withProperty(this.getStringProp(), this.getValues().get(meta % 2)); return iblockstate; } // Meta looks like: {1|11|1} = {HALF|FACING|TYPE} @Override public int getMetaFromState(IBlockState state) { int i = 0; if (state.getValue(BlockStairs.HALF) == BlockStairs.EnumHalf.TOP) { i |= 4; } i = i | 5 - ((EnumFacing) state.getValue(FACING)).getIndex(); return i * 2 + this.getValues().indexOf(state.getValue(this.getStringProp())); } @Override public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos) { return state.withProperty(BlockStairs.SHAPE, getStairsShape(state, worldIn, pos)); } private static BlockStairs.EnumShape getStairsShape(IBlockState state, IBlockAccess world, BlockPos pos) { EnumFacing enumfacing = (EnumFacing) state.getValue(FACING); IBlockState iblockstate = world.getBlockState(pos.offset(enumfacing)); if (isBlockStairs(iblockstate) && state.getValue(BlockStairs.HALF) == iblockstate.getValue(BlockStairs.HALF)) { EnumFacing enumfacing1 = (EnumFacing) iblockstate.getValue(FACING); if (enumfacing1.getAxis() != ((EnumFacing) state.getValue(FACING)).getAxis() && isDifferentStairs(state, world, pos, enumfacing1.getOpposite())) { if (enumfacing1 == enumfacing.rotateYCCW()) { return BlockStairs.EnumShape.OUTER_LEFT; } return BlockStairs.EnumShape.OUTER_RIGHT; } } IBlockState iblockstate1 = world.getBlockState(pos.offset(enumfacing.getOpposite())); if (isBlockStairs(iblockstate1) && state.getValue(BlockStairs.HALF) == iblockstate1.getValue(BlockStairs.HALF)) { EnumFacing enumfacing2 = (EnumFacing) iblockstate1.getValue(FACING); if (enumfacing2.getAxis() != ((EnumFacing) state.getValue(FACING)).getAxis() && isDifferentStairs(state, world, pos, enumfacing2)) { if (enumfacing2 == enumfacing.rotateYCCW()) { return BlockStairs.EnumShape.INNER_LEFT; } return BlockStairs.EnumShape.INNER_RIGHT; } } return BlockStairs.EnumShape.STRAIGHT; } private static boolean isDifferentStairs(IBlockState state, IBlockAccess world, BlockPos pos, EnumFacing facing) { IBlockState iblockstate = world.getBlockState(pos.offset(facing)); return !isBlockStairs(iblockstate) || iblockstate.getValue(FACING) != state.getValue(FACING) || iblockstate.getValue(BlockStairs.HALF) != state.getValue(BlockStairs.HALF); } public static boolean isBlockStairs(IBlockState state) { return state.getBlock() instanceof BlockStairs || state.getBlock() instanceof BlockStringStairs; } @Override public IBlockState withRotation(IBlockState state, Rotation rot) { return state.withProperty(FACING, rot.rotate((EnumFacing) state.getValue(FACING))); } @SuppressWarnings("incomplete-switch") @Override public IBlockState withMirror(IBlockState state, Mirror mirrorIn) { EnumFacing enumfacing = (EnumFacing) state.getValue(FACING); BlockStairs.EnumShape blockstairs$enumshape = (BlockStairs.EnumShape) state.getValue(BlockStairs.SHAPE); switch (mirrorIn) { case LEFT_RIGHT: if (enumfacing.getAxis() == EnumFacing.Axis.Z) { switch (blockstairs$enumshape) { case OUTER_LEFT: return state.withRotation(Rotation.CLOCKWISE_180).withProperty(BlockStairs.SHAPE, BlockStairs.EnumShape.OUTER_RIGHT); case OUTER_RIGHT: return state.withRotation(Rotation.CLOCKWISE_180).withProperty(BlockStairs.SHAPE, BlockStairs.EnumShape.OUTER_LEFT); case INNER_RIGHT: return state.withRotation(Rotation.CLOCKWISE_180).withProperty(BlockStairs.SHAPE, BlockStairs.EnumShape.INNER_LEFT); case INNER_LEFT: return state.withRotation(Rotation.CLOCKWISE_180).withProperty(BlockStairs.SHAPE, BlockStairs.EnumShape.INNER_RIGHT); default: return state.withRotation(Rotation.CLOCKWISE_180); } } break; case FRONT_BACK: if (enumfacing.getAxis() == EnumFacing.Axis.X) { switch (blockstairs$enumshape) { case OUTER_LEFT: return state.withRotation(Rotation.CLOCKWISE_180).withProperty(BlockStairs.SHAPE, BlockStairs.EnumShape.OUTER_RIGHT); case OUTER_RIGHT: return state.withRotation(Rotation.CLOCKWISE_180).withProperty(BlockStairs.SHAPE, BlockStairs.EnumShape.OUTER_LEFT); case INNER_RIGHT: return state.withRotation(Rotation.CLOCKWISE_180).withProperty(BlockStairs.SHAPE, BlockStairs.EnumShape.INNER_RIGHT); case INNER_LEFT: return state.withRotation(Rotation.CLOCKWISE_180).withProperty(BlockStairs.SHAPE, BlockStairs.EnumShape.INNER_LEFT); case STRAIGHT: return state.withRotation(Rotation.CLOCKWISE_180); } } } return super.withMirror(state, mirrorIn); } @Override protected BlockStateContainer createRealBlockState() { return new ExtendedBlockState(this, new IProperty[] { BlockStairs.HALF, BlockStairs.SHAPE, FACING, this.getStringProp() }, new IUnlistedProperty[] { this.getUnlistedStringProp() }); } @Override protected ItemStack createStackedBlock(IBlockState state) { return new ItemStack(this, 1, damageDropped(state)); } @Override public int damageDropped(IBlockState state) { return this.getValues().indexOf(state.getValue(this.getStringProp())); } @Override public ItemStack getPickBlock(IBlockState state, RayTraceResult target, World world, BlockPos pos, EntityPlayer player) { return new ItemStack(this, 1, damageDropped(state)); } @Override public boolean doesSideBlockRendering(IBlockState state, IBlockAccess world, BlockPos pos, EnumFacing face) { if (net.minecraftforge.common.ForgeModContainer.disableStairSlabCulling) return super.doesSideBlockRendering(state, world, pos, face); if (state.isOpaqueCube()) return true; state = this.getActualState(state, world, pos); EnumHalf half = state.getValue(BlockStairs.HALF); EnumFacing side = state.getValue(FACING); EnumShape shape = state.getValue(BlockStairs.SHAPE); if (face == EnumFacing.UP) return half == EnumHalf.TOP; if (face == EnumFacing.DOWN) return half == EnumHalf.BOTTOM; if (shape == EnumShape.OUTER_LEFT || shape == EnumShape.OUTER_RIGHT) return false; if (face == side) return true; if (shape == EnumShape.INNER_LEFT && face.rotateY() == side) return true; if (shape == EnumShape.INNER_RIGHT && face.rotateYCCW() == side) return true; return false; } }