#get player name in EntityEvents

12 messages · Page 1 of 1 (latest)

mental tusk
#
  const username = event.source.player.username;
  console.log(username);
});```

This returns ```{MINECRAFT_USERNAME}```. Is there a way to get the username when the damage source is a player ?
narrow solarBOT
#

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

plush rose
#

try event.source.actual.player.username

#

but you might want to put if (!event.source.actual.isPlayer()) return before that

mental tusk
plush rose
#

hmm

#

i haven't worked with fabric much, you might want to wait a bit longer until someone can help

mellow hornet
# mental tusk ```EntityEvents.hurt(event => { const username = event.source.player.username;...

if youre soley trying to get the username of the player on the entity hurt event then it stands to reason you could also do it the other way around and use the player attack entity forge event ```js
ForgeEvents.onEvent('net.minecraftforge.event.entity.player.AttackEntityEvent', event => global.playerattack(event))
/**
*

  • @param {Internal.AttackEntityEvent} event
  • @returns
    */
    global.playerattack = event => {
    //You can edit anything in here and run /kubejs reload startup_scripts to reload without having to do a full game restart!
    const {entity,target,entity:{persistentData,username},target:{type}} = event
    //This persistent data is important, preventing the event from firing twice
    if (persistentData.fireonce != 1){return}
    //debug stuff
    entity.tell(username)
    entity.tell(type)
    //here we give the player a diamond for hitting a cow :D
    if(type == 'minecraft:cow'){
    entity.give('minecraft:diamond')
    }
    }```
#

should let you do stuff to the target as you need to

#

crap im dumb, this is a fabric ticket

mental tusk
#

it is 🙂