From ea40814c714fe8a19fd3c1b8efd4a5ce4daa6e28 Mon Sep 17 00:00:00 2001 From: AlexDev_ <56083016+alexdev03@users.noreply.github.com> Date: Sun, 5 Jan 2025 18:30:39 +0100 Subject: [PATCH] Sorting improvements --- .../william278/velocitab/config/Group.java | 4 +-- .../velocitab/hook/LuckPermsHook.java | 3 +- .../velocitab/packet/ScoreboardManager.java | 12 +++++-- .../placeholder/PlaceholderManager.java | 4 ++- .../net/william278/velocitab/player/Role.java | 2 +- .../velocitab/player/TabPlayer.java | 7 ----- .../velocitab/sorting/SortingManager.java | 2 +- .../velocitab/tab/PlayerTabList.java | 31 +++++++++---------- .../velocitab/tab/TabListListener.java | 2 +- 9 files changed, 34 insertions(+), 33 deletions(-) diff --git a/src/main/java/net/william278/velocitab/config/Group.java b/src/main/java/net/william278/velocitab/config/Group.java index cdf1118..4b1b965 100644 --- a/src/main/java/net/william278/velocitab/config/Group.java +++ b/src/main/java/net/william278/velocitab/config/Group.java @@ -163,7 +163,7 @@ public record Group( return plugin.getTabList().getPlayers() .values() .stream() - .filter(tabPlayer -> tabPlayer.getGroup().equals(this)) + .filter(tabPlayer -> tabPlayer.isLoaded() && tabPlayer.getGroup().equals(this)) .collect(Collectors.toSet()); } @@ -177,7 +177,7 @@ public record Group( return plugin.getTabList().getPlayers() .values() .stream() - .filter(player -> player.getGroup().equals(this) && player.getServerName().equals(tabPlayer.getServerName())) + .filter(player -> player.isLoaded() && player.getGroup().equals(this) && player.getServerName().equals(tabPlayer.getServerName())) .collect(Collectors.toSet()); } diff --git a/src/main/java/net/william278/velocitab/hook/LuckPermsHook.java b/src/main/java/net/william278/velocitab/hook/LuckPermsHook.java index a418c44..69d94d2 100644 --- a/src/main/java/net/william278/velocitab/hook/LuckPermsHook.java +++ b/src/main/java/net/william278/velocitab/hook/LuckPermsHook.java @@ -120,7 +120,7 @@ public class LuckPermsHook extends Hook { tabList.getVanishTabList().recalculateVanishForPlayer(tabPlayer); checkRoleUpdate(tabPlayer, oldRole); }) - .delay(500, TimeUnit.MILLISECONDS) + .delay(100, TimeUnit.MILLISECONDS) .schedule()); } @@ -145,6 +145,7 @@ public class LuckPermsHook extends Hook { if (oldRole.equals(player.getRole())) { return; } + plugin.getTabList().updatePlayer(player, false); } diff --git a/src/main/java/net/william278/velocitab/packet/ScoreboardManager.java b/src/main/java/net/william278/velocitab/packet/ScoreboardManager.java index 870de0b..fc04a86 100644 --- a/src/main/java/net/william278/velocitab/packet/ScoreboardManager.java +++ b/src/main/java/net/william278/velocitab/packet/ScoreboardManager.java @@ -41,6 +41,8 @@ import org.jetbrains.annotations.NotNull; import org.slf4j.event.Level; import java.util.*; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import static com.velocitypowered.api.network.ProtocolVersion.*; @@ -48,6 +50,7 @@ public class ScoreboardManager { private PacketRegistration packetRegistration; private final Velocitab plugin; + private final ExecutorService executorService; private final boolean teams; private final Map versions; @Getter @@ -64,8 +67,9 @@ public class ScoreboardManager { this.nametags = Maps.newConcurrentMap(); this.versions = Maps.newHashMap(); this.trackedTeams = Multimaps.synchronizedMultimap(Multimaps.newSetMultimap(Maps.newConcurrentMap(), Sets::newConcurrentHashSet)); - this.sortedTeams = new SortedSet(Comparator.reverseOrder()); + this.sortedTeams = new SortedSet(Comparator.reverseOrder()); //Comparator.reverseOrder() this.registerVersions(); + this.executorService = Executors.newFixedThreadPool(2); } public boolean handleTeams() { @@ -389,8 +393,10 @@ public class ScoreboardManager { return; } - final ConnectedPlayer connectedPlayer = (ConnectedPlayer) player; - connectedPlayer.getConnection().write(packet); + executorService.submit(() -> { + final ConnectedPlayer connectedPlayer = (ConnectedPlayer) player; + connectedPlayer.getConnection().write(packet); + }); } public void registerPacket() { diff --git a/src/main/java/net/william278/velocitab/placeholder/PlaceholderManager.java b/src/main/java/net/william278/velocitab/placeholder/PlaceholderManager.java index 70eedc5..985160c 100644 --- a/src/main/java/net/william278/velocitab/placeholder/PlaceholderManager.java +++ b/src/main/java/net/william278/velocitab/placeholder/PlaceholderManager.java @@ -76,7 +76,9 @@ public class PlaceholderManager { } final Map parsed = placeholders.computeIfAbsent(uuid, k -> Maps.newConcurrentMap()); final TabPlayer tabPlayer = plugin.getTabList().getTabPlayer(player) - .orElse(new TabPlayer(plugin, player, Role.DEFAULT_ROLE, plugin.getTabList().getGroupOrDefault(player))); + .orElse(new TabPlayer(plugin, player, + plugin.getLuckPermsHook().map(hook -> hook.getPlayerRole(player)).orElse(Role.DEFAULT_ROLE), + plugin.getTabList().getGroupOrDefault(player))); final List placeholders = texts.stream() .map(PlaceholderManager::extractPlaceholders) diff --git a/src/main/java/net/william278/velocitab/player/Role.java b/src/main/java/net/william278/velocitab/player/Role.java index d62c539..99568ad 100644 --- a/src/main/java/net/william278/velocitab/player/Role.java +++ b/src/main/java/net/william278/velocitab/player/Role.java @@ -65,7 +65,7 @@ public class Role implements Comparable { } @NotNull - protected String getWeightString() { + public String getWeightString() { return Integer.toString(weight); } diff --git a/src/main/java/net/william278/velocitab/player/TabPlayer.java b/src/main/java/net/william278/velocitab/player/TabPlayer.java index cee4200..8e669b8 100644 --- a/src/main/java/net/william278/velocitab/player/TabPlayer.java +++ b/src/main/java/net/william278/velocitab/player/TabPlayer.java @@ -51,7 +51,6 @@ public final class TabPlayer implements Comparable { // Each TabPlayer contains the components for each TabPlayer it's currently viewing this player private final Map relationalDisplayNames; private final Map relationalNametags; - private final Map cachedListOrders; private String lastDisplayName; private Component lastHeader; private Component lastFooter; @@ -81,7 +80,6 @@ public final class TabPlayer implements Comparable { this.group = group; this.relationalDisplayNames = Maps.newConcurrentMap(); this.relationalNametags = Maps.newConcurrentMap(); - this.cachedListOrders = Maps.newConcurrentMap(); } @NotNull @@ -183,10 +181,6 @@ public final class TabPlayer implements Comparable { relationalNametags.remove(target); } - public void unsetTabListOrder(@NotNull UUID target) { - cachedListOrders.remove(target); - } - public Optional getRelationalNametag(@NotNull UUID target) { return Optional.ofNullable(relationalNametags.get(target)); } @@ -199,7 +193,6 @@ public final class TabPlayer implements Comparable { lastFooter = null; role = Role.DEFAULT_ROLE; teamName = null; - cachedListOrders.clear(); } /** diff --git a/src/main/java/net/william278/velocitab/sorting/SortingManager.java b/src/main/java/net/william278/velocitab/sorting/SortingManager.java index 37ab5cc..e9609ac 100644 --- a/src/main/java/net/william278/velocitab/sorting/SortingManager.java +++ b/src/main/java/net/william278/velocitab/sorting/SortingManager.java @@ -48,7 +48,7 @@ public class SortingManager { .stream() .map(s -> plugin.getPlaceholderManager().applyPlaceholders(player, s)) .map(s -> adaptValue(s, player)) - .toList(); + .collect(Collectors.toList()); return handleList(player, placeholders); } diff --git a/src/main/java/net/william278/velocitab/tab/PlayerTabList.java b/src/main/java/net/william278/velocitab/tab/PlayerTabList.java index 90348be..7118878 100644 --- a/src/main/java/net/william278/velocitab/tab/PlayerTabList.java +++ b/src/main/java/net/william278/velocitab/tab/PlayerTabList.java @@ -28,8 +28,6 @@ import com.velocitypowered.api.proxy.player.TabListEntry; import com.velocitypowered.api.proxy.server.RegisteredServer; import com.velocitypowered.api.proxy.server.ServerInfo; import com.velocitypowered.api.util.ServerLink; -import com.velocitypowered.proxy.connection.client.ConnectedPlayer; -import com.velocitypowered.proxy.protocol.packet.UpsertPlayerInfoPacket; import com.velocitypowered.proxy.tablist.KeyedVelocityTabList; import com.velocitypowered.proxy.tablist.VelocityTabList; import lombok.AccessLevel; @@ -51,8 +49,6 @@ import java.util.*; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; -import static com.velocitypowered.proxy.protocol.packet.UpsertPlayerInfoPacket.Action.UPDATE_LIST_ORDER; - /** * The main class for tracking the server TAB list for a map of {@link TabPlayer}s */ @@ -172,7 +168,6 @@ public class PlayerTabList { players.values().forEach(p -> { p.unsetRelationalDisplayName(player.getUniqueId()); p.unsetRelationalNametag(player.getUniqueId()); - p.unsetTabListOrder(player.getUniqueId()); }); } @@ -185,6 +180,7 @@ public class PlayerTabList { tabPlayerOptional.get().setGroup(group); tabPlayerOptional.get().setRole(plugin.getLuckPermsHook().map(hook -> hook.getPlayerRole(joined)).orElse(Role.DEFAULT_ROLE)); } + final TabPlayer tabPlayer = tabPlayerOptional.orElseGet(() -> createTabPlayer(joined, group)); final String serverName = getServerName(joined); // Store last server, so it's possible to have the last server on disconnect @@ -193,7 +189,6 @@ public class PlayerTabList { // Send server URLs (1.21 clients) sendPlayerServerLinks(tabPlayer); - tabPlayer.getDisplayName(plugin); handleDisplayLoad(tabPlayer); } @@ -404,7 +399,13 @@ public class PlayerTabList { return; } - updateSorting(tabPlayer, force); + plugin.getPlaceholderManager().fetchPlaceholders(tabPlayer.getPlayer().getUniqueId(), tabPlayer.getGroup().sortingPlaceholders()); + + //to make sure that role placeholder is updated even for a backend placeholder + plugin.getServer().getScheduler().buildTask(plugin, + () -> updateSorting(tabPlayer, force)) + .delay(100, TimeUnit.MILLISECONDS) + .schedule(); } public void updateSorting(@NotNull Group group) { @@ -416,13 +417,13 @@ public class PlayerTabList { if (teamName.isBlank()) { return; } - plugin.getScoreboardManager().updateRole(tabPlayer, teamName, force); final int order = plugin.getScoreboardManager().getPosition(teamName); if (order == -1) { plugin.log(Level.ERROR, "Failed to get position for " + tabPlayer.getPlayer().getUsername()); return; } + tabPlayer.setListOrder(order); final Set players = tabPlayer.getGroup().getTabPlayers(plugin, tabPlayer); players.forEach(p -> recalculateSortingForPlayer(p, players)); @@ -586,15 +587,13 @@ public class PlayerTabList { if (!tabPlayer.getPlayer().getTabList().containsEntry(uuid)) { return; } - if (tabPlayer.getCachedListOrders().containsKey(uuid) && tabPlayer.getCachedListOrders().get(uuid) == position) { + + final Optional entry = tabPlayer.getPlayer().getTabList().getEntry(uuid); + if(entry.isEmpty() || entry.get().getListOrder() == position) { return; } - tabPlayer.getCachedListOrders().put(uuid, position); - final UpsertPlayerInfoPacket packet = new UpsertPlayerInfoPacket(UPDATE_LIST_ORDER); - final UpsertPlayerInfoPacket.Entry entry = new UpsertPlayerInfoPacket.Entry(uuid); - entry.setListOrder(position); - packet.addEntry(entry); - ((ConnectedPlayer) tabPlayer.getPlayer()).getConnection().write(packet); + + entry.get().setListOrder(position); } public synchronized void recalculateSortingForPlayer(@NotNull TabPlayer tabPlayer, @NotNull Set players) { @@ -603,7 +602,7 @@ public class PlayerTabList { } players.forEach(p -> { - final int order = p.getListOrder(); + final int order = plugin.getScoreboardManager().getPosition(p.getLastTeamName().orElse("")); updateSorting(tabPlayer, p.getPlayer().getUniqueId(), order); }); } diff --git a/src/main/java/net/william278/velocitab/tab/TabListListener.java b/src/main/java/net/william278/velocitab/tab/TabListListener.java index b5a590c..2ca7bea 100644 --- a/src/main/java/net/william278/velocitab/tab/TabListListener.java +++ b/src/main/java/net/william278/velocitab/tab/TabListListener.java @@ -155,7 +155,7 @@ public class TabListListener { .repeat(50, TimeUnit.MILLISECONDS) .schedule(); - final int delay = justQuit.contains(joined.getUniqueId()) ? 650 : 500; + final int delay = justQuit.contains(joined.getUniqueId()) ? 400 : 200; plugin.getServer().getScheduler().buildTask(plugin, () -> { task.cancel();