Skip to content

Commit

Permalink
add location.facing pitch degrees, fixes #1485
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Nov 27, 2018
1 parent 46e9d8b commit 7247475
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,13 @@ class MapTraceResult {
*/
Location eyeTrace(LivingEntity from, double range);

Location faceLocation(Location from, Location at);
default Location faceLocation(Location from, Location at) {
Vector direction = from.toVector().subtract(at.toVector()).normalize();
Location newLocation = from.clone();
newLocation.setYaw(180 - (float) Math.toDegrees(Math.atan2(direction.getX(), direction.getZ())));
newLocation.setPitch(90 - (float) Math.toDegrees(Math.acos(direction.getY())));
return newLocation;
}

/**
* Changes an entity's yaw and pitch to make it face a location.
Expand All @@ -131,6 +137,17 @@ class MapTraceResult {
*/
void faceEntity(Entity entity, Entity target);

default boolean isFacingLocation(Location from, Location at, float yawLimitDegrees, float pitchLimitDegrees) {
Vector direction = from.toVector().subtract(at.toVector()).normalize();
float pitch = 90 - (float) Math.toDegrees(Math.acos(direction.getY()));
if (from.getPitch() > pitch + pitchLimitDegrees
|| from.getPitch() < pitch - pitchLimitDegrees) {
return false;
}

return isFacingLocation(from, at, yawLimitDegrees);
}

/**
* Checks if a Location's yaw is facing another Location.
* <p/>
Expand Down
31 changes: 25 additions & 6 deletions plugin/src/main/java/net/aufdemrand/denizen/objects/dLocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -1097,18 +1097,37 @@ else if (getBlock().getType() == Material.FLOWER_POT) {
int attributePos = 1;

// <--[tag]
// @attribute <l@location.facing[<entity>/<location>].degrees[<#>]>
// @attribute <l@location.facing[<entity>/<location>].degrees[<#>(,<#>)]>
// @returns Element(Boolean)
// @description
// Returns whether the location's yaw is facing another
// entity or location, within a specified degree range.
// Optionally specify a pitch limit as well.
// -->
if (attribute.getAttribute(2).startsWith("degrees") &&
attribute.hasContext(2) &&
aH.matchesInteger(attribute.getContext(2))) {

degrees = attribute.getIntContext(2);
attributePos++;
attribute.hasContext(2)) {
String context = attribute.getContext(2);
if (context.contains(",")) {
String yaw = context.substring(0, context.indexOf(','));
String pitch = context.substring(context.indexOf(',') + 1);
degrees = aH.getIntegerFrom(yaw);
int pitchDegrees = aH.getIntegerFrom(pitch);
if (dLocation.matches(attribute.getContext(1))) {
return new Element(NMSHandler.getInstance().getEntityHelper().isFacingLocation
(this, dLocation.valueOf(attribute.getContext(1)), degrees, pitchDegrees))
.getAttribute(attribute.fulfill(attributePos));
}
else if (dEntity.matches(attribute.getContext(1))) {
return new Element(NMSHandler.getInstance().getEntityHelper().isFacingLocation
(this, dEntity.valueOf(attribute.getContext(1))
.getBukkitEntity().getLocation(), degrees, pitchDegrees))
.getAttribute(attribute.fulfill(attributePos));
}
}
else {
degrees = attribute.getIntContext(2);
attributePos++;
}
}

if (dLocation.matches(attribute.getContext(1))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -501,15 +501,6 @@ public Location eyeTrace(LivingEntity from, double range) {
return rayTrace(start, new Vector(nx, -ny, nz), range);
}

@Override
public Location faceLocation(Location from, Location at) {
Vector direction = from.toVector().subtract(at.toVector()).normalize();
Location newLocation = from.clone();
newLocation.setYaw(180 - (float) Math.toDegrees(Math.atan2(direction.getX(), direction.getZ())));
newLocation.setPitch(90 - (float) Math.toDegrees(Math.acos(direction.getY())));
return newLocation;
}

@Override
public void faceLocation(Entity from, Location at) {
if (from.getWorld() != at.getWorld()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -500,15 +500,6 @@ public Location eyeTrace(LivingEntity from, double range) {
return rayTrace(start, new Vector(nx, -ny, nz), range);
}

@Override
public Location faceLocation(Location from, Location at) {
Vector direction = from.toVector().subtract(at.toVector()).normalize();
Location newLocation = from.clone();
newLocation.setYaw(180 - (float) Math.toDegrees(Math.atan2(direction.getX(), direction.getZ())));
newLocation.setPitch(90 - (float) Math.toDegrees(Math.acos(direction.getY())));
return newLocation;
}

@Override
public void faceLocation(Entity from, Location at) {
if (from.getWorld() != at.getWorld()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -632,15 +632,6 @@ public Location eyeTrace(LivingEntity from, double range) {
return rayTrace(start, new Vector(nx, -ny, nz), range);
}

@Override
public Location faceLocation(Location from, Location at) {
Vector direction = from.toVector().subtract(at.toVector()).normalize();
Location newLocation = from.clone();
newLocation.setYaw(180 - (float) Math.toDegrees(Math.atan2(direction.getX(), direction.getZ())));
newLocation.setPitch(90 - (float) Math.toDegrees(Math.acos(direction.getY())));
return newLocation;
}

@Override
public void faceLocation(Entity from, Location at) {
if (from.getWorld() != at.getWorld()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -632,15 +632,6 @@ public Location eyeTrace(LivingEntity from, double range) {
return rayTrace(start, new Vector(nx, -ny, nz), range);
}

@Override
public Location faceLocation(Location from, Location at) {
Vector direction = from.toVector().subtract(at.toVector()).normalize();
Location newLocation = from.clone();
newLocation.setYaw(180 - (float) Math.toDegrees(Math.atan2(direction.getX(), direction.getZ())));
newLocation.setPitch(90 - (float) Math.toDegrees(Math.acos(direction.getY())));
return newLocation;
}

@Override
public void faceLocation(Entity from, Location at) {
if (from.getWorld() != at.getWorld()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -501,15 +501,6 @@ public Location eyeTrace(LivingEntity from, double range) {
return rayTrace(start, new Vector(nx, -ny, nz), range);
}

@Override
public Location faceLocation(Location from, Location at) {
Vector direction = from.toVector().subtract(at.toVector()).normalize();
Location newLocation = from.clone();
newLocation.setYaw(180 - (float) Math.toDegrees(Math.atan2(direction.getX(), direction.getZ())));
newLocation.setPitch(90 - (float) Math.toDegrees(Math.acos(direction.getY())));
return newLocation;
}

@Override
public void faceLocation(Entity from, Location at) {
if (from.getWorld() != at.getWorld()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -501,15 +501,6 @@ public Location eyeTrace(LivingEntity from, double range) {
return rayTrace(start, new Vector(nx, -ny, nz), range);
}

@Override
public Location faceLocation(Location from, Location at) {
Vector direction = from.toVector().subtract(at.toVector()).normalize();
Location newLocation = from.clone();
newLocation.setYaw(180 - (float) Math.toDegrees(Math.atan2(direction.getX(), direction.getZ())));
newLocation.setPitch(90 - (float) Math.toDegrees(Math.acos(direction.getY())));
return newLocation;
}

@Override
public void faceLocation(Entity from, Location at) {
if (from.getWorld() != at.getWorld()) {
Expand Down

0 comments on commit 7247475

Please sign in to comment.