How to change a block into a (material) Sign?

Discussion in 'Plugin Development' started by bergerkiller, Jul 17, 2011.

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

    bergerkiller

    I need to use the attachedToBlock function of the sign material class, but I need to inherit this class from a Block. How can I do this?

    in this format:
    Code:
    org.bukkit.material.Sign s = (org.bukkit.material.Sign) <?>;
    Note: this is not!
    Do I have to use the getState function or some sort of conversion from the getType?
    (This is probably a simple question to those that make plugins for longer, but the documentation for this is missing, so yeah...)
     
  2. Offline

    MadMonkeyCo

    Isn't there a getMaterial() in Block?
     
  3. Offline

    bergerkiller

    getType() returns a Material yes, but wasn't this an enumeration?

    To get an idea of what I try to accomplish; here is a snippet:
    Code:
            public Vector[] blockLocations;
            public void setLocations(Block sign, Block button, Block chest) {
                setBlock(0, sign);
                setBlock(1, button);
                setBlock(2, chest);
                //set block holding sign here
                //set block holding button here
            }
            public void setBlock(int index, Block b) {
                this.blockLocations[index] = new Vector(b.getX(), b.getY(), b.getZ());
            }
    EDIT

    I think I found the solution:
    Code:
            public Vector[] blockLocations;
            public void setLocations(Block sign, Block button, Block chest) {
                setBlock(0, sign);
                setBlock(1, button);
                setBlock(2, chest);
                //set block holding sign here
                org.bukkit.material.Sign s = (org.bukkit.material.Sign) sign.getState().getData();
                setBlock(3, sign.getFace(s.getAttachedFace()));
                //set block holding button here
                Button b = (Button) sign.getState().getData();
                setBlock(4, sign.getFace(b.getAttachedFace()));
            }
            public void setBlock(int index, Block b) {
                this.blockLocations[index] = new Vector(b.getX(), b.getY(), b.getZ());
            }
    Some of material.Sign's functions came from the superclass MaterialData, which could be retrieved using getData() of BlockState returned by Block.getState(). Yes, it is confusing. :)

    Only have to test this out of course.
     
  4. Offline

    Telgar

    I wouldnt use getData(), did you try
    Code:
    import org.bukkit.material.Sign;
    
    Block signBlock = (your sign block);
    Sign sign = (Sign) signBlock.getState();
    Edit: You're right, above is wrong. The sign that works there is org.bukkit.block.sign.
     
  5. Offline

    bergerkiller

    Thats the mean part, there are two types of "Sign" classes:
    One under block inheriting "BlockState"
    One under material inheriting "MaterialData"

    Simple check: look for the functions of the material.Sign and block.Sign classes. You'll see that they have different types of classes (grey) next to some functions.

    As far I believe MaterialData does not inherit from BlockState, thus there is no possible way to cast from BlockState to a Sign that inherits from MaterialData.

    Source1
    Source2
    Source3
     
  6. Offline

    Telgar

    Wow youre right, this is more complicated that I originally thought. Ill look into the bukkit source too then.

    Edit: Alright, what you wrote earlier (getData()) should work, good to know.
     
  7. Offline

    bergerkiller

    Also, I made it even better. :D
    This function works for every single block; it gets the block the input block is attached to!
    If it is not attachable, it will simply return the block underneath. (since that is always the case then)
    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.getFace(face);
        }
    Not sure about paintings though, but ow well. :)
     
    Wizardo367 likes this.
Thread Status:
Not open for further replies.

Share This Page