Scoreboard Trouble

Discussion in 'Plugin Development' started by mydeblob, Apr 5, 2014.

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

    mydeblob

    I'm making a scoreboard which displays the online users as a string like this
    [​IMG]
    So to update this (when a player joins the server) I have tried resetting that value with .resetScore. However this does not work, and results in this
    [​IMG]
    I have no idea why it's doing this. Here is my code
    Code:java
    1. public void updateSB(){
    2. for(Player p:Bukkit.getOnlinePlayers()){
    3. if(p.getScoreboard() != null){
    4. Scoreboard board = p.getScoreboard();
    5. Objective o = board.getObjective(DisplaySlot.SIDEBAR);
    6. for(OfflinePlayer player : board.getPlayers())
    7. {
    8. if(player.equals("" + getPlayerCount())){
    9.  
    10. board.resetScores(player);
    11. }
    12. }
    13. o.getScore(Bukkit.getOfflinePlayer("" + getConfig().getString("online-value").replaceAll("&", "\u00A7").replaceAll("ply", "" + Bukkit.getOnlinePlayers()))).setScore(8);
    14. p.setScoreboard(board);
    15. }
    16. }
    17. }

    This method uses one other method called getPlayerCount. The setter and getter for that can be seen here
    Code:java
    1.  
    2. private int players = 0;
    3. public void setPlayercount(int playercount){
    4. players = playercount;
    5. }
    6. public int getPlayerCount(){
    7. return players;
    8. }

    I'm setting the player count in a join event
    Code:java
    1. @EventHandler
    2. public void onJoin(final PlayerJoinEvent e){
    3. setPlayercount((Bukkit.getOnlinePlayers().length - 1));
    4. }

    I'm subtracting one from that because when you call the Bukkit.getOnlinePlayers it includes the player that had just joined.
     
  2. Offline

    Bionicrm

    Simply put, resetting scores won't work. You'll have to destroy the objective every time you want to update.
    You can have a look at my API: SideAPI, (GitHub), so you can see what it's doing.
     
  3. Offline

    mydeblob

    Bionicrm
    I don't understand why it won't work. The way I have it now, it actually works fine until someone quits. When a player joins it updates fine but when a player quits it does whats shown in the second screenshot in the op.
     
  4. Offline

    Bionicrm

    mydeblob You're destroying the objective everytime you want to update it?
     
  5. To update it you'll have to loop through all your offline players, check for the one that has 8 as a score (which is the scoreboard slot of your playercount).
    Then you reset the score for that offline player and create a new one with the same score and updated playercount.

    Some snippets that might be useful for you:

    Code:java
    1.  
    2. for(OfflinePlayer offp : scoreboard.getPlayers()) {
    3. int i = objective.getScore(offp).getScore();
    4. if(i == 8) {
    5. scoreboard.resetScores(offp);
    6. fake(new playercount here).setScore(8);
    7. }
    8. }
    9.  
    10. public Score fake(String fakeName) {
    11. return objective.getScore(Bukkit.getOfflinePlayer(fakeName));
    12. }
     
Thread Status:
Not open for further replies.

Share This Page