Search in sources :

Example 1 with SlabType

use of net.minecraft.state.properties.SlabType in project Materialis by RCXcrafter.

the class WrenchingModifier method blockUse.

public ActionResultType blockUse(IModifierToolStack tool, int level, World world, BlockPos pos, BlockState state, ItemUseContext context) {
    if (context.getPlayer().isSecondaryUseActive() || !state.getBlock().canEntityDestroy(state, world, pos, context.getPlayer()) || state.getHarvestLevel() > tool.getStats().getInt(ToolStats.HARVEST_LEVEL) || !isRotatable(state, world, pos))
        return ActionResultType.PASS;
    Direction face = context.getClickedFace();
    Rotation rotation = context.getClickedFace().getAxisDirection() == Direction.AxisDirection.POSITIVE ? Rotation.CLOCKWISE_90 : Rotation.COUNTERCLOCKWISE_90;
    for (Property<?> prop : state.getProperties()) {
        if (prop instanceof DirectionProperty) {
            Direction direction = state.getValue((DirectionProperty) prop);
            Direction newDirection = rotateDirection(direction, face.getAxis(), rotation);
            if (newDirection != direction) {
                // make sure something changed
                BlockState newState = null;
                // check if the block can even be rotated like this
                int attempts = 0;
                boolean success = false;
                while (attempts < 3) {
                    if (prop.getPossibleValues().contains(newDirection)) {
                        newState = state.setValue((DirectionProperty) prop, newDirection);
                        if (newState.canSurvive(world, pos)) {
                            success = true;
                            break;
                        }
                    }
                    // try the next rotation
                    newDirection = rotateDirection(newDirection, face.getAxis(), rotation);
                    attempts++;
                }
                if (success) {
                    world.setBlock(pos, newState, Constants.BlockFlags.DEFAULT_AND_RERENDER);
                    ToolDamageUtil.damage(tool, 1, context.getPlayer(), context.getItemInHand());
                    return ActionResultType.SUCCESS;
                }
            }
        }
        if (prop == BlockStateProperties.AXIS || prop == BlockStateProperties.HORIZONTAL_AXIS) {
            Direction.Axis axis = prop == BlockStateProperties.AXIS ? state.getValue(BlockStateProperties.AXIS) : state.getValue(BlockStateProperties.HORIZONTAL_AXIS);
            Direction.Axis newAxis = rotateAxis(axis, face.getAxis(), rotation);
            if (newAxis != axis) {
                // check if the block can even be rotated like this
                if (prop.getPossibleValues().contains(newAxis)) {
                    BlockState newState;
                    if (prop == BlockStateProperties.AXIS)
                        newState = state.setValue(BlockStateProperties.AXIS, newAxis);
                    else
                        newState = state.setValue(BlockStateProperties.HORIZONTAL_AXIS, newAxis);
                    if (newState.canSurvive(world, pos)) {
                        world.setBlock(pos, newState, Constants.BlockFlags.DEFAULT_AND_RERENDER);
                        ToolDamageUtil.damage(tool, 1, context.getPlayer(), context.getItemInHand());
                        return ActionResultType.SUCCESS;
                    }
                }
            }
        }
        if (prop == BlockStateProperties.RAIL_SHAPE || prop == BlockStateProperties.RAIL_SHAPE_STRAIGHT) {
            RailShape shape = prop == BlockStateProperties.RAIL_SHAPE ? state.getValue(BlockStateProperties.RAIL_SHAPE) : state.getValue(BlockStateProperties.RAIL_SHAPE_STRAIGHT);
            RailShape newShape = rotateRail(shape, face.getAxis(), rotation);
            if (newShape != shape) {
                // check if the block can even be rotated like this
                if (shouldRailBeRemoved(pos, world, newShape))
                    newShape = rotateRail(shape, face.getAxis(), rotation.getRotated(Rotation.CLOCKWISE_180));
                if (!shouldRailBeRemoved(pos, world, newShape) && prop.getPossibleValues().contains(newShape)) {
                    BlockState newState;
                    if (prop == BlockStateProperties.RAIL_SHAPE)
                        newState = state.setValue(BlockStateProperties.RAIL_SHAPE, newShape);
                    else
                        newState = state.setValue(BlockStateProperties.RAIL_SHAPE_STRAIGHT, newShape);
                    world.setBlock(pos, newState, Constants.BlockFlags.DEFAULT_AND_RERENDER);
                    ToolDamageUtil.damage(tool, 1, context.getPlayer(), context.getItemInHand());
                    return ActionResultType.SUCCESS;
                }
            }
        }
        if (prop == BlockStateProperties.ROTATION_16) {
            int rotation16 = state.getValue(BlockStateProperties.ROTATION_16);
            rotation16 = (rotation16 + 1) % 16;
            world.setBlock(pos, state.setValue(BlockStateProperties.ROTATION_16, rotation16), Constants.BlockFlags.DEFAULT_AND_RERENDER);
            ToolDamageUtil.damage(tool, 1, context.getPlayer(), context.getItemInHand());
            return ActionResultType.SUCCESS;
        }
        if (prop == BlockStateProperties.HALF) {
            Half half = state.getValue(BlockStateProperties.HALF);
            if (half == Half.TOP)
                half = Half.BOTTOM;
            else
                half = Half.TOP;
            BlockState newState = state.setValue(BlockStateProperties.HALF, half);
            if (newState.canSurvive(world, pos)) {
                world.setBlock(pos, state.setValue(BlockStateProperties.HALF, half), Constants.BlockFlags.DEFAULT_AND_RERENDER);
                ToolDamageUtil.damage(tool, 1, context.getPlayer(), context.getItemInHand());
                return ActionResultType.SUCCESS;
            }
        }
        if (prop == BlockStateProperties.SLAB_TYPE) {
            SlabType half = state.getValue(BlockStateProperties.SLAB_TYPE);
            if (half != SlabType.DOUBLE) {
                if (half == SlabType.TOP)
                    half = SlabType.BOTTOM;
                else
                    half = SlabType.TOP;
                BlockState newState = state.setValue(BlockStateProperties.SLAB_TYPE, half);
                if (newState.canSurvive(world, pos)) {
                    world.setBlock(pos, newState, Constants.BlockFlags.DEFAULT_AND_RERENDER);
                    ToolDamageUtil.damage(tool, 1, context.getPlayer(), context.getItemInHand());
                    return ActionResultType.SUCCESS;
                }
            }
        }
    }
    return ActionResultType.PASS;
}
Also used : Half(net.minecraft.state.properties.Half) SlabType(net.minecraft.state.properties.SlabType) BlockState(net.minecraft.block.BlockState) RailShape(net.minecraft.state.properties.RailShape) DirectionProperty(net.minecraft.state.DirectionProperty) Direction(net.minecraft.util.Direction) Rotation(net.minecraft.util.Rotation)

Aggregations

BlockState (net.minecraft.block.BlockState)1 DirectionProperty (net.minecraft.state.DirectionProperty)1 Half (net.minecraft.state.properties.Half)1 RailShape (net.minecraft.state.properties.RailShape)1 SlabType (net.minecraft.state.properties.SlabType)1 Direction (net.minecraft.util.Direction)1 Rotation (net.minecraft.util.Rotation)1