minecraftminecraft-fabric

Scoreboard Team update does not show for Players (Fabric MC)


I am currently programming a server-sided Lifesteal Mod for Fabric and want to display the amount of lives a player has left using colors in the scoreboard. I am currently struggling to do so since I can't really find a good documentation apart from the generated Javadocs that don't feature any explanations. I was hoping someone here could help me:

According to the Logs, the player is assigned to the corresponding team, but it's just not "shown" for the players.

So the problem is probably that I am not updating it. But how does one update it?

public class ScoreboardUtils {
    public static void updatePlayerTeam(ServerPlayerEntity p) {
        MinecraftServer server = p.getServer();
        if (server == null)
            return;

        int lives = PlayerDataManager.loadPlayerInt(p, "lives");

        ServerScoreboard sb = server.getScoreboard();
        String teamName = Integer.toString(lives);
        Formatting teamColor = getColorFromValue(lives);
        
        Team team = sb.getTeam(teamName);
        if(team == null) {
            team = sb.addTeam(teamName);
            team.setColor(teamColor);
        }
        
        String playername = p.getUuidAsString();
        removePlayerFromOtherTeams(playername, sb);
        team.getPlayerList().add(playername);
        Lifeanarchy.LOGGER.info(team.getPlayerList().toString() + " for " + teamName);
    }
    
    private static void removePlayerFromOtherTeams(String playerName, ServerScoreboard scoreboard) {
        for (Team team : scoreboard.getTeams()) {
            if (team.getPlayerList().contains(playerName)) {
                team.getPlayerList().remove(playerName);
            }
        }
    }

    private static Formatting getColorFromValue(int lives) {
        if (lives > 3)
            return Formatting.DARK_GREEN;
        else if (lives == 3)
            return Formatting.GREEN;
        else if (lives == 2)
            return Formatting.YELLOW;
        else if (lives == 1)
            return Formatting.RED;
        else return Formatting.GRAY;
    }
}

Solution

  • FIXED: Since I was always checking if the team already exists, the new colors could never be set