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

    DrAgonmoray

    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

    apiocera

    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

    bergerkiller

    Here ya go:
    Code:
        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

    Bone008

  5. Offline

    bergerkiller

    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

    apiocera

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

    bergerkiller

    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:
    Code:
    material.Sign s = block.getState();
    You would need to do this:
    Code:
    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:
    Code:
    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

    Bone008

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

    apiocera

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

    bergerkiller

  11. Offline

    moo3oo3oo3

    What about wall sign?
     
  12. Offline

    mythbusterma

    @moo3oo3oo3

    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

    ChipDev

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

    eyamaz

    [​IMG]

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

Share This Page