#how2fix error in entityDeath event when the entity is an item entity?

1 messages · Page 1 of 1 (latest)

harsh turret
#

did not give this problem in the 1.19.80
I do /kill @e to kill item for this to happen

#

only happens when the item is killed

#

not other animal

ivory stratus
#

send code

harsh turret
#

world.afterEvents.entityDie.subscribe(ev => {

switch (ev.deadEntity.typeId) {
    case "minecraft:sheep": {
        ev.deadEntity.runCommandAsync("say hi");
        break;
    }
}

});

#

also this error is given for projectile entity

ivory stratus
#

bro wdym the code legit works?

umbral quiver
#

use 2nd parameter

harsh turret
#

?

umbral quiver
#

world.afterEvents.entityDie.subscribe(e => {
    const d = e.deadEntity
    //code
}, { entityTypes:['minecraft:sheep','minecraft:player'] })

ivory stratus
#

@harsh turret the code works perfectly

harsh turret
#

The code works fine

#

But still gives error when item is killed

clear kettle
#

just add ? (optional chaining operator) to safely access deadEntity.typeId without an error.

ev.deadEntity?.typeId

umbral quiver
#

nope

#

deadEntity is defined

#

it's just too late to call typeId

harsh turret
#

That wasn't the case before

#

And doesn't make sense

#

/kill some random animals and it won't complain but if there are some item or projectile it will give error for those specific entities

umbral quiver
#

cus animals triggers when they die

#

and they have "dying animation"

#

thats enoug time for 'deadEntity' to be defined in afterEvent

harsh turret
#

Nah that shouldn't matter

umbral quiver
#

as well as call their properties

harsh turret
#

i need to run specific code for each animal i want

umbral quiver
#

;-;

#

ik of a way but cant write it rn

harsh turret
#

i dont really want to refactor my code