net.minecraft.block.TrapdoorBlock Java Examples

The following examples show how to use net.minecraft.block.TrapdoorBlock. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BlockPlacer.java    From carpet-extra with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static BlockState alternativeBlockPlacement(Block block, ItemPlacementContext context)//World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
{
    //actual alternative block placement code

    Direction facing;
    Vec3d vec3d = context.getHitPos();
    BlockPos pos = context.getBlockPos();
    float hitX = (float) vec3d.x - pos.getX();
    if (hitX<2) // vanilla
        return null;
    int code = (int)(hitX-2)/2;
    //
    // now it would be great if hitX was adjusted in context to original range from 0.0 to 1.0
    // since its actually using it. Its private - maybe with Reflections?
    //
    PlayerEntity placer = context.getPlayer();
    World world = context.getWorld();

    if (block instanceof GlazedTerracottaBlock)
    {
        facing = Direction.byId(code);
        if(facing == Direction.UP || facing == Direction.DOWN)
        {
            facing = placer.getHorizontalFacing().getOpposite();
        }
        return block.getDefaultState().with(HorizontalFacingBlock.FACING, facing);
    }
    else if (block instanceof ObserverBlock)
    {
        return block.getDefaultState()
                .with(FacingBlock.FACING, Direction.byId(code))
                .with(ObserverBlock.POWERED, true);
    }
    else if (block instanceof RepeaterBlock)
    {
        facing = Direction.byId(code % 16);
        if(facing == Direction.UP || facing == Direction.DOWN)
        {
            facing = placer.getHorizontalFacing().getOpposite();
        }
        return block.getDefaultState()
                .with(HorizontalFacingBlock.FACING, facing)
                .with(RepeaterBlock.DELAY, MathHelper.clamp(code / 16, 1, 4))
                .with(RepeaterBlock.LOCKED, Boolean.FALSE);
    }
    else if (block instanceof TrapdoorBlock)
    {
        return block.getDefaultState()
                .with(TrapdoorBlock.FACING, Direction.byId(code % 16))
                .with(TrapdoorBlock.OPEN, Boolean.FALSE)
                .with(TrapdoorBlock.HALF, (code >= 16) ? BlockHalf.TOP : BlockHalf.BOTTOM)
                .with(TrapdoorBlock.OPEN, world.isReceivingRedstonePower(pos));
    }
    else if (block instanceof ComparatorBlock)
    {
        facing = Direction.byId(code % 16);
        if((facing == Direction.UP) || (facing == Direction.DOWN))
        {
            facing = placer.getHorizontalFacing().getOpposite();
        }
        ComparatorMode m = (hitX >= 16)?ComparatorMode.SUBTRACT: ComparatorMode.COMPARE;
        return block.getDefaultState()
                .with(HorizontalFacingBlock.FACING, facing)
                .with(ComparatorBlock.POWERED, Boolean.FALSE)
                .with(ComparatorBlock.MODE, m);
    }
    else if (block instanceof DispenserBlock)
    {
        return block.getDefaultState()
                .with(DispenserBlock.FACING, Direction.byId(code))
                .with(DispenserBlock.TRIGGERED, Boolean.FALSE);
    }
    else if (block instanceof PistonBlock)
    {
        return block.getDefaultState()
                .with(FacingBlock.FACING, Direction.byId(code))
                .with(PistonBlock.EXTENDED, Boolean.FALSE);
    }
    else if (block instanceof StairsBlock)
    {
        return block.getPlacementState(context)//worldIn, pos, facing, hitX, hitY, hitZ, meta, placer)
                .with(StairsBlock.FACING, Direction.byId(code % 16))
                .with(StairsBlock.HALF, ( hitX >= 16)?BlockHalf.TOP : BlockHalf.BOTTOM);
    }
    return null;
}
 
Example #2
Source File: BlockRotator.java    From fabric-carpet with MIT License 4 votes vote down vote up
public static BlockState alternativeBlockPlacement(Block block,  ItemPlacementContext context)//World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer)
{
    //actual alternative block placement code
    //
    if (true) throw new UnsupportedOperationException("Alternative Block Placement / client controlled / is not implemnted");

    Direction facing;
    Vec3d vec3d = context.getHitPos();
    float hitX = (float) vec3d.x;

    if (hitX<2) // vanilla
        return null;
    int code = (int)(hitX-2)/2;
    //
    // now it would be great if hitX was adjusted in context to original range from 0.0 to 1.0
    // since its actually using it. Its private - maybe with Reflections?
    //
    PlayerEntity placer = context.getPlayer();
    BlockPos pos = context.getBlockPos();
    World world = context.getWorld();

    if (block instanceof GlazedTerracottaBlock)
    {
        facing = Direction.byId(code);
        if(facing == Direction.UP || facing == Direction.DOWN)
        {
            facing = placer.getHorizontalFacing().getOpposite();
        }
        return block.getDefaultState().with(HorizontalFacingBlock.FACING, facing);
    }
    else if (block instanceof ObserverBlock)
    {
        return block.getDefaultState()
                .with(FacingBlock.FACING, Direction.byId(code))
                .with(ObserverBlock.POWERED, true);
    }
    else if (block instanceof RepeaterBlock)
    {
        facing = Direction.byId(code % 16);
        if(facing == Direction.UP || facing == Direction.DOWN)
        {
            facing = placer.getHorizontalFacing().getOpposite();
        }
        return block.getDefaultState()
                .with(HorizontalFacingBlock.FACING, facing)
                .with(RepeaterBlock.DELAY, MathHelper.clamp(code / 16, 1, 4))
                .with(RepeaterBlock.LOCKED, Boolean.FALSE);
    }
    else if (block instanceof TrapdoorBlock)
    {
        return block.getDefaultState()
                .with(TrapdoorBlock.FACING, Direction.byId(code % 16))
                .with(TrapdoorBlock.OPEN, Boolean.FALSE)
                .with(TrapdoorBlock.HALF, (code >= 16) ? BlockHalf.TOP : BlockHalf.BOTTOM)
                .with(TrapdoorBlock.OPEN, world.isReceivingRedstonePower(pos));
    }
    else if (block instanceof ComparatorBlock)
    {
        facing = Direction.byId(code % 16);
        if((facing == Direction.UP) || (facing == Direction.DOWN))
        {
            facing = placer.getHorizontalFacing().getOpposite();
        }
        ComparatorMode m = (hitX >= 16)?ComparatorMode.SUBTRACT: ComparatorMode.COMPARE;
        return block.getDefaultState()
                .with(HorizontalFacingBlock.FACING, facing)
                .with(ComparatorBlock.POWERED, Boolean.FALSE)
                .with(ComparatorBlock.MODE, m);
    }
    else if (block instanceof DispenserBlock)
    {
        return block.getDefaultState()
                .with(DispenserBlock.FACING, Direction.byId(code))
                .with(DispenserBlock.TRIGGERED, Boolean.FALSE);
    }
    else if (block instanceof PistonBlock)
    {
        return block.getDefaultState()
                .with(FacingBlock.FACING, Direction.byId(code))
                .with(PistonBlock.EXTENDED, Boolean.FALSE);
    }
    else if (block instanceof StairsBlock)
    {
        return block.getPlacementState(context)//worldIn, pos, facing, hitX, hitY, hitZ, meta, placer)
                .with(StairsBlock.FACING, Direction.byId(code % 16))
                .with(StairsBlock.HALF, ( hitX >= 16)?BlockHalf.TOP : BlockHalf.BOTTOM);
    }
    return null;
}
 
Example #3
Source File: BlockRotator.java    From fabric-carpet with MIT License 4 votes vote down vote up
public static boolean flip_block(BlockState state, World world, PlayerEntity player, Hand hand, BlockHitResult hit)
{
    Block block = state.getBlock();
    BlockPos pos = hit.getBlockPos();
    Vec3d hitVec = hit.getPos().subtract(pos.getX(), pos.getY(), pos.getZ());
    Direction facing = hit.getSide();
    BlockState newState = null;
    if ( (block instanceof GlazedTerracottaBlock) || (block instanceof AbstractRedstoneGateBlock) || (block instanceof RailBlock) ||
         (block instanceof TrapdoorBlock)         || (block instanceof LeverBlock)         || (block instanceof FenceGateBlock))
    {
        newState = state.rotate(BlockRotation.CLOCKWISE_90);
    }
    else if ((block instanceof ObserverBlock) || (block instanceof EndRodBlock))
    {
        newState = state.with(FacingBlock.FACING, (Direction) state.get(FacingBlock.FACING).getOpposite());
    }
    else if (block instanceof DispenserBlock)
    {
        newState = state.with(DispenserBlock.FACING, state.get(DispenserBlock.FACING).getOpposite());
    }
    else if (block instanceof PistonBlock)
    {
        if (!(state.get(PistonBlock.EXTENDED)))
            newState = state.with(FacingBlock.FACING, state.get(FacingBlock.FACING).getOpposite());
    }
    else if (block instanceof SlabBlock)
    {
        if (((SlabBlock) block).hasSidedTransparency(state))
        {
            newState =  state.with(SlabBlock.TYPE, state.get(SlabBlock.TYPE) == SlabType.TOP ? SlabType.BOTTOM : SlabType.TOP);
        }
    }
    else if (block instanceof HopperBlock)
    {
        if ((Direction)state.get(HopperBlock.FACING) != Direction.DOWN)
        {
            newState =  state.with(HopperBlock.FACING, state.get(HopperBlock.FACING).rotateYClockwise());
        }
    }
    else if (block instanceof StairsBlock)
    {
        //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.with(StairsBlock.HALF, state.get(StairsBlock.HALF) == BlockHalf.TOP ? BlockHalf.BOTTOM : BlockHalf.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(BlockRotation.COUNTERCLOCKWISE_90);
            }
            else
            {
                newState = state.rotate(BlockRotation.CLOCKWISE_90);
            }
        }
    }
    else
    {
        return false;
    }
    if (newState != null)
    {
        world.setBlockState(pos, newState, 2 | 1024);
        world.checkBlockRerender(pos, state, newState);
        return true;
    }
    return false;
}