Track header/footer index per-player, clarify configuration, fix #29

This commit is contained in:
William278 2023-03-31 14:43:35 +01:00
parent 4c10d27b6b
commit 18086fffa4
3 changed files with 57 additions and 26 deletions

View File

@ -77,7 +77,8 @@ public class Settings {
); );
@YamlKey("update_rate") @YamlKey("update_rate")
@YamlComment("How often to periodically update the TAB list, including header and footer, for all users.\nWill only update on player join/leave if set to 0.") @YamlComment("How often in milliseconds to periodically update the TAB list, including header and footer, for all users.\n" +
"If set to 0, TAB will be updated on player join/leave instead. (1s = 1000ms)")
private int updateRate = 0; private int updateRate = 0;
public Settings(@NotNull Velocitab plugin) { public Settings(@NotNull Velocitab plugin) {
@ -92,14 +93,16 @@ public class Settings {
@NotNull @NotNull
public String getHeader(@NotNull String serverGroup, int index) { public String getHeader(@NotNull String serverGroup, int index) {
return StringEscapeUtils.unescapeJava( final List<String> groupHeaders = headers.getOrDefault(serverGroup, List.of(""));
headers.getOrDefault(serverGroup, List.of("")).get(index)); return groupHeaders.isEmpty() ? "" : StringEscapeUtils.unescapeJava(groupHeaders
.get(Math.max(0, Math.min(index, getHeaderListSize(serverGroup) - 1))));
} }
@NotNull @NotNull
public String getFooter(@NotNull String serverGroup, int index) { public String getFooter(@NotNull String serverGroup, int index) {
return StringEscapeUtils.unescapeJava( final List<String> groupFooters = footers.getOrDefault(serverGroup, List.of(""));
footers.getOrDefault(serverGroup, List.of("")).get(index)); return groupFooters.isEmpty() ? "" : StringEscapeUtils.unescapeJava(groupFooters
.get(Math.max(0, Math.min(index, getFooterListSize(serverGroup) - 1))));
} }
public int getHeaderListSize(@NotNull String serverGroup) { public int getHeaderListSize(@NotNull String serverGroup) {
@ -137,7 +140,8 @@ public class Settings {
public String getServerGroup(String serverName) { public String getServerGroup(String serverName) {
return serverGroups.entrySet().stream() return serverGroups.entrySet().stream()
.filter(entry -> entry.getValue().contains(serverName)).findFirst() .filter(entry -> entry.getValue().contains(serverName)).findFirst()
.map(Map.Entry::getKey).orElse(fallbackGroup); .map(Map.Entry::getKey)
.orElse(fallbackGroup);
} }
public boolean isPapiHookEnabled() { public boolean isPapiHookEnabled() {

View File

@ -17,6 +17,8 @@ public final class TabPlayer implements Comparable<TabPlayer> {
private final Player player; private final Player player;
private final Role role; private final Role role;
private final int highestWeight; private final int highestWeight;
private int headerIndex = 0;
private int footerIndex = 0;
public TabPlayer(@NotNull Player player, @NotNull Role role, int highestWeight) { public TabPlayer(@NotNull Player player, @NotNull Role role, int highestWeight) {
this.player = player; this.player = player;
@ -47,6 +49,16 @@ public final class TabPlayer implements Comparable<TabPlayer> {
.orElse("unknown"); .orElse("unknown");
} }
/**
* Get the TAB server group this player is connected to
* @param plugin instance of the {@link Velocitab} plugin
* @return the name of the server group the player is on
*/
@NotNull
public String getServerGroup(@NotNull Velocitab plugin) {
return plugin.getSettings().getServerGroup(this.getServerName());
}
/** /**
* Get the display name of the server the player is currently on. * Get the display name of the server the player is currently on.
* Affected by server aliases defined in the config. * Affected by server aliases defined in the config.
@ -79,6 +91,30 @@ public final class TabPlayer implements Comparable<TabPlayer> {
.thenAccept(footer -> player.sendPlayerListHeaderAndFooter(header, footer))); .thenAccept(footer -> player.sendPlayerListHeaderAndFooter(header, footer)));
} }
public int getHeaderIndex() {
return headerIndex;
}
public void incrementHeaderIndex(@NotNull Velocitab plugin) {
if (headerIndex >= plugin.getSettings().getHeaderListSize(getServerGroup(plugin))) {
headerIndex = 0;
return;
}
headerIndex++;
}
public int getFooterIndex() {
return footerIndex;
}
public void incrementFooterIndex(@NotNull Velocitab plugin) {
if (footerIndex >= plugin.getSettings().getFooterListSize(getServerGroup(plugin))) {
footerIndex = 0;
return;
}
footerIndex++;
}
@Override @Override
public int compareTo(@NotNull TabPlayer o) { public int compareTo(@NotNull TabPlayer o) {
final int roleDifference = role.compareTo(o.role); final int roleDifference = role.compareTo(o.role);

View File

@ -26,8 +26,6 @@ public class PlayerTabList {
private final Velocitab plugin; private final Velocitab plugin;
private final ConcurrentLinkedQueue<TabPlayer> players; private final ConcurrentLinkedQueue<TabPlayer> players;
private final ConcurrentLinkedQueue<String> fallbackServers; private final ConcurrentLinkedQueue<String> fallbackServers;
private int headerIndex = 0;
private int footerIndex = 0;
public PlayerTabList(@NotNull Velocitab plugin) { public PlayerTabList(@NotNull Velocitab plugin) {
this.plugin = plugin; this.plugin = plugin;
@ -157,6 +155,7 @@ public class PlayerTabList {
if (!tabPlayer.getPlayer().isActive()) { if (!tabPlayer.getPlayer().isActive()) {
return; return;
} }
players.forEach(player -> tabPlayer.getDisplayName(plugin).thenAccept(displayName -> { players.forEach(player -> tabPlayer.getDisplayName(plugin).thenAccept(displayName -> {
player.getPlayer().getTabList().getEntries().stream() player.getPlayer().getTabList().getEntries().stream()
.filter(e -> e.getProfile().getId().equals(tabPlayer.getPlayer().getUniqueId())).findFirst() .filter(e -> e.getProfile().getId().equals(tabPlayer.getPlayer().getUniqueId())).findFirst()
@ -167,27 +166,19 @@ public class PlayerTabList {
} }
public CompletableFuture<Component> getHeader(@NotNull TabPlayer player) { public CompletableFuture<Component> getHeader(@NotNull TabPlayer player) {
if (headerIndex >= plugin.getSettings().getHeaderListSize(plugin.getSettings().getServerGroup(player.getServerName()))){ final String header = plugin.getSettings().getHeader(player.getServerGroup(plugin), player.getHeaderIndex());
headerIndex = 0; player.incrementHeaderIndex(plugin);
}
CompletableFuture<Component> headerComponent = Placeholder.replace(plugin.getSettings().getHeader(
plugin.getSettings().getServerGroup(player.getServerName()), headerIndex), plugin, player)
.thenApply(header -> plugin.getFormatter().format(header, player, plugin));
headerIndex++;
return headerComponent;
return Placeholder.replace(header, plugin, player)
.thenApply(replaced -> plugin.getFormatter().format(replaced, player, plugin));
} }
public CompletableFuture<Component> getFooter(@NotNull TabPlayer player) { public CompletableFuture<Component> getFooter(@NotNull TabPlayer player) {
if (footerIndex >= plugin.getSettings().getFooterListSize(plugin.getSettings().getServerGroup(player.getServerName()))){ final String footer = plugin.getSettings().getFooter(player.getServerGroup(plugin), player.getFooterIndex());
footerIndex = 0; player.incrementFooterIndex(plugin);
}
CompletableFuture<Component> footerComponent = Placeholder.replace(plugin.getSettings().getFooter(
plugin.getSettings().getServerGroup(player.getServerName()), footerIndex), plugin, player)
.thenApply(footer -> plugin.getFormatter().format(footer, player, plugin));
footerIndex++;
return footerComponent;
return Placeholder.replace(footer, plugin, player)
.thenApply(replaced -> plugin.getFormatter().format(replaced, player, plugin));
} }
// Update the tab list periodically // Update the tab list periodically