CraftBlock cannot be cast to Sign

Discussion in 'Plugin Development' started by n1ghtk1n9, Jun 29, 2014.

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

    n1ghtk1n9

    Currently I'm trying to make a sort-of PvP plugin, in which one would right-click on a sign, which would add them to the game, and the game would start after a timer and so on. Currently my problem is when I'm writing my SignListener, whenever I try to update my Sign, it returns:
    Code:
    [SignLogin] Task #4 for SignLogin v0.0.0.0.01 generated an exception
    java.lang.ClassCastException: org.bukkit.craftbukkit.v1_7_R3.block.CraftBlock cannot be cast to org.bukkit.block.Sign
        at com.daycraft.plugins.SignLoginTests.SignListener2.updateSign(SignListener2.java:154) ~[?:?]
        at com.daycraft.plugins.SignLoginTests.SignListener2$UpdateSign.run(SignListener2.java:189) ~[?:?]
        at org.bukkit.craftbukkit.v1_7_R3.scheduler.CraftTask.run(CraftTask.java:53) ~[craftbukkit-1.7.9-R0.2.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at org.bukkit.craftbukkit.v1_7_R3.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:345) [craftbukkit-1.7.9-R0.2.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:600) [craftbukkit-1.7.9-R0.2.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:260) [craftbukkit-1.7.9-R0.2.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:558) [craftbukkit-1.7.9-R0.2.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:469) [craftbukkit-1.7.9-R0.2.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [craftbukkit-1.7.9-R0.2.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    [20:11:41] [Server thread/INFO]
    I've searched around and apparently that exception occurs when you click on a sign post or a wall-sign, but when I go to the JavaDocs, it says that org.bukkit.block.Sign: "Represents either a SignPost or a WallSign" Why am I getting this error if that's not the case?

    My SignListener:
    Code:java
    1. public class SignListener2 extends BukkitRunnable implements Listener {
    2. Main plugin;
    3.  
    4. private World world = Bukkit.getWorld("world");
    5.  
    6. private int maxPlayerCount = 16;
    7. public ArrayList<Player> lobby0 = new ArrayList<Player>(maxPlayerCount);
    8. public ArrayList<Player> lobby1 = new ArrayList<Player>(maxPlayerCount);
    9. public ArrayList<Player> lobby2 = new ArrayList<Player>(maxPlayerCount);
    10.  
    11. Sign sign;
    12.  
    13. public int time[] = {
    14. 180, 180, 180
    15. };
    16.  
    17. public int signlocations[][] = {
    18. {1561, 4, -140},
    19. {1562, 4, -140},
    20. {1563, 4, -140}
    21. };
    22.  
    23. public SignListener2(Main plugin) {
    24. this.plugin = plugin;
    25. }
    26.  
    27. @EventHandler
    28. public void onSignClick(PlayerInteractEvent event) {
    29. Player player = event.getPlayer();
    30. new UpdateSign().runTaskTimer(plugin, 0l, 5l);
    31.  
    32. if(event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    33. if(event.getClickedBlock().getType() == Material.SIGN || event.getClickedBlock().getType() == Material.SIGN_POST || event.getClickedBlock().getType() == Material.WALL_SIGN) {
    34. if(event.getClickedBlock() != null) {
    35. sign = (Sign) event.getClickedBlock().getState();
    36.  
    37. }
    38.  
    39. String line0 = sign.getLine(0);
    40.  
    41. switch(line0) {
    42. case "§8Lobby §d0":
    43. if(lobby0.size() < maxPlayerCount) {
    44. if(!lobby0.contains(player) && !lobby1.contains(player) && !lobby2.contains(player)) {
    45. player.sendMessage("§9 Joining lobby 0");
    46. lobby0.add(player);
    47. updateSign(0);
    48.  
    49. } else {
    50. player.sendMessage("You're already in a lobby!");
    51. }
    52. }
    53. break;
    54. case "§8Lobby §b1":
    55. if(lobby1.size() < maxPlayerCount) {
    56. if(!lobby1.contains(player) && !lobby0.contains(player) && !lobby2.contains(player)) {
    57. player.sendMessage("§9 Joining lobby 1");
    58. lobby1.add(player);
    59. updateSign(1);
    60.  
    61. } else {
    62. player.sendMessage("You're already in a lobby!");
    63. }
    64. }
    65. break;
    66. case "§8Lobby §a2":
    67. if(lobby2.size() < maxPlayerCount) {
    68. if(!lobby2.contains(player) && !lobby0.contains(player) && !lobby1.contains(player)) {
    69. player.sendMessage("§9 Joining lobby 2");
    70. lobby2.add(player);
    71. updateSign(2);
    72.  
    73. } else {
    74. player.sendMessage("You're already in a lobby!");
    75. }
    76. }
    77. break;
    78. case "Leave":
    79. if(lobby0.contains(player)) {
    80. lobby0.remove(player);
    81.  
    82. } else if(lobby1.contains(player)) {
    83. lobby1.remove(player);
    84.  
    85. } else if(lobby2.contains(player)) {
    86. lobby2.remove(player);
    87.  
    88. } else if(!lobby0.contains(player) && !lobby1.contains(player) && !lobby2.contains(player)) {
    89. player.sendMessage("§4You're not in a lobby!");
    90.  
    91. }
    92.  
    93.  
    94. break;
    95. case "Format":
    96. String line1 = sign.getLine(1);
    97. if(line1 == "0") {
    98. sign.setLine(0, "§8Lobby §d0");
    99. sign.setLine(1, "§1" + lobby0.size() + "/" + maxPlayerCount);
    100. sign.setLine(2, "Time: " + time[0]);
    101.  
    102. } else if(line1 == "1") {
    103. sign.setLine(0, "§8Lobby §b1");
    104. sign.setLine(1, "§1" + lobby2.size() + "/" + maxPlayerCount);
    105. sign.setLine(2, "Time: " + time[1]);
    106.  
    107. } else if(line1 == "2") {
    108. sign.setLine(0, "§8Lobby §a2");
    109. sign.setLine(1, "§1" + lobby2.size() + "/" + maxPlayerCount);
    110. sign.setLine(2, "Time: " + time[2]);
    111.  
    112. }
    113.  
    114. break;
    115. default:
    116. break;
    117. }
    118. }
    119. }
    120. }
    121.  
    122. public void updateSign(int lobby) {
    123. switch(lobby) {
    124. case 0:
    125. sign = (Sign) world.getBlockAt(signlocations[0][0], signlocations[0][1], signlocations[0][2]).getState();
    126. sign.setLine(1, "§1" + lobby0.size() + "/" + maxPlayerCount);
    127. sign.setLine(2, "Time: " + time[0]);
    128. sign.update();
    129. Bukkit.broadcastMessage("Updating Sign 0");
    130.  
    131. break;
    132. case 1:
    133. sign = (Sign) world.getBlockAt(signlocations[1][0], signlocations[1][1], signlocations[1][2]).getState();
    134. sign.setLine(1, "§1" + lobby2.size() + "/" + maxPlayerCount);
    135. sign.setLine(2, "Time: " + time[2]);
    136. sign.update();
    137. Bukkit.broadcastMessage("Updating Sign 1");
    138.  
    139. break;
    140. case 2:
    141. sign = (Sign) world.getBlockAt(signlocations[2][0], signlocations[2][1], signlocations[2][2]).getState();
    142. sign.setLine(1, "§1" + lobby2.size() + "/" + maxPlayerCount);
    143. sign.setLine(2, "Time: " + time[2]);
    144. sign.update();
    145. Bukkit.broadcastMessage("Updating Sign 2");
    146.  
    147. break;
    148. default: break;
    149. }
    150. }
    151.  
    152. @Override
    153. public void run() {
    154. //do time subtractions here
    155. }
    156.  
    157. class UpdateSign extends BukkitRunnable {
    158. @Override
    159. public void run() {
    160. updateSign(0);
    161. updateSign(1);
    162. updateSign(2);
    163.  
    164. Bukkit.broadcastMessage("runnable");
    165. }
    166. }
    167. }
    168.  
     
  2. Offline

    evilmidget38

    n1ghtk1n9 You need to get the state of the Block, then cast it to Sign.
     
    3 people like this.
  3. Offline

    n1ghtk1n9


    Wow, that was so simple I didn't even think of it(cause I figured I had already done it), silly mistake. Thanks for the quick response. I updated the code, but now I'm getting
    Code:
    [SignLogin] Task #2 for SignLogin v0.0.0.0.01 generated an exception
    java.lang.ClassCastException: org.bukkit.craftbukkit.v1_7_R3.block.CraftBlockState cannot be cast to org.bukkit.block.Sign
        at com.daycraft.plugins.SignLoginTests.SignListener2.updateSign(SignListener2.java:140) ~[?:?]
        at com.daycraft.plugins.SignLoginTests.SignListener2$UpdateSign.run(SignListener2.java:175) ~[?:?]
        at org.bukkit.craftbukkit.v1_7_R3.scheduler.CraftTask.run(CraftTask.java:53) ~[craftbukkit-1.7.9-R0.2.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at org.bukkit.craftbukkit.v1_7_R3.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:345) [craftbukkit-1.7.9-R0.2.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:600) [craftbukkit-1.7.9-R0.2.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:260) [craftbukkit-1.7.9-R0.2.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:558) [craftbukkit-1.7.9-R0.2.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:469) [craftbukkit-1.7.9-R0.2.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
        at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [craftbukkit-1.7.9-R0.2.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    [20:37:24] [Server thread/INFO]
     
  4. Offline

    chasechocolate

    n1ghtk1n9 then perhaps the block at that exact location in the world isn't a sign?

    EDIT: 5000th post :p
     
    xTigerRebornx and n1ghtk1n9 like this.
  5. Offline

    n1ghtk1n9

    Possibly, I'll double check right now
     
  6. Offline

    n1ghtk1n9

    I double checked, it definitely isn't that it's the wrong block
     
Thread Status:
Not open for further replies.

Share This Page