Search in sources :

Example 11 with BaseRailBlock

use of net.minecraft.world.level.block.BaseRailBlock in project fabric-carpet by gnembon.

the class BlockRotator method flip_block.

public static boolean flip_block(BlockState state, Level world, Player player, InteractionHand hand, BlockHitResult hit) {
    Block block = state.getBlock();
    BlockPos pos = hit.getBlockPos();
    Vec3 hitVec = hit.getLocation().subtract(pos.getX(), pos.getY(), pos.getZ());
    Direction facing = hit.getDirection();
    BlockState newState = null;
    if ((block instanceof GlazedTerracottaBlock) || (block instanceof DiodeBlock) || (block instanceof BaseRailBlock) || (block instanceof TrapDoorBlock) || (block instanceof LeverBlock) || (block instanceof FenceGateBlock)) {
        newState = state.rotate(Rotation.CLOCKWISE_90);
    } else if ((block instanceof ObserverBlock) || (block instanceof EndRodBlock)) {
        newState = state.setValue(DirectionalBlock.FACING, state.getValue(DirectionalBlock.FACING).getOpposite());
    } else if (block instanceof DispenserBlock) {
        newState = state.setValue(DispenserBlock.FACING, state.getValue(DispenserBlock.FACING).getOpposite());
    } else if (block instanceof PistonBaseBlock) {
        if (!(state.getValue(PistonBaseBlock.EXTENDED)))
            newState = state.setValue(DirectionalBlock.FACING, state.getValue(DirectionalBlock.FACING).getOpposite());
    } else if (block instanceof SlabBlock) {
        if (((SlabBlock) block).useShapeForLightOcclusion(state)) {
            newState = state.setValue(SlabBlock.TYPE, state.getValue(SlabBlock.TYPE) == SlabType.TOP ? SlabType.BOTTOM : SlabType.TOP);
        }
    } else if (block instanceof HopperBlock) {
        if (state.getValue(HopperBlock.FACING) != Direction.DOWN) {
            newState = state.setValue(HopperBlock.FACING, state.getValue(HopperBlock.FACING).getClockWise());
        }
    } else if (block instanceof StairBlock) {
        // LOG.error(String.format("hit with facing: %s, at side %.1fX, X %.1fY, Y %.1fZ",facing, hitX, hitY, hitZ));
        if ((facing == Direction.UP && hitVec.y == 1.0f) || (facing == Direction.DOWN && hitVec.y == 0.0f)) {
            newState = state.setValue(StairBlock.HALF, state.getValue(StairBlock.HALF) == Half.TOP ? Half.BOTTOM : Half.TOP);
        } else {
            boolean turn_right;
            if (facing == Direction.NORTH) {
                turn_right = (hitVec.x <= 0.5);
            } else if (facing == Direction.SOUTH) {
                turn_right = !(hitVec.x <= 0.5);
            } else if (facing == Direction.EAST) {
                turn_right = (hitVec.z <= 0.5);
            } else if (facing == Direction.WEST) {
                turn_right = !(hitVec.z <= 0.5);
            } else {
                return false;
            }
            if (turn_right) {
                newState = state.rotate(Rotation.COUNTERCLOCKWISE_90);
            } else {
                newState = state.rotate(Rotation.CLOCKWISE_90);
            }
        }
    } else if (block instanceof RotatedPillarBlock) {
        switch((Direction.Axis) state.getValue(RotatedPillarBlock.AXIS)) {
            case X:
                newState = (BlockState) state.setValue(RotatedPillarBlock.AXIS, Direction.Axis.Z);
                break;
            case Z:
                newState = (BlockState) state.setValue(RotatedPillarBlock.AXIS, Direction.Axis.Y);
                break;
            case Y:
                newState = (BlockState) state.setValue(RotatedPillarBlock.AXIS, Direction.Axis.X);
                break;
        }
    } else {
        return false;
    }
    if (newState != null) {
        world.setBlock(pos, newState, 2 | 1024);
        world.setBlocksDirty(pos, state, newState);
        return true;
    }
    return false;
}
Also used : GlazedTerracottaBlock(net.minecraft.world.level.block.GlazedTerracottaBlock) DispenserBlock(net.minecraft.world.level.block.DispenserBlock) SlabBlock(net.minecraft.world.level.block.SlabBlock) HopperBlock(net.minecraft.world.level.block.HopperBlock) LeverBlock(net.minecraft.world.level.block.LeverBlock) ObserverBlock(net.minecraft.world.level.block.ObserverBlock) StairBlock(net.minecraft.world.level.block.StairBlock) Direction(net.minecraft.core.Direction) PistonBaseBlock(net.minecraft.world.level.block.piston.PistonBaseBlock) DiodeBlock(net.minecraft.world.level.block.DiodeBlock) TrapDoorBlock(net.minecraft.world.level.block.TrapDoorBlock) BlockState(net.minecraft.world.level.block.state.BlockState) BaseRailBlock(net.minecraft.world.level.block.BaseRailBlock) FenceGateBlock(net.minecraft.world.level.block.FenceGateBlock) Vec3(net.minecraft.world.phys.Vec3) BaseRailBlock(net.minecraft.world.level.block.BaseRailBlock) FenceGateBlock(net.minecraft.world.level.block.FenceGateBlock) TrapDoorBlock(net.minecraft.world.level.block.TrapDoorBlock) LeverBlock(net.minecraft.world.level.block.LeverBlock) ObserverBlock(net.minecraft.world.level.block.ObserverBlock) SlabBlock(net.minecraft.world.level.block.SlabBlock) BedBlock(net.minecraft.world.level.block.BedBlock) EndRodBlock(net.minecraft.world.level.block.EndRodBlock) RepeaterBlock(net.minecraft.world.level.block.RepeaterBlock) GlazedTerracottaBlock(net.minecraft.world.level.block.GlazedTerracottaBlock) DiodeBlock(net.minecraft.world.level.block.DiodeBlock) DispenserBlock(net.minecraft.world.level.block.DispenserBlock) ComparatorBlock(net.minecraft.world.level.block.ComparatorBlock) HopperBlock(net.minecraft.world.level.block.HopperBlock) PistonBaseBlock(net.minecraft.world.level.block.piston.PistonBaseBlock) Block(net.minecraft.world.level.block.Block) RotatedPillarBlock(net.minecraft.world.level.block.RotatedPillarBlock) HorizontalDirectionalBlock(net.minecraft.world.level.block.HorizontalDirectionalBlock) StairBlock(net.minecraft.world.level.block.StairBlock) DirectionalBlock(net.minecraft.world.level.block.DirectionalBlock) BlockPos(net.minecraft.core.BlockPos) EndRodBlock(net.minecraft.world.level.block.EndRodBlock) RotatedPillarBlock(net.minecraft.world.level.block.RotatedPillarBlock)

Aggregations

BaseRailBlock (net.minecraft.world.level.block.BaseRailBlock)11 BlockState (net.minecraft.world.level.block.state.BlockState)9 BlockPos (net.minecraft.core.BlockPos)8 RailShape (net.minecraft.world.level.block.state.properties.RailShape)8 AbstractMinecart (net.minecraft.world.entity.vehicle.AbstractMinecart)6 Vec3 (net.minecraft.world.phys.Vec3)5 Direction (net.minecraft.core.Direction)4 Level (net.minecraft.world.level.Level)4 Nonnull (javax.annotation.Nonnull)2 Player (net.minecraft.world.entity.player.Player)2 ItemStack (net.minecraft.world.item.ItemStack)2 Block (net.minecraft.world.level.block.Block)2 EntityPoolMinecart (vazkii.botania.common.entity.EntityPoolMinecart)2 KineticTileEntity (com.simibubi.create.content.contraptions.base.KineticTileEntity)1 StabilizedContraption (com.simibubi.create.content.contraptions.components.structureMovement.bearing.StabilizedContraption)1 CapabilityMinecartController (com.simibubi.create.content.contraptions.components.structureMovement.train.capability.CapabilityMinecartController)1 MinecartController (com.simibubi.create.content.contraptions.components.structureMovement.train.capability.MinecartController)1 ControllerRailBlock (com.simibubi.create.content.contraptions.components.tracks.ControllerRailBlock)1 VecHelper (com.simibubi.create.foundation.utility.VecHelper)1 AxisDirection (net.minecraft.core.Direction.AxisDirection)1