#How to fix this log spam when I kill a entity with no item equipped in the mainhand slot

1 messages · Page 1 of 1 (latest)

blissful tusk
#

when I kill a mob with no item in my hand, this log starts appearing, I want to get rid of this log, anyone that could help me please answer this question

#
world.afterEvents.entityDie.subscribe((fx) => {
    const killer = fx.damageSource.damagingEntity
    const killed = fx.deadEntity

    if (killer.typeId === "minecraft:player") {
        const itemEquipped = killer.getComponent("minecraft:equippable").getEquipment('Mainhand')
        if (killed.typeId === "minecraft:player") {
            if (itemEquipped.typeId === "fec:tenacity" || itemEquipped.typeId === "fec:tenacity_axe") {
                killed.runCommand(`particle fec:tenacity_player_kill_fx ~~0.5~`)
                killed.runCommand(`particle fec:tenacity_player_kill_flash ~~0.5~`)
            }
        } else {
            if (itemEquipped.typeId === "fec:tenacity" || itemEquipped.typeId === "fec:tenacity_axe") {
                killed.runCommand(`particle fec:tenacity_entity_kill_fx ~~0.5~`)
                killed.runCommand(`particle fec:tenacity_entity_kill_flash ~~0.5~`)
            }
        }
    } else return 0;
})

this is my code for that thing

nimble lagoon
unkempt swift
#

or just add a && itemEquipped after your killed.typeId === "minecraft:player" if

nimble lagoon
#

Both works tho

blissful tusk
#

wait let me try

blissful tusk
nimble lagoon
#

So kind alike itemEquipped?.typeId === ''

blissful tusk
#

on my first try it failed because I didn't add the ? at the second condition of itemEquipped

#

that's why i asked twice 😐

#

sorry

#

thanks for the solution 🙂

empty jay
# blissful tusk thanks for the solution 🙂

"?." is the optional chaining operator, if you want to learn more about it, here.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining

MDN Web Docs

The optional chaining (?.) operator accesses an object's property or calls a function. If the object accessed or function called using this operator is undefined or null, the expression short circuits and evaluates to undefined instead of throwing an error.