[SOLVED]Get the Entity a Player is looking at

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

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

    tips48

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

    tips48

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

    Lathanael

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    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)
    Minecraft account:
    MCUSERNAME
    I was PM'd the answer, but the code is private, and I don't remember who gave it to me. Sorry!
  8. Offline

    nkrecklow

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

    tips48

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

    Taco

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

    nkrecklow

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

    nkrecklow

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

    nkrecklow

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

    nkrecklow

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

    nkrecklow

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

    Lathanael

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

    hayhaycrusher

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    But that still gets entitys if there behind a block. @DirtyStarfish

    This post has been edited 1 time. It was last edited by hayhaycrusher Aug 6, 2013.
  26. Offline

    fanaticmw2

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Replied to a 2 year thread, nice.
  27. Offline

    BorisTheTerrible

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
  28. Offline

    tips48

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Really do we have to keep necroposting?
  29. Online

    c0mp 13371331337 Moderator

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Aaaand we're done here. Locking old post.
Thread Status:
Not open for further replies.

Share This Page