How can I create beds programmatically?

Hi!

I’m trying to create a bed programmatically. I had following code for inserting custom blocks (e. g. TV from Mr. Crayfish’s Furniture Mod), and it worked fine there. customBlockCommand.blockType is the ID of the block type you can see in Minecraft, when you press the F3 button.

    internal open fun createCustomBlock(
          customBlockCommand: CustomBlock,
          coords: McCoords
    ) {
        if (game == null) {
            logger.error("Game is null. Can't insert custom block.")
            return
        }
        val customBlockType:Optional<BlockType> = game.registry.getType(
              BlockType::class.java,
              customBlockCommand.blockType)
        if (!customBlockType.isPresent) {
            logger.error("Block type '${customBlockCommand.blockType}' doesn't exist")
            return
        }
        val loc = createSpongeLocation(
              x = coords.x + customBlockCommand.x,
              y = coords.y + customBlockCommand.elevation,
              z = coords.z + customBlockCommand.z
        )
        setBlockType(loc, customBlockType.get())
    }

But when I try to insert a bed with it (customBlockCommand.blockType is equal to minecraft:bed), it doesn’t work - only half of the bed is created. If do it twice, i. e. try to put two bed blocks next to each other, then one half of the bed is created, and the other is lying on the floor (can be picked up).

How can I insert a bed correctly?

Thanks in advance

Dmitri Pisarenko

Apart from having a bunch of methods whose functionality i can’t see, your biggest issue is that you need to place both parts of the bed down at the same time.

Basically, beds contain rotation data as well as data to tell it which end of the bed it is.

You need to create two blocks to place a proper bed, and be sure to assign the right data to the right blocks.

There’s not much I can do to help you other than that.

The problem is that for a bed to visually look correct you need to set whether it’s the head or foot of the bed. Unfortunately there’s no implementation of a DataManipulator for this.

I’ve made a comment in the data implementations issue here:

1 Like

I created methods createSpongeLocation and setBlockType for testing purposes. For example, I can verify in my unit test, that this method creates a Location instance with the right parameters. I wouldn’t be able to do it easily, if instead of createSpongeLocation I had new Location<World>(...).

Can I contribute this functionality? In other words: Is it missing because nobody did it yet, or because it goes against some decision?

It’s missing because no one noticed.