Skip to content

Commit

Permalink
add tag location.vector_to_face
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Mar 30, 2019
1 parent a1ca71a commit 0d5579c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
18 changes: 18 additions & 0 deletions plugin/src/main/java/net/aufdemrand/denizen/objects/dLocation.java
Expand Up @@ -1982,6 +1982,24 @@ else if (dLocation.matches(attribute.getContext(1))) {
.getAttribute(attribute.fulfill(1));
}

// <--[tag]
// @attribute <l@location.vector_to_face>
// @returns Element
// @description
// Returns the name of the BlockFace represented by a vector.
// Result can be any of the following:
// NORTH, EAST, SOUTH, WEST, UP, DOWN, NORTH_EAST, NORTH_WEST, SOUTH_EAST, SOUTH_WEST,
// WEST_NORTH_WEST, NORTH_NORTH_WEST, NORTH_NORTH_EAST, EAST_NORTH_EAST, EAST_SOUTH_EAST,
// SOUTH_SOUTH_EAST, SOUTH_SOUTH_WEST, WEST_SOUTH_WEST, SELF
// -->
if (attribute.startsWith("vector_to_face")) {
BlockFace face = Utilities.faceFor(toVector());
if (face != null) {
return new Element(face.name())
.getAttribute(attribute.fulfill(1));
}
}

// <--[tag]
// @attribute <l@location.distance_squared[<location>]>
// @returns Element(Decimal)
Expand Down
Expand Up @@ -23,6 +23,7 @@
import org.bukkit.block.Sign;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -128,6 +129,15 @@ public static boolean isSafeFile(File f) {
}
}

public static BlockFace faceFor(Vector vec) {
for (BlockFace face : BlockFace.values()) {
if (face.getDirection().distanceSquared(vec) < 0.01) { // floating-point safe check
return face;
}
}
return null;
}

/**
* Gets a Location within a range that an entity can walk in.
*
Expand Down
@@ -1,5 +1,6 @@
package net.aufdemrand.denizen.utilities.blocks;

import net.aufdemrand.denizen.utilities.Utilities;
import net.aufdemrand.denizen.utilities.debugging.dB;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
Expand Down Expand Up @@ -35,15 +36,6 @@ public static Vector getFacing(Block b) {
return null;
}

public static BlockFace faceFor(Vector vec) {
for (BlockFace face : BlockFace.values()) {
if (face.getDirection().distanceSquared(vec) < 0.01) { // floating-point safe check
return face;
}
}
return null;
}

public static void setFace(Block b, BlockFace face) {
if (b.getBlockData() instanceof Directional) {
Directional dir = (Directional) b.getBlockData();
Expand All @@ -58,7 +50,7 @@ else if (b.getBlockData() instanceof Rotatable) {
}

public static void setFacing(Block b, Vector faceVec) {
BlockFace newFace = faceFor(faceVec);
BlockFace newFace = Utilities.faceFor(faceVec);
if (newFace == null) {
dB.echoError("Direction '" + faceVec + "' does not appear to be a valid block face.");
return;
Expand Down

0 comments on commit 0d5579c

Please sign in to comment.