Find The Killer Of A Player

Discussion in 'Plugin Development' started by 8thDimension, Aug 22, 2011.

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

    8thDimension

    Hello Bukkit forums,

    The plugin I'm working on is a PvP plugin that checks for a killer of user. Now, what I'm doing does find a killer but if you swing your weapon crazily and hit a player multiple times, sometimes it will register the "death" multiple times.

    This is the code I'm using

    Code:
    
     public void onEntityDamage(EntityDamageEvent event) {
    	    if (!(event.getEntity() instanceof Player)) {
    	      return;
    	    }
    
    	    if (!(event instanceof EntityDamageByEntityEvent)) {
    	      return;
    	    }
    
    	    if (!(((EntityDamageByEntityEvent)event).getDamager() instanceof Player)) {
    	      return;
    	    }
    
    	    final Player killer = (Player)((EntityDamageByEntityEvent)event).getDamager();
    	    Player victim = (Player)event.getEntity();
    	    //If there is not enough damage for a kill...
    	    if (event.getDamage() - victim.getHealth() < 0) {
    	      return;
    	    }
    
    	    //If the damage caused led to a kill, we shall do the following
    
    	    PlayerKilledStuff();
    
    	  }
    }
    
    Obviously I left some parts out, but that's the main code I use for finding a killer. Again it works but "PlayerKilledStuff()" will happen multiple times if the killer is swinging wildly. Much thanks to you if you can help me :)

    Shameless and quick bump

    Sorry, I would like to know a fix :3
    (I have a work-around for it which requires a hashmap and a scheduler but I want to know if there's a better way)

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

    DrBowe

    I think I'm going to make another guide that includes some of the top-asked questions...because this is definitely one of them.

    Anywho:
    Code:java
    1.  
    2. public void onEntityDeath(EntityDeathEvent event){
    3. Entity e = event.getEntity();
    4. if(e.getLastDamageCause() instanceof EntityDamageByEntityEvent){
    5. EntityDamageByEntityEvent nEvent = (EntityDamageByEntityEvent) e.getLastDamageCause();
    6. if(nEvent.getDamager() instanceof Player){
    7. Player p = (Player) nEvent.getDamager();
    8. //do stuff
    9. }
    10. }
    11. }
    12.  
     
  3. Offline

    8thDimension

    Sorry if my question is asked a lot, my google searches didn't bring up anything :p

    Thanks a lot anyways n_n
     
  4. Offline

    DrBowe

    Haha, its fine. In any case, I doubt you'd find much of anything from Google when it comes to coding plugins.
     
Thread Status:
Not open for further replies.

Share This Page