Why does this not default to...

Discussion in 'Spigot Plugin Development' started by valaiyar, May 29, 2017.

  1. Why isn't this defaulting to the message I set?

    Code (Text):
       
    @EventHandler(priority = EventPriority.HIGHEST)
        public void onPlayerDeath(PlayerDeathEvent e) {
            Player player = (Player) e.getEntity();
            Profile profile = bunkers.getProfileManager().getProfileByUuid(player.getUniqueId());
            profile.setDead(true);
            switch (player.getLastDamageCause().getCause()) {
                case FALL:
                    e.setDeathMessage(bunkers.getLangConfig().getString("DEATH_MESSAGES.FALL").replace("%PLAYER%", player.getName() + "%KILLS%")
                            .replace("%KILLS%", bunkers.getLangConfig().getString("DEATH_MESSAGES.KILLS").replaceFirst("%KILLS%", String.valueOf(profile.getKills()))));
                    break;
                case ENTITY_ATTACK:
                    if (player.getLastDamageCause().getEntity() instanceof Arrow) {
                        Arrow arrow = (Arrow) player.getLastDamageCause().getEntity();
                        if (arrow.getShooter() instanceof Player) {
                            Player killer = (Player) arrow.getShooter();
                            Profile killerProfile = bunkers.getProfileManager().getProfileByUuid(killer.getUniqueId());
                            killerProfile.setKills(killerProfile.getKills() + 1);
                            e.setDeathMessage(bunkers.getLangConfig().getString("DEATH_MESSAGES.SHOT")
                                    .replace("%PLAYER%", player.getName() + bunkers.getLangConfig().getString("DEATH_MESSAGES.KILLS")
                                            .replace("%KILLS%", String.valueOf(profile.getKills())))
                                    .replace("%KILLER%", killer.getName() + bunkers.getLangConfig().getString("DEATH_MESSAGES.KILLS")
                                            .replace("%KILLS%", String.valueOf(killerProfile.getKills())))
                                    .replace("%BLOCKS%", String.valueOf(killer.getLocation().distance(player.getLocation()))));
                            break;
                        }
                    } else if (player.getLastDamageCause().getEntity() instanceof Player) {
                        Player killer = (Player) player.getLastDamageCause();
                        Profile killerProfile = bunkers.getProfileManager().getProfileByUuid(killer.getUniqueId());
                        killerProfile.setKills(killerProfile.getKills() + 1);
                        e.setDeathMessage(bunkers.getLangConfig().getString("DEATH_MESSAGES.SLAIN")
                                .replace("%PLAYER%", player.getName() + bunkers.getLangConfig().getString("DEATH_MESSAGES.KILLS")
                                        .replace("%KILLS%", String.valueOf(profile.getKills())))
                                .replace("%KILLER%", killer.getName() + bunkers.getLangConfig().getString("DEATH_MESSAGES.KILLS")
                                        .replace("%KILLS%", String.valueOf(killerProfile.getKills())))
                                .replace("%ITEM%", WordUtils.capitalize(killer.getItemInHand().getType().toString().toLowerCase().replaceFirst("_", " "))));
                        break;
                    }
                default:
                    e.setDeathMessage(bunkers.getLangConfig().getString("DEATH_MESSAGES.OTHER").replace("%PLAYER%", player.getName() + "%KILLS%")
                            .replace("%KILLS%", bunkers.getLangConfig().getString("DEATH_MESSAGES.KILLS").replaceFirst("%KILLS%", String.valueOf(profile.getKills()))));
                    break;
            }
            profile.getFaction().setDeathsTilRaidable(profile.getFaction().getDeathsTilRaidable() - 1, true);
            profile.getFaction().sendFactionMessage(bunkers.getLangConfig().getString("FACTION.ACTION.MEMBER_OFFLINE").replaceFirst("%PLAYER%", player.getName()));
        }
     
  2. Where is your problem exactly?
     
  3. When a player dies, let's say from another player, it does the kill message, but for some reason it will say "valaiyar was slain by valaiyar using (whatever item I was using).". So somehow, the server is thinking that I was the one doing the last damage to myself, and obviously I wasn't if someone else killed me.
     
  4. Can you please indent your code a bit better ?
     
  5. With the replacements?
     
  6. How are you getting away with this cast?

    Code (Text):
    Player killer = (Player) player.getLastDamageCause();
    You've got a flaw in your code. player.getLastDamageCause returns a EntityDamageEvent, and calling .getEntity() is going to return the player.. always. You need to cast player.getLastDamageCause to an EntityDamageByEntityEvent in order to call .getDamager() and find out who hurt your player.

    For example, with your flaw, the code below would only occur if the player was the instance of an arrow. Probably never going to happen.

    Code (Text):
    if (player.getLastDamageCause().getEntity() instanceof Arrow) {
    Try casting to EntityDamageByEntityEvent and fixing your code to work with that.
     
    • Agree Agree x 1
  7. Thanks. I didn't know it returned EntityDamgeByEntityEvent, and I would have never thought about that at 6 in the morning lol.
     
  8. If that worked and solved your issue, please mark this thread as solved.
     
  9. I'm going to try it when I get back in an IDE.