Cleanup ScoreboardUtil

This commit is contained in:
Marc Baloup 2021-08-28 16:20:14 +02:00
parent 2c933529f1
commit e721642780

View File

@ -14,28 +14,24 @@ import fr.pandacube.lib.core.chat.ChatUtil;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
public class ScoreBoardUtil { public class ScoreBoardUtil {
/**
* Met à jour la Sidebar d'un Scoreboard donné
*
* @param scBrd Le Scoreboard à mettre à jour (ne doit pas être null)
* @param title Le titre de la Sidebar, limité à 32 caractères
* @param lines Les lignes qui doivent être affichés. Si un éléments du
* tableau est null, il sera compté comme une chaine vide. Toutes les
* lignes seront rognés aux 40 premiers caractères
*/
@Deprecated
public static void updateScoreboardSidebar(Scoreboard scBrd, String title, String[] lines) {
updateScoreboardSidebar(scBrd, Chat.legacyText(title), lines);
}
/** /**
* Met à jour la Sidebar d'un Scoreboard donné * Update the sidebar of the provided scoreboard, with the given title and lines.
* *
* @param scBrd Le Scoreboard à mettre à jour (ne doit pas être null) * @param scBrd the scoreboard
* @param title Le titre de la Sidebar * @param title the title of the sidebar
* @param lines Les lignes qui doivent être affichés. Si un éléments du * @param lines the lines that have to be displayed. Null values are treated as empty lines.
* tableau est null, il sera compté comme une chaine vide. Toutes les * The lines support legacy formatting only, and will be truncated to 40 characters.
* lignes seront rognés aux 40 premiers caractères * Lines present multiple times will have hidden characters appended to make them different.
* Vanilla Java Edition clients only display the 15 first lines.
* @implNote The implementation makes sure that the minimum amount of data is transmitted to the client,
* to reduce bandwith usage and avoid the sidebar flickering.
* <ul>
* <li>If a provided line is already present in the sidebar, and at the same line number, it will not be updated.
* <li>If a provided line is already present but at another position, only the score (i.e. the line number) is updated.
* <li>If a provided line was not present before, it is added as a new score entry in the scoreboard.
* <li>If a line that was already present is not in the provided lines, it is removed from the scoreboard.
* <li>The title is only updated if it has actually changed.
*/ */
public static void updateScoreboardSidebar(Scoreboard scBrd, Chat title, String[] lines) { public static void updateScoreboardSidebar(Scoreboard scBrd, Chat title, String[] lines) {
if (scBrd == null) throw new IllegalArgumentException("scBrd doit être non null"); if (scBrd == null) throw new IllegalArgumentException("scBrd doit être non null");
@ -43,16 +39,22 @@ public class ScoreBoardUtil {
Objective obj = scBrd.getObjective("sidebar_autogen"); Objective obj = scBrd.getObjective("sidebar_autogen");
if (obj != null && !obj.getCriteria().equalsIgnoreCase("dummy")) { if (obj != null && !obj.getCriteria().equalsIgnoreCase("dummy")) {
// objective present but with wrong criteria, removing it
obj.unregister(); obj.unregister();
obj = null; obj = null;
} }
if (obj == null)
if (obj == null) {
obj = scBrd.registerNewObjective("sidebar_autogen", "dummy", title.getAdv()); obj = scBrd.registerNewObjective("sidebar_autogen", "dummy", title.getAdv());
obj.setDisplaySlot(DisplaySlot.SIDEBAR);
}
else { else {
// only update title if needed
if (!title.getAdv().equals(obj.displayName())) if (!title.getAdv().equals(obj.displayName()))
obj.displayName(title.getAdv()); obj.displayName(title.getAdv());
if (!DisplaySlot.SIDEBAR.equals(obj.getDisplaySlot())) // fix display slot if someone else changed it
if (DisplaySlot.SIDEBAR != obj.getDisplaySlot())
obj.setDisplaySlot(DisplaySlot.SIDEBAR); obj.setDisplaySlot(DisplaySlot.SIDEBAR);
} }
@ -61,18 +63,16 @@ public class ScoreBoardUtil {
List<String> listLines = Arrays.asList(lines); List<String> listLines = Arrays.asList(lines);
// remove lines that are not in the array // remove lines from the scoreboard that are not in the provided array
Objective fObj = obj; Objective fObj = obj;
scBrd.getEntries().stream() scBrd.getEntries().stream()
.filter(e -> !listLines.contains(e)) .filter(e -> !listLines.contains(e))
.filter(e -> fObj.getScore(e).isScoreSet()) .filter(e -> fObj.getScore(e).isScoreSet())
.forEach(scBrd::resetScores); .forEach(scBrd::resetScores);
// add/update others lines // add and update others lines
int boardPos = lines.length; int boardPos = lines.length;
for (String line : lines) { for (String line : lines) {
if (line == null) line = "";
Score score = obj.getScore(line); Score score = obj.getScore(line);
if (score.getScore() != boardPos) if (score.getScore() != boardPos)
@ -83,15 +83,24 @@ public class ScoreBoardUtil {
} }
/** /**
* Met à jour la Sidebar d'un Scoreboard donné * Update the sidebar of the provided scoreboard, with the given title and lines.
* *
* @param scBrd Le Scoreboard à mettre à jour * @param scBrd the scoreboard
* @param title Le titre de la Sidebar, limité à 32 caractères * @param title the title of the sidebar
* @param lines Les lignes qui doivent être affichés. Si un éléments du * @param lines the lines that have to be displayed. Null values are treated as empty lines.
* tableau est null, il sera compté comme une chaine vide. Toutes les * The lines support legacy formatting only, and will be truncated to 40 characters.
* lignes seront rognés aux 40 premiers caractères * Lines present multiple times will have hidden characters appended to make them different.
* Vanilla Java Edition clients only display the 15 first lines.
* @implNote The implementation makes sure that the minimum amount of data is transmitted to the client,
* to reduce bandwith usage and avoid the sidebar flickering.
* <ul>
* <li>If a provided line is already present in the sidebar, and at the same line number, it will not be updated.
* <li>If a provided line is already present but at another position, only the score (i.e. the line number) is updated.
* <li>If a provided line was not present before, it is added as a new score entry in the scoreboard.
* <li>If a line that was already present is not in the provided lines, it is removed from the scoreboard.
* <li>The title is only updated if it has actually changed.
*/ */
public static void updateScoreboardSidebar(Scoreboard scBrd, String title, List<String> lines) { public static void updateScoreboardSidebar(Scoreboard scBrd, Chat title, List<String> lines) {
updateScoreboardSidebar(scBrd, title, lines.toArray(new String[lines.size()])); updateScoreboardSidebar(scBrd, title, lines.toArray(new String[lines.size()]));
} }