[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

    My BukkitDev Profile
    My Plugins (2)
    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Pm'd
     
  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

    My BukkitDev Profile
    My Plugins (2)
    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

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

    Lathanael

    My BukkitDev Profile
    My Plugins (5)
    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

    My BukkitDev Profile
    My Plugins (2)
    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

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

    tips48

    My BukkitDev Profile
    My Plugins (2)
    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Eh, Maybe.. Lemme find who gave it to me
    @DirtyStarfish :D
     
  10. Offline

    Taco

    My BukkitDev Profile
    My Plugins (6)
    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

    My BukkitDev Profile
    My Plugins (2)
    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    kinda :p
     
  12. Offline

    Taco

    My BukkitDev Profile
    My Plugins (6)
    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

    My BukkitDev Profile
    My Plugins (2)
    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...
     
  14. Offline

    nkrecklow

    My BukkitDev Profile
    My Plugins (2)
    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    How to get nearby entities of a block? :/
     
  15. Offline

    Taco

    My BukkitDev Profile
    My Plugins (6)
    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

    My BukkitDev Profile
    My Plugins (2)
    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Only in player.

    Edit, just getting that 1 final piece:
    Code (Text):
    1.         Block targetBlock = p.getTargetBlock(null, 50);
    2.         for (Entity e : targetBlock.) {
    3.             if (e == heroEntity) {
    4.                 e.remove();
    5.             }
    6.         }
     
  17. Offline

    Taco

    My BukkitDev Profile
    My Plugins (6)
    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();
    }
    }
     
    steaks4uce likes this.
  18. Offline

    nkrecklow

    My BukkitDev Profile
    My Plugins (2)
    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
     
  19. Offline

    nkrecklow

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

    Lathanael

    My BukkitDev Profile
    My Plugins (5)
    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

    My BukkitDev Profile
    My Plugins (6)
    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 (Text):
    1.     void getTarget() {
    2.         List<Entity> nearbyE = plugin.player.getNearbyEntities(plugin.range,
    3.                 plugin.range, plugin.range);
    4.         ArrayList<LivingEntity> livingE = new ArrayList<LivingEntity>();
    5.  
    6.         for (Entity e : nearbyE) {
    7.             if (e instanceof LivingEntity) {
    8.                 livingE.add((LivingEntity) e);
    9.             }
    10.         }
    11.  
    12.         plugin.target = null;
    13.         BlockIterator bItr = new BlockIterator(plugin.player, plugin.range);
    14.         Block block;
    15.         Location loc;
    16.         int bx, by, bz;
    17.         double ex, ey, ez;
    18.         // loop through player's line of sight
    19.         while (bItr.hasNext()) {
    20.                 block = bItr.next();
    21.                 bx = block.getX();
    22.                 by = block.getY();
    23.                 bz = block.getZ();
    24.                         // check for entities near this block in the line of sight
    25.                         for (LivingEntity e : livingE) {
    26.                                 loc = e.getLocation();
    27.                                 ex = loc.getX();
    28.                                 ey = loc.getY();
    29.                                 ez = loc.getZ();
    30.                                 if ((bx-.75 <= ex && ex <= bx+1.75) && (bz-.75 <= ez && ez <= bz+1.75) && (by-1 <= ey && ey <= by+2.5)) {
    31.                                         // entity is close enough, set target and stop
    32.                                         plugin.target = e;
    33.                                         break;
    34.                                 }
    35.                         }
    36.                 }
    37.  
    38.             }
    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.
     
    CD3 and puyttre like this.
  24. Offline

    Lathanael

    My BukkitDev Profile
    My Plugins (5)
    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    @DirtyStarfish and the rest:

    Thx for helping me understand it :)
     
  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
     
  26. Offline

    fanaticmw2

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

    BorisTheTerrible

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

    tips48

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

    c0mp

    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