What am I doing wrong here?

Discussion in 'Plugin Development' started by aPandaification, Jul 11, 2012.

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

    aPandaification

    So I am making this plugin that lets me send custom death messages to people without broadcasting it to the server but whenever someone dies I get a null pointer when it tries to send the message to the killer. I think it has to do with how I am declaring the player because it gave me errors with the version that I will post so I used eclipse's suggestions to fix it.

    Code:
    if (deadPlayer.getKiller() instanceof Player) {
     
                Player killerPlayer = (Player) player.getKiller();
    
    Also how can I send a message with the players name? Is there a method for getting a players name or what? Thanks in advance for your help.
     
  2. Offline

    CorrieKay

    i think if the player is offline it will return null. What are the circumstances going on when this error is thrown?
     
  3. Offline

    EnvisionRed

    No need to check if the killer is an instanceof player, that method always returns a player anyway. If the player died due to something other than a player, it will return null which might be your problem
     
  4. Offline

    ThatBox

    PHP:
    deadPlayer.getKiller();
    You tried to do player.getKiller(); which is not the object you checked for.
     
  5. Offline

    aPandaification

    Okay but I'm not sure how to fix it because I have tried changing everything and it isn't working.
    Here is my whole class:
    Code:
    public class PlayerDeathListener implements Listener {
       
        @EventHandler(priority = EventPriority.NORMAL)
        public void onPlayerDeathEvent(PlayerDeathEvent e) {
     
            // Player who died
            Player deadPlayer = (Player) e.getEntity();
     
            // send message to the dead player
            deadPlayer.sendMessage("[NemesisDeath] You were slain.");
     
            // If the killer was a Player
            if (deadPlayer.getKiller() instanceof Player) {
     
                Player killerPlayer = (Player) player.getKiller();
     
            // send message to the killer
                killerPlayer.sendMessage("[NemesisDeath] You killed someone.");
        }
        }
    }
     
  6. Offline

    one4me

    I had this saved in eclipse, so I've probably posted it before... maybe it will help?
    Code:
    @EventHandler
    public boolean onPlayerDeathEvent(PlayerDeathEvent e) {
      Player deadPlayer = (Player) e.getEntity();
      if (deadPlayer.getKiller() instanceof Player) {
        Player killerPlayer = (Player) deadPlayer.getKiller();
        deadPlayer.sendMessage("§eYou have been slain by " + killerPlayer.getName() + "!");
        killerPlayer.sendMessage("§eYou have slain " + deadPlayer.getName() + "!");
        e.setDeathMessage(null);
        return true;
      }
      deadPlayer.sendMessage("§e" + e.getDeathMessage());
      e.setDeathMessage(null);
    }
    
     
Thread Status:
Not open for further replies.

Share This Page