BlockState arguments

A block state argument with suggestions for Minecraft items

The BlockStateArgument is used to represent data about blocks in the world. These refer to any blocks that have data or states, such as dispensers, signs, doors and pistons. The BlockStateArgument creates a Bukkit BlockData object when used.

Developer's Note:

Make sure to not confuse the cast type with BlockState. The naming of this argument refers to the internal Minecraft vanilla argument naming convention - this argument casts to BlockData and NOT BlockState.

Example - Setting a block

Say we want a simple command to set the block that you're looking at. We'll use the following command syntax:

/set <block>

And then we can simply set our block using setBlockData():

new CommandAPICommand("set")
    .withArguments(new BlockStateArgument("block"))
    .executesPlayer((player, args) -> {
        BlockData blockdata = (BlockData) args.get("block");
        Block targetBlock = player.getTargetBlockExact(256);
        
        // Set the block, along with its data
        targetBlock.setType(blockdata.getMaterial());
        targetBlock.getState().setBlockData(blockdata);
    })
    .register();
CommandAPICommand("set")
    .withArguments(BlockStateArgument("block"))
    .executesPlayer(PlayerCommandExecutor { player, args ->
        val blockdata = args["block"] as BlockData
        val targetBlock = player.getTargetBlockExact(256)

        // Set the block, along with its data
        targetBlock?.type = blockdata.material
        targetBlock?.state?.blockData = blockdata
    })
    .register()
commandAPICommand("set") {
    blockStateArgument("block")
    playerExecutor { player, args ->
        val blockdata = args["block"] as BlockData
        val targetBlock = player.getTargetBlockExact(256)

        // Set the block, along with its data
        targetBlock?.type = blockdata.material
        targetBlock?.state?.blockData = blockdata
    }
}