How do i get the player from this event?

Discussion in 'Plugin Development' started by CMG, Mar 3, 2013.

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

    CMG

    Basically i have this code below and i have got the player who is taking the damage when i throw the fishing line so playerHit is the player who gets hit by the fishing line, now i want to know how do i get the player who actually throws the fishing line.
    Code:
    @EventHandler
        public void onFishingLineHit(EntityDamageByEntityEvent event)
        {
            if(event.getCause() == DamageCause.PROJECTILE)
            {
                if(event.getDamager() instanceof Fish)
                {
                    Material material = Material.FISHING_ROD;
                    if(material == Material.FISHING_ROD)
                    {
                        Entity entity1thrower = (Entity) event.getEntity();
                        if(entity1thrower instanceof Player)
                        {
                            Player playerHit = (Player) entity1thrower;
                        }
                        else
                        {
                        //Do nothing
                        }
                    }
                }
            }
            else
            {
                //do nothing
            }
        }
     
  2. Code:
    event.getEntity(); // Entity that gets hit
    ((Projectile) event.getDamager()).getShooter(); // Get attacker
    Something like that, I can't remember perfectly.
     
  3. Offline

    xmarinusx

    I think you'll have to do it using this:
    event.getDamager().getShooter()
    I hope this works.
     
  4. Offline

    CMG

    How would i cast this to the projectile?
     
  5. Offline

    minoneer

    Since Fish is a subclass of projectile, you can just do ist like this:

    Code:
    Projectile projectile = (Projectile) event.getDamager();
    Then you check if the shooter is a player and if so, store it in a variable.

    Code:
    if (projectile.getShooter() instanceof Player) {
     
        Player playerShooter = (Player) projectile.getShooter();
     
    //Do your stuff
     
    }
     
Thread Status:
Not open for further replies.

Share This Page