#How to make a custom block play a sound when fallen on?

4 messages · Page 1 of 1 (latest)

feral cedar
#

I've got a rubber block implemented in the block registry, like so:

    .displayName('Block of Rubber')
    .soundType('shroomlight')
    .bounciness(0.9)
    .fallenOn(entity => { entity.applyFallDamage(0)})
    .requiresTool(true)
    .tagBlock('create:wrench_pickup')
    .tagBlock('minecraft:mineable/pickaxe')
    .tagBlock('minecraft:mineable/axe')```

I'd like it to play a sound when it bounces entities that fall on it. I figured I could just add `entity.playSound('<THE SOUND>', 1, 1)` to the fallenOn parameter, after the fall damage nullification, eg.
```.fallenOn(entity => { 
    entity.applyFallDamage(0)
    entity.playSound('minecraft:entity.enderman.teleport', 1, 1)
})```

but this doesn't work; it just throws an error like this each time the block's fallen on:
```[19:53:18] [Server thread/ERROR] [KubeJS Startup/]: block_registry.js#27: Error while an entity fell on custom block : dev.latvian.mods.rhino.EcmaError: TypeError: Cannot find function playSound in object dev.latvian.mods.kubejs.block.callbacks.EntityFallenOnBlockCallbackJS@5f5dd18e.```

I'm not really sure how to work with playSound; it's not really documented at all on the wikis and I cribbed the code I'm (successfully!) using elsewhere with it from here previously since the wiki examples didn't work.
gusty flareBOT
#

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

slate robin
#

Here ya go.

StartupEvents.registry('block', event => {
  event.create('rubber_block')
    .displayName('Block of Rubber')
    .soundType('shroomlight')
    .bounciness(0.9)
    .fallenOn(event => {
      if (event.entity != null) {
        event.entity.fallDistance = 0
        event.entity.playSound('minecraft:entity.enderman.teleport', 1.0, 1.0)
      }
    })
    .requiresTool(true)
    .tagBlock('create:wrench_pickup')
    .tagBlock('minecraft:mineable/pickaxe')
    .tagBlock('minecraft:mineable/axe')
})
feral cedar
#

Took me a couple days to actually get back to my computer and check, but this works. Thank you so much!