Skip to content

Commit

Permalink
Fix dLocation yaws. Add <location.facing[entity]> and <location.facin…
Browse files Browse the repository at this point in the history
…g[location]> tags.
  • Loading branch information
davidcernat committed Jul 14, 2013
1 parent 9412fb7 commit eca22ad
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 49 deletions.
7 changes: 7 additions & 0 deletions src/main/java/net/aufdemrand/denizen/objects/Element.java
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/aufdemrand/denizen/objects/dColor.java
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/net/aufdemrand/denizen/objects/dEntity.java
Expand Up @@ -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")) {
Expand Down
4 changes: 2 additions & 2 deletions 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;
Expand Down Expand Up @@ -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);
}
Expand Down
72 changes: 53 additions & 19 deletions src/main/java/net/aufdemrand/denizen/objects/dLocation.java
Expand Up @@ -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());
}

/**
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand All @@ -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));
Expand Down Expand Up @@ -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"))
Expand Down
12 changes: 0 additions & 12 deletions src/main/java/net/aufdemrand/denizen/objects/dPlayer.java
Expand Up @@ -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())
Expand Down
53 changes: 39 additions & 14 deletions src/main/java/net/aufdemrand/denizen/utilities/entity/Rotation.java
Expand Up @@ -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 ||
Expand All @@ -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);
}


/**
Expand Down

0 comments on commit eca22ad

Please sign in to comment.