Solved Getting an entity you're looking at from distance?

Discussion in 'Plugin Development' started by ItsOneAndTwo, Aug 28, 2014.

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

    ItsOneAndTwo

    So I would like to make it so, that if I left/right click with a certain item, it will get the entity I'm pointing at (even if the entity is like 20 blocks away), how would I get the entity I'm pointing at?
     
  2. Offline

    TheMintyMate

    ItsOneAndTwo

    Found related thread:
    https://forums.bukkit.org/threads/solved-getting-entity-in-sight.89167/

    Final solution was:
    Code:
    List<Entity> e = player.getNearbyEntities(100, 100, 100);
    Block targetBlock = //The block your currently iterated
    Location blockLoc = targetBlock.getLocation();
    double bx = blockLoc.getX();
    double by = blockLoc.getY();
    double bz = blockLoc.getZ();
    for (Entity entity : e) {
    Location loc = entity.getLocation();
    double ex = loc.getX();
    double ey = loc.getY();
    double ez = loc.getZ();
    if ((bx-1.5 <= ex && ex <= bx+2) && (bz-1.5 <= ez && ez <= bz+2) && (by-1 <= ey && ey <= by+2.5)) {
    Now you have an entity near the location specified by targetBlock
    }
    }
    
    Hope this helped,
    - Minty

    Edit: Otherwise maybe look at the coding behind the Event for PlayerInteractEntityEvent and see how the event works - then take from it what you need. (may be irrelevant)
     
  3. Offline

    ItsOneAndTwo

    TheMintyMate
    Code:java
    1. Block targetBlock = //The block your currently iterated

    I don't have a block iterator, since I'm not familiar with it.
     
  4. Offline

    TheMintyMate

    I believe they are suggesting something such as player.getDirection() or player.getLineOfSight()
     
  5. Offline

    Syd

    1. Get all entities that could be in range (.getNearbyEntities)
    2. For every entity get the angle between the direction of the player and the entities location (vector math)
    3. check if it's the smallest angle you observed so far, if yes store this entity
    4. return the stored entity
     
  6. Offline

    Flamedek

    ItsOneAndTwo
    You will still need a BlockIterator, which is realy easy to make.
    But the code TheMintyMate found is horribly inefficient if you ask me.

    I would spawn an entity (like an endersignal or something else small) at the location you found with the Iterator, and then check for nearbyEntitys with a range of 1 or 2 around that entity (dont forget to despawn the entity you just spawned)

    Here is a how you could use a BlockIterator:
    GitHub
     
    TheMintyMate likes this.
  7. Offline

    Syd

    For what do you need a BlockIterator, when you are working with Entities only? oO
    As I already said: just do the math instead of some inefficient loops. ;)
     
  8. Offline

    BillyGalbreath

    This is how I do it in the Pl3xPets plugin (source):

    Code:java
    1.  
    2. public static Entity getTargetedEntity(Player player) {
    3. List<Entity> nearbyE = player.getNearbyEntities(6, 6, 6);
    4. ArrayList<LivingEntity> livingE = new ArrayList<LivingEntity>();
    5. for (Entity e : nearbyE) {
    6. if (e instanceof LivingEntity)
    7. livingE.add((LivingEntity) e);
    8. }
    9. BlockIterator bItr = new BlockIterator(player, 6);
    10. Block block;
    11. Location loc;
    12. int bx, by, bz;
    13. double ex, ey, ez;
    14. while (bItr.hasNext()) {
    15. block = bItr.next();
    16. bx = block.getX();
    17. by = block.getY();
    18. bz = block.getZ();
    19. for (LivingEntity e : livingE) {
    20. loc = e.getLocation();
    21. ex = loc.getX();
    22. ey = loc.getY();
    23. ez = loc.getZ();
    24. if ((bx-.75 <= ex && ex <= bx+1.75) && (bz-.75 <= ez && ez <= bz+1.75) && (by-1 <= ey && ey <= by+2.5)) {
    25. if(e instanceof LeashHitch)
    26. return ((LeashHitch) e).getPassenger();
    27. return e;
    28. }
    29. }
    30. }
    31. return null;
    32. }
    33.  

    The code pasted to this thread earlier was incomplete, but ultimately looks to be from the same source I looked up years ago. ;)

    I added some things, like the leash detection. If it finds a leash, it tries to get the entity attached to the leash.

    Note: This method is extremely vague. There is no way to get the exact entity the player is looking at. This is the "best guess" calculated.
     
    ItsOneAndTwo and TheMintyMate like this.
  9. Offline

    ItsOneAndTwo

  10. Offline

    TheMintyMate

Thread Status:
Not open for further replies.

Share This Page