Solved 1.8.8 WallSign Direction

Discussion in 'Plugin Development' started by Lightcaster5, Feb 23, 2020.

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

    Lightcaster5

    How do you set the direction of a sign? Specifically a wall sign.
    Heres what I have:
    Code:
    Location location = new Location(world, x, y, z);
    Block block = world.getBlockAt(location);
    block.setType(Material.WALL_SIGN);
    Sign sign = (Sign) block.getState().getData();
    // Somehow change the direction of the wall sign
    // sign#setData() doesn't work from what I've done
     
  2. Sign is a subinterface of BlockState so just cast block.getState(). Sign is also a subinterface of Directorial which provides functions to rotate the sign
     
  3. Offline

    Lightcaster5

    I tired the following and got the following error
    Code:
    BlockFace blockFace = BlockFace.NORTH;
    ((Directional) block.getState()).setFacingDirection(blockFace);
    Code:
    java.lang.ClassCastException: org.bukkit.craftbukkit.v1_8_R3.block.CraftSign cannot be cast to org.bukkit.material.Directional
    EDIT:
    I see that you said that Sign is part of Directional and when I tried to cast my sign variable from
    Code:
    Sign sign = (Sign) block.getState();
    And then cast it like so
    Code:
    Directional directional = (Directional) sign;
    It gave me the following error:
    Code:
    java.lang.ClassCastException: org.bukkit.craftbukkit.v1_8_R3.block.CraftSign cannot be cast to org.bukkit.material.Directional
     
    Last edited: Feb 25, 2020
  4. Offline

    Lightcaster5

  5. Well seems you are right...
    You have to use org.bukkit.material.Sign (or org.bukkit.material.Directional), and now also use Block#getState().getData() as you already did.
    I wasn't able to apply the new direction with this but you can use it as a "translation" and do Block#setData(Sign.getData())
    I know it's Deprecated and there is probably a better way but at least it works
    Sorry for that misdirection
     
  6. Offline

    Lightcaster5

    I don't know if I'm like most people for doing this but I've grown against using deprecated methods. There's no other way to achieve this task?
     
  7. Offline

    Lightcaster5

    Alright, I've tried several things and nothing as worked... It creates the sign just fine beforehand but I can't change the direction. I'm using a depreciated method so yeah.
    Code:
    @SuppressWarnings("deprecation")
        public void setFacing(Block block, BlockFace facing) {
            org.bukkit.material.Sign sign = new org.bukkit.material.Sign();
            sign.setFacingDirection(facing);
            block.setData(sign.getData(), true);
        }
     
  8. Offline

    timtower Administrator Administrator Moderator

    @Lightcaster5 Why are you making a new sign? Get the old one of the block.
    Change the facing.
    Then update the block.
     
  9. Offline

    Lightcaster5

    As I said, I create the sign elsewhere and im trying to update it here but this isnt working
     
  10. Offline

    timtower Administrator Administrator Moderator

    You have the block. You are calling "new Sign" there, that is wrong.
    Get the original data.
    Then change the facing.
    Then update the block.
    If it is not working then post your updated code.
     
  11. Offline

    Lightcaster5

    This is what I've changed it to but it is giving me an error
    Code:
    org.bukkit.material.Sign sign = (org.bukkit.material.Sign) block.getState();
    sign.setFacingDirection(facing);
    block.setData(sign.getData(), true);
    Code:
    java.lang.ClassCastException: org.bukkit.craftbukkit.v1_8_R3.block.CraftSign cannot be cast to org.bukkit.material.Sign
     
  12. Offline

    timtower Administrator Administrator Moderator

  13. Offline

    Lightcaster5

    Code:
    The method setFacingDirection(BlockFace) is undefined for the type Sign
     
  14. Import is correct but Sign inherits from MaterialData not BlockState
     
  15. Offline

    Lightcaster5

    Everything is now working correctly. Thanks everyone!
    For those who need it, here is a method to set the face direction of a wall sign
    (Don't be scared of the depreciation, I was before but it works fine)
    Code:
        @SuppressWarnings("deprecation")
        public void setFacing(Block block, BlockFace facing) {
            if (block.getType() == Material.WALL_SIGN) {
                org.bukkit.material.Sign sign = (org.bukkit.material.Sign) block.getState().getData();
                sign.setFacingDirection(facing);
                block.setData(sign.getData(), true);
            }
        }
     
Thread Status:
Not open for further replies.

Share This Page