[SOLVED] Turning a Block Object into a sign...?

Discussion in 'Plugin Development' started by Hotshot, Dec 25, 2011.

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

    Hotshot

    If you have a block object and test it out to be a sign, how to do get a Sign Object and then get the text on the sign?

    Code:
    Location loca = new Location(event.getFrom().getWorld(), x2, y2, z2);
    Block block = loca.getBlock();
    if (block.getType().equals(Material.SIGN))
    {
    (?)  String line1 = block....
    }
     
  2. Offline

    Lolmewn

    Sign s = (Sign)block;
    ONLY cast object if you are certain it is what it is.
    For example, if it's not a sign but a stone, it will give quite some errors.
     
  3. Offline

    Hotshot

    Are you sure you can just cast it like that?
     
  4. Offline

    Sagacious_Zed Bukkit Docs

    you can check it.
    Code:
    if (block instanceof Sign) {
     // do sign stuff
    }
     
  5. Not block, but block.getState(), as well for casting... and as I heard, you shouldn't call it frequently, so store it:

    Code:
    if(block.getType() == Material.SIGN) // Material.SIGN is a enum, you can use ==
    {
        BlockState blockState = block.getState();
    
        if(blockState instanceof Sign)
        {
            Sign sign = (Sign)blockState;
    
            sign.setLine(1, "TEST !!!");
        }
    }
    EDIT: also, instead of that location code you can just use:
    Code:
    Block block = event.getFrom().getWorld().getBlockAt(x2, y2, z2)
    Location.getBlock() calls that same method too, so no need to create a new location in the process :p ... unless you need the location for later xD
     
    HappyPikachu likes this.
  6. Offline

    Hotshot

Thread Status:
Not open for further replies.

Share This Page