Solved Custom death messages

Discussion in 'Spigot Plugin Development' started by Shyos, May 7, 2017.

  1. So I am trying to create custom death messages when a player gets killed by entities (with custom names).
    For example:
    Code (Text):
    Shyos has been killed by a Necromancer
    in where the Necromancer would be a Zombie with a custom name for example.
    My problem is that I can't figure out what Event to listen to :eek:
  2. Well, the event to set the death message is:
    Code (Text):
        public void onPlayerDeath(PlayerDeathEvent event) {
            event.setDeathMessage(event.getEntity().getName() + " was killed by a ____.");
    The issue is that you cannot get the entity that killed the player in this event. I recommend using the EntityDamageByEntityEvent, and keep a HashMap filled with the last entity that damaged every player.

    * You can use Player#getKiller(), however, that's if a player kills you :)
  3. @Redraskal
    Haven't seen you on in a while, hey.
    To answer your question, what I would do, is make a lot of individual PlayerDeathEvent and I think you can get the e#getCause() and just make a lot of those.
  4. Yeah, was thinking about actually doing it with the EntityDamageByEntityEvent. One flaw is though that I would have to set/display am essage some other way then :/
  5. There is no #getCause() method.
  6. You could detect in the EntityDamageByEntityEvent if this attack will be the kill, and then add it to the HashMap instead. If the map is empty when the PlayerDeathEvent is called, there's some other reason.
  7. Why use a hashmap and everything?

    Listen for EntityDamageByEntityEvent, check if the attack will kill the victim and do your other checks, and if it all returns true, broadcast your custom death message.
    In the PlayerDeathEvent, just set the death message to "", no matter what. It'll be broadcasted from the EntityDamageByEntityEvent.
  8. You can use PlayerDeathEvent#getLastDamageCause - it returns the last EntityDamageEvent that happened to the player (the one that killed them). Use that to get the cause.
  9. That's a great idea, although I think if you set the death message to null, there will be no message at all. (I could be wrong)
    • Like Like x 1
  10. Well, for now I ended up using this method :)
    Thanks everyone for your help/input!