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;
}
Aggregations