[HELP]Casting a variable depending on who triggered

Discussion in 'Plugin Development' started by xXSniperzzXx_SD, Sep 15, 2012.

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

    xXSniperzzXx_SD

    How can i define player as the person who shot an arrow if the damage was caused by and arrow, but if it wasnt by an arrow set player as the damager so if this didnt make sense look,

    Code:
     if( e.getEntity() instanceof Player){
                      if(ee.getDamager() instanceof Arrow){
                          Arrow arrow = (Arrow) ee.getDamager();
                          if(arrow.getShooter() instanceof Player){
                              Player player = (Player)arrow.getShooter();
                       
                       
                          }
                    }else if(ee.getDamager() instanceof Player){
                          Player player = (Player)ee.getDamager();
                    }
                    //then be able to use player here so i don't have to duplicate the code
                }

    could i just do this?

    Code:
        Player player = null;
     
        @EventHandler(priority = EventPriority.HIGH)
        public void onPlayerKillZombie(EntityDamageEvent e) {
                EntityDamageByEntityEvent ee = (EntityDamageByEntityEvent) e;
                if( e.getEntity() instanceof Player){
                    if(ee.getDamager() instanceof Arrow){
                        Arrow arrow = (Arrow) ee.getDamager();
                        if(arrow.getShooter() instanceof Player){
                            Player pplayer = (Player)arrow.getShooter();
                            player=pplayer;
                           
                           
                        }
                    }else if(ee.getDamager() instanceof Player){
                        Player pplayer = (Player)ee.getDamager();
                        player=pplayer;
                    }
                    //then be able to use player here so i don't have to duplicate the code
                }
        }
     
  2. Offline

    evilmidget38

    Before casting it to an EntityDamageByEntityEvent, you should probably make sure that it can be cast, otherwise it's going to throw a ClassCastException.
     
  3. Offline

    xXSniperzzXx_SD

    You mean like checking the damagecause? so something along the lines of:
    Code:
     if((e.getCause() == EntityDamageEvent.DamageCause.ENTITY_ATTACK) || (e.getCause() == EntityDamageEvent.DamageCause.PROJECTILE)) {
                EntityDamageByEntityEvent ee = (EntityDamageByEntityEvent) e;
     
  4. Offline

    evilmidget38

    if ( e instanceof EntityDamageByEntityEvent)
     
  5. Offline

    xXSniperzzXx_SD

    But so you never did answer my original question, would what i did in the second example work? Or would it cause some error further down the road
     
  6. Offline

    evilmidget38

    Sorry, I wasn't very specific in my response. What I'm saying is " EntityDamageByEntityEvent ee = (EntityDamageByEntityEvent) e;
    " would not work, as if it wasn't an EntityDamageByEntityEvent it would through a ClassCastException. So you should do an instanceof check as I described above before you cast.
     
  7. Offline

    xXSniperzzXx_SD

    Thanks for pointing that out, never actually thought about that... but what my original question is, is "How can i define player as the person who shot an arrow if the damage was caused by and arrow, but if it wasnt by an arrow set player as the damager"

    and would this work?


    Code:
        Player player = null;
     
        @EventHandler(priority = EventPriority.HIGH)
        public void onPlayerKillZombie(EntityDamageEvent e) {
                EntityDamageByEntityEvent ee = (EntityDamageByEntityEvent) e;
                if( e.getEntity() instanceof Player){
                    if(ee.getDamager() instanceof Arrow){
                        Arrow arrow = (Arrow) ee.getDamager();
                        if(arrow.getShooter() instanceof Player){
                            Player pplayer = (Player)arrow.getShooter();
                            player=pplayer;
                         
                         
                        }
                    }else if(ee.getDamager() instanceof Player){
                        Player pplayer = (Player)ee.getDamager();
                        player=pplayer;
                    }
                    //then be able to use player here so i don't have to duplicate the code
                }
        }
     
  8. Offline

    stelar7

    you could just listen to the EntityDamageByEntityEvent...
     
  9. Offline

    xXSniperzzXx_SD

    That's what i thought i was using for the whole time lol, about 2 days ago i realized i wasn't
     
Thread Status:
Not open for further replies.

Share This Page