From 60115200439362bb9f470e522c126bc4acee9f21 Mon Sep 17 00:00:00 2001 From: Tim203 Date: Mon, 4 Oct 2021 20:46:38 +0200 Subject: [PATCH] Allow scores and teams to update more than once every millisecond --- .../geysermc/connector/scoreboard/Score.java | 12 ++-- .../connector/scoreboard/Scoreboard.java | 62 ++++++++++--------- .../geysermc/connector/scoreboard/Team.java | 8 +-- 3 files changed, 45 insertions(+), 37 deletions(-) diff --git a/connector/src/main/java/org/geysermc/connector/scoreboard/Score.java b/connector/src/main/java/org/geysermc/connector/scoreboard/Score.java index 7f3ac6e60..10abfa29a 100644 --- a/connector/src/main/java/org/geysermc/connector/scoreboard/Score.java +++ b/connector/src/main/java/org/geysermc/connector/scoreboard/Score.java @@ -59,6 +59,10 @@ public final class Score { return name; } + public int getScore() { + return currentData.getScore(); + } + public Score setScore(int score) { currentData.score = score; return this; @@ -90,14 +94,14 @@ public final class Score { public Score setUpdateType(UpdateType updateType) { if (updateType != UpdateType.NOTHING) { - currentData.updateTime = System.currentTimeMillis(); + currentData.changed = true; } currentData.updateType = updateType; return this; } public boolean shouldUpdate() { - return cachedData == null || currentData.updateTime > cachedData.updateTime || + return cachedData == null || currentData.changed || (currentData.team != null && currentData.team.shouldUpdate()); } @@ -112,7 +116,7 @@ public final class Score { cachedData.updateType = currentData.updateType; } - cachedData.updateTime = currentData.updateTime; + currentData.changed = false; cachedData.team = currentData.team; cachedData.score = currentData.score; @@ -127,7 +131,7 @@ public final class Score { @Getter public static final class ScoreData { private UpdateType updateType; - private long updateTime; + private boolean changed; private Team team; private int score; diff --git a/connector/src/main/java/org/geysermc/connector/scoreboard/Scoreboard.java b/connector/src/main/java/org/geysermc/connector/scoreboard/Scoreboard.java index 3596907e7..6f4b540ac 100644 --- a/connector/src/main/java/org/geysermc/connector/scoreboard/Scoreboard.java +++ b/connector/src/main/java/org/geysermc/connector/scoreboard/Scoreboard.java @@ -135,35 +135,6 @@ public final class Scoreboard { return team; } - public Objective getObjective(String objectiveName) { - return objectives.get(objectiveName); - } - - public Collection getObjectives() { - return objectives.values(); - } - - public Team getTeam(String teamName) { - return teams.get(teamName); - } - - public void unregisterObjective(String objectiveName) { - Objective objective = getObjective(objectiveName); - if (objective != null) { - objective.pendingRemove(); - } - } - - public void removeTeam(String teamName) { - Team remove = teams.remove(teamName); - if (remove != null) { - remove.setUpdateType(REMOVE); - // We need to use the direct entities list here, so #refreshSessionPlayerDisplays also updates accordingly - // With the player's lack of a team in visibility checks - updateEntityNames(remove, remove.getEntities(), true); - } - } - public void onUpdate() { List addScores = new ArrayList<>(lastAddScoreCount); List removeScores = new ArrayList<>(lastRemoveScoreCount); @@ -324,6 +295,29 @@ public final class Scoreboard { session.sendUpstreamPacket(removeObjectivePacket); } + public Objective getObjective(String objectiveName) { + return objectives.get(objectiveName); + } + + public Collection getObjectives() { + return objectives.values(); + } + + public void unregisterObjective(String objectiveName) { + Objective objective = getObjective(objectiveName); + if (objective != null) { + objective.pendingRemove(); + } + } + + public Objective getSlot(ScoreboardPosition slot) { + return objectiveSlots.get(slot); + } + + public Team getTeam(String teamName) { + return teams.get(teamName); + } + public Team getTeamFor(String entity) { for (Team team : teams.values()) { if (team.hasEntity(entity)) { @@ -333,6 +327,16 @@ public final class Scoreboard { return null; } + public void removeTeam(String teamName) { + Team remove = teams.remove(teamName); + if (remove != null) { + remove.setUpdateType(REMOVE); + // We need to use the direct entities list here, so #refreshSessionPlayerDisplays also updates accordingly + // With the player's lack of a team in visibility checks + updateEntityNames(remove, remove.getEntities(), true); + } + } + /** * Updates the display names of all entities in a given team. * @param teamChange the players have either joined or left the team. Used for optimizations when just the display name updated. diff --git a/connector/src/main/java/org/geysermc/connector/scoreboard/Team.java b/connector/src/main/java/org/geysermc/connector/scoreboard/Team.java index b5aef31d0..fe986b7aa 100644 --- a/connector/src/main/java/org/geysermc/connector/scoreboard/Team.java +++ b/connector/src/main/java/org/geysermc/connector/scoreboard/Team.java @@ -139,7 +139,7 @@ public final class Team { } public boolean shouldUpdate() { - return updating || cachedData == null || currentData.updateTime > cachedData.updateTime; + return updating || cachedData == null || currentData.changed; } public void prepareUpdate() { @@ -155,7 +155,7 @@ public final class Team { cachedData.updateType = currentData.updateType; } - cachedData.updateTime = currentData.updateTime; + cachedData.changed = currentData.changed; cachedData.name = currentData.name; cachedData.prefix = currentData.prefix; cachedData.suffix = currentData.suffix; @@ -171,7 +171,7 @@ public final class Team { public Team setUpdateType(UpdateType updateType) { if (updateType != UpdateType.NOTHING) { - currentData.updateTime = System.currentTimeMillis(); + currentData.changed = true; } currentData.updateType = updateType; return this; @@ -198,7 +198,7 @@ public final class Team { @Getter public static final class TeamData { private UpdateType updateType; - private long updateTime; + private boolean changed; private String name; private String prefix;