2222
2323import com .ayakacraft .authlibproxyforserver .AuthlibProxyForServer ;
2424import com .ayakacraft .authlibproxyforserver .ProxyConfig ;
25- import com .ayakacraft .authlibproxyforserver .mixin .YggdrasilAuthenticationServiceAccessor ;
2625import com .ayakacraft .authlibproxyforserver .utils .NetworkUtils ;
2726import com .ayakacraft .authlibproxyforserver .utils .preprocess .PreprocessPattern ;
27+ import com .google .common .collect .Maps ;
28+ import com .google .common .collect .Sets ;
2829import com .mojang .brigadier .CommandDispatcher ;
2930import com .mojang .brigadier .arguments .IntegerArgumentType ;
3031import com .mojang .brigadier .arguments .StringArgumentType ;
3637
3738import java .io .IOException ;
3839import java .net .URI ;
39- import java .util .LinkedList ;
40- import java .util .List ;
40+ import java .util .Map ;
41+ import java .util .Set ;
4142
4243import static com .ayakacraft .authlibproxyforserver .AuthlibProxyForServer .LOGGER ;
4344import static com .ayakacraft .authlibproxyforserver .AuthlibProxyForServer .config ;
@@ -163,13 +164,13 @@ private static int type(CommandContext<ServerCommandSource> context, String type
163164 }
164165
165166 private static int ping (CommandContext <ServerCommandSource > context ) {
166- List <String > hosts = new LinkedList <> ();
167+ Set <String > hosts = Sets . newHashSet ();
167168 //#if MC>=12006
168- com .mojang .authlib .Environment env = YggdrasilAuthenticationServiceAccessor .determineEnvironment ();
169+ com .mojang .authlib .Environment env = com . ayakacraft . authlibproxyforserver . mixin . YggdrasilAuthenticationServiceAccessor .determineEnvironment ();
169170 hosts .add (env .sessionHost ());
170171 hosts .add (env .servicesHost ());
171172 //#elseif MC>=11600
172- //$$ com.mojang.authlib.Environment env = YggdrasilAuthenticationServiceAccessor.determineEnvironment();
173+ //$$ com.mojang.authlib.Environment env = com.ayakacraft.authlibproxyforserver.mixin. YggdrasilAuthenticationServiceAccessor.determineEnvironment();
173174 //$$ hosts.add(env.getAuthHost());
174175 //$$ hosts.add(env.getAccountsHost());
175176 //$$ hosts.add(env.getSessionHost());
@@ -248,11 +249,11 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
248249 private static class TcpingThread extends Thread {
249250
250251 private static Text packetLossStatusText (int packetsReceived ) {
251- double packetLoss = 1d - (double ) packetsReceived / AuthProxyCommand .TCPING_TIMES ;
252+ double packetLoss = 1D - (double ) packetsReceived / AuthProxyCommand .TCPING_TIMES ;
252253 Formatting colour ;
253- if (packetLoss >= 0.8d ) {
254+ if (packetLoss > 0.8D ) {
254255 colour = Formatting .RED ;
255- } else if (packetLoss >= 0.3d ) {
256+ } else if (packetLoss > 0.2D ) {
256257 colour = Formatting .YELLOW ;
257258 } else {
258259 colour = Formatting .GREEN ;
@@ -264,9 +265,11 @@ private static Text packetLossStatusText(int packetsReceived) {
264265
265266 private static Text averagePingText (long ping ) {
266267 Formatting colour ;
267- if (ping >= 800 ) {
268+ if (ping > 1000 ) {
269+ colour = Formatting .LIGHT_PURPLE ;
270+ } else if (ping >= 800 ) {
268271 colour = Formatting .RED ;
269- } else if (ping >= 400 ) {
272+ } else if (ping >= 300 ) {
270273 colour = Formatting .YELLOW ;
271274 } else {
272275 colour = Formatting .GREEN ;
@@ -275,40 +278,56 @@ private static Text averagePingText(long ping) {
275278 .append (Text .literal (ping + "ms" ).formatted (colour ));
276279 }
277280
278- private final List <String > hosts ;
281+ private final Set <String > hosts ;
279282
280283 private final ServerCommandSource source ;
281284
282- public TcpingThread (List <String > hosts , ServerCommandSource source ) {
285+ private final Map <String , Pair <Long , Integer >> results ;
286+
287+ private long startTimeMillis ;
288+
289+ public TcpingThread (Set <String > hosts , ServerCommandSource source ) {
290+ super ("TCPing Thread" );
283291 this .hosts = hosts ;
284292 this .source = source ;
293+ this .results = Maps .newHashMap ();
294+ }
295+
296+ private synchronized void saveResult (String host , Pair <Long , Integer > res ) {
297+ results .put (host , res );
298+ if (results .size () >= hosts .size ()) {
299+ results .forEach (this ::sendResult );
300+ sendFeedback (source , Text .literal ("Ping finished after " + (System .currentTimeMillis () - startTimeMillis ) + "ms" ), false );
301+ }
285302 }
286303
287- private void tcping (String host ) {
288- Pair <Long , Integer > res = NetworkUtils .tcpingMultiple (URI .create (host ), proxy , TCPING_TIMES );
304+ private void sendResult (String h , Pair <Long , Integer > r ) {
289305 sendFeedback (
290306 source ,
291- Text .literal (String .format ("Ping for '%s':" , host )),
307+ Text .literal (String .format ("Ping for '%s':" , h )),
292308 false
293309 );
294310 sendFeedback (
295311 source ,
296- packetLossStatusText (res .getRight ()),
312+ packetLossStatusText (r .getRight ()),
297313 false
298314 );
299- if (res .getRight () > 0 ) {
315+ if (r .getRight () > 0 ) {
300316 sendFeedback (
301317 source ,
302- averagePingText (res .getLeft () / res .getRight ()),
318+ averagePingText (r .getLeft () / r .getRight ()),
303319 false
304320 );
305321 }
306322 }
307323
324+ @ Override
308325 public void run () {
309326 sendFeedback (source , Text .literal ("Ping started" ), false );
310- hosts .forEach (this ::tcping );
311- sendFeedback (source , Text .literal ("Ping finished" ), false );
327+ startTimeMillis = System .currentTimeMillis ();
328+ hosts .forEach (host -> new Thread (() ->
329+ saveResult (host , NetworkUtils .tcpingMultiple (URI .create (host ), proxy , TCPING_TIMES ))).start ()
330+ );
312331 }
313332
314333 }
0 commit comments