Tutorial Set block direction easy

Discussion in 'Resources' started by The_Spaceman, Aug 15, 2018.

  1. Offline

    The_Spaceman

    This is a way to easily set a block with directions. because logs and pistons (ect.) are diffrent I made this:
    Code:
    public static void setBlock(Block block, Material material, BlockFace blockFace) {
        block.setType(material);
        BlockData blockData = block.getBlockData();
        if (blockData instanceof Directional) {
            ((Directional) blockData).setFacing(blockFace);
            block.setBlockData(blockData);
        }
        if (blockData instanceof Orientable) {
            ((Orientable) blockData).setAxis(convertBlockFaceToAxis(blockFace));
            block.setBlockData(blockData);
        }
        if (blockData instanceof Rotatable) {
            ((Rotatable) blockData).setRotation(blockFace);
            block.setBlockData(blockData);
        }
    }
    
     
    Zombie_Striker likes this.
  2. Offline

    The_Spaceman

    UPDATE:
    Here you have the convertBlockFaceToAxis(blockFace)

    Code:
    private static Axis convertBlockFaceToAxis(BlockFace face) {
        switch (face) {
            case NORTH:
            case SOUTH:
                return Axis.Z;
            case EAST:
            case WEST:
                return Axis.X;
            case UP:
            case DOWN:
                return Axis.Y;
                default:
                    return Axis.X;
        }
    }
    
    BlockFace.NORTH_WEST will throw an error because this isn't a valid direction, so please take that in mind...
     
  3. Offline

    Tahmo

    Where do you get the blockFace variable from?
    I tried this

    BlockFace blockFace = block.getFace();

    but it wont work.
     
  4. Online

    timtower Moderator Moderator

    @Tahmo The blockface is the direction you need to point the block to.
    Depends on your plugin where you get it from.
     
  5. Offline

    The_Spaceman

    if you want to get the BlockFace of a block:
    BlockFace blockFace = ((Directional) block.getBlockData()).getFacing();
    where block is your Block

    if you want to set a BlockFace:
    setBlock(player.getLocation().getBlock(), Material.PISTON, BlockFace.WEST);
     

Share This Page