From 493854977711f2834ac861bc36c8218f2010b7d5 Mon Sep 17 00:00:00 2001 From: CraftBukkit/Spigot Date: Fri, 7 Jan 2011 10:43:51 +0000 Subject: [PATCH] Added block.getFace(Block) and block.getFace(BlockFace, int) By: Dinnerbone --- .../org/bukkit/craftbukkit/CraftBlock.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftBlock.java b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftBlock.java index 19dce3c70e..4048434a38 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftBlock.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftBlock.java @@ -156,6 +156,26 @@ public class CraftBlock implements Block { return getRelative(face.getModX(), face.getModY(), face.getModZ()); } + /** + * Gets the block at the given distance of the given face
+ *
+ * For example, the following method places water at 100,102,100; two blocks + * above 100,100,100. + *
+     * Block block = world.getBlockAt(100,100,100);
+     * Block shower = block.getFace(BlockFace.Up, 2);
+     * shower.setType(Material.WATER);
+     * 
+ * + * @param face Face of this block to return + * @param distance Distance to get the block at + * @return Block at the given face + */ + public Block getFace(final BlockFace face, final int distance) { + return getRelative(face.getModX() * distance, face.getModY() * distance, + face.getModZ() * distance); + } + /** * Gets the block at the given offsets * @@ -168,6 +188,38 @@ public class CraftBlock implements Block { return getWorld().getBlockAt(getX() + modX, getY() + modY, getZ() + modZ); } + /** + * Gets the face relation of this block compared to the given block
+ *
+ * For example: + *
+     * Block current = world.getBlockAt(100, 100, 100);
+     * Block target = world.getBlockAt(100, 101, 100);
+     *
+     * current.getFace(target) == BlockFace.Up;
+     * 
+ *
+ * If the given block is not connected to this block, null may be returned + * + * @param block Block to compare against this block + * @return BlockFace of this block which has the requested block, or null + */ + public BlockFace getFace(final Block block) { + BlockFace[] values = BlockFace.values(); + + for (BlockFace face : values) { + if ( + (this.getX() + face.getModX() == block.getX()) && + (this.getY() + face.getModY() == block.getY()) && + (this.getZ() + face.getModZ() == block.getZ()) + ) { + return face; + } + } + + return null; + } + @Override public String toString() { return "CraftBlock{" + "world=" + world + "x=" + x + "y=" + y + "z=" + z + "type=" + type + "data=" + data + '}';