Skip to content

Commit

Permalink
add block_facing mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jan 16, 2019
1 parent b2fa22e commit 2cfecbb
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions plugin/src/main/java/net/aufdemrand/denizen/objects/dLocation.java
Expand Up @@ -2167,6 +2167,15 @@ public void applyProperty(Mechanism mechanism) {
dB.echoError("Cannot apply properties to a location!");
}

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;
}

@Override
public void adjust(Mechanism mechanism) {

Expand All @@ -2178,6 +2187,28 @@ public void adjust(Mechanism mechanism) {
blockData.setBlock(getBlock(), false);
}

// <--[mechanism]
// @object dLocation
// @name block_facing
// @input dLocation
// @description
// Sets the facing direction of the block, as a vector.
// @tags
// <l@location.block_facing>
// -->
if (mechanism.matches("block_facing") && mechanism.requireObject(dLocation.class)) {
dLocation faceVec = value.asType(dLocation.class);
if (getBlock().getBlockData() instanceof Directional) {
Directional dir = (Directional) getBlock().getBlockData();
BlockFace newFace = faceFor(faceVec.toVector());
if (newFace == null) {
dB.echoError("Direction '" + faceVec + "' does not appear to be a valid block face.");
}
dir.setFacing(newFace);
getBlock().setBlockData(dir);
}
}

// <--[mechanism]
// @object dLocation
// @name block_type
Expand Down

0 comments on commit 2cfecbb

Please sign in to comment.