Updated TabList to handle new viewers seeing header and footer

This commit is contained in:
Nixotica 2023-04-19 09:45:34 -07:00
parent 48843c88cf
commit 984df871fb

View File

@ -2,6 +2,7 @@ package net.minestom.server.scoreboard;
import net.kyori.adventure.text.Component;
import net.minestom.server.entity.Player;
import net.minestom.server.network.packet.server.play.PlayerListHeaderAndFooterPacket;
import net.minestom.server.network.packet.server.play.ScoreboardObjectivePacket;
import org.jetbrains.annotations.NotNull;
@ -11,6 +12,10 @@ import java.util.concurrent.CopyOnWriteArraySet;
/**
* Represents the {@link Player} tab list as a {@link Scoreboard}.
* <p>
* This modified version for Hollow Cube mimicks Sidebars so that when players are added as a viewer, the packets aren't
* needed to be sent out again to all viewers of the TabList.
* </p>
*/
public class TabList implements Scoreboard {
@ -23,6 +28,9 @@ public class TabList implements Scoreboard {
private final Set<Player> unmodifiableViewers = Collections.unmodifiableSet(viewers);
private final String objectiveName;
private Component header = Component.empty();
private Component footer = Component.empty();
private ScoreboardObjectivePacket.Type type;
public TabList(String name, ScoreboardObjectivePacket.Type type) {
@ -49,12 +57,23 @@ public class TabList implements Scoreboard {
this.type = type;
}
public void setHeader(@NotNull Component header) {
this.header = header;
sendPacketToViewers(new PlayerListHeaderAndFooterPacket(header, footer));
}
public void setFooter(@NotNull Component footer) {
this.footer = footer;
sendPacketToViewers(new PlayerListHeaderAndFooterPacket(header, footer));
}
@Override
public boolean addViewer(@NotNull Player player) {
final boolean result = this.viewers.add(player);
if (result) {
player.sendPacket(this.getCreationObjectivePacket(Component.empty(), this.type));
player.sendPacket(this.getDisplayScoreboardPacket((byte) 0));
player.sendPacket(new PlayerListHeaderAndFooterPacket(header, footer));
}
return result;
}