Solved Sign Updater....

Discussion in 'Plugin Development' started by PreFiXAUT, Aug 17, 2013.

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

    PreFiXAUT

    Hi, I was trying to update all Signs in the loaded Chunks, but it doesn't seem to work.
    Does someone know why?
    Code:java
    1. this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable()
    2. {
    3. public void run()
    4. {
    5. for(final World world : Bukkit.getWorlds())
    6. {
    7. for(final Chunk chunk : world.getLoadedChunks())
    8. {
    9. for(final BlockState b : chunk.getTileEntities())
    10. {
    11. if(b.getBlock() instanceof Sign)
    12. {
    13. Sign sign = (Sign) b.getBlock();
    14. if (sign.getLine(0).equalsIgnoreCase("§f[Lobby 1]"))
    15. {
    16. sign.setLine(1,"§4[ " + SignCommand.players_lobby1 +" / 24 ]");
    17. sign.update();
    18. return;
    19. }
    20. else if (sign.getLine(0).equalsIgnoreCase("§f[Lobby 2]"))
    21. {
    22. sign.setLine(1,"§4[ " + SignCommand.players_lobby2 +" / 24 ]");
    23. sign.update();
    24. return;
    25. }
    26. else if ((sign.getLine(1).equalsIgnoreCase("§4[Team Red]") && (sign.getLine(3).equalsIgnoreCase("§b[Lobby 1]"))))
    27. {
    28. sign.setLine(2,"§6[ " + SignCommand.players_lobby1_red + " / 12 ]");
    29. sign.update();
    30. return;
    31. }
    32. else if ((sign.getLine(1).equalsIgnoreCase("§1[Team Blue]") && (sign.getLine(3).equalsIgnoreCase("§b[Lobby 1]"))))
    33. {
    34. sign.setLine(2,"§6[ " + SignCommand.players_lobby1_blue + " / 12 ]");
    35. sign.update();
    36. return;
    37. }
    38. else if ((sign.getLine(1).equalsIgnoreCase("§4[Team Red]") && (sign.getLine(3).equalsIgnoreCase("§b[Lobby 2]"))))
    39. {
    40. sign.setLine(2,"§6[ " + SignCommand.players_lobby2_red + " / 12 ]");
    41. sign.update();
    42. return;
    43. }
    44. else if ((sign.getLine(1).equalsIgnoreCase("§1[Team Blue]") && (sign.getLine(3).equalsIgnoreCase("§b[Lobby 1]"))))
    45. {
    46. sign.setLine(2,"§6[ " + SignCommand.players_lobby2_blue + " / 12 ]");
    47. sign.update();
    48. return;
    49. }
    50. else
    51. {
    52. return;
    53. // Sign without special Data
    54. }
    55. }
    56. return;
    57. // No Sign
    58. }
    59. }
    60. }
    61. }
    62. }, 5L, 5L);
     
  2. Offline

    PreFiXAUT

  3. Offline

    slater96

    PreFiXAUT
    It may be easier to save the location of the sign, when it is placed instead of looping through every world, tile entities etc...
    If you save it in the a config like this
    LobbySign:
    Loc: 0,0,0
    World: world

    Then call this method in your onEnable()
    Code:
    private void updateSigns() {
            this.getServer().getScheduler().runTaskTimer(this, new Runnable() {
                @Override
                public void run() {
                    if (getConfig().contains("LobbySign")) {
                        World world = Bukkit.getWorld(getConfig().getString("LobbySign.World"));
                        String[] lobbySign = getConfig().getString("LobbySign.Loc").split(",");
                        double x = Double.parseDouble(lobbySign[0]);
                        double y = Double.parseDouble(lobbySign[1]);
                        double z = Double.parseDouble(lobbySign[2]);
                        Location loc = new Location(world, x, y, z);
                        if (world.getBlockAt(loc).getState() instanceof Sign) {
                            Sign sign = (Sign) loc.getBlock().getState();
                            if (ChatColor.stripColor(sign.getLine(0)).equals("Example")) {
                                sign.setLine(0, ChatColor.DARK_RED + "Example");
                                sign.setLine(1, ChatColor.GREEN + "Test");
                                sign.update(true);
                            }
                        }
                    }
                }
            }, 0L, 20L);
        }
     
  4. Offline

    fireblast709

    b.getBlock() returns a Block, which can never be a Sign. Use
    Code:
    if(b instanceof Sign)
    Also, make sure you use org.bukkit.block.Sign and not org.bukkit.material.Sign ;)
     
  5. Offline

    PreFiXAUT

    slater96
    But it would do nearly the same, its just checking if the Sign is on a special Location, which would create more problems than checking an Line I think.
    And I'm currently not realy searching for annother searching method, I wan't to know why my Code won't work.

    fireblast709
    I checked the import and its block.Sign, and changed it to b instanceof Sign.
    But it's still not working :/
    Do you might need the entire Source?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  6. Offline

    fireblast709

    PreFiXAUT debug your code and see what setLine()s it reaches. Maybe the lines are not equal
     
  7. Offline

    PreFiXAUT

    fireblast709
    Normaly can't be possible cause I copied the entire Line/String of the Sign(I insert the Text with a Command) and pasted it in.
    But I will give it a try.
    EDIT: I tried debugging, and it realy won't get any Sign with the Data, so you we're right. But why? Normaly it should work :/
    I changed also the Source a bit, cause I gained some Errors.
    NEW SOURCE:
    Show Spoiler
    Code:java
    1. this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable()
    2. {
    3. public void run()
    4. {
    5. for(final World world : Bukkit.getWorlds())
    6. {
    7. for(final Chunk chunk : world.getLoadedChunks())
    8. {
    9. for(final BlockState b : chunk.getTileEntities())
    10. {
    11. if(b instanceof Sign)
    12. {
    13. Sign sign = (Sign) b;
    14.  
    15. if (sign.getLine(0).equalsIgnoreCase("§f[Lobby 1]"))
    16. {
    17. sign.setLine(1, "§4[ " + SignCommand.players_lobby1 +" / 24 ]");
    18. sign.update();
    19. System.out.println("Updated Sign to Join Lobby 1");
    20. return;
    21. }
    22. else if (sign.getLine(0).equalsIgnoreCase("§f[Lobby 2]"))
    23. {
    24. sign.setLine(1, "§4[ " + SignCommand.players_lobby2 +" / 24 ]");
    25. sign.update();
    26. return;
    27. }
    28. else if (sign.getLine(1).equalsIgnoreCase("§4[Team Red]") && (sign.getLine(3).equalsIgnoreCase("§b[Lobby 1]")))
    29. {
    30. sign.setLine(2, "§6[ " + SignCommand.players_lobby1_red + " / 12 ]");
    31. sign.update();
    32. return;
    33. }
    34. else if (sign.getLine(1).equalsIgnoreCase("§1[Team Blue]") && (sign.getLine(3).equalsIgnoreCase("§b[Lobby 1]")))
    35. {
    36. sign.setLine(2, "§6[ " + SignCommand.players_lobby1_blue + " / 12 ]");
    37. sign.update();
    38. return;
    39. }
    40. else if (sign.getLine(1).equalsIgnoreCase("§4[Team Red]") && (sign.getLine(3).equalsIgnoreCase("§b[Lobby 2]")))
    41. {
    42. sign.setLine(2, "§6[ " + SignCommand.players_lobby2_red + " / 12 ]");
    43. sign.update();
    44. return;
    45. }
    46. else if (sign.getLine(1).equalsIgnoreCase("§1[Team Blue]") && (sign.getLine(3).equalsIgnoreCase("§b[Lobby 1]")))
    47. {
    48. sign.setLine(2, "§6[ " + SignCommand.players_lobby2_blue + " / 12 ]");
    49. sign.update();
    50. return;
    51. }
    52. else
    53. {
    54. return;
    55. }
    56. }
    57. }
    58. }
    59. }
    60. }
    61. }, 5L, 5L);

    At least I know where the Errors are now. I'll try my best to fix it. When someone of you find the Error please tell me, maybe I can't figure out why :/
     
  8. Offline

    fireblast709

    Errors come with those nice stacktraces
     
  9. Offline

    PreFiXAUT

    fireblast709
    this was the stactrace:
    Show Spoiler
    Code:
    20:43:29 [WARNING] [PVP Lobbys] Task #5 for PVP Lobbys v0.2 generated an exception
    java.lang.ClassCastException: org.bukkit.craftbukkit.v1_6_R2.block.CraftBlock cannot be cast to org.
    bukkit.block.Sign
            at at.prefixaut.pvp_plugin.main$1.run(main.java:375)
            at org.bukkit.craftbukkit.v1_6_R2.scheduler.CraftTask.run(CraftTask.java:53)
            at org.bukkit.craftbukkit.v1_6_R2.scheduler.CraftScheduler.mainThreadHeartbeat(CraftSchedule
    r.java:345)
            at net.minecraft.server.v1_6_R2.MinecraftServer.t(MinecraftServer.java:522)
            at net.minecraft.server.v1_6_R2.DedicatedServer.t(DedicatedServer.java:226)
            at net.minecraft.server.v1_6_R2.MinecraftServer.s(MinecraftServer.java:486)
            at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java:419)
            at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:582)
    Line 375:
    Code:java
    1. Sign sign = (Sign) b.getBlock();

    New Line 375:
    Code:java
    1. Sign sign = (Sign) b;

    Now it's fixed(at least no stacktraces)
     
  10. Offline

    fireblast709

    -facepalms- why didn't I notice it before... In the last code, replace all
    Code:java
    1. return;
    with
    Code:java
    1. continue;
     
  11. Offline

    PreFiXAUT

    fireblast709
    Thanks not it's working :D
    Sorry for that Error, but I never made a Task before :eek:
     
Thread Status:
Not open for further replies.

Share This Page