From 7247475743b89b378585367a6e1d30788c68c4ca Mon Sep 17 00:00:00 2001 From: mcmonkey Date: Mon, 26 Nov 2018 22:13:12 -0800 Subject: [PATCH] add location.facing pitch degrees, fixes #1485 --- .../denizen/nms/interfaces/EntityHelper.java | 19 +++++++++++- .../aufdemrand/denizen/objects/dLocation.java | 31 +++++++++++++++---- .../nms/helpers/EntityHelper_v1_10_R1.java | 9 ------ .../nms/helpers/EntityHelper_v1_11_R1.java | 9 ------ .../nms/helpers/EntityHelper_v1_12_R1.java | 9 ------ .../nms/helpers/EntityHelper_v1_13_R2.java | 9 ------ .../nms/helpers/EntityHelper_v1_8_R3.java | 9 ------ .../nms/helpers/EntityHelper_v1_9_R2.java | 9 ------ 8 files changed, 43 insertions(+), 61 deletions(-) diff --git a/nmshandler/src/main/java/net/aufdemrand/denizen/nms/interfaces/EntityHelper.java b/nmshandler/src/main/java/net/aufdemrand/denizen/nms/interfaces/EntityHelper.java index eea164ceaa..7167c451e2 100644 --- a/nmshandler/src/main/java/net/aufdemrand/denizen/nms/interfaces/EntityHelper.java +++ b/nmshandler/src/main/java/net/aufdemrand/denizen/nms/interfaces/EntityHelper.java @@ -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. @@ -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. *

diff --git a/plugin/src/main/java/net/aufdemrand/denizen/objects/dLocation.java b/plugin/src/main/java/net/aufdemrand/denizen/objects/dLocation.java index 0f5a379531..81d1d2c463 100644 --- a/plugin/src/main/java/net/aufdemrand/denizen/objects/dLocation.java +++ b/plugin/src/main/java/net/aufdemrand/denizen/objects/dLocation.java @@ -1097,18 +1097,37 @@ else if (getBlock().getType() == Material.FLOWER_POT) { int attributePos = 1; // <--[tag] - // @attribute /].degrees[<#>]> + // @attribute /].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))) { diff --git a/v1_10_R1/src/main/java/net/aufdemrand/denizen/nms/helpers/EntityHelper_v1_10_R1.java b/v1_10_R1/src/main/java/net/aufdemrand/denizen/nms/helpers/EntityHelper_v1_10_R1.java index a280d9895d..8f4032acbe 100644 --- a/v1_10_R1/src/main/java/net/aufdemrand/denizen/nms/helpers/EntityHelper_v1_10_R1.java +++ b/v1_10_R1/src/main/java/net/aufdemrand/denizen/nms/helpers/EntityHelper_v1_10_R1.java @@ -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()) { diff --git a/v1_11_R1/src/main/java/net/aufdemrand/denizen/nms/helpers/EntityHelper_v1_11_R1.java b/v1_11_R1/src/main/java/net/aufdemrand/denizen/nms/helpers/EntityHelper_v1_11_R1.java index 1ab2914e78..42aa37fdbc 100644 --- a/v1_11_R1/src/main/java/net/aufdemrand/denizen/nms/helpers/EntityHelper_v1_11_R1.java +++ b/v1_11_R1/src/main/java/net/aufdemrand/denizen/nms/helpers/EntityHelper_v1_11_R1.java @@ -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()) { diff --git a/v1_12_R1/src/main/java/net/aufdemrand/denizen/nms/helpers/EntityHelper_v1_12_R1.java b/v1_12_R1/src/main/java/net/aufdemrand/denizen/nms/helpers/EntityHelper_v1_12_R1.java index 5cea13ba42..e4203cf880 100644 --- a/v1_12_R1/src/main/java/net/aufdemrand/denizen/nms/helpers/EntityHelper_v1_12_R1.java +++ b/v1_12_R1/src/main/java/net/aufdemrand/denizen/nms/helpers/EntityHelper_v1_12_R1.java @@ -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()) { diff --git a/v1_13_R2/src/main/java/net/aufdemrand/denizen/nms/helpers/EntityHelper_v1_13_R2.java b/v1_13_R2/src/main/java/net/aufdemrand/denizen/nms/helpers/EntityHelper_v1_13_R2.java index ab2d092533..f548aa7100 100644 --- a/v1_13_R2/src/main/java/net/aufdemrand/denizen/nms/helpers/EntityHelper_v1_13_R2.java +++ b/v1_13_R2/src/main/java/net/aufdemrand/denizen/nms/helpers/EntityHelper_v1_13_R2.java @@ -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()) { diff --git a/v1_8_R3/src/main/java/net/aufdemrand/denizen/nms/helpers/EntityHelper_v1_8_R3.java b/v1_8_R3/src/main/java/net/aufdemrand/denizen/nms/helpers/EntityHelper_v1_8_R3.java index 5cba7acefe..bb53a371ff 100644 --- a/v1_8_R3/src/main/java/net/aufdemrand/denizen/nms/helpers/EntityHelper_v1_8_R3.java +++ b/v1_8_R3/src/main/java/net/aufdemrand/denizen/nms/helpers/EntityHelper_v1_8_R3.java @@ -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()) { diff --git a/v1_9_R2/src/main/java/net/aufdemrand/denizen/nms/helpers/EntityHelper_v1_9_R2.java b/v1_9_R2/src/main/java/net/aufdemrand/denizen/nms/helpers/EntityHelper_v1_9_R2.java index 8b86b7d8e1..1f84c21fa7 100644 --- a/v1_9_R2/src/main/java/net/aufdemrand/denizen/nms/helpers/EntityHelper_v1_9_R2.java +++ b/v1_9_R2/src/main/java/net/aufdemrand/denizen/nms/helpers/EntityHelper_v1_9_R2.java @@ -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()) {