Detect if player uses archery to kill a target.

Discussion in 'Plugin Development' started by Wolf7115, Dec 19, 2011.

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

    Wolf7115

    For detecting if the killing blow was done with archery, I use this:
    Code:
    EntityDamageByEntityEvent nEvent = (EntityDamageByEntityEvent) entity.getLastDamageCause();
    DamageCause damagetype = nEvent.getCause();
    Player killer = (Player) nEvent.getDamager();
    if(damagetype == DamageCause.PROJECTILE){
         //Code here
    }
    This doesn't seem to work for me. If I use this but replace DamageCause.PROJECTILE with DamageCause.ENTITY_ATTACK it works fine for melee. Does projectile attacks have a way to detect exactly which player shot the projectile?

    I still need assistance on this if anybody is willing.

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

    xKYLERxx

    I think this may help you.
    Code:
    Event.Type.PROJECTILE_HIT
     
  3. Your approach is fine as it is (through EntityDeathEvent)

    Since a couple of RBs projectile damage API is a bit different, though: If getCause() returns DamageCause.PROJECTILE, getDamager() returns the projectile itself - not its shooter.

    So you'd do something like that:
    Code:
    if(damagetype == DamageCause.PROJECTILE){
        Entity projectile = yourevent.getDamager();
        Entity attacker = projectile.getShooter();
        if(attacker instanceof Player) ...
    }
     
  4. Offline

    Wolf7115

    Thanks. I'll try that.

    @Bone008 Every time I use getShooter() I get an issue with just that. I'm using the latest build of Bukkit, is there a chance that they changed how this works? Or am I just forgetting something?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 22, 2016
  5. Sorry, I forgot the casting part. Entity has no getShooter(), but Projectile has it, so you have to cast "projectile" to a Projectile first

    Now I remember why I used that other if-clause in my plugin ^.^

    I did it like that:
    Code:
    Entity damager = yourevent.getDamager();
    if(damager instanceof Projectile){
        Entity attacker = ((Projectile)damager).getShooter();
        ...
    }
    If you are using instanceof or the comparison with DamageCause.PROJECTILE in your if-clause doesn't really matter. As of now, they should always match the same cases, but if that ever gets changed (maybe another cause will be added that is not called PROJECTILE but also has a Projectile as a damager, who knows ...) you are on the safer side.
     
    Wolf7115 likes this.
  6. Offline

    Wolf7115

    Ah! That works! Thank you very much.
     
Thread Status:
Not open for further replies.

Share This Page