Solved Whats the best way to update/refresh a certain line on a scoreboard?

Discussion in 'Plugin Development' started by McKiller5252, Mar 31, 2014.

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

    McKiller5252

    For example this is what i have right now in my code:
    Code:java
    1. public void addPlayer(final Player player) {
    2. final Scoreboard board = Bukkit.getScoreboardManager().getNewScoreboard();
    3. o = board.registerNewObjective("XaeusHub", "dummy");
    4. o.setDisplaySlot(DisplaySlot.SIDEBAR);
    5. o.setDisplayName(prefix);
    6.  
    7. int a = Bukkit.getOnlinePlayers().length;
    8.  
    9. o.getScore(Bukkit.getOfflinePlayer(g + b.toString() + "Online:")).setScore(5);
    10. o.getScore(Bukkit.getOfflinePlayer(y + "" + a)).setScore(4);
    11. o.getScore(Bukkit.getOfflinePlayer("")).setScore(3);
    12. o.getScore(Bukkit.getOfflinePlayer(r.toString() + line.toString() + "------------")).setScore(2);
    13.  
    14.  
    15. playerboards.put(player.getName(), board);
    16. plugin.getServer().getScheduler().runTask(plugin, new Runnable() {
    17. public void run() {
    18. player.setScoreboard(board);
    19.  
    20. }
    21. });
    22.  
    23. }

    And i want to update the Online: when a player joins or leaves.
     
  2. Offline

    xXSniperzzXx_SD

    McKiller5252
    To update a scoreboard, what I do in Info-Board is; I remove what the line said previously, and then readd the new line.
     
  3. I wrote a pretty solid class for using the scoreboard as a Pixelated grid take a look at it and see if you can learn from it. http://pastebin.com/Psg4Epaq
     
  4. Offline

    McKiller5252

    xXSniperzzXx_SD
    Could i have a example of what you mean, I sorta get what you getting at the point but can't picture it in my head.
     
  5. Offline

    xXSniperzzXx_SD

    goeiecool9999 Oh cool, you made it play the game snake on a scoreboard?
     
  6. Yep thats exactly what I did! :p
     
  7. Offline

    xXSniperzzXx_SD

    @McKiller5252
    For Info-Board I have the lines the people want on the scoreboard, and then when it's time to update them, I loop through the scoreboard and make sure that the string array in the config contains the lines on the scoreboard, if not, I remove it. Then to re-add them, I loop back through the string array from the config and see if the scoreboard contains every line from the array, if not I add it.

    This way I'm not resetting the whole scoreboard everytime it goes through. Because when a server is using Spigot, it makes the scoreboard flash

    https://github.com/SniperzCiinema/Info-Board/blob/master/Info Board/src/me/xxsniperzzxxsd/infoboard/Scoreboard/Update.java

    Then down at the bottom of the class(I'm not the neatest coder, but I try :D)
     
  8. Basicly what I did for my snake was a little less effective but still I think mineplex uses the same method. I basicly remove all the scores of the OfflinePlayers Take a look at this loop:
    Code:java
    1. for(OfflinePlayer p : sb.getPlayers()){
    2. sb.resetScores(p);
    3. }
    Where sb is the scoreboard.

    Also the lines are sorted by score so basicly have the top line have the highest score.

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

    coasterman10

    You should not be creating a new scoreboard every time a player joins or leaves. Store the scoreboard as a field, and then just get the score corresponding to the line you want updated and set it to the new value.

    So, you would get the new scoreboard and do the setup once when the plugin loads, and then on the PlayerJoinEvent you update the score on the scoreboard and set the player's current scoreboard to the scoreboard you have stored. On the PlayerLeaveEvent you also update the score.

    Also, if this is not on an asynchronous separate thread, you don't need to schedule a runnable.
     
  10. Offline

    McKiller5252

    I've solved it! Thanks everyone for all the help! <3
     
Thread Status:
Not open for further replies.

Share This Page