Solved Looping through all the scores on a scoreboard?

Discussion in 'Plugin Development' started by kameronn, Sep 1, 2016.

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

    kameronn

    What I'm currently trying to do is loop through all the scores on a scoreboard but I havent found a method or way to do it, any ideas?
     
  2. Offline

    InstanceofDeath

    I dont know exactely what you want, so I listed them up

    There are two type of scoreboards:

    The getMainScoreboard and the getNewScoreboard();

    getMainScoreboard();
    returns the ingamescoreboard .. So you can access it there trough /scoreboard and is in every plugin the same.
    A good use are rank plugins with nametag-color

    getNewScoreboard();
    returns a new scoreboard and can only acessed trough the instance itself .. gets autodeleted when there is no reference anymore (gc)

    https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/scoreboard/Scoreboard.html

    Scoreboard#getEntries(); returns a set of Strings Set<String>
    All Entities that get tracked by this scoreboard .. The name was updated to Entries, because you can create fake players in the sidebar

    Scoreboard#getObjectives(); returns a set of Objectives
    https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/scoreboard/Objective.html
    All objectives that are registered in the scoreboard

    Scoreboard#getScores(String entry); returns a set of Scores
    https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/scoreboard/Score.html
    All Scores that the player has in the specified scoreboard (playername = entry)

    Scoreboard#getTeams(); returns a set of Teams
    https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/scoreboard/Team.html
    // You should know blablabla

    And then you just use an expanded for loop or I'd like to say more for-each loop

    Code:
    List<Score> scores = new ArrayList<Score>();
    
    Scoreboard sbMain = Bukkit.getMainScoreboard();
    
    for(Objective objectivesMain : sbMain.getObjectives()) {
        for(String entries : sbMain.getEntries() {
            if(objectiveMain.getScore(entries) != null) {
                scores.add(objectiveMain.getScore(entries);
            }
        }
    }
    
    There is 100percent a better way, but I would do it like this
     
  3. Offline

    kameronn

    @InstanceofDeath
    Well, what I'm using is "player.getScoreboard();" and then getting on of the objectives that I have listed for the scoreboard, however this should work the same right?

    Also, after I add the scores to the list, just make another for loop to get each individual one? or should I use the same loop and edit their scores

    Nevermind thanks! I understand now, I just had to make a few changes and now it seems like it works, thanks again.
     
    Last edited: Sep 1, 2016
  4. Offline

    InstanceofDeath

    yeah should work, but remember that the getScoreboard method takes much memory
     
  5. @InstanceofDeath
    Could you please explain why getScoreboard takes "much memory", because as far as I can see, it doesn't.
     
  6. Offline

    kameronn

    @InstanceofDeath @AlvinB
    As what AlvinB says, I'm kind of confused as to why it would take a lot of memory, can you please explain?
     
  7. Offline

    InstanceofDeath

    I made a loop, that executed 10 times and wrote p.getScoreboard() and saved in a Map .. it laaged a little bit on the server (only 1gb ram) The Scoreboard has many subclasses, so it has much data
     
  8. Offline

    kameronn

Thread Status:
Not open for further replies.

Share This Page