Solved Custom death message after instant death

Discussion in 'Plugin Development' started by Akilogramofstone, Dec 6, 2021.

Thread Status:
Not open for further replies.
  1. Hello, so I was wondering how I could make a custom death message for a player that died from player#setHealth(). I've tried many ways to do it, but I couldn't. Any help is appreciated!

    (Sorry for my bad English)
     
    Last edited: Dec 6, 2021
  2. Offline

    byteful

  3. Offline

    xelatercero

    Doesn't that method trigger playerDeathEvent?
     
  4. Well yeah, but after the player dies, I want the death message to be replaced with a new one.

    I tried this, but it didn't work. Just left a huge error in the console.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Dec 6, 2021
  5. Offline

    pixelrider2000

    I'm pretty sure that when you set a players health to 0 it is 'DamageCause.SUICIDE'. But why do you even get errors? May you send us your code?

    This works perfectly fine for me:

    if(player.getLastDamageCause().getCause() == DamageCause.SUICIDE) {
    event.setDeathMessage("This is a custom death message!");
    }
     
  6. Offline

    Strahan

    No, last damage cause will just show whatever hurt them last, period. If they run into a cactus then a minute later you kill them via setHealth(0), it will return CONTACT as last damage cause. If they hadn't gotten hurt at all since they logged on, it will be null.

    I'd just add them to a List and check it on death to see if they died via special means.

    EDIT: Example of what I mean:
    Code:
    public class Test17 extends JavaPlugin implements Listener {
      private List<UUID> adminDeaded = new ArrayList<>();
    
      @Override
      public void onEnable() {
        getServer().getPluginManager().registerEvents(this, this);
      }
    
      @Override
      public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (args.length == 0) {
          sender.sendMessage(getConfig().getString("messages.pass-player", "Pass the player to kill"));
          return true;
        }
      
        Player target = getServer().getPlayer(args[0]);
        if (target == null) {
          sender.sendMessage(getConfig().getString("messages.invalid-player", "That person is not online"));
          return true;
        }
      
        adminDeaded.add(target.getUniqueId());
        target.setHealth(0);
        sender.sendMessage(getConfig().getString("messages.player-killed", "Player has been killed"));
        return true;
      }
    
      @EventHandler
      public void onPlayerDeath(PlayerDeathEvent e) {
        if (!adminDeaded.contains(e.getEntity().getUniqueId())) return;
      
        e.setDeathMessage(getConfig().getString("messages.custom-death", "You were killed by an admin"));
        adminDeaded.remove(e.getEntity().getUniqueId());
      }
    
      @EventHandler
      public void onPlayerQuit(PlayerQuitEvent e) {
        if (!adminDeaded.contains(e.getPlayer().getUniqueId())) return;
      
        adminDeaded.remove(e.getPlayer().getUniqueId());  
      }
    }
     
    Last edited: Dec 8, 2021
  7. It didn't work for me, here's my code (I've imported everything):
    Code:
    public class PlayerEvents implements Listener {
    
        @EventHandler
        public void onPlayerDeath(PlayerDeathEvent e) {
            Player player = e.getEntity().getPlayer();
          
            else if (e.getDeathMessage().contains("fell out of the world") || player.getLastDamageCause().getCause() == EntityDamageEvent.DamageCause.SUICIDE) {
                final String[] causes = {" was reduced to atoms.", " fell into the void.", " got divided by zero.", " discovered the black hole.", " fell into the abyss."};
                Random random = new Random();
                int index = random.nextInt(causes.length);
    
                player.getWorld().strikeLightningEffect(player.getLocation());
                e.setDeathMessage(ChatColor.GRAY + player.getDisplayName() + causes[index]);
                player.spigot().respawn();
            }
    
            else {
                player.getWorld().strikeLightningEffect(player.getLocation());
                e.setDeathMessage(ChatColor.GRAY + player.getDisplayName() + " died.");
                player.spigot().respawn();
            }
        }
    
        @EventHandler
        public void onPlayerMove(PlayerMoveEvent e) {
            Player player = e.getPlayer();
          
            if (e.getTo().getY() <= 0) {
                player.setHealth(0);
            }
        }
    
    }
    
     
  8. Offline

    Strahan

    "It didn't work for me" is less than helpful. Did you get an error? If so, what's the stack trace? Did you try adding debug messages to check how far it gets in the code? Let me guess; it's throwing a NullPointerException on the line where you check for SUICIDE? If so, it's because the player wasn't hurt. If they aren't hurt by anything, you'll get a null for getLastDamageCause() and since you're hanging another method off of it, it'll NPE. Anything that is nullable should be null checked before use.

    That aside, as I mentioned before, you can't use the damage cause as you think you can. Also you really should filter PlayerMoveEvent when you use it.
     
    Last edited: Dec 8, 2021
  9. Yes I did get an error, and it was indeed an NPE. This thread helped me fix it:
    https://bukkit.org/threads/nullpointerexception-in-entitydamageevent.487597/

    Also, your example was the solution to the problem I had, so thank you!

    EDIT: And also thanks to everyone who helped me!
     
    Strahan likes this.
Thread Status:
Not open for further replies.

Share This Page