Skip to content

Commit

Permalink
[Scoreboard] Use separate maps for scoreboard-related properties for …
Browse files Browse the repository at this point in the history
…faster access (also fix a theoretical memory leak if sending too many custom scoreboards with API)
  • Loading branch information
NEZNAMY committed Sep 1, 2024
1 parent d0476a9 commit 6485356
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ public class ScoreRefresher extends RefreshableFeature implements CustomThreaded

private static final StringToComponentCache cache = new StringToComponentCache("Scoreboard NumberFormat", 1000);

private final String NUMBER_FORMAT_PROPERTY = Property.randomName();

/** Line this score belongs to */
@NonNull private final ScoreboardLine line;

Expand All @@ -38,7 +36,7 @@ public String getRefreshDisplayName() {
@Override
public void refresh(@NotNull TabPlayer refreshed, boolean force) {
if (refreshed.scoreboardData.activeScoreboard != line.getParent()) return; //player has different scoreboard displayed
if (refreshed.getProperty(NUMBER_FORMAT_PROPERTY) == null) return; // Shrug
if (refreshed.scoreboardData.numberFormatProperties.get(line) == null) return; // Shrug
refreshed.getScoreboard().setScore(
ScoreboardManagerImpl.OBJECTIVE_NAME,
line.getPlayerName(refreshed),
Expand All @@ -55,7 +53,7 @@ public void refresh(@NotNull TabPlayer refreshed, boolean force) {
* Player to register properties for
*/
public void registerProperties(@NotNull TabPlayer player) {
player.setProperty(this, NUMBER_FORMAT_PROPERTY, numberFormat);
player.scoreboardData.numberFormatProperties.put(line, new Property(this, player, numberFormat));
}

/**
Expand All @@ -67,7 +65,7 @@ public void registerProperties(@NotNull TabPlayer player) {
*/
@Nullable
public TabComponent getNumberFormat(@NotNull TabPlayer player) {
return cache.get(player.getProperty(NUMBER_FORMAT_PROPERTY).updateAndGet());
return cache.get(player.scoreboardData.numberFormatProperties.get(line).updateAndGet());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
@Getter
public class ScoreboardImpl extends RefreshableFeature implements me.neznamy.tab.api.scoreboard.Scoreboard, CustomThreaded {

private final String titleProperty = Property.randomName();

//scoreboard manager
private final ScoreboardManagerImpl manager;

Expand Down Expand Up @@ -135,11 +133,11 @@ public boolean isConditionMet(@NonNull TabPlayer p) {
*/
public void addPlayer(@NonNull TabPlayer p) {
if (p.scoreboardData.activeScoreboard == this) return; // already registered
p.setProperty(this, titleProperty, title);
p.scoreboardData.titleProperty = new Property(this, p, title);
p.getScoreboard().registerObjective(
Scoreboard.DisplaySlot.SIDEBAR,
ScoreboardManagerImpl.OBJECTIVE_NAME,
manager.getCache().get(p.getProperty(titleProperty).updateAndGet()),
manager.getCache().get(p.scoreboardData.titleProperty.get()),
Scoreboard.HealthDisplay.INTEGER,
new SimpleComponent("")
);
Expand All @@ -166,6 +164,10 @@ public void removePlayer(@NonNull TabPlayer p) {
p.getScoreboard().unregisterTeam(((ScoreboardLine)line).getTeamName());
}
p.scoreboardData.activeScoreboard = null;
p.scoreboardData.titleProperty = null;
p.scoreboardData.lineProperties.clear();
p.scoreboardData.lineNameProperties.clear();
p.scoreboardData.numberFormatProperties.clear();
TAB.getInstance().getPlaceholderManager().getTabExpansion().setScoreboardName(p, "");
}

Expand All @@ -180,7 +182,7 @@ public void refresh(@NotNull TabPlayer refreshed, boolean force) {
if (refreshed.scoreboardData.activeScoreboard != this) return; //player has different scoreboard displayed
refreshed.getScoreboard().updateObjective(
ScoreboardManagerImpl.OBJECTIVE_NAME,
manager.getCache().get(refreshed.getProperty(titleProperty).updateAndGet()),
manager.getCache().get(refreshed.scoreboardData.titleProperty.updateAndGet()),
Scoreboard.HealthDisplay.INTEGER,
new SimpleComponent("")
);
Expand All @@ -199,7 +201,7 @@ public void recalculateScores(@NonNull TabPlayer p) {
Collections.reverse(linesReversed);
int score = manager.getConfiguration().staticNumber;
for (Line line : linesReversed) {
Property pr = p.getProperty(((ScoreboardLine) line).getTextProperty());
Property pr = p.scoreboardData.lineProperties.get((ScoreboardLine) line);
if (pr.getCurrentRawValue().isEmpty() || (!pr.getCurrentRawValue().isEmpty() && !pr.get().isEmpty())) {
p.getScoreboard().setScore(
ScoreboardManagerImpl.OBJECTIVE_NAME,
Expand Down Expand Up @@ -237,10 +239,10 @@ public void setTitle(@NonNull String title) {
ensureActive();
this.title = title;
for (TabPlayer p : players) {
p.setProperty(this, titleProperty, title);
p.scoreboardData.titleProperty.changeRawValue(title);
p.getScoreboard().updateObjective(
ScoreboardManagerImpl.OBJECTIVE_NAME,
manager.getCache().get(p.getProperty(titleProperty).get()),
manager.getCache().get(p.scoreboardData.titleProperty.get()),
Scoreboard.HealthDisplay.INTEGER,
new SimpleComponent("")
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import lombok.Getter;
import lombok.NonNull;
import me.neznamy.tab.shared.Property;
import me.neznamy.tab.shared.TabConstants;
import me.neznamy.tab.api.scoreboard.ScoreboardManager;
import me.neznamy.tab.shared.TAB;
import me.neznamy.tab.shared.config.files.config.ScoreboardConfiguration;
import me.neznamy.tab.shared.config.files.config.ScoreboardConfiguration.ScoreboardDefinition;
import me.neznamy.tab.shared.cpu.ThreadExecutor;
import me.neznamy.tab.shared.cpu.TimedCaughtTask;
import me.neznamy.tab.shared.features.scoreboard.lines.ScoreboardLine;
import me.neznamy.tab.shared.platform.decorators.SafeScoreboard;
import me.neznamy.tab.shared.platform.Scoreboard;
import me.neznamy.tab.shared.platform.TabPlayer;
Expand Down Expand Up @@ -377,5 +379,21 @@ public static class PlayerData {
/** Scoreboard sent by another plugin (objective name) */
@Nullable
public String otherPluginScoreboard;

/** Property of scoreboard title of scoreboard the player can currently see */
@Nullable
public Property titleProperty;

/** Map of line text properties */
@NotNull
public final Map<ScoreboardLine, Property> lineProperties = new IdentityHashMap<>();

/** Map of line player name properties (used in long lines) */
@NotNull
public final Map<ScoreboardLine, Property> lineNameProperties = new IdentityHashMap<>();

/** Map of line NumberFormat properties */
@NotNull
public final Map<ScoreboardLine, Property> numberFormatProperties = new IdentityHashMap<>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
*/
public class LongLine extends ScoreboardLine {

private final String nameProperty = Property.randomName();

/**
* Constructs new instance with given parameters
*
Expand All @@ -35,45 +33,46 @@ public LongLine(@NonNull ScoreboardImpl parent, int lineNumber, @NonNull String
@Override
public void refresh(@NotNull TabPlayer refreshed, boolean force) {
if (refreshed.scoreboardData.activeScoreboard != parent) return; //player has different scoreboard displayed
if (refreshed.getProperty(textProperty).update()) {
Property lineProperty = refreshed.scoreboardData.lineProperties.get(this);
if (lineProperty.update()) {
if (refreshed.getVersion().getMinorVersion() >= 13) {
updateTeam(refreshed, refreshed.getProperty(textProperty).get(), "");
updateTeam(refreshed, lineProperty.get(), "");
} else {
removeLine(refreshed, refreshed.getProperty(nameProperty).get());
removeLine(refreshed, refreshed.scoreboardData.lineNameProperties.get(this).get());
String[] values = splitText(
getPlayerName(lineNumber),
parent.getManager().getCache().get(refreshed.getProperty(textProperty).get()).toLegacyText(),
parent.getManager().getCache().get(lineProperty.get()).toLegacyText(),
refreshed.getVersion().getMinorVersion() >= 8 ? Limitations.SCOREBOARD_SCORE_LENGTH_1_8 : Limitations.SCOREBOARD_SCORE_LENGTH_1_7
);
addLine(refreshed, values[1], values[0], values[2]);
refreshed.setProperty(this, nameProperty, values[1]);
refreshed.scoreboardData.lineNameProperties.get(this).changeRawValue(values[1]);
}
}
}

@Override
public void register(@NonNull TabPlayer p) {
p.setProperty(this, textProperty, text);
p.scoreboardData.lineProperties.put(this, new Property(this, p, text));
getScoreRefresher().registerProperties(p);
String value = p.getProperty(textProperty).get();
String value = p.scoreboardData.lineProperties.get(this).get();
if (p.getVersion().getMinorVersion() >= 13) {
addLine(p, playerName, value, "");
p.setProperty(this, nameProperty, playerName);
p.scoreboardData.lineNameProperties.put(this, new Property(this, p, playerName));
} else {
String[] values = splitText(
playerName,
parent.getManager().getCache().get(value).toLegacyText(),
p.getVersion().getMinorVersion() >= 8 ? Limitations.SCOREBOARD_SCORE_LENGTH_1_8 : Limitations.SCOREBOARD_SCORE_LENGTH_1_7
);
addLine(p, values[1], values[0], values[2]);
p.setProperty(this, nameProperty, values[1]);
p.scoreboardData.lineNameProperties.put(this, new Property(this, p, values[1]));
}
}

@Override
public void unregister(@NonNull TabPlayer p) {
if (p.scoreboardData.activeScoreboard == parent) {
removeLine(p, p.getProperty(nameProperty).get());
removeLine(p, p.scoreboardData.lineNameProperties.get(this).get());
}
}

Expand All @@ -82,13 +81,13 @@ public void setText(@NonNull String text) {
ensureActive();
initializeText(text);
for (TabPlayer p : parent.getPlayers()) {
p.setProperty(this, textProperty, text);
p.scoreboardData.lineProperties.get(this).changeRawValue(text);
refresh(p, false);
}
}

@Override
public String getPlayerName(@NonNull TabPlayer viewer) {
return viewer.getProperty(nameProperty).get();
return viewer.scoreboardData.lineNameProperties.get(this).get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import lombok.Getter;
import lombok.NonNull;
import me.neznamy.tab.shared.Limitations;
import me.neznamy.tab.shared.Property;
import me.neznamy.tab.shared.TAB;
import me.neznamy.tab.shared.TabConstants;
import me.neznamy.tab.shared.chat.EnumChatFormat;
Expand Down Expand Up @@ -31,8 +30,6 @@ public abstract class ScoreboardLine extends RefreshableFeature implements Line,
//ID of this line
protected final int lineNumber;

protected final String textProperty = Property.randomName();

//text to display
protected String text;
protected String numberFormat;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void refresh(@NotNull TabPlayer refreshed, boolean force) {

@Override
public void register(@NonNull TabPlayer p) {
p.setProperty(this, textProperty, text);
p.scoreboardData.lineProperties.put(this, new Property(this, p, text));
getScoreRefresher().registerProperties(p);
String[] prefixSuffix = replaceText(p, true, true);
if (prefixSuffix.length == 0) return;
Expand All @@ -51,7 +51,7 @@ public void register(@NonNull TabPlayer p) {

@Override
public void unregister(@NonNull TabPlayer p) {
if (p.scoreboardData.activeScoreboard == parent && !p.getProperty(textProperty).get().isEmpty()) {
if (p.scoreboardData.activeScoreboard == parent && !p.scoreboardData.lineProperties.get(this).get().isEmpty()) {
removeLine(p, getPlayerName());
}
}
Expand All @@ -69,7 +69,7 @@ public void unregister(@NonNull TabPlayer p) {
* @return array of 2 elements for prefix/suffix
*/
private String[] replaceText(TabPlayer p, boolean force, boolean suppressToggle) {
Property scoreProperty = p.getProperty(textProperty);
Property scoreProperty = p.scoreboardData.lineProperties.get(this);
if (scoreProperty == null) return EMPTY_ARRAY; //not actually loaded yet (force refresh called from placeholder manager register method)
boolean emptyBefore = scoreProperty.get().isEmpty();
if (!scoreProperty.update() && !force) return EMPTY_ARRAY;
Expand Down Expand Up @@ -131,7 +131,7 @@ public void setText(@NonNull String text) {
ensureActive();
initializeText(text);
for (TabPlayer p : parent.getPlayers()) {
p.setProperty(this, textProperty, text);
p.scoreboardData.lineProperties.get(this).changeRawValue(text);
refresh(p, true);
}
}
Expand Down

0 comments on commit 6485356

Please sign in to comment.