#EntityEvents help

6 messages · Page 1 of 1 (latest)

tepid path
#

I'm trying to spawn a wandering trader when the player throws an apple into the water, but I'm not sure how to do it correctly. I found/created this mess 😅, and it doesn't seem to work.


    const entity = event.entity;

    
    if (entity.type == 'minecraft:item' && entity.item.id == 'minecraft:stone') {
        const world = entity.level;
        const x = entity.x;
        const y = entity.y;
        const z = entity.z;
        
        
        if (world.getBlock(x, y, z).id == 'minecraft:water') {
           
            const wanderingTrader = world.createEntity('minecraft:wandering_trader');
            if (wanderingTrader) {
                wanderingTrader.setPosition(x, y + 1, z);
                world.spawnEntity(wanderingTrader);
                console.log(`Wandering Trader spawned at (${x}, ${y + 1}, ${z}) in water`);
            }
        }
    }

})```
worn ventureBOT
#

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

lapis plover
#

when this event happen the entity doesn't exist yet, so you can't get their pos. so we need to use scheduleEvent to get the entity later.

EntityEvents.spawned('minecraft:item', event => {
    const { entity, level, server } = event
    if (entity.item.id != 'minecraft:stone')return 
    const uid = entity.uuid

    server.scheduleInTicks(40, () => {
        const item = level.getEntity(UUID.fromString(uid))
            if(item === null)return
        const x = item.x
        const y = item.y
        const z = item.z

        if(level.getBlock(x, y, z).id == 'minecraft:water'){
            const wanderingTrader = level.createEntity('minecraft:wandering_trader')
            wanderingTrader.setPosition(x, y + 1, z)
            wanderingTrader.spawn()
            item.item.shrink(1)
            console.log(`Wandering Trader spawned at (${x.toFixed(0)}, ${(y + 1).toFixed(0)}, ${z.toFixed(0)}) in water`);
        }
    })
})```
tepid path
#

Ohhh, I see

#

Thank you so much!

#

It works now!