Advertisement
Guest User

Untitled

a guest
Nov 30th, 2015
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. public LivingEntity getLookingAtEntity(Player p, int maxDistance) {
  2. List<LivingEntity> entities = p.getNearbyEntities(maxDistance, maxDistance, maxDistance).stream().filter(e -> e instanceof LivingEntity)
  3. .map(e -> (LivingEntity) e).collect(Collectors.toList());
  4.  
  5. Iterator<Block> itr = new BlockIterator(p, maxDistance);
  6.  
  7. Location entityLoc = new Location(null, 0, 0, 0);
  8. Location playerLoc = p.getLocation();
  9. Block block;
  10. int bx, by, bz;
  11. double ex, ey, ez;
  12. // These values will be different for each entity. They can probably be figured out by checking the dimensions of the bounding box for each entity.
  13. final double blockOffset = 0.5;
  14. final double vectorOffset = 0.325;
  15. final double heightOffset = 1;
  16. final double heightCheckOffset = 1;
  17. while (itr.hasNext()) {
  18. block = itr.next();
  19. if (block.getType().isSolid())
  20. break;
  21. bx = block.getX();
  22. by = block.getY();
  23. bz = block.getZ();
  24. for (LivingEntity entity : entities) {
  25. entity.getLocation(entityLoc);
  26.  
  27. ex = entityLoc.getX();
  28. ey = entityLoc.getY();
  29. ez = entityLoc.getZ();
  30.  
  31. if ((bx + 0.5 - blockOffset <= ex && bx + 0.5 + blockOffset >= ex) && (bz + 0.5 - blockOffset <= ez && bz + 0.5 + blockOffset >= ez)
  32. && (by - heightCheckOffset <= ey && by + heightCheckOffset >= ey)) {
  33. entityLoc.add(0, heightOffset, 0); // Possibly use entity.getEyeHeight(); instead of heightOffset
  34. // Screw readability...
  35. if (playerLoc.add(0, p.getEyeHeight(), 0).subtract(entityLoc).toVector().normalize().crossProduct(playerLoc.getDirection().normalize())
  36. .length() < vectorOffset)
  37. return entity;
  38. }
  39. }
  40. }
  41.  
  42. return null;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement