我目前正在为 Fabric 编写服务器端 Lifesteal Mod,并希望使用 记分板 中的 colors 显示玩家剩余的生命量。 我目前正在努力做到这一点,因为除了生成的没有任何解释的 Javadoc 之外,我找不到真正好的文档。 我希望这里有人能帮助我:
根据日志,玩家被分配到相应的团队,但只是没有“显示”给玩家。
所以问题可能是我没有更新它。但如何更新呢?
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;
}
}
已修复:由于我总是检查团队是否已经存在,因此永远无法设置新颜色