Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public LivingEntity getLookingAtEntity(Player p, int maxDistance) {
- List<LivingEntity> entities = p.getNearbyEntities(maxDistance, maxDistance, maxDistance).stream().filter(e -> e instanceof LivingEntity)
- .map(e -> (LivingEntity) e).collect(Collectors.toList());
- Iterator<Block> itr = new BlockIterator(p, maxDistance);
- Location entityLoc = new Location(null, 0, 0, 0);
- Location playerLoc = p.getLocation();
- Block block;
- int bx, by, bz;
- double ex, ey, ez;
- // 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.
- final double blockOffset = 0.5;
- final double vectorOffset = 0.325;
- final double heightOffset = 1;
- final double heightCheckOffset = 1;
- while (itr.hasNext()) {
- block = itr.next();
- if (block.getType().isSolid())
- break;
- bx = block.getX();
- by = block.getY();
- bz = block.getZ();
- for (LivingEntity entity : entities) {
- entity.getLocation(entityLoc);
- ex = entityLoc.getX();
- ey = entityLoc.getY();
- ez = entityLoc.getZ();
- if ((bx + 0.5 - blockOffset <= ex && bx + 0.5 + blockOffset >= ex) && (bz + 0.5 - blockOffset <= ez && bz + 0.5 + blockOffset >= ez)
- && (by - heightCheckOffset <= ey && by + heightCheckOffset >= ey)) {
- entityLoc.add(0, heightOffset, 0); // Possibly use entity.getEyeHeight(); instead of heightOffset
- // Screw readability...
- if (playerLoc.add(0, p.getEyeHeight(), 0).subtract(entityLoc).toVector().normalize().crossProduct(playerLoc.getDirection().normalize())
- .length() < vectorOffset)
- return entity;
- }
- }
- }
- return null;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement