[SOLVED]Get the Entity a Player is looking at

Discussion in 'Plugin Development' started by tips48, Oct 8, 2011.

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

    FenixAzul

    i think u can getting where its the player range (looking) then get the location
     
  2. ?
    I know I can get a list of the blocks a player is looking at...That doesn't really help
     
  3. Offline

    FenixAzul

  4. True_DarkAngel likes this.
  5. I know this has been a while, but either i'm blind and can't see the solution in the given thread or it isn't there. Could someone pls point it out to me?
     
  6. I was PM'd the answer, but the code is private, and I don't remember who gave it to me. Sorry!
     
  7. Offline

    user_43347

    Any chance I could see? :p
     
  8. Eh, Maybe.. Lemme find who gave it to me
    @DirtyStarfish :D
     
  9. Offline

    Taco

    Here's how I would do it in a nutshell (little to no code):

    Get the block the player is staring at.
    Get the nearby entities to the location of said block within a tolerance of 1 block.
    ???
    Profit!
     
  10. Offline

    Taco

    I've never actually attempted the above method, but I imagine it'd work.
     
  11. Offline

    user_43347

    I was thinking...
    1. Get nearby entities and put them in a list
    2. Do a foreach and if the entity equals the entity of herobrine
    3. Profit???

    But that doesn't cover them looking at him...
    Edit trying Taco's method...

    How to get nearby entities of a block? :/

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

    Taco

    Isn't there a method for that in the Location interface? I haven't messed with plugin coding for a while, so I'm a little rusty.
     
  13. Offline

    user_43347

    Only in player.

    Edit, just getting that 1 final piece:
    Code:
            Block targetBlock = p.getTargetBlock(null, 50);
            for (Entity e : targetBlock.) {
                if (e == heroEntity) {
                    e.remove();
                }
            }
     
  14. Offline

    Taco

    For one, I recommend changing that == to instanceof. Second, let me look and see what I can find in the API.

    Try something like....

    for(Entity e : block.getChunk().getEntities())
    {
    if(e instanceof heroEntity && block.getLocation().distance(e.getLocation()) < 2)
    {
    e.remove();
    }
    }
     
    steaks4uce likes this.
  15. Offline

    user_43347

    Ah, whoops, over looked it :D
    Err, nvm cant set instanceof to a protected entity

    Thanks, working!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
  16. Hmm what happens if you directly look at the entity, does it return the air-block then?
    I think i'll have to take a look into the java-docs to see what i can achieve with targetBlock...
    Thanks for your answer, it brings me closer to a solution :)
     
  17. Offline

    Taco

    The only mobs I see this being an issue with are flying mobs because I'm not sure if line of sight would return the airblock that stops at that mob.
     
  18. Offline

    blaatz0r

    Another trick that I'm using is just firing a projectile (arrow/snowball) at very high speed. The entity that it hits is what you are looking at :) You can set the projectile damage to zero as well.

    Another idea: maybe you can make this projectile invisible, same way people make the vanish mods, so you don't even see the projectile. Then there's just a little delay caused by travel time of the projectile :)
     
  19. Offline

    DirtyStarfish

    @Lathanael

    This is what I use to get the entity the player is looking at. Someone better at Java could probably improve on this, but this does work.

    Code:
        void getTarget() {
            List<Entity> nearbyE = plugin.player.getNearbyEntities(plugin.range,
                    plugin.range, plugin.range);
            ArrayList<LivingEntity> livingE = new ArrayList<LivingEntity>();
    
            for (Entity e : nearbyE) {
                if (e instanceof LivingEntity) {
                    livingE.add((LivingEntity) e);
                }
            }
    
            plugin.target = null;
            BlockIterator bItr = new BlockIterator(plugin.player, plugin.range);
            Block block;
            Location loc;
            int bx, by, bz;
            double ex, ey, ez;
            // loop through player's line of sight
            while (bItr.hasNext()) {
                    block = bItr.next();
                    bx = block.getX();
                    by = block.getY();
                    bz = block.getZ();
                            // check for entities near this block in the line of sight
                            for (LivingEntity e : livingE) {
                                    loc = e.getLocation();
                                    ex = loc.getX();
                                    ey = loc.getY();
                                    ez = loc.getZ();
                                    if ((bx-.75 <= ex && ex <= bx+1.75) && (bz-.75 <= ez && ez <= bz+1.75) && (by-1 <= ey && ey <= by+2.5)) {
                                            // entity is close enough, set target and stop
                                            plugin.target = e;
                                            break;
                                    }
                            }
                    }
    
                }
    target is a LivingEntity variable. range is an int.
    Basically it adds all entities near the player (set by the range) to a list, and gets the living entities from them.
    Then it checks the location of the LivingEntity against the location of the blocks. If its close enough, then you have your entity, which you can then do whatever you want with.
     
    Casinator, CD3 and puyttre like this.
  20. @DirtyStarfish and the rest:

    Thx for helping me understand it :)
     
  21. Offline

    hayhaycrusher

    But that still gets entitys if there behind a block. @DirtyStarfish
     
  22. Offline

    fanaticmw2

    Replied to a 2 year thread, nice.
     
  23. Really do we have to keep necroposting?
     
  24. Offline

    c0mp

    Aaaand we're done here. Locking old post.
     
Thread Status:
Not open for further replies.

Share This Page