Skip to content

Commit

Permalink
[Scoreboard] Fix ConcurrentModificationException when using API (#1410)
Browse files Browse the repository at this point in the history
  • Loading branch information
NEZNAMY committed Jan 18, 2025
1 parent 1f879ac commit 1cef440
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
* A class representing a scoreboard configured in config
Expand Down Expand Up @@ -50,7 +51,7 @@ public class ScoreboardImpl extends RefreshableFeature implements me.neznamy.tab
private boolean containsNumberFormat;

//players currently seeing this scoreboard
private final Set<TabPlayer> players = Collections.newSetFromMap(new WeakHashMap<>());
private final Set<TabPlayer> players = Collections.newSetFromMap(new ConcurrentHashMap<>());

/**
* Constructs new instance with given parameters and registers lines to feature manager
Expand Down Expand Up @@ -238,6 +239,9 @@ public void recalculateScores(@NonNull TabPlayer p) {
*/
public void removePlayerFromSet(@NonNull TabPlayer player) {
players.remove(player);
for (Line line : lines) {
((ScoreboardLine)line).removePlayerSilently(player);
}
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@

import lombok.Getter;
import lombok.NonNull;
import me.neznamy.tab.api.scoreboard.Line;
import me.neznamy.tab.shared.Limitations;
import me.neznamy.tab.shared.TAB;
import me.neznamy.tab.shared.TabConstants;
import me.neznamy.tab.shared.chat.EnumChatFormat;
import me.neznamy.tab.api.scoreboard.Line;
import me.neznamy.tab.shared.chat.TextColor;
import me.neznamy.tab.shared.cpu.ThreadExecutor;
import me.neznamy.tab.shared.features.scoreboard.ScoreRefresher;
import me.neznamy.tab.shared.features.scoreboard.ScoreboardImpl;
import me.neznamy.tab.shared.features.scoreboard.ScoreboardManagerImpl;
import me.neznamy.tab.shared.features.types.CustomThreaded;
import me.neznamy.tab.shared.features.types.RefreshableFeature;
import me.neznamy.tab.shared.platform.Scoreboard;
import me.neznamy.tab.shared.platform.TabPlayer;
import me.neznamy.tab.shared.features.scoreboard.ScoreboardImpl;
import me.neznamy.tab.shared.features.scoreboard.ScoreboardManagerImpl;
import org.jetbrains.annotations.NotNull;

import java.util.Collections;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;

/**
* Abstract class representing a line of scoreboard
Expand All @@ -46,7 +46,7 @@ public abstract class ScoreboardLine extends RefreshableFeature implements Line,

private final ScoreRefresher scoreRefresher;

private final Set<TabPlayer> shownPlayers = Collections.newSetFromMap(new WeakHashMap<>());
private final Set<TabPlayer> shownPlayers = Collections.newSetFromMap(new ConcurrentHashMap<>());

/**
* Constructs new instance with given parameters
Expand Down Expand Up @@ -258,6 +258,16 @@ protected void updateTeam(@NotNull TabPlayer player, @NotNull String prefix, @No
);
}

/**
* Silently removes players from the list of players this line is shown to.
*
* @param player
* Player to remove
*/
public void removePlayerSilently(@NonNull TabPlayer player) {
shownPlayers.remove(player);
}

@Override
@NotNull
public ThreadExecutor getCustomThread() {
Expand Down

0 comments on commit 1cef440

Please sign in to comment.