How to fix error message when killed by non-players

Discussion in 'Plugin Development' started by Ibix13, May 16, 2013.

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

    Ibix13

    I am getting this error when I die from a cause not from a player, and I know the reason why. I just don't know how to fix it :/ heres the error:
    http://pastebin.com/kRz4GhJU
    Here is my code for the playerDeathEvent too :)
    Code:
    @EventHandler
        public void onPlayerDeath(PlayerDeathEvent event) {
            event.setDeathMessage("");
            event.getDrops().clear();
            Player p = (Player) event.getEntity();
            Player v = (Player) event.getEntity().getKiller();
            p.sendMessage("You have been killed by " + ChatColor.RED
                    + p.getKiller().getName());
            v.sendMessage("You have killed " + ChatColor.GREEN
                    + p.getPlayer().getName());
            if (hasKit.contains(p.getName())) {
                hasKit.remove(p.getName());
            }
     
        }
     
  2. Check if the entity is a player in the first place before casting to it.
     
  3. Offline

    Ibix13

    I don't know how to check if it's a player but I know it's something like:

    if(entity.getEntity().getPlayer()){
    //Stuff
    }
     
  4. Offline

    kiwhen

    It would be similar to what you're doing when you're checking if the CommandSender is a Player in onCommand. (I'm gonna take a wild guess and say that you're not doing that either.)

    This is a pretty neat way:
    Code:java
    1. if(!(e.getEntity().getKiller() instanceof Player))
    2. return;

    You don't really need to cast at all, because they both return Players. You might just as well just check if getKiller() == null. As you can see from the error you're getting, this is a NullPointerException-error, which means that it crashes because you're trying to do something with an object that is null (aka. nothing).

    In the code above, I'm using negating, which means that the return-statement will run if getKiller() is not an instance of a Player.
     
  5. Offline

    Ibix13

    I want it to check if it's dying from something different now? If I wanted it to be lava would I do intanceof Lava or something.lava?

    Oh wait that is making it so it's not a player... lemme test it real quick :D

    It sends me a message when I die from something that is not a player but I still get same error D:
    Code:

    Code:
    @EventHandler
        public void onPlayerDeath(PlayerDeathEvent event) {
            event.setDeathMessage("");
            event.getDrops().clear();
            Player p = (Player) event.getEntity();
            Player v = (Player) event.getEntity().getKiller();
            if (hasKit.contains(p.getName())) {
                hasKit.remove(p.getName());
            }
            if(!(event.getEntity().getKiller() instanceof Player)){
                p.sendMessage("You have " + ChatColor.RED + "died");
            }
                if((event.getEntity().getKiller() instanceof Player))
            p.sendMessage("You have been killed by " + ChatColor.RED
                    + p.getKiller().getName());
            v.sendMessage("You have killed " + ChatColor.GREEN
                    + p.getPlayer().getName());
            }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  6. Put the check if the entity is an instance of the player BEFORE you cast it to a player.
     
  7. Offline

    Ibix13

    wut would that look like?
     
  8. Offline

    colony88

    Code:java
    1. @EventHandler
    2. public void onPlayerDeath(PlayerDeathEvent event){
    3. if(event.getEntity().getKiller() instanceof Player){
    4. Player v = (Player)event.getEntity().getKiller();
    5. }
    6. }
     
  9. Offline

    Ibix13

    <3 Thank you :D
     
Thread Status:
Not open for further replies.

Share This Page