Get block behind sign

Discussion in 'Plugin Development' started by Numenorean95, Sep 26, 2011.

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

    Numenorean95

    Hi,
    I want to get the block that a sign is placed on top of (When placed on the side of a block). Does anyone know how to do this?
    I tried this:
    Code:
            Block back;
            int x = block.getX();
            int y = block.getY();
            int z = block.getZ();
            if ((back =block.getChunk().getBlock(x - 1, y, z)).getTypeId() == id){
                return back;
            } else if ((back = block.getChunk().getBlock(x + 1, y, z)).getTypeId() == id){
                return back;
            } else if ((back = block.getChunk().getBlock(x, y + 1, z)).getTypeId() == id){
                return back;
            } else if ((back = block.getChunk().getBlock(x, y - 1, z)).getTypeId() == id){
                return back;
            }
    But realised it would fail if there was a block next to the sign. Any ideas?
     
  2. Offline

    dbizzzle

    would this be onBlockPlace or just the block behind all signs?
     
  3. Offline

    Numenorean95

    I have a sign, it is irrelevant where it comes from. However, the event is onSignChange
     
  4. Offline

    dbizzzle

    @Numenorean95 What are you planning on doing once it gets the block?
     
  5. Offline

    desht

    Code:java
    1.  
    2. if (block.getType() == Material.SIGN_POST || b.getType() == Material.WALL_SIGN) {
    3. Sign s = (Sign) block.getState().getData();
    4. Block attachedBlock = b.getRelative(s.getAttachedFace());
    5. // ...
    6. }
    7.  


    That gets the block that a sign is attached to, either a wall for a wall sign, or the floor for a floor sign.
     
  6. Offline

    Numenorean95

    You are brilliant, thank you!
    Checking the type to see if it is the correct block.

    Hmm, i just tried it, it doesnt recognize "s.getAttachedFace". What version of bukkit are you running?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
  7. Offline

    desht

    The most up-to-date, whatever that is, but that part of the API hasn't changed in a long time.

    Make sure you have the right Sign import (org.bukkit.material.Sign, not org.bukkit.block.Sign).
     
  8. Offline

    daemitus

    Code:
        public static Block getBlockSignAttachedTo(Block block) {
            if (block.getType().equals(Material.WALL_SIGN))
                switch (block.getData()) {
                    case 2:
                        return block.getRelative(BlockFace.WEST);
                    case 3:
                        return block.getRelative(BlockFace.EAST);
                    case 4:
                        return block.getRelative(BlockFace.SOUTH);
                    case 5:
                        return block.getRelative(BlockFace.NORTH);
                }
            return null;
        }
    
     
  9. Offline

    Numenorean95

    Ahh, i didnt think of that, works as a material, thanks again!

    Nice way to do it, but other is easier.
     
Thread Status:
Not open for further replies.

Share This Page