From ab6026184265ef86346457d142b310c4598b4fb5 Mon Sep 17 00:00:00 2001 From: Timongcraft Date: Sun, 27 Oct 2024 18:48:03 +0100 Subject: [PATCH 1/3] feat: expose list order in TabListEntry --- .../api/proxy/player/TabList.java | 21 +++++++++- .../api/proxy/player/TabListEntry.java | 39 ++++++++++++++++++- .../proxy/tablist/KeyedVelocityTabList.java | 9 ++++- .../proxy/tablist/VelocityTabList.java | 12 ++++-- .../proxy/tablist/VelocityTabListEntry.java | 22 ++++++++++- 5 files changed, 93 insertions(+), 10 deletions(-) diff --git a/api/src/main/java/com/velocitypowered/api/proxy/player/TabList.java b/api/src/main/java/com/velocitypowered/api/proxy/player/TabList.java index 4d03d3a87f..feffd76e2e 100644 --- a/api/src/main/java/com/velocitypowered/api/proxy/player/TabList.java +++ b/api/src/main/java/com/velocitypowered/api/proxy/player/TabList.java @@ -168,6 +168,25 @@ default TabListEntry buildEntry(GameProfile profile, @Nullable Component display * @deprecated Internal usage. Use {@link TabListEntry.Builder} instead. */ @Deprecated + default TabListEntry buildEntry(GameProfile profile, @Nullable Component displayName, int latency, + int gameMode, @Nullable ChatSession chatSession, boolean listed) { + return buildEntry(profile, displayName, latency, gameMode, chatSession, listed, 0); + } + + /** + * Represents an entry in a {@link Player}'s tab list. + * + * @param profile the profile + * @param displayName the display name + * @param latency the latency + * @param gameMode the game mode + * @param chatSession the chat session + * @param listed the visible status of entry + * @param listOrder the order/priority of entry in the tab list + * @return the entry + * @deprecated Internal usage. Use {@link TabListEntry.Builder} instead. + */ + @Deprecated TabListEntry buildEntry(GameProfile profile, @Nullable Component displayName, int latency, - int gameMode, @Nullable ChatSession chatSession, boolean listed); + int gameMode, @Nullable ChatSession chatSession, boolean listed, int listOrder); } diff --git a/api/src/main/java/com/velocitypowered/api/proxy/player/TabListEntry.java b/api/src/main/java/com/velocitypowered/api/proxy/player/TabListEntry.java index 401d6a8da4..b5140776e0 100644 --- a/api/src/main/java/com/velocitypowered/api/proxy/player/TabListEntry.java +++ b/api/src/main/java/com/velocitypowered/api/proxy/player/TabListEntry.java @@ -139,6 +139,27 @@ default TabListEntry setListed(boolean listed) { return this; } + /** + * Returns the order/priority of this entry in the tab list. + * + * @return order of this entry + * @sinceMinecraft 1.21.2 + */ + default int getListOrder() { + return 0; + } + + /** + * Sets the order/priority of this entry in the tab list. + * + * @param order order of this entry + * @return {@code this}, for chaining + * @sinceMinecraft 1.21.2 + */ + default TabListEntry setListOrder(int order) { + return this; + } + /** * Returns a {@link Builder} to create a {@link TabListEntry}. * @@ -161,6 +182,7 @@ class Builder { private int latency = 0; private int gameMode = 0; private boolean listed = true; + private int listOrder = 0; private @Nullable ChatSession chatSession; @@ -243,7 +265,7 @@ public Builder gameMode(int gameMode) { } /** - * Sets wether this entry should be visible. + * Sets whether this entry should be visible. * * @param listed to set * @return ${code this}, for chaining @@ -254,6 +276,19 @@ public Builder listed(boolean listed) { return this; } + /** + * Sets the order/priority of this entry in the tab list. + * + * @param order to set + * @return ${code this}, for chaining + * @sinceMinecraft 1.21.2 + * @see TabListEntry#getListOrder() + */ + public Builder listOrder(int order) { + this.listOrder = order; + return this; + } + /** * Constructs the {@link TabListEntry} specified by {@code this} {@link Builder}. * @@ -266,7 +301,7 @@ public TabListEntry build() { if (profile == null) { throw new IllegalStateException("The GameProfile must be set when building a TabListEntry"); } - return tabList.buildEntry(profile, displayName, latency, gameMode, chatSession, listed); + return tabList.buildEntry(profile, displayName, latency, gameMode, chatSession, listed, listOrder); } } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/tablist/KeyedVelocityTabList.java b/proxy/src/main/java/com/velocitypowered/proxy/tablist/KeyedVelocityTabList.java index 61967fbc2a..daaa9b0a62 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/tablist/KeyedVelocityTabList.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/tablist/KeyedVelocityTabList.java @@ -159,12 +159,17 @@ public TabListEntry buildEntry(GameProfile profile, @Override public TabListEntry buildEntry(GameProfile profile, @Nullable Component displayName, int latency, - int gameMode, - @Nullable ChatSession chatSession, boolean listed) { + int gameMode, @Nullable ChatSession chatSession, boolean listed) { return new KeyedVelocityTabListEntry(this, profile, displayName, latency, gameMode, chatSession == null ? null : chatSession.getIdentifiedKey()); } + @Override + public TabListEntry buildEntry(GameProfile profile, @Nullable Component displayName, int latency, + int gameMode, @Nullable ChatSession chatSession, boolean listed, int listOrder) { + return buildEntry(profile, displayName, latency, gameMode, chatSession, listed); + } + @Override public void processLegacy(LegacyPlayerListItemPacket packet) { // Packets are already forwarded on, so no need to do that here diff --git a/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabList.java b/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabList.java index d6b4143ce4..a2cc1955c5 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabList.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabList.java @@ -89,7 +89,7 @@ public void addEntry(TabListEntry entry1) { } else { entry = new VelocityTabListEntry(this, entry1.getProfile(), entry1.getDisplayNameComponent().orElse(null), - entry1.getLatency(), entry1.getGameMode(), entry1.getChatSession(), entry1.isListed()); + entry1.getLatency(), entry1.getGameMode(), entry1.getChatSession(), entry1.isListed(), entry1.getListOrder()); } EnumSet actions = EnumSet @@ -207,9 +207,9 @@ public void clearAllSilent() { @Override public TabListEntry buildEntry(GameProfile profile, @Nullable Component displayName, int latency, int gameMode, - @Nullable ChatSession chatSession, boolean listed) { + @Nullable ChatSession chatSession, boolean listed, int listOrder) { return new VelocityTabListEntry(this, profile, displayName, latency, gameMode, chatSession, - listed); + listed, listOrder); } @Override @@ -246,7 +246,8 @@ private void processUpsert(EnumSet actions, 0, -1, null, - false + false, + 0 ) ); } else { @@ -274,6 +275,9 @@ private void processUpsert(EnumSet actions, if (actions.contains(UpsertPlayerInfoPacket.Action.UPDATE_LISTED)) { currentEntry.setListedWithoutUpdate(entry.isListed()); } + if (actions.contains(UpsertPlayerInfoPacket.Action.UPDATE_LIST_ORDER)) { + currentEntry.setListOrderWithoutUpdate(entry.getListOrder()); + } } @Override diff --git a/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabListEntry.java b/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabListEntry.java index 4e036504a0..1828255388 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabListEntry.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabListEntry.java @@ -38,6 +38,7 @@ public class VelocityTabListEntry implements TabListEntry { private int latency; private int gameMode; private boolean listed; + private int listOrder; private @Nullable ChatSession session; /** @@ -45,7 +46,7 @@ public class VelocityTabListEntry implements TabListEntry { */ public VelocityTabListEntry(VelocityTabList tabList, GameProfile profile, Component displayName, int latency, - int gameMode, @Nullable ChatSession session, boolean listed) { + int gameMode, @Nullable ChatSession session, boolean listed, int listOrder) { this.tabList = tabList; this.profile = profile; this.displayName = displayName; @@ -53,6 +54,7 @@ public VelocityTabListEntry(VelocityTabList tabList, GameProfile profile, Compon this.gameMode = gameMode; this.session = session; this.listed = listed; + this.listOrder = listOrder; } @Override @@ -150,4 +152,22 @@ public VelocityTabListEntry setListed(boolean listed) { void setListedWithoutUpdate(boolean listed) { this.listed = listed; } + + @Override + public int getListOrder() { + return listOrder; + } + + @Override + public VelocityTabListEntry setListOrder(int listOrder) { + this.listOrder = listOrder; + UpsertPlayerInfoPacket.Entry upsertEntry = this.tabList.createRawEntry(this); + upsertEntry.setListOrder(listOrder); + this.tabList.emitActionRaw(UpsertPlayerInfoPacket.Action.UPDATE_LIST_ORDER, upsertEntry); + return this; + } + + void setListOrderWithoutUpdate(int listOrder) { + this.listOrder = listOrder; + } } \ No newline at end of file From 6cf9be128dafc7261ab2f15e97cf2191d3619dbb Mon Sep 17 00:00:00 2001 From: Timongcraft Date: Sun, 27 Oct 2024 21:38:03 +0100 Subject: [PATCH 2/3] fix: address comment (from github) --- .../velocitypowered/proxy/tablist/VelocityTabList.java | 6 ++++++ .../proxy/tablist/VelocityTabListEntry.java | 9 ++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabList.java b/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabList.java index a2cc1955c5..115261a712 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabList.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabList.java @@ -19,6 +19,7 @@ import com.google.common.base.Preconditions; import com.google.common.collect.Maps; +import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.player.ChatSession; import com.velocitypowered.api.proxy.player.TabListEntry; @@ -128,6 +129,11 @@ public void addEntry(TabListEntry entry1) { actions.add(UpsertPlayerInfoPacket.Action.UPDATE_LISTED); playerInfoEntry.setListed(entry.isListed()); } + if (!Objects.equals(previousEntry.getListOrder(), entry.getListOrder()) + && player.getProtocolVersion().noLessThan(ProtocolVersion.MINECRAFT_1_21_2)) { + actions.add(UpsertPlayerInfoPacket.Action.UPDATE_LIST_ORDER); + playerInfoEntry.setListOrder(entry.getListOrder()); + } if (!Objects.equals(previousEntry.getChatSession(), entry.getChatSession())) { ChatSession from = entry.getChatSession(); if (from != null) { diff --git a/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabListEntry.java b/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabListEntry.java index 1828255388..352d627168 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabListEntry.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabListEntry.java @@ -17,6 +17,7 @@ package com.velocitypowered.proxy.tablist; +import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.api.proxy.player.ChatSession; import com.velocitypowered.api.proxy.player.TabList; import com.velocitypowered.api.proxy.player.TabListEntry; @@ -161,9 +162,11 @@ public int getListOrder() { @Override public VelocityTabListEntry setListOrder(int listOrder) { this.listOrder = listOrder; - UpsertPlayerInfoPacket.Entry upsertEntry = this.tabList.createRawEntry(this); - upsertEntry.setListOrder(listOrder); - this.tabList.emitActionRaw(UpsertPlayerInfoPacket.Action.UPDATE_LIST_ORDER, upsertEntry); + if (tabList.getPlayer().getProtocolVersion().noLessThan(ProtocolVersion.MINECRAFT_1_21_2)) { + UpsertPlayerInfoPacket.Entry upsertEntry = this.tabList.createRawEntry(this); + upsertEntry.setListOrder(listOrder); + tabList.emitActionRaw(UpsertPlayerInfoPacket.Action.UPDATE_LIST_ORDER, upsertEntry); + } return this; } From d067bdb167ffd4f6fd13b0799cc15a8ab3401158 Mon Sep 17 00:00:00 2001 From: Timongcraft Date: Mon, 28 Oct 2024 09:16:47 +0100 Subject: [PATCH 3/3] fix: address another comment (from github) --- .../com/velocitypowered/proxy/tablist/VelocityTabList.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabList.java b/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabList.java index 115261a712..0990119c68 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabList.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabList.java @@ -168,6 +168,11 @@ public void addEntry(TabListEntry entry1) { } playerInfoEntry.setLatency(entry.getLatency()); playerInfoEntry.setListed(entry.isListed()); + if (entry.getListOrder() != 0 + && player.getProtocolVersion().noLessThan(ProtocolVersion.MINECRAFT_1_21_2)) { + actions.add(UpsertPlayerInfoPacket.Action.UPDATE_LIST_ORDER); + playerInfoEntry.setListOrder(entry.getListOrder()); + } } return entry; });