Solved Detecting Murder

Discussion in 'Plugin Development' started by Zachster, May 24, 2013.

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

    Zachster

    How do you detect if a player was killed another player? Also, how do you get info on both players (location, etc)
     
  2. Offline

    chasechocolate

    Code:java
    1. @EventHandler
    2. public void onPlayerDeath(PlayerDeathEvent event){
    3. Player killed = event.getEntity();
    4. Player killer = killed.getKiller();
    5.  
    6. if(killer != null){
    7. //They were killed by a player
    8. //Do stuff
    9. }
    10. }
     
  3. Offline

    Skyshayde

    Code:java
    1.  
    2. @EventHandler
    3. private void onPlayerDamage(OnEntityDamageByEntityEvent event) {
    4. if(event.getEntity() instanceof Player) {
    5. if(event.getDamager() instanceof Player) {
    6. //run stuff here
    7. // event.getEntity().getLocation() will return a location object with the location of the damaged being
    8. // event.getDamager().getLocation() will return a location object with the location of the damager
    9.  
    10. }
    11. }
    12. }
     
  4. Offline

    chasechocolate

    Skyshayde
    Your code will be run when a player damages another player. Also, it's "EntityDamageByEntityEvent" not "OnEntityDamageByEntityEvent".
     
  5. Offline

    Zachster

    Thanks! It worked. I didn't know Player had that method.
     
  6. Offline

    Skyshayde

    Oh, I read that wrong, sorry
     
  7. Offline

    Trung_Lam

    Sort of unrelated questions, in a EntityDamageByEntityEvent, I understand how to get the entity that gets damaged, but how do you get entity, or in my case the player, that caused the damage becasue getDamager() gets you the weapon used.
     
  8. Offline

    Skyshayde

    Does it? getDamager returns the entity who hit them for me
     
  9. I think it's just event.getEntity();
     
  10. Offline

    Skyshayde

    That returns the entity who was hit. The javadocs say i am right.

    getDamager()
    Returns the entity that damaged the defender.
     
  11. Trung_Lam Skyshayde
    getDamager() is the entity that melee'd the victim but if the entity was shot by an arrow the damager is the arrow entity, you must check if the damager is instanceof Projectile, cast then get the shooter entity, then check if it's instanceof Player and so on...

    Oh and there's also Tameable (as in tamed wolf attack) if you want to check that too... same thing, check instanceof, cast then get owner entity.
     
    Skyshayde likes this.
Thread Status:
Not open for further replies.

Share This Page