Solved How do I place a WALL_SIGN onto a chest

Discussion in 'Plugin Development' started by RROD, Aug 24, 2011.

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

    RROD

    Ok, this is a real pain.

    I'm looking for a simple code which when a SIGN_POST with Line(0) = "Some Text" is near a chest (or other block) - it will become a WALL_SIGN and attach onto the block.

    Bukkit documentation and looking at some other code doesn't help. It can't be that hard, surely? If I find it's simple then I'm a noob, lol.
    I've spent some time now working with Block Listeners trying to get this solved but to no avail; help, please?

    RROD
     
  2. Offline

    Pencil

    Hmm check the relatives. something like block.getRelatives (check the directions north south etc), then if relative = Material.Chest then change it to a wallsign D:
     
  3. Offline

    RROD

    Ok, will look into it a bit more.
    RROD

    Looks like I solved it, but there's a deprecation. =/

    Code:
    public void onBlockPlace(BlockPlaceEvent event) {
            Block placed = event.getBlockPlaced();
            Player player = event.getPlayer();
            if (placed.getType() == Material.SIGN_POST) {
                @SuppressWarnings("deprecation")
                Block chest = placed.getFace(BlockFace.NORTH, 1);
                 if (chest.getType() == Material.CHEST) {
                     player.sendMessage("Solved!");
                     chest.setType(Material.DIAMOND_BLOCK);
                 }
            }
        }
    Would be nice to fix this also. =^_^=

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

    Crash

    That's because it's changed to getRelative
     
  5. Offline

    Pencil

    exactly ^^

    Just like I said in the post above XD
     
  6. Offline

    RROD

    Err, I don't get it lol. There's getRelative but not getRelatives? Any chance of a code example?

    EDIT: It's ok now I think I've got it.
    EDIT x2: Yep, fixed it.

    Code:
    public void onBlockPlace(BlockPlaceEvent event) {
            Block block = event.getBlockPlaced();
            Block blockRel = block.getRelative(BlockFace.NORTH, 1);
            Player player = event.getPlayer();
            if (block.getType() == Material.SIGN_POST) {
                if (blockRel.getType() == Material.CHEST) {
                    blockRel.setType(Material.DIAMOND_BLOCK);
                    player.sendMessage("Fixed");
                }
            }
        }
     
  7. Offline

    Pencil

    Yeah sorry lol :p I was at work and had no chance to open eclipse XD
     
  8. Offline

    RROD

    Np, at least I've got it ;)

    Hmm, now I'm kinda stuck again lol. :S

    Now I want to find out the text on the sign (getLine(0)). The bukkit documentation isn't very detailed. =/

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 19, 2016
    Pencil likes this.
  9. Offline

    oliverw92

    Code:
    //Set the block to a wall sign
    block.setType(Material.WALL_SIGN);
    
    //Cast to a sign block
    org.bukkit.block.Sign sign = (org.bukkit.block.Sign)block;
    //Get line 0
    System.out.println(sign.getLine(0));
    
    //Cast BlockState to sign material
    org.bukkit.material.Sign signData = (org.bukkit.material.Sign)(block.getState().getData());
    //Set the sign to face north
    signData.setFacingDirection(BlockFace.NORTH);
     
  10. Offline

    RROD

    Err, I tried that now I get this:
    Code:
    [SEVERE] Could not pass event BLOCK_PLACE to Lockette
    java.lang.ClassCastException: org.bukkit.craftbukkit.block.CraftBlock cannot be cast to org.bukkit.block.Sign
        at org.rrod.lockette.LockBlockListener.onBlockPlace(LockBlockListener.java:28)
        at org.bukkit.plugin.java.JavaPluginLoader$29.execute(JavaPluginLoader.java:441)
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:58)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:338)
        at org.bukkit.craftbukkit.event.CraftEventFactory.callBlockPlaceEvent(CraftEventFactory.java:90)
        at org.bukkit.craftbukkit.event.CraftEventFactory.callBlockPlaceEvent(CraftEventFactory.java:74)
        at net.minecraft.server.ItemSign.a(ItemSign.java:56)
        at net.minecraft.server.ItemStack.placeItem(ItemStack.java:56)
        at net.minecraft.server.ItemInWorldManager.interact(ItemInWorldManager.java:223)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:573)
        at net.minecraft.server.Packet15Place.a(SourceFile:57)
        at net.minecraft.server.NetworkManager.b(NetworkManager.java:226)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:89)
        at net.minecraft.server.NetworkListenThread.a(SourceFile:105)
        at net.minecraft.server.MinecraftServer.h(MinecraftServer.java:454)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:363)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:422)
    2011-08-24 19:22:25 [SEVERE] Could not pass event BLOCK_PLACE to Lockette
    java.lang.ClassCastException: org.bukkit.craftbukkit.block.CraftBlock cannot be cast to org.bukkit.block.Sign
        at org.rrod.lockette.LockBlockListener.onBlockPlace(LockBlockListener.java:28)
        at org.bukkit.plugin.java.JavaPluginLoader$29.execute(JavaPluginLoader.java:441)
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:58)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:338)
        at org.bukkit.craftbukkit.event.CraftEventFactory.callBlockPlaceEvent(CraftEventFactory.java:90)
        at org.bukkit.craftbukkit.event.CraftEventFactory.callBlockPlaceEvent(CraftEventFactory.java:74)
        at net.minecraft.server.ItemSign.a(ItemSign.java:56)
        at net.minecraft.server.ItemStack.placeItem(ItemStack.java:56)
        at net.minecraft.server.ItemInWorldManager.interact(ItemInWorldManager.java:223)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:573)
        at net.minecraft.server.Packet15Place.a(SourceFile:57)
        at net.minecraft.server.NetworkManager.b(NetworkManager.java:226)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:89)
        at net.minecraft.server.NetworkListenThread.a(SourceFile:105)
        at net.minecraft.server.MinecraftServer.h(MinecraftServer.java:454)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:363)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:422)
    EDIT: I've also just tried this, but removing the org.bukkit.etc...
    Bare with me, I'm working on it.
     
  11. Offline

    oliverw92

    My bad. Cast the block state to org.bukkit.block.Sign, then cast sign.getData() to org.bukkit.material.Sign
     
  12. Offline

    RROD

    Hmm. Sorry if I'm being a pain. lol.

    Code:
    [SEVERE] Could not pass event BLOCK_PLACE to Lockette
    java.lang.ClassCastException: org.bukkit.craftbukkit.block.CraftBlock cannot be cast to org.bukkit.block.Sign
        at org.rrod.lockette.LockBlockListener.onBlockPlace(LockBlockListener.java:33)
        at org.bukkit.plugin.java.JavaPluginLoader$29.execute(JavaPluginLoader.java:441)
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:58)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:338)
        at org.bukkit.craftbukkit.event.CraftEventFactory.callBlockPlaceEvent(CraftEventFactory.java:90)
        at org.bukkit.craftbukkit.event.CraftEventFactory.callBlockPlaceEvent(CraftEventFactory.java:74)
        at net.minecraft.server.ItemSign.a(ItemSign.java:56)
        at net.minecraft.server.ItemStack.placeItem(ItemStack.java:56)
        at net.minecraft.server.ItemInWorldManager.interact(ItemInWorldManager.java:223)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:573)
        at net.minecraft.server.Packet15Place.a(SourceFile:57)
        at net.minecraft.server.NetworkManager.b(NetworkManager.java:226)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:89)
        at net.minecraft.server.NetworkListenThread.a(SourceFile:105)
        at net.minecraft.server.MinecraftServer.h(MinecraftServer.java:454)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:363)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:422)
    
     
  13. Offline

    oliverw92

    Look at the error - you are still trying to cast the block to Sign. You need to cast BlockState to sign.
     
  14. Offline

    RROD

    Hmm, now I'm getting: Could you fix the code?
    java.lang.ClassCastException: org.bukkit.craftbukkit.block.CraftBlock cannot be cast to org.bukkit.material.Sign
     
  15. Offline

    Tster

    Cast the craft block's state to a block state(or to a block then a block state) and then to a sign
     
  16. Offline

    oliverw92

    Code:
    //Set the block to a wall sign
    block.setType(Material.WALL_SIGN);
    
    //Cast to a sign block
    org.bukkit.block.Sign sign = (org.bukkit.block.Sign)(block.getState());
    //Get line 0
    System.out.println(sign.getLine(0));
    
    //Cast BlockState to sign material
    org.bukkit.material.Sign signData = (org.bukkit.material.Sign)(block.getState().getData());
    //Set the sign to face north
    signData.setFacingDirection(BlockFace.NORTH);
     
  17. Offline

    RROD

    Ok, it works but printLn (I replaced with log.info) Doesn't print any text, ie line 0 is blank even if I type something on it.
     
  18. Offline

    oliverw92

    It will probably destroy the sign text when you do block.setType(); Do the sign cast before you set the block type to a wall sign. You may have to 'backup' the sign text with sign.getLines();
     
Thread Status:
Not open for further replies.

Share This Page