Allow scores and teams to update more than once every millisecond

This commit is contained in:
Tim203 2021-10-04 20:46:38 +02:00
parent 11bc083885
commit 6011520043
No known key found for this signature in database
GPG key ID: 064EE9F5BF7C3EE8
3 changed files with 45 additions and 37 deletions

View file

@ -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;

View file

@ -135,35 +135,6 @@ public final class Scoreboard {
return team;
}
public Objective getObjective(String objectiveName) {
return objectives.get(objectiveName);
}
public Collection<Objective> 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<ScoreInfo> addScores = new ArrayList<>(lastAddScoreCount);
List<ScoreInfo> 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<Objective> 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.

View file

@ -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;