Getting Killer and Victim error?!?

Discussion in 'Plugin Development' started by Lewishjames, Feb 21, 2015.

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

    Lewishjames

    Hello everyone!

    I have quickly coded something to show an example but I have no idea how to fix it what so ever.
    Here is my code.
    Code:
    package me.kitpvp;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener {
       
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
       
        }
    
        @EventHandler
        public void onPlayerDeath(PlayerDeathEvent event){
            Player killer = event.getEntity().getKiller();
            Entity victim = event.getEntity();
            ItemStack weapon = killer.getInventory().getItemInHand();
            event.setDeathMessage("§c " + victim + " §fhas been killed by §c" + killer + " §fusing §c " + weapon + ".");
        }
    }
    However I get this in game???

    http://prntscr.com/68135u


    Thanks in advance

    -Lewis
     
  2. Offline

    glory_fades

    do victim.getName() and killer.getName() on your event.sentDeathMessage
     
  3. Offline

    Kassestral

    You want to get the killers name, as well as the victims name, also you want to get the weapons type ^_^
     
  4. Offline

    Lewishjames

    Yes problem with that?
     
  5. Offline

    Kassestral

    @Lewishjames
    What sorry? I was telling you how to fix your problem without giving you the direct code :D
     
  6. Offline

    MordorKing78

    @Lewishjames You are getting the Entity (the player) himself not his name. Use .getName() to get his name.
     
  7. Offline

    FerusGrim

    Code:java
    1. @EventHandler
    2. public void onPlayerDeath(PlayerDeathEvent event){
    3. Player killer = event.getEntity().getKiller();
    4. Entity victim = event.getEntity();
    5. ItemStack weapon = killer.getInventory().getItemInHand();
    6. event.setDeathMessage("§c " + victim + " §fhas been killed by §c" + killer + " §fusing §c " + weapon + ".");
    7. }


    First of all, PlayerDeathEvent will fire any time that a player dies, no matter the reason. Entity#getKiller is only ever not null when the killer of the player is another Player.

    So this line: ItemStack weapon = killer.getInventory().getItemInHand(); is going to throw an NPE most of the time. So would the next line, but your event will have already crashed before it reaches it.

    To be honest, this is more of a Bukkit issue, imo. To have an Entity object store a killer in the first place is kind of silly. To limit it to a Player, even more so. Not sure why we don't have something like EntityDeathByEntityEvent#getKillingEntity.
     
Thread Status:
Not open for further replies.

Share This Page