Get name of an player that killed certain mob

Discussion in 'Plugin Development' started by TerThesz, Apr 3, 2020.

Thread Status:
Not open for further replies.
  1. Offline

    TerThesz

    Hello,
    I'm making a plugin for an event on my server and I need to get name of a player that killed ender dragon. Any ideas?
     
    Last edited: Apr 4, 2020
  2. Offline

    HCMatt

    1. EntityDeathEvent
    2. Check if Entity instaceof EnderDragon
    3. Check if killer instanceof Player
    4. Player killer = (Player) event.getEntity().getKiller();
    5. killer.getName() <---- that is the name of the player who killed the dragon.

    Untested but that is how I would do it.
     
    pietermantel likes this.
  3. Offline

    KarimAKL

    @HCMatt A little fix is needed but, otherwise that's correct.

    LivingEntity#getKiller() returns Player, so there's no need to check for that.

    There's also no need to cast it to Player.

    You need to do a null check before this, since getKiller() returns Player, it'll be null if the killer wasn't a player.

    Also, getKiller() will return null if you were indirectly killed by a player, i.e. an arrow from a player or something like that.
    Since you're talking about killing an EnderDragon, i think you want that as well.
    Here's how you do it:
    1. Listen to the EntityDeathEvent.
    2. Check if getEntity() is an instanceof EnderDragon.
    3. Get the last EntityDamageEvent thrown from the EnderDragon by using Entity#getLastDamageCause().
    4. Check if the EntityDamageEvent is an instanceof EntityDamageByEntityEvent, if so, cast it to that.
    5. Check if EntityDamageByEntityEvent#getDamager() instanceof Player, if so, that's your player, otherwise, check if it's an instanceof Projectile, and if so, check if Projectile#getShooter() is an instanceof Player, if so, you've got your player.
    6. Then you just use Player#getName() to get their name.
     
Thread Status:
Not open for further replies.

Share This Page