11package co .bugg .quickplay .util ;
22
33import co .bugg .quickplay .Quickplay ;
4+ import com .google .gson .Gson ;
5+ import com .google .gson .JsonObject ;
6+ import com .google .gson .JsonSyntaxException ;
47import net .minecraftforge .client .event .ClientChatReceivedEvent ;
58import net .minecraftforge .fml .common .eventhandler .SubscribeEvent ;
69
710import java .util .regex .Matcher ;
811import java .util .regex .Pattern ;
912
1013/**
11- * Wrapper for the <code>/whereami </code> command on Hypixel and determining the client's location
14+ * Wrapper for the <code>/locraw </code> command on Hypixel and determining the client's location
1215 */
13- public class WhereamiWrapper {
16+ public class LocrawWrapper {
1417 /**
1518 * Whether this wrapper should listen for & action on chat messages
1619 */
1720 boolean listening ;
1821 /**
19- * Whether this wrapper should cancel whereami messages it finds
22+ * Whether this wrapper should cancel locraw messages it finds
2023 */
2124 boolean cancel ;
2225 /**
23- * Callback when this wrapper finds a /whereami message
26+ * Callback when this wrapper finds a /locraw message
2427 */
25- final WhereamiListenerCallback callback ;
28+ final LocrawListenerCallback callback ;
2629
2730 /**
2831 * Constructor
2932 *
30- * @param callback Callback when this wrapper finds a /whereami message
33+ * @param callback Callback when this wrapper finds a /locraw message
3134 */
32- public WhereamiWrapper ( WhereamiListenerCallback callback ) {
35+ public LocrawWrapper ( LocrawListenerCallback callback ) {
3336
3437 Quickplay .INSTANCE .registerEventHandler (this );
3538 this .callback = callback ;
3639 this .listening = true ;
3740 this .cancel = true ;
3841
39- // Send the /whereami command
40- Quickplay .INSTANCE .chatBuffer .push ("/whereami " );
41- // If a /whereami isn't received within 120 ticks (6 seconds), don't cancel the message
42+ // Send the /locraw command
43+ Quickplay .INSTANCE .chatBuffer .push ("/locraw " );
44+ // If a /locraw isn't received within 120 ticks (6 seconds), don't cancel the message
4245 new TickDelay (this ::stopCancelling , 120 );
43- // If a /whereami isn't received within 1200 ticks (60 seconds), stop listening
46+ // If a /locraw isn't received within 1200 ticks (60 seconds), stop listening
4447 new TickDelay (() -> stopListening (null ), 1200 );
4548 }
4649
@@ -69,10 +72,8 @@ public void stopListening(String instance) {
6972 @ SubscribeEvent
7073 public void onChat (ClientChatReceivedEvent event ) {
7174 final String message = event .message .getUnformattedText ();
72- // Regex for the /whereami response
73- // §bYou are currently connected to server §r§6lobby5§r
74- final Pattern pattern = Pattern .compile ("^(?:\u00a7 b)?You are currently (?:in limbo|" +
75- "connected to server (?:\u00a7 6)?([a-zA-Z]+\\ d+[A-Z]?))$" );
75+ // Regex for the /locraw response
76+ final Pattern pattern = Pattern .compile ("^\\ {\" server\" :" );
7677 final Matcher matcher = pattern .matcher (message );
7778
7879 if (
@@ -87,20 +88,32 @@ public void onChat(ClientChatReceivedEvent event) {
8788 event .setCanceled (true );
8889 }
8990
90- // Get the regex group containing the current instance
91- final String instance = matcher .group (1 );
92- stopListening (instance );
91+ try {
92+ String instance = null ;
93+ final JsonObject locrawResponse = new Gson ().fromJson (message , JsonObject .class );
94+ // Try lobby name first -- If null, use server name.
95+ if (locrawResponse .get ("lobbyname" ) != null ) {
96+ instance = locrawResponse .get ("lobbyname" ).getAsString ();
97+ }
98+ if (instance == null && locrawResponse .get ("server" ) != null ) {
99+ instance = locrawResponse .get ("server" ).getAsString ();
100+ }
101+ stopListening (instance );
102+ } catch (JsonSyntaxException e ) {
103+ e .printStackTrace ();
104+ stopListening (null );
105+ }
93106 }
94107
95108 }
96109
97110 /**
98111 * Interface for inline callbacks
99- * Called when a response to /whereami is received,
112+ * Called when a response to /locraw is received,
100113 * or after 60 seconds of no response with "null" passed
101114 */
102115 @ FunctionalInterface
103- public interface WhereamiListenerCallback {
116+ public interface LocrawListenerCallback {
104117
105118 void call (String instance );
106119 }
0 commit comments