[SOLVED]Get the Entity a Player is looking at

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

  1. Offline

    tips48

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Pm'd

    This post has been edited 1 time. It was last edited by tips48 Oct 9, 2011.
  2. Offline

    FenixAzul

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    i think u can getting where its the player range (looking) then get the location
  3. Offline

    tips48

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    ?
    I know I can get a list of the blocks a player is looking at...That doesn't really help
  4. Offline

    FenixAzul

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
  5. Offline

    tips48

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
  6. Offline

    Lathanael

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    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?
  7. Offline

    tips48

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    I was PM'd the answer, but the code is private, and I don't remember who gave it to me. Sorry!
  8. Online

    nkrecklow

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Any chance I could see? :p
  9. Offline

    tips48

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Eh, Maybe.. Lemme find who gave it to me
    @DirtyStarfish :D

    This post has been edited 1 time. It was last edited by tips48 Nov 8, 2011.
  10. Offline

    Taco

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    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!
  11. Offline

    tips48

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
  12. Offline

    Taco

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    I've never actually attempted the above method, but I imagine it'd work.
  13. Online

    nkrecklow

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    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...

    This post has been edited 2 times. It was last edited by steaks4uce Nov 8, 2011.
  14. Online

    nkrecklow

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    How to get nearby entities of a block? :/

    This post has been edited 3 times. It was last edited by steaks4uce Nov 8, 2011.
  15. Offline

    Taco

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    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.
  16. Online

    nkrecklow

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    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();
                }
            }

    This post has been edited 1 time. It was last edited by steaks4uce Nov 8, 2011.
  17. Offline

    Taco

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    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();
    }
    }

    This post has been edited 2 times. It was last edited by Taco Nov 8, 2011.
    steaks4uce likes this.
  18. Online

    nkrecklow

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Ah, whoops, over looked it :D
    Err, nvm cant set instanceof to a protected entity

    This post has been edited 1 time. It was last edited by steaks4uce Nov 8, 2011.
  19. Online

    nkrecklow

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Thanks, working!
  20. Offline

    Lathanael

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    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 :)
  21. Offline

    Taco

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    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.
  22. Offline

    blaatz0r

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    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 :)
  23. Offline

    DirtyStarfish

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    @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.

    This post has been edited 2 times. It was last edited by DirtyStarfish Dec 6, 2011.
    CD3 and puyttre like this.
  24. Offline

    Lathanael

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)

Share This Page