How to set facing direction of blocks?

Hello!

How can I set the direction of a block at a given location?

In another thread I’ve read that the direction can be set via the DirectionalData interface.

If I have

  1. instance of org.spongepowered.api.world.Location<World> and
  2. org.spongepowered.api.util.Direction,

how can I set the direction of 1 to 2?

Using the code below or differently?

private fun setDirection(
        loc: org.spongepowered.api.world.Location<World>,
        direction: Direction) {
    loc.offer(Keys.DIRECTION, direction)
}

Here’s the Bukkit analog of what I’m trying to achieve:

protected void setFacingDirection(final BlockFace face, final Block block) {
    final BlockState state = block.getState();
    final MaterialData materialData = state.getData();
    if (materialData instanceof Directional) {
        final Directional directional = (Directional) materialData;
        directional.setFacingDirection(face);
        state.update();
    }
}

Thanks

Dmitri Pisarenko

Which BlockType are you trying to set the data on? (e.g. stairs)
I know the Keys.DIRECTION works for stairs because this works for my project

BlockTypes.STONE_BRICK_STAIRS.getDefaultState().with(Keys.DIRECTION, Direction.NORTH).get()

Have you checked the block at the location supports() the key?

1 Like