Search in sources :

Example 1 with HorizontalDirectionalBlock

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

the class BlockRotator method dispenserRotate.

public static ItemStack dispenserRotate(BlockSource source, ItemStack stack) {
    Direction sourceFace = source.getBlockState().getValue(DispenserBlock.FACING);
    Level world = source.getLevel();
    // offset
    BlockPos blockpos = source.getPos().relative(sourceFace);
    BlockState iblockstate = world.getBlockState(blockpos);
    Block block = iblockstate.getBlock();
    // Block rotation for blocks that can be placed in all 6 or 4 rotations.
    if (block instanceof DirectionalBlock || block instanceof DispenserBlock) {
        Direction face = iblockstate.getValue(DirectionalBlock.FACING);
        if (block instanceof PistonBaseBlock && (iblockstate.getValue(PistonBaseBlock.EXTENDED) || (((PistonBlockInterface) block).publicShouldExtend(world, blockpos, face) && (new PistonStructureResolver(world, blockpos, face, true)).resolve())))
            return stack;
        Direction rotated_face = rotateClockwise(face, sourceFace.getAxis());
        if (sourceFace.get3DDataValue() % 2 == 0 || rotated_face == face) {
            // Flip to make blocks always rotate clockwise relative to the dispenser
            // when index is equal to zero. when index is equal to zero the dispenser is in the opposite direction.
            rotated_face = rotated_face.getOpposite();
        }
        world.setBlock(blockpos, iblockstate.setValue(DirectionalBlock.FACING, rotated_face), 3);
    } else if (// Block rotation for blocks that can be placed in only 4 horizontal rotations.
    block instanceof HorizontalDirectionalBlock) {
        if (block instanceof BedBlock)
            return stack;
        Direction face = iblockstate.getValue(HorizontalDirectionalBlock.FACING);
        face = rotateClockwise(face, Direction.Axis.Y);
        if (sourceFace == Direction.DOWN) {
            // same as above.
            face = face.getOpposite();
        }
        world.setBlock(blockpos, iblockstate.setValue(HorizontalDirectionalBlock.FACING, face), 3);
    } else if (block == Blocks.HOPPER) {
        Direction face = iblockstate.getValue(HopperBlock.FACING);
        if (face != Direction.DOWN) {
            face = rotateClockwise(face, Direction.Axis.Y);
            world.setBlock(blockpos, iblockstate.setValue(HopperBlock.FACING, face), 3);
        }
    }
    // Send block update to the block that just have been rotated.
    world.neighborChanged(blockpos, block, source.getPos());
    return stack;
}
Also used : BedBlock(net.minecraft.world.level.block.BedBlock) HorizontalDirectionalBlock(net.minecraft.world.level.block.HorizontalDirectionalBlock) BlockState(net.minecraft.world.level.block.state.BlockState) HorizontalDirectionalBlock(net.minecraft.world.level.block.HorizontalDirectionalBlock) DirectionalBlock(net.minecraft.world.level.block.DirectionalBlock) DispenserBlock(net.minecraft.world.level.block.DispenserBlock) PistonStructureResolver(net.minecraft.world.level.block.piston.PistonStructureResolver) 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) Level(net.minecraft.world.level.Level) BlockPos(net.minecraft.core.BlockPos) Direction(net.minecraft.core.Direction) PistonBaseBlock(net.minecraft.world.level.block.piston.PistonBaseBlock)

Aggregations

BlockPos (net.minecraft.core.BlockPos)1 Direction (net.minecraft.core.Direction)1 Level (net.minecraft.world.level.Level)1 BaseRailBlock (net.minecraft.world.level.block.BaseRailBlock)1 BedBlock (net.minecraft.world.level.block.BedBlock)1 Block (net.minecraft.world.level.block.Block)1 ComparatorBlock (net.minecraft.world.level.block.ComparatorBlock)1 DiodeBlock (net.minecraft.world.level.block.DiodeBlock)1 DirectionalBlock (net.minecraft.world.level.block.DirectionalBlock)1 DispenserBlock (net.minecraft.world.level.block.DispenserBlock)1 EndRodBlock (net.minecraft.world.level.block.EndRodBlock)1 FenceGateBlock (net.minecraft.world.level.block.FenceGateBlock)1 GlazedTerracottaBlock (net.minecraft.world.level.block.GlazedTerracottaBlock)1 HopperBlock (net.minecraft.world.level.block.HopperBlock)1 HorizontalDirectionalBlock (net.minecraft.world.level.block.HorizontalDirectionalBlock)1 LeverBlock (net.minecraft.world.level.block.LeverBlock)1 ObserverBlock (net.minecraft.world.level.block.ObserverBlock)1 RepeaterBlock (net.minecraft.world.level.block.RepeaterBlock)1 RotatedPillarBlock (net.minecraft.world.level.block.RotatedPillarBlock)1 SlabBlock (net.minecraft.world.level.block.SlabBlock)1