#Set block while preserving blockstate

3 messages · Page 1 of 1 (latest)

gaunt nest
#

I'm trying to make it so that you can turn a block into its mossy variant by right-clicking the block with a moss block.

The function below works like a charm for full blocks.

    BlockEvents.rightClicked(bare_block, event => {
        const { block, item, player } = event
        if (item == moss_item) {
            let state = block.blockState // Ensure block state is loaded
            // Add the moss
            block.set(mossy_block)
            block.blockState = state // Restore block state
            // Consume the moss item
            if (!player.isCreative()){
                item.shrink(1);
            }
            
            player.swing() //makes the hand swing as if an item was used
        }
    });```
However, whenever I replace stairs or slabs, the new mossy block is set to the default blockstate...

How can I set a new block with the same blockstate as the previous one?
ruby pikeBOT
#

Once your ticket has been resolved, please close it with </ticket close:1054771505520717835> command!

gaunt nest
#

Turns out I only needed to set the new block with the properties of the old one. This script works :

    BlockEvents.rightClicked(bare_block, event => {
        const { block, item, player } = event
        const properties = block.properties; // Store block properties

        if (item == moss_item) {
            // Add the moss
            block.set(mossy_block, properties)
          
            // Consume the moss item
            if (!player.isCreative()){
                item.shrink(1);
            }
            
            player.swing() //makes the hand swing as if an item was used
        }
    });```