2626import com .velocitypowered .api .util .Favicon ;
2727import com .velocitypowered .api .util .ModInfo ;
2828import com .velocitypowered .proxy .VelocityServer ;
29+ import com .velocitypowered .proxy .config .LegacyPingPassthroughMode ;
2930import com .velocitypowered .proxy .config .PingPassthroughMode ;
3031import com .velocitypowered .proxy .config .VelocityConfiguration ;
3132import com .velocitypowered .proxy .server .VelocityRegisteredServer ;
@@ -64,7 +65,8 @@ private ServerPing constructLocalPing(ProtocolVersion version) {
6465 }
6566
6667 private CompletableFuture <ServerPing > attemptPingPassthrough (VelocityInboundConnection connection ,
67- PingPassthroughMode mode , List <String > servers , ProtocolVersion responseProtocolVersion ) {
68+ PingPassthroughMode mode , LegacyPingPassthroughMode legacyMode ,
69+ List <String > servers , ProtocolVersion responseProtocolVersion ) {
6870 ServerPing fallback = constructLocalPing (connection .getProtocolVersion ());
6971 List <CompletableFuture <ServerPing >> pings = new ArrayList <>();
7072 for (String s : servers ) {
@@ -82,62 +84,119 @@ private CompletableFuture<ServerPing> attemptPingPassthrough(VelocityInboundConn
8284
8385 CompletableFuture <List <ServerPing >> pingResponses = CompletableFutures .successfulAsList (pings ,
8486 (ex ) -> fallback );
85- return pingResponses .thenApply (responses -> {
86- // Find the first non-fallback. If it includes a modlist, add it too.
87- for (ServerPing response : responses ) {
88- if (response == fallback ) {
89- continue ;
87+ // Use the new ping passthrough mode if enabled.
88+ if (mode .enabled ()) {
89+ return pingResponses .thenApply (responses -> {
90+ // Find the first non-fallback. If it includes a modlist, add it too.
91+ for (ServerPing response : responses ) {
92+ if (response == fallback ) {
93+ continue ;
94+ }
95+
96+ if (response .getDescriptionComponent () == null ) {
97+ continue ;
98+ }
99+
100+ ServerPing .Version version ;
101+ if (mode .version ) {
102+ version = response .getVersion ();
103+ } else {
104+ version = fallback .getVersion ();
105+ }
106+
107+ ServerPing .Players players ;
108+ if (mode .players ) {
109+ players = response .getPlayers ().orElse (null );
110+ } else {
111+ players = fallback .getPlayers ().orElse (null );
112+ }
113+
114+ net .kyori .adventure .text .Component description ;
115+ if (mode .description ) {
116+ description = response .getDescriptionComponent ();
117+ } else {
118+ description = fallback .getDescriptionComponent ();
119+ }
120+
121+ Favicon favicon ;
122+ if (mode .favicon ) {
123+ favicon = response .getFavicon ().orElse (null );
124+ } else {
125+ favicon = fallback .getFavicon ().orElse (null );
126+ }
127+
128+ ModInfo modinfo ;
129+ if (mode .modinfo ) {
130+ modinfo = response .getModinfo ().orElse (null );
131+ } else {
132+ modinfo = fallback .getModinfo ().orElse (null );
133+ }
134+
135+ return new ServerPing (
136+ version ,
137+ players ,
138+ description ,
139+ favicon ,
140+ modinfo
141+ );
90142 }
91-
92- if (response .getDescriptionComponent () == null ) {
93- continue ;
94- }
95-
96- ServerPing .Version version ;
97- if (mode .version ) {
98- version = response .getVersion ();
99- } else {
100- version = fallback .getVersion ();
101- }
102-
103- ServerPing .Players players ;
104- if (mode .players ) {
105- players = response .getPlayers ().orElse (null );
106- } else {
107- players = fallback .getPlayers ().orElse (null );
108- }
109-
110- net .kyori .adventure .text .Component description ;
111- if (mode .description ) {
112- description = response .getDescriptionComponent ();
113- } else {
114- description = fallback .getDescriptionComponent ();
115- }
116-
117- Favicon favicon ;
118- if (mode .favicon ) {
119- favicon = response .getFavicon ().orElse (null );
120- } else {
121- favicon = fallback .getFavicon ().orElse (null );
122- }
123-
124- ModInfo modinfo ;
125- if (mode .modinfo ) {
126- modinfo = response .getModinfo ().orElse (null );
127- } else {
128- modinfo = fallback .getModinfo ().orElse (null );
129- }
130-
131- return new ServerPing (
132- version ,
133- players ,
134- description ,
135- favicon ,
136- modinfo
137- );
143+ return fallback ;
144+ });
145+ } else {
146+ // Otherwise, use the legacy ping passthrough mode.
147+ switch (legacyMode ) {
148+ case ALL :
149+ return pingResponses .thenApply (responses -> {
150+ // Find the first non-fallback
151+ for (ServerPing response : responses ) {
152+ if (response == fallback ) {
153+ continue ;
154+ }
155+ return response ;
156+ }
157+ return fallback ;
158+ });
159+ case MODS :
160+ return pingResponses .thenApply (responses -> {
161+ // Find the first non-fallback that contains a mod list
162+ for (ServerPing response : responses ) {
163+ if (response == fallback ) {
164+ continue ;
165+ }
166+ Optional <ModInfo > modInfo = response .getModinfo ();
167+ if (modInfo .isPresent ()) {
168+ return fallback .asBuilder ().mods (modInfo .get ()).build ();
169+ }
170+ }
171+ return fallback ;
172+ });
173+ case DESCRIPTION :
174+ return pingResponses .thenApply (responses -> {
175+ // Find the first non-fallback. If it includes a modlist, add it too.
176+ for (ServerPing response : responses ) {
177+ if (response == fallback ) {
178+ continue ;
179+ }
180+
181+ if (response .getDescriptionComponent () == null ) {
182+ continue ;
183+ }
184+
185+ return new ServerPing (
186+ fallback .getVersion (),
187+ fallback .getPlayers ().orElse (null ),
188+ response .getDescriptionComponent (),
189+ fallback .getFavicon ().orElse (null ),
190+ response .getModinfo ().orElse (null )
191+ );
192+ }
193+ return fallback ;
194+ });
195+ // Not possible, but covered for completeness.
196+ default :
197+ return CompletableFuture .completedFuture (fallback );
138198 }
139- return fallback ;
140- });
199+ }
141200 }
142201
143202 /**
@@ -151,16 +210,17 @@ public CompletableFuture<ServerPing> getInitialPing(VelocityInboundConnection co
151210 ProtocolVersion shownVersion = connection .getProtocolVersion ().isSupported ()
152211 ? connection .getProtocolVersion () : ProtocolVersion .MAXIMUM_VERSION ;
153212 PingPassthroughMode passthroughMode = configuration .getPingPassthrough ();
213+ LegacyPingPassthroughMode legacyPassthroughMode = configuration .getLegacyPingPassthrough ();
154214
155- if (!passthroughMode .enabled ()) {
215+ if (!passthroughMode .enabled () && legacyPassthroughMode == LegacyPingPassthroughMode . DISABLED ) {
156216 return CompletableFuture .completedFuture (constructLocalPing (shownVersion ));
157217 } else {
158218 String virtualHostStr = connection .getVirtualHost ().map (InetSocketAddress ::getHostString )
159219 .map (str -> str .toLowerCase (Locale .ROOT ))
160220 .orElse ("" );
161221 List <String > serversToTry = server .getConfiguration ().getForcedHosts ().getOrDefault (
162222 virtualHostStr , server .getConfiguration ().getAttemptConnectionOrder ());
163- return attemptPingPassthrough (connection , passthroughMode , serversToTry , shownVersion );
223+ return attemptPingPassthrough (connection , passthroughMode , legacyPassthroughMode , serversToTry , shownVersion );
164224 }
165225 }
166226}
0 commit comments