Interacting with the Player Head ?

Discussion in 'Plugin Development' started by Ziden, Nov 7, 2011.

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

    Ziden

    Hallo there.

    Is it possible somehow to detect, when a arrow hits a player, if it hits the player head ?
    Kinda simple question, but i couldnt figure it out. Thanx for any help !
     
  2. Take a look at this.
     
    Ziden likes this.
  3. Offline

    Ziden

    thanx loads

    it seems
    if (event instanceof EntityDamageByProjectileEvent)
    is deprecated. Does it works ?

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

    Lolmewn

    Use EntityDamageByEntity, then check if the entity is an arrow. (or snowball, whatever your Projectile was)
     
    r3Fuze likes this.
  5. Offline

    Ziden

    am i doing the right way ? i aways kinda had a hard job dealing with arrows

    Code:
      @Override
        public void onProjectileHit(ProjectileHitEvent event) {
            if(event.getEntity() instanceof Arrow) {
    
            }
        }
    
        @Override
        public void onEntityInteract(EntityInteractEvent event) {
    
            if( event.getEntity().getLastDamageCause().getCause()==DamageCause.PROJECTILE) {
               if(event.getEntity() instanceof Arrow) {
                    if(event.getEntity().getLastDamageCause().getEntity() instanceof Player) {
                        Player p = (Player)event.getEntity().getLastDamageCause().getEntity();
    
                    }
               }
            }
        }
    witch event should i use from those ?
    and , if event.getEntity() is an arrow and event.getEntity().getLastDamageCause().getEntity(), how can i check if the damaged entity is a player too ?

    Thanx alot for your help !
     
  6. Offline

    Lolmewn

    Well, the entity that gets interacted with is for example a player or Mob, don't think it's an arrow.
     
    Ziden likes this.
  7. None:
     
  8. Offline

    Ziden

    how ? :B

    onEntityDamage(EntityDamageEvent event) ?

    i couldnt do this:

    Code:
    public void onEntityInteract(EntityInteractEvent event) {
          if( event instanceof EntityDamageByEntityEvent ) {

    edit: got closer i guess
    is this correct ?

    Code:
    public void onEntityDamage(EntityDamageEvent event) {
                if (event instanceof EntityDamageByEntityEvent) {
                    EntityDamageByEntityEvent dmgEvent = (EntityDamageByEntityEvent) event;
                    Entity damager = ((EntityDamageByEntityEvent) event).getDamager();
                    Entity entity = event.getEntity();
                    if (entity instanceof Player && damager instanceof Player) {
                        ((Player) damager).sendMessage(dmgEvent.getEventName());
                        if (dmgEvent.getCause() == DamageCause.PROJECTILE && dmgEvent.getEntity() instanceof Arrow) {
                            double dif = dmgEvent.getEntity().getLocation().getY() - dmgEvent.getEntity().getLocation().getY() - 1.5;
                            if (dif > 0.1 && dif < 0.5) {
                                ((Player) damager).sendMessage("HeadShot !");
                            }
    
                        }
                    }
    
    
                }
    dmgEvent.getEntity() instanceof Arrow) gets me an cast error... how can i know when its an arrow ?

    thanx for the attention

    Still couldnt manage to do even with all those tips =(

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

    Lolmewn

    How can an entity be a player and an arrow at the same time?
     
    Ziden likes this.
  10. Offline

    Ziden

    Where can i check its an arrow then ? :S
     
  11. Offline

    bergerkiller

    Code:
        private final static double headShotRadius = 1;
        private static boolean getHeadshot(Projectile projectile, Entity e) {
            if (projectile == null) return false;
            if (e == null) return false;
            Location eyeloc = null;
            if (e instanceof LivingEntity) {
                eyeloc = ((LivingEntity) e).getEyeLocation();
            } else {
                return false;
            }
            return eyeloc.distance(projectile.getLocation()) <= headShotRadius;
        }
    Should help ya out.
     
    Ziden likes this.
  12. Offline

    Ziden

    thanx alot man, it really helped, but at this time im having difficulties to find the right classes.

    Projectile projectile

    where do i get that from ? xD I couldnt even find to verify if the projectile was an arrow

    thanx alot for the help.
     
  13. Offline

    undeadmach1ne

    if the EntityDamageByEntityEvent is an instanceof Projectile, getDamager will return the arrow, getShooter will return the player (or skeleton) that shot it.
     
    Ziden likes this.
  14. Offline

    Ziden

    AHAAA now i got it !
    thanx alot man !

    ill try this later when i get home and post tha results.
     
  15. Offline

    undeadmach1ne

    :) i forgot to specify that you have to getShooter on the arrow, not the projectile event.
     
    Ziden likes this.
  16. Offline

    Ziden

    dude i still couldnt manage to do this, its being very confusing i dont know witch classes to use. I know this is not a good thing to ask, but could u gimme a simple snipper about the event handling ?

    Code:
     public void onEntityDamage(EntityDamageEvent event) {
                if (event instanceof EntityDamageByEntityEvent) {
                    EntityDamageByEntityEvent dmgEvent = (EntityDamageByEntityEvent) event;
    
                    if(dmgEvent instanceof Projectile) {
                        Projectile prj = (Projectile)dmgEvent;
                        if( prj.getShooter() instanceof Player & event.getEntity() instanceof Player) {
                            if(getHeadshot( ??? , event.getEntity()))
                                //
                        }
                    }
    Thanx alot for your time, sorry for my slowliness lol
     
  17. Offline

    bergerkiller

    @Ziden you are casting the event as a projectile now? lol...
    Code:
     public void onEntityDamage(EntityDamageEvent event) {
                if (event instanceof EntityDamageByEntityEvent) {
                    EntityDamageByEntityEvent dmgEvent = (EntityDamageByEntityEvent) event;
                    Entity damaged = dmgEvent.getEntity();
                    Entity damager = dmgEvent.getDamager();
                    if(damager instanceof Projectile) {
                        Projectile prj = (Projectile) damager;
                        Entity shooter = prj.getShooter();
                        if (shooter instanceof Player) {
                            Player player = (Player) shooter;
                            if (getHeadshot(prj, damaged)) {
                                player.sendMessage("You gave him a headshot!");
                            }
                         }
                    }
                }
           }
     
    Ziden likes this.
  18. Offline

    Ziden

    yeah im lost.. but this is old read the last code ive posted..

    damn it sounds so simple and i cant get a solution !
     
  19. He...posted the answer?
     
    Ziden likes this.
  20. Offline

    Ziden

    aw lol
    sry

    this has been solved thank you all for the help

    PS: tips im waiting for yar rpgworld man !! xD
     
    tips48 likes this.
Thread Status:
Not open for further replies.

Share This Page