Change Sidebar's queue into a Set, added Sidebar#getLines

This commit is contained in:
themode 2021-01-25 17:48:08 +01:00
parent fa02a12c1e
commit f4a3a9e733

View File

@ -1,6 +1,5 @@
package net.minestom.server.scoreboard; package net.minestom.server.scoreboard;
import com.google.common.collect.Queues;
import it.unimi.dsi.fastutil.ints.IntLinkedOpenHashSet; import it.unimi.dsi.fastutil.ints.IntLinkedOpenHashSet;
import net.minestom.server.chat.ChatParser; import net.minestom.server.chat.ChatParser;
import net.minestom.server.chat.ColoredText; import net.minestom.server.chat.ColoredText;
@ -16,8 +15,6 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.Collections; import java.util.Collections;
import java.util.Iterator;
import java.util.Queue;
import java.util.Set; import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet; import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
@ -37,7 +34,7 @@ public class Sidebar implements Scoreboard {
private static final AtomicInteger COUNTER = new AtomicInteger(); private static final AtomicInteger COUNTER = new AtomicInteger();
/** /**
* <b>WARNING:</b> You shouldn't create scoreboards/teams with the same prefixes as those * <b>WARNING:</b> You should NOT create any scoreboards/teams with the same prefixes as those
*/ */
private static final String SCOREBOARD_PREFIX = "sb-"; private static final String SCOREBOARD_PREFIX = "sb-";
private static final String TEAM_PREFIX = "sbt-"; private static final String TEAM_PREFIX = "sbt-";
@ -48,9 +45,8 @@ public class Sidebar implements Scoreboard {
private static final int MAX_LINES_COUNT = 15; private static final int MAX_LINES_COUNT = 15;
private final Set<Player> viewers = new CopyOnWriteArraySet<>(); private final Set<Player> viewers = new CopyOnWriteArraySet<>();
private final Set<Player> unmodifiableViewers = Collections.unmodifiableSet(viewers);
private final Queue<ScoreboardLine> lines = Queues.newConcurrentLinkedQueue(); private final Set<ScoreboardLine> lines = new CopyOnWriteArraySet<>();
private final IntLinkedOpenHashSet availableColors = new IntLinkedOpenHashSet(); private final IntLinkedOpenHashSet availableColors = new IntLinkedOpenHashSet();
private final String objectiveName; private final String objectiveName;
@ -164,26 +160,33 @@ public class Sidebar implements Scoreboard {
return null; return null;
} }
/**
* Gets a {@link Set} containing all the registered lines.
*
* @return an unmodifiable set containing the sidebar's lines
*/
@NotNull
public Set<ScoreboardLine> getLines() {
return Collections.unmodifiableSet(lines);
}
/** /**
* Removes a {@link ScoreboardLine} through the given identifier * Removes a {@link ScoreboardLine} through the given identifier
* *
* @param id the identifier of the {@link ScoreboardLine} * @param id the identifier of the {@link ScoreboardLine}
*/ */
public void removeLine(@NotNull String id) { public void removeLine(@NotNull String id) {
synchronized (lines) { this.lines.removeIf(line -> {
Iterator<ScoreboardLine> iterator = lines.iterator();
while (iterator.hasNext()) {
final ScoreboardLine line = iterator.next();
if (line.id.equals(id)) { if (line.id.equals(id)) {
// Remove the line for current viewers // Remove the line for current viewers
sendPacketsToViewers(line.getScoreCreationPacket(objectiveName), line.sidebarTeam.getDestructionPacket()); sendPacketsToViewers(line.getScoreCreationPacket(objectiveName), line.sidebarTeam.getDestructionPacket());
line.returnName(availableColors); line.returnName(availableColors);
iterator.remove(); return true;
}
}
} }
return false;
});
} }
@Override @Override
@ -221,7 +224,7 @@ public class Sidebar implements Scoreboard {
@NotNull @NotNull
@Override @Override
public Set<Player> getViewers() { public Set<Player> getViewers() {
return unmodifiableViewers; return Collections.unmodifiableSet(viewers);
} }
@Override @Override
@ -258,7 +261,7 @@ public class Sidebar implements Scoreboard {
*/ */
private SidebarTeam sidebarTeam; private SidebarTeam sidebarTeam;
public ScoreboardLine(String id, JsonMessage content, int line) { public ScoreboardLine(@NotNull String id, @NotNull JsonMessage content, int line) {
this.id = id; this.id = id;
this.content = content; this.content = content;
this.line = line; this.line = line;
@ -271,6 +274,7 @@ public class Sidebar implements Scoreboard {
* *
* @return the line identifier * @return the line identifier
*/ */
@NotNull
public String getId() { public String getId() {
return id; return id;
} }
@ -280,6 +284,7 @@ public class Sidebar implements Scoreboard {
* *
* @return The line content * @return The line content
*/ */
@NotNull
public JsonMessage getContent() { public JsonMessage getContent() {
return sidebarTeam == null ? content : sidebarTeam.getPrefix(); return sidebarTeam == null ? content : sidebarTeam.getPrefix();
} }
@ -473,7 +478,7 @@ public class Sidebar implements Scoreboard {
* *
* @param prefix The refreshed prefix * @param prefix The refreshed prefix
*/ */
private void refreshPrefix(JsonMessage prefix) { private void refreshPrefix(@NotNull JsonMessage prefix) {
this.prefix = prefix; this.prefix = prefix;
} }
} }