# Solved Find coords 10 blocks infront of direction looking

Discussion in 'Spigot Plugin Development' started by brian310, Dec 30, 2019.

Not open for further replies.
1. ### brian310

Hey anyone know how I could possibly get the coordinates of a point 10 blocks infront of where a armorstand's head is looking?

2. ### KuramaStone

Player#getLineOfSight() will work for this. You can use null for the first argument (just cast to Set<Material>) and the second arg is the range.

Another possibility if you want to be more DIY is to get the eye location of the player, get the direction, get the block X meters away, and make a loop to get every block in that line until you reach Y blocks away.

3. ### brian310

getlineofsight wont work as that only gets the block coords, It wouldn't be able to get precise cords like 12.8 5.2 23.0 & also I'm using armorstands not players.

4. ### Fr33styler

p.getlocation get direction multiply 10

5. ### 7smile7

Code (Java):
public Location getLooking(LivingEntity livingEntity, double distance) {
// Getting eye location
Location eyeLocation = livingEntity.getEyeLocation();
// Get dir the eyes are looking at
Vector direction = eyeLocation.getDirection();
// Normalize (set lenght to 1) then scale
Vector scaledDir = direction.clone().normalize().multiply(distance);
// Adding scaled direction to eye location
World world = eyeLocation.getWorld();
double x = scaledDir.getX() + eyeLocation.getX();
double y = scaledDir.getY() + eyeLocation.getY();
double z = scaledDir.getZ() + eyeLocation.getZ();

return new Location(world, x, y, z);
}
PS if you want the first Block or Entity in this line you can just raytrace.

• Like x 1
6. ### Fr33styler

both getEyeLocation, getDirection creates a new object, I would just reuse them whenever possible like this:
Code (Text):
Location eyeLocation = p.getEyeLocation();
Vector direction = eyeLocation.getDirection();

7. ### Dirantos

You don't need to normalize the direction since Location#getDirection returns an unit vector

8. ### Fr33styler

Oh, good to know

9. ### brian310

That works for player but for armorstands it just gets the rotation of it instead of the head pose for some reason.

10. ### 7smile7

1) I'm only creating exactly one more Object (Pointers aren't new Objects just new references)
2) Its bad practice to modify hard linked objects like an entities Location. (Danger of teleporting the entity etc)
3) this is a extra detailed example to comment my steps

11. ### Fr33styler

actually no, the api does not give you access to linked objects and instead when you call getLocation it creates a new object like this:

Code (Text):
public Location getLocation() {
return new Location(getWorld(), this.entity.locX(), this.entity.locY(), this.entity.locZ(), this.entity.getBukkitYaw(), this.entity.pitch);
}
same for getDirection()
if you don't believe me then look into the craftbukkit yourself or you can always call 2 getLocations and see if they are ==

12. ### Fr33styler

Do you have the yaw and pitch of the armorstand head?

Yes.

14. ### 7smile7

Huh.. you are right.
Since when?

Still. I only call it the same amount you do ^^

15. ### Fr33styler

then just do this for direction tho i'm pretty sure it should work as is for armorstands:
Code (Text):
Vector vector = new Vector();

double rotX = //yaw
double rotY = //pitch

#15
Last edited: Dec 30, 2019
• Winner x 1
16. ### Fr33styler

the first time i checked was in 1.7.10 but i think it was always like this to prevent what you said earlier

17. ### brian310

Thanks that worked.