Solved How do i get the location of the hit player?

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

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

    CMG

    For my plugin i want to be able to use the fishing rod to teleport players to you when they get hit by the rod, i have this code so far:
    Code:
        @EventHandler
        public void onPlayerInteractEvent(PlayerInteractEvent event)
        {
            Player player = (Player) event.getPlayer();
            Location locationThrower = player.getLocation();
     
            int blockId = player.getItemInHand().getType().getId();
            if (blockId == 546)// Fishing rod
            {
               //Main code
            }
        }
    So what i need to know is how would i get the hit player from the rod?
     
  2. Offline

    caseif

    This is mostly a guess, but I think a ProjectileHitEvent is thrown when a player is hit by a fishing hook. You could listen to that instead, check the entity, then get the hit entity and shooter.
     
  3. Offline

    CMG

    Nope the projectile hit event is for arrows and snowballs, already tried using that.

    Anyone?

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

    frozenpoptartmc

    use entitydamagebyentity
    check if it's damage by a projectile
    run a check for the projectile for the fishing rod
    get the Y axis for the projectile
    run a check to see if the Y axis is 1.35 or higher than the player's foot level,
    do stuff
     
  5. Offline

    CMG

    And how would i do that?

    bump

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

    caseif

    Listen to EntityDamageByEntity, check the damage cause, check the damager entity's type, and the rest is self-explanatory.
     
  7. Offline

    CMG

    But how though?, (like what code) im not very good with making plugins, this is something i just really need helping with
     
  8. Offline

    frozenpoptartmc

    sorry if this comes across as rude or snobby, but
    start with the basics and work your way up.
    if we tell you how to do everything, you won't learn.
     
  9. Offline

    CMG

    Yeah i understand what you are saying dude, i just need this asap

    So am i on the right lines? frozenpoptartmc AngryNerd
    Code:
    @EventHandler
        public void onPlayerInteractEvent(EntityDamageByEntityEvent event)
        {
            if(DamageCause.PROJECTILE != null)
            {
                Player player2 = (Player) event.getDamager();
                int blockId = player2.getItemInHand().getType().getId();
                if (blockId == 346)// Fishing rod
                {
                   
                }
            }
        }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  10. Offline

    caseif

    No. First, the line checking the DamageCause will always rerun true. You need to check the cause of the cent with .getCause(). Second, you should check the damaging entity's type, which will be a fishing hook. Third, it's bad practice to check block IDs; it's better to just check the material directly. You really need to do some research on the event, and maybe learn a bit more about the Bukkit API.
     
  11. Offline

    CMG

    Right, i now have this code and it works fine, i just need to know know how would i get the player who actually throws the fishing line, (PlayerGet is the player who i am hitting) ?
    Code:
    @EventHandler
        public void onPlayerInteractEvent(EntityDamageByEntityEvent event)
        {
            if(DamageCause.PROJECTILE != null)
            {
                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 playerGet = (Player) entity1thrower;
                        }
                        else
                        {
                        //Do nothing
                        }
                    }
                }
            }
        }
     
  12. This will never be false. You need to check whether the damage cause of the event is the same as the projectile damage cause.
     
  13. Offline

    CMG

    Like this?
    Code:
    if(event.getCause() == DamageCause.PROJECTILE)
     
  14. Offline

    CMG

    So how do i get the player throwing the line then?
     
  15. this will also never be false.

    This will not get you the player which threw the rod but rather the one that got hit. To get one who threw it, you need to cast the damager to a projectile (as we are sure it's a fishing rod because of the checks earlier) and then you can get the shooter of it. The shooter might be a different entity than the player so you need to do checks there as well.
     
  16. Offline

    CMG

    How do i cast the damager to the projectile can you show me how all i know is this bit: event.getDamager().getShooter();
     
  17. Offline

    CMG

    Last edited by a moderator: May 31, 2016
Thread Status:
Not open for further replies.

Share This Page