Getting nearby blocks, depending on direction

Discussion in 'Plugin Development' started by anakhon, Dec 20, 2012.

Thread Status:
Not open for further replies.
  1. Offline



    New into bukkit plugins devs, i'm trying to make my own plugin, so, i'm sorry if my question is stupid or already asked, but i didn't find the answer anywhere here or on google... :(

    I'm trying to get the block on the right, or on the left of another reference block. It sounds probably easy, but I need to get the right one, or left one, depending on the direction a sign on the reference block is facing...

    Here is a little shape for example (A = air, B = block ) :

    front view (S = Sign in front of the hidden "reference" block):
    A B S B A

    top view (S = Sign):
    A B B B A
    A A S A A

    So, if i want to get the red Block which is on the right of the blue block (my "reference block" where the sign is placed) according to the direction the sign is facing.

    To get that block, I was going to make something like :
        public Block getRightBlockFromRef(Sign sign) {
            // get the direction the sign is facing
            BlockFace signFace = ((org.bukkit.material.Sign) sign.getData()).getFacing();
            // we get the block that support the sign
            Block refBlock = sign.getBlock().getRelative(signFace.getOppositeFace());
            // now check the direction and get the block depending on the direction
            if(signFace == BlockFace.EAST) {
                return refBlock.getRelative(BlockFace.SOUTH);           
            } else if(signFace == BlockFace.SOUTH) {
                return refBlock.getRelative(BlockFace.WEST);
            } else if(signFace == BlockFace.WEST) {
                return refBlock.getRelative(BlockFace.NORTH);
            } else { //if(signFace == BlockFace.NORTH)
                return refBlock.getRelative(BlockFace.EAST);
    It should work (I have not tested it yet, since i'm at work for now), but I was wondering if there was an easiest way to get it, instead of checking all four direction each time ... using angles or coords maybe ?

    Thanks for your help !

  2. Offline


    1. Vector mod = new Vector(bf.getModX(), bf.getModY(), bf.getModZ()).crossProduct(new Vector(0,1,0));
    2. Block left = block.getRelative(mod.getBlockX(), mod.getBlockY(), mod.getBlockZ());

    Assuming you have BlockFace bf and Block block. Not sure if it returns the left or right one (not even sure if you get the correct one, you should experiment with that).
  3. Offline


    Hmm. Yeah, using Vector could be easier (or maybe not lol). But I think i'll have to do a crossProduct depending on the direction... Or dunno.. thanks anyway, i'll check it :)
Thread Status:
Not open for further replies.

Share This Page