Making Scoreboard with Teams (No flicker) - Historical

Applied By RandomRobinnnn: Aug 25, 2019 at 11:49 AM

Making Scoreboard with Teams (No flicker)
Hello there :),

In this Wiki you will read on how to create a Scoreboard with Teams which has no flicker.

Please read below and don't copy and paste :D


Setting scoreboard
Here we set the scoreboard for the player if player has no scoreboard yet!
Code (Java):

      public void setScoreBoard(Player player) {
            Scoreboard board = Bukkit.getScoreboardManager().getNewScoreboard();
            Objective obj = board.registerNewObjective("ServerName", "dummy", "DisplayName");
            obj.setDisplaySlot(DisplaySlot.SIDEBAR);
            obj.setDisplayName("Test Server ");
      }
 
Now we make a Score that is just there and wont update.
Like » Online and below that one we show the onlineplayers (Using teams)
Code (Java):

        Score onlineName = obj.getScore(ChatColor.GRAY + "» Online");
        onlineName.setScore(15);
 
/*
* Scoreboard looks now like:
*
* == Title ==
* » Online
*/

Now we going to make the onlineCounter using teams (Wont be visible by /scoreboard teams list etc.
Register a new team called "onlineCounter".
We will need to use the "onlineCounter" later to update it! They are case Sensitive.
Code (Java):

        Team onlineCounter = board.registerNewTeam("onlineCounter");
 
Now we add just a blank entry instead of a player.
Note that entry's for teams much be identical! You can't have 2 different teams with same entry!
Code (Java):

        // Note: The ' + "" + ' is there to convert the colors into strings without manually calling ChatColor#toString
        onlineCounter.addEntry(ChatColor.BLACK + "" + ChatColor.WHITE);
 
Here we set the prefix of the team.
This will look like:
onlineplayers/maxplayers
Example: 4/200
You can change it yourself!

Important: One thing to note is that up until 1.13, the prefix length has to have a maximum of 16 characters (in 1.13+ it is 64), so always check for the length of your string, or players will get kicked off the server and your server logs might get spammed!
To get a small workaround to that, go to the bottom of the page :)
Code (Java):

        if (Bukkit.getOnlinePlayers().size() == 0) {
            onlineCounter.setPrefix(ChatColor.DARK_RED + "0" + ChatColor.RED + "/" + ChatColor.DARK_RED + Bukkit.getMaxPlayers());
        } else {
            onlineCounter.setPrefix("" + ChatColor.DARK_RED + Bukkit.getOnlinePlayers().size() + ChatColor.RED + "/" + ChatColor.DARK_RED + Bukkit.getMaxPlayers());
        }
 
Now we add the team into the scoreboard so it will display it!
Note that the getScore(""); must be the same as your team entry!
This one is the same as the onlineCounter entry!
Code (Java):

        obj.getScore(ChatColor.BLACK + "" + ChatColor.WHITE).setScore(14);
 
/*
* Scoreboard will now look like:
*
* == Title ==
* » Online
* 4/200 (From example)
*/


Going to make a moneyCounter that will display your vault balance!
Code (Java):

        Score money = obj.getScore(ChatColor.GRAY + "» Money");
        money.setScore(13);

        Team moneyCounter = board.registerNewTeam("moneyCounter");
        moneyCounter.addEntry(ChatColor.RED + "" + ChatColor.WHITE);
        moneyCounter.setPrefix(ChatColor.GREEN + "$" + economy.getBalance(player));
        obj.getScore(ChatColor.RED + "" + ChatColor.WHITE).setScore(12);
 
* Scoreboard looks now like:
*
* == Title ==
* » Online
* 4/200
* » Money
* ${player's balance}



Now we just set the scoreboard with:
Code (Java):

        player.setScoreboard(board);
 
Scoreboard been set! Time to update the scoreboard.

Updating scoreboard
So now we are going to update the scoreboard for a player.

First we need to get the player's scoreboard

Code (Java):

    public void updateScoreBoard(Player player) {

        Scoreboard board = player.getScoreboard();
 
Now we have that we can access the scoreboard stuff.
Now lets update the onlineCounter and the moneyCounter

Code (Java):

        if (Bukkit.getOnlinePlayers().size() == 0) {
            board.getTeam("onlineCounter").setPrefix(ChatColor.DARK_RED + "0" + ChatColor.RED + "/" + ChatColor.DARK_RED + Bukkit.getMaxPlayers());
        } else {
            board.getTeam("onlineCounter").setPrefix(ChatColor.DARK_RED + "" + Bukkit.getOnlinePlayers().size() + ChatColor.RED + "/" + ChatColor.DARK_RED + Bukkit.getMaxPlayers();
        }
 
Here we get the team called "onlineCounter" and we update it by setting the new prefix
Now lets update the moneyCounter
Code (Java):

        board.getTeam("moneyCounter").setPrefix(ChatColor.GREEN + "$" + economy.getBalance(player));
 
And now you have updated the scoreboard!

Using more than 16 characters under 1.13
If you are running a server with a version lower than 1.13 but still want to have up to 32 characters in a single line, you can do the following:
For that I am creating a new Team variable.
Code (Java):

        Team team = scoreboard.registerNewTeam("rank");
        // This string should be the message you want to put into the line
        String rank = getRank();
 
        // As above, create a new entry for the team
        team.addEntry(ChatColor.AQUA.toString() + ChatColor.DARK_AQUA);

        // The text doesn't have more than 16 characters, so we are fine
        if (rank.length() <= 16) {
            team.setPrefix(rank);
            return;
        }

        // If the text actually goes above 32, cut it to 32 to prevent kicks and errors
        if (rank.length() > 32) {
            rank = rank.substring(32);
        }
 
        // Set the prefix to the first 16 characters
        team.setPrefix(rank.substring(0, 16));
 
        // Now use the last 16 characters and put them into the suffix
        team.setSuffix(rank.substring(16));
 
But there's still a catch - the entry (which you set to be a random color) will be placed between prefix and suffix, which means the color will change inbetween. Thus, you have to set your original color at the beginning of the suffix (= right after the 16 character mark of your string) again!

If done, get the score in the scoreboard (in this case `ChatColor.AQUA.toString() + ChatColor.DARK_AQUA`) and set it to another line.
... You could actually go as far as 48 characters, but that would require yet another little workaround and 32 should really be enough for a single line. ;)

Thanks for reading and have a nice day :D