diff --git a/src/main/java/net/aufdemrand/denizen/objects/Element.java b/src/main/java/net/aufdemrand/denizen/objects/Element.java index a0b9bf4434..c78bd6f409 100644 --- a/src/main/java/net/aufdemrand/denizen/objects/Element.java +++ b/src/main/java/net/aufdemrand/denizen/objects/Element.java @@ -204,6 +204,13 @@ else if (element.toLowerCase().contains(contains.toLowerCase())) return new Element("true").getAttribute(attribute.fulfill(1)); else return new Element("false").getAttribute(attribute.fulfill(1)); } + + if (attribute.startsWith("after")) { + String delimiter = attribute.getContext(1); + return new Element(String.valueOf(element.substring + (element.indexOf(delimiter) + delimiter.length()))) + .getAttribute(attribute.fulfill(1)); + } if (attribute.startsWith("substring")) { // substring[2,8] int beginning_index = Integer.valueOf(attribute.getContext(1).split(",")[0]) - 1; diff --git a/src/main/java/net/aufdemrand/denizen/objects/dColor.java b/src/main/java/net/aufdemrand/denizen/objects/dColor.java index 5b7ae55d98..ee09cefb0d 100644 --- a/src/main/java/net/aufdemrand/denizen/objects/dColor.java +++ b/src/main/java/net/aufdemrand/denizen/objects/dColor.java @@ -12,7 +12,7 @@ public class dColor implements dObject { - final static Pattern rgbPattern = Pattern.compile("(\\d+),(\\d+),(\\d+)"); + final static Pattern rgbPattern = Pattern.compile("(\\d+)[,:](\\d+)[,:](\\d+)"); ////////////////// // OBJECT FETCHER diff --git a/src/main/java/net/aufdemrand/denizen/objects/dEntity.java b/src/main/java/net/aufdemrand/denizen/objects/dEntity.java index 68a95005b5..280eb6844c 100644 --- a/src/main/java/net/aufdemrand/denizen/objects/dEntity.java +++ b/src/main/java/net/aufdemrand/denizen/objects/dEntity.java @@ -739,8 +739,10 @@ public String getAttribute(Attribute attribute) { location.setYaw(location.getYaw() - 90); return location.getAttribute(attribute.fulfill(1)); } - else return new dLocation(entity.getLocation()) + else { + return new dLocation(entity.getLocation()) .getAttribute(attribute.fulfill(1)); + } } if (attribute.startsWith("health.formatted")) { diff --git a/src/main/java/net/aufdemrand/denizen/objects/dInventory.java b/src/main/java/net/aufdemrand/denizen/objects/dInventory.java index ab5e3b9a6c..fccbf1d6cb 100644 --- a/src/main/java/net/aufdemrand/denizen/objects/dInventory.java +++ b/src/main/java/net/aufdemrand/denizen/objects/dInventory.java @@ -1,6 +1,5 @@ package net.aufdemrand.denizen.objects; -import java.util.regex.Pattern; import org.bukkit.Bukkit; import org.bukkit.block.BlockState; import org.bukkit.entity.LivingEntity; @@ -462,7 +461,8 @@ public String getAttribute(Attribute attribute) { int qty = 1; if (attribute.getAttribute(2).startsWith("qty") && - attribute.hasContext(2) && aH.matchesInteger(attribute.getContext(2))) { + attribute.hasContext(2) && + aH.matchesInteger(attribute.getContext(2))) { qty = attribute.getIntContext(2); } diff --git a/src/main/java/net/aufdemrand/denizen/objects/dLocation.java b/src/main/java/net/aufdemrand/denizen/objects/dLocation.java index 1794e9ccb6..646c9fd9f4 100644 --- a/src/main/java/net/aufdemrand/denizen/objects/dLocation.java +++ b/src/main/java/net/aufdemrand/denizen/objects/dLocation.java @@ -208,14 +208,11 @@ public static boolean matches(String string) { * @param location the Bukkit Location to reference */ public dLocation(Location location) { - super(location.getWorld(), location.getX(), location.getY(), location.getZ()); - // If supplied location has pitch/yaw, set it. - if (location.getPitch() > 0f - && location.getYaw() > 0f) { - hasPitchYaw = true; - this.setPitch(location.getPitch()); - this.setYaw(location.getYaw()); - } + // Just save the yaw and pitch as they are; don't check if they are + // higher than 0, because Minecraft yaws are weird and can have + // negative values + super(location.getWorld(), location.getX(), location.getY(), location.getZ(), + location.getYaw(), location.getPitch()); } /** @@ -235,27 +232,18 @@ public dLocation(World world, double x, double y, double z) { public dLocation(World world, double x, double y, double z, float yaw, float pitch) { super(world, x, y, z, pitch, yaw); - hasPitchYaw = true; } - boolean hasPitchYaw = false; - @Override public void setPitch(float pitch) { - hasPitchYaw = true; super.setPitch(pitch); } @Override public void setYaw(float yaw) { - hasPitchYaw = true; super.setYaw(yaw); } - public boolean hasPitchYaw() { - return hasPitchYaw; - } - public dLocation rememberAs(String id) { dLocation.saveAs(this, id); return this; @@ -297,7 +285,7 @@ public boolean isUnique() { public String identify() { if (isSaved(this)) return "l@" + getSaved(this); - else if (hasPitchYaw()) return "l@" + getX() + "," + getY() + else if (getYaw() != 0.0 && getPitch() != 0.0) return "l@" + getX() + "," + getY() + "," + getZ() + "," + getPitch() + "," + getYaw() + "," + getWorld().getName(); else return "l@" + getX() + "," + getY() + "," + getZ() + "," + getWorld().getName(); @@ -438,6 +426,7 @@ else if (attribute.startsWith("surface_blocks") return new Element(getBlock().getType().toString()).getAttribute(attribute.fulfill(2)); if (attribute.startsWith("direction")) { + // Get the cardinal direction from this location to another if (attribute.hasContext(1) && dLocation.matches(attribute.getContext(1))) { // Subtract this location's vector from the other location's vector, // not the other way around @@ -446,6 +435,7 @@ else if (attribute.startsWith("surface_blocks") .normalize()))) .getAttribute(attribute.fulfill(1)); } + // Get a cardinal direction from this location's yaw else { return new Element(Rotation.getCardinal(getYaw())) .getAttribute(attribute.fulfill(1)); @@ -517,8 +507,52 @@ else return new Element(String.valueOf(this.distance(toLocation))) return new Element(String.valueOf(getPitch())).getAttribute(attribute.fulfill(1)); } + // Get the raw yaw of this location + if (attribute.startsWith("yaw.raw")) { + return new Element(String.valueOf + (getYaw())).getAttribute(attribute.fulfill(2)); + } + + // Provide a normalized yaw that people can actually make use of, + // instead of Minecraft's weird yaws that can have negative values + // or exceed 360 if (attribute.startsWith("yaw")) { - return new Element(String.valueOf(getYaw())).getAttribute(attribute.fulfill(1)); + return new Element(String.valueOf + (Rotation.normalizeYaw(getYaw()))).getAttribute(attribute.fulfill(1)); + } + + // Check if this location's yaw is facing another location or entity + if (attribute.startsWith("facing")) { + if (attribute.hasContext(1)) { + + // The default number of degrees if there is no degrees attribute + int degrees = 45; + + // The attribute to fulfill from + int attributePos = 1; + + // If there is a degrees attribute with an integer context, + // set degrees to the value in that context + if (attribute.getAttribute(2).startsWith("degrees") && + attribute.hasContext(2) && + aH.matchesInteger(attribute.getContext(2))) { + + degrees = attribute.getIntContext(2); + attributePos++; + } + + if (dLocation.matches(attribute.getContext(1))) { + return new Element(Rotation.isFacingLocation + (this, dLocation.valueOf(attribute.getContext(1)), degrees)) + .getAttribute(attribute.fulfill(attributePos)); + } + else if (dEntity.matches(attribute.getContext(1))) { + return new Element(Rotation.isFacingLocation + (this, dEntity.valueOf(attribute.getContext(1)) + .getBukkitEntity().getLocation(), degrees)) + .getAttribute(attribute.fulfill(attributePos)); + } + } } if (attribute.startsWith("power")) diff --git a/src/main/java/net/aufdemrand/denizen/objects/dPlayer.java b/src/main/java/net/aufdemrand/denizen/objects/dPlayer.java index a126d9d20e..64772c5d01 100644 --- a/src/main/java/net/aufdemrand/denizen/objects/dPlayer.java +++ b/src/main/java/net/aufdemrand/denizen/objects/dPlayer.java @@ -345,18 +345,6 @@ else if (attribute.startsWith("list.offline")) { if (attribute.startsWith("inventory")) return new dInventory(getPlayerEntity().getInventory()) .getAttribute(attribute.fulfill(1)); - - // Estimate the location of the item the player is holding - if (attribute.startsWith("item_in_hand.location")) { - - dLocation location = new dLocation(getPlayerEntity().getLocation()); - //location.setYaw((float) Rotation.normalizeYaw(Rotation.getYaw(location.getDirection()))); - location.setYaw(location.getYaw() + 30); - //location.add(location.getDirection().multiply(1.2)); - getPlayerEntity().teleport(location); - - return location.getAttribute(attribute.fulfill(2)); - } if (attribute.startsWith("item_in_hand")) return new dItem(getPlayerEntity().getItemInHand()) diff --git a/src/main/java/net/aufdemrand/denizen/utilities/entity/Rotation.java b/src/main/java/net/aufdemrand/denizen/utilities/entity/Rotation.java index e3ac0712fc..e5ca13104f 100644 --- a/src/main/java/net/aufdemrand/denizen/utilities/entity/Rotation.java +++ b/src/main/java/net/aufdemrand/denizen/utilities/entity/Rotation.java @@ -117,30 +117,30 @@ public static void faceEntity(Entity entity, Entity target) { faceLocation(entity, target.getLocation()); } - + /** - * Checks if an Entity is facing a Location. + * Checks if a Location's yaw is facing another Location. + * + * Note: do not use a player's location as the first argument, + * because player yaws need to modified. Use the method + * below this one instead. * - * @param from The Entity we check. - * @param at The Location we want to know if it is looking at. + * @param from The Location we check. + * @param at The Location we want to know if the first Location's yaw + * is facing * @param degreeLimit How many degrees can be between the direction the - * Entity is facing and the direction we check if it - * is facing. + * first location's yaw is facing and the direction + * we check if it is facing. * * @return Returns a boolean. */ - public static boolean isFacingLocation(Entity from, Location at, float degreeLimit) { + public static boolean isFacingLocation(Location from, Location at, float degreeLimit) { - double currentYaw; - - if (from instanceof Player) // need to subtract 90 from player yaws - currentYaw = normalizeYaw(from.getLocation().getYaw() - 90); - else - currentYaw = normalizeYaw(from.getLocation().getYaw()); + double currentYaw = normalizeYaw(from.getYaw()); double requiredYaw = normalizeYaw(getYaw(at.toVector().subtract( - from.getLocation().toVector()).normalize())); + from.toVector()).normalize())); if (Math.abs(requiredYaw - currentYaw) < degreeLimit || Math.abs(requiredYaw + 360 - currentYaw) < degreeLimit || @@ -149,6 +149,31 @@ public static boolean isFacingLocation(Entity from, Location at, float degreeLim return false; } + + + /** + * Checks if an Entity is facing a Location. + * + * @param from The Entity we check. + * @param at The Location we want to know if it is looking at. + * @param degreeLimit How many degrees can be between the direction the + * Entity is facing and the direction we check if it + * is facing. + * + * @return Returns a boolean. + */ + + public static boolean isFacingLocation(Entity from, Location at, float degreeLimit) { + + Location location = from.getLocation(); + + // Important! Need to subtract 90 from player yaws + if (from instanceof Player) { + location.setYaw(location.getYaw() - 90); + } + + return isFacingLocation(from, at, degreeLimit); + } /**