Solved Detect killing player

Discussion in 'Plugin Development' started by 567legodude, Jul 20, 2014.

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

    567legodude

    Hey, can someone tell me what event or code I need to use to detect which player killed another player.
    So if PlayerA kills PlayerB, I need to detect who PlayerA is.
    If a player dies naturally such as falling I dont need to know anything, only PvP kills.
    I might find it somewhere, but I decided to ask here first.
     
  2. Offline

    The Fancy Whale

    PlayerDeathEvent.
     
  3. Offline

    mine-care

    567legodude as The Fancy Whale said, its the playerdeathevent, other than that the damager changes so if i damage you and you die from falling then i am not the one who killed you, falling is. if you need help detecting who was the last player who damaged the playerB before he died by any other death cause let mew know! :D
     
  4. Offline

    567legodude

    mine-care Yes, I need to know if a player attacked them before they died, and if so who was the last person.
     
  5. Offline

    Cryteria

    Code:java
    1. public void anyName(PlayerDeathEvent event) {
    2. Player p = event.getEntity();
    3. if(p.isDead()) {
    4. p.getKiller();
    5. if(p.getKiller() instanceof Player) {
    6. p.sendMessage(ChatColor.RED +"You've killed" + p.getName() + "!");
    7. }
    8. }
    9. }


    That should do the trick.
     
  6. Offline

    567legodude

    Cryteria Is there a way I could detect the last player that hit them within a certain amount of time before they died.
     
  7. Offline

    daavko

    567legodude Try this:
    Code:java
    1. @EventHandler (priority = EventPriority.MONITOR, ignoreCancelled = true)
    2. public void onDeath(PlayerDeathEvent event) {
    3. Player killed = event.getEntity();
    4. if (killed.getLastDamageCause() instanceof EntityDamageByEntityEvent) {
    5. EntityDamageByEntityEvent dmgEvent = (EntityDamageByEntityEvent) killed.getLastDamageCause();
    6. if (dmgEvent.getDamager() instanceof Player) {
    7. Player killer = (Player) dmgEvent.getDamager();
    8. //some more code here
    9. }
    10. }
    11. }


    Note: That piece of code was made off top of my head. Might not work.
     
  8. Offline

    567legodude

    daavko I'll give it a try and get back with the results.

    daavko It doesn't seem to work. When I die I set it to replace a certain piece of text with the name. So the command I have is "tell <killer> You killed a player." I set that text to a string called killerMessage, then do this:
    Code:java
    1. String killerMessage = replaceAll("<killer>", killer.getName());

    and then I run the new command, which is supposed to be "tell 567legodude You killed a player." But the console says "There is no player with that name online"
    So Im not sure what to do.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  9. Offline

    artish1

    567legodude daavko

    Instead of using e.getDamager();

    You should do exactly what Cryteria has provided, EXCEPT... you need to check IF the killer is NOT null first. before putting it into a Player object.
    Example:
    Code:
    public void onDeath(PlayerDeathEvent e){
    Player player = e.getEntity(); 
    if(player.getKiller() != null){
    Player killer = player.getKiller();
     
    player.sendMessage(killer.getName() + " Killed you!");
    killer.sendMessage("You have killed " + player.getName());
     
    }else{
    //killer is null, means you did not die by another player, but by a mob/falldamage/Other
    }
     
     
     
    }
     
    iTzSimPvP likes this.
  10. Offline

    567legodude

    artish1 daavko Alright, I'll try that tomorrow. I have also found that it does not work for bowshot kills, can you tell me what to do for that? The bowshot death would not register as being killed by another player. So it would not try and send the message.
     
  11. Offline

    Paradrakor

    If I read your goal correctly, I believe the previous examples posted are too complicated.

    Code:text
    1.  
    2. @EventHandler
    3. public void kill(PlayerDeathEvent event) {
    4. if (event.getPlayer().getKiller() instanceof Player) { //code below will only run if killer is a player
    5. Player killer = event.getPlayer().getKiller(); //killer is now "Player A" from your example
    6. Player victim = event.getPlayer(); //victim is now "Player B" from your example
    7. killer.sendMessage(<KILL MESSAGE>);
    8. }
    9. }
    10.  
    11. [quote="567legodude, post: 2680784, member: 90909839"][USER=90636523]artish1[/USER] [USER=90962913]daavko[/USER] Alright, I'll try that tomorrow. I have also found that it does not work for bowshot kills, can you tell me what to do for that? The bowshot death would not register as being killed by another player. So it would not try and send the message.[/quote]
    12.  
    13. @576legodude
    14.  
    15. Did you test this on other players or by shooting yourself with a bow?
    16.  
    17. Minecraft can detect who shot the arrow that killed another player and use that in the deathmessage, but I'm not sure how to do that. You might be mistaken, otherwise you would need to say something like "else if killer is arrow entity"
    18.  
    19. [URL]http://jd.bukkit.org/rb/apidocs/org/bukkit/entity/EntityType.html[/URL]
    20.  
    21. EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  12. Offline

    ZodiacTheories

    All this spoon feeding is hurting my eyes :eek:
     
    CorrieKay likes this.
  13. Offline

    AoH_Ruthless

    567legodude
    Forget the PlayerDeathEvent.
    1. Use EntityDamageByEntityEvent
    2. Check if the entity is an instance of a player + cast
    3. Check if the damage is greater than the player's health (means that player will die)
    4. Check if the damager is an instance of player + code
    5. Check if damager is instance of an arrow + get shooter + check if shooter instance of player + code
    Because we are executing code twice (see steps 4 and 5), you might want to put that in a separate method and call that where ever you need it. You might need to do more casts (i.e if damager is a wolf, get owner if not null ... etc)
     
  14. Offline

    artish1

    AoH_Ruthless

    If he does EntityDamageByEntity event, it will just be a clusterfck of a bunch of code that PlayerDeathEvent can already cover up.

    You'd have to check if entities are a bunch of things (Wolves,Arrows,Regular Players,Eggs,Snowballs, etc), But if you went to PlayerDeathEvent and just checked if player.getKiller() is not null, and then make the player object... it would be so much easier because player.getKiller() does all that for us.


    It works perfectly fine in PlayerDeathEvent with "Bowshot" kills.
    I have even used the same code in my plugin OITC, and it has no problems whatsoever with PlayerDeathEvent.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  15. Offline

    567legodude

    artish1 When my friend killed me with a bow it never even tried to send me a message like it did when he killed me with a sword.
    Paradrakor I'll try that later.
    artish1 I'll keep that in mind.

    Paradrakor When I use your code it says "The method getPlayer() is undefined for the type PlayerDeathEvent"

    Paradrakor Alright your code worked, I just changed Player to String because I just need their name, and also it doesn't work unless you change getPlayer() to getEntity()

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  16. Offline

    Paradrakor

    No, I think you should change string back to player. When you use player in a message, it will still work.
     
  17. Offline

    mine-care

    Keep a hashmap of string , string and store the player who got hit and his attacker, (use entity damage by entity) and when the player dies get his attacker from the hashmap :)
     
  18. Offline

    567legodude

    Paradrakor No, I don't need to get the player at all, because I just need their name.
    Here is what I have going on:
    When someone dies to a PvP kill, it grabs the name of the killer and the killed. Then grabs the line from the config file(in this case the config said "tell <killer> You killed <killed>". Then it replaces <killer> and <killed> with the peoples names and sends the command to the console. There are two commands in the config for the killer and the killed.
    So that is why I just need their names and not the actual Player.
     
  19. Offline

    Paradrakor

    I'm just saying that using killer in a death message as type Player will give you the username
     
  20. Offline

    567legodude

    Paradrakor It's not a death message, I already finished it, but I made a plugin that executes a command on the players when they die.
     
Thread Status:
Not open for further replies.

Share This Page