Efficient way of getting the block a wall-sign is attached to?

Discussion in 'Plugin Development' started by DrAgonmoray, Jul 24, 2011.

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


    Could anybody tell me what the most efficient way to get the block a wall sign is attached to? I'm a little bit stumped. :|
  2. Offline


    I would use sign.getData() and then sign.getRelative(org.bukkit.block.BlockFace) depending on data value:
    0x2: Facing east -> BlockFace.WEST
    0x3: Facing west -> BlockFace.EAST
    0x4: Facing north -> BlockFace.SOUTH
    0x5: Facing south -> BlockFace.NORTH
  3. Offline


    Here ya go:
        public static Block getAttachedBlock(Block b) {
            MaterialData m = b.getState().getData();
            BlockFace face = BlockFace.DOWN;
            if (m instanceof Attachable) {
                face = ((Attachable) m).getAttachedFace();
            return b.getRelative(face);
    Just pass your sign block into this function and it returns the block the input block is attached to.
  4. Offline


  5. Offline


    I win. :D Neh jk, but a Sign inherits from Attachable, so it is better and more efficient to cast to Attachable and use the function of this. It also works for levers, buttons, torches, etc. this way.
  6. Offline


    LOL, I need "inexperienced developer of the year" award :]
    Wouldn't it be simpler to so just sign.getRelative(sign.getAttachedFace()) ?
  7. Offline


    Nope, there are two types of Signs. One material.Sign and one block.Sign. You use block.Sign for regular stuff like lines. The material.Sign is hardly ever used. This is not possible, for example:
    material.Sign s = block.getState();
    You would need to do this:
    block.Sign s = block.getState();
    org.bukkit.material.Sign ms = (org.bukkit.material.Sign) s.getData();
    Block attached = block.getRelative(ms.getAttachedFace());
    This is also possible, but material.Sign has no other uses. It is far easier to stick with block.Sign and leave the attachedFace feature to a separate function. Where else would you use the attached face other than get an attached Block? :)

    Unless you plan not to read the Sign at all later on, and only need to handle the Block, you can stick with this:
    Block b = ??;
    Block attached = b.getRelative(((org.bukkit.material.Sign) b.getState().getData()).getAttachedFace());
    But as you see it is quite lengthy. I rather stick with one versatile "it can handle all" function than adding multiple of those lines. :)
    Pandemoneus likes this.
  8. Offline


    Oh well, I didn't see it was was the "wrong" sign-class inheriting from Attachable.
  9. Offline


    @bergerkiller Whoa. Thanks for the answer, I didn't expect it to be that elucidative :) and I feel a bit stupid now :)
  10. Offline


  11. Offline


    What about wall sign?
  12. Offline



    You already posted in another thread concerning this topic, did you really need to thread necro a thread that is 3 and a half years old with a more or less duplicate post?
  13. Offline


    I didn't look into the date until you said that.
  14. Offline



    Thread is now locked
    Jaaakee224 likes this.
Thread Status:
Not open for further replies.

Share This Page