Skip to content

Commit

Permalink
Fix issues with /whereami by replacing with /locraw. Also update Hypi…
Browse files Browse the repository at this point in the history
…xel IP support
  • Loading branch information
robere2 committed Jul 11, 2021
1 parent 9fe37c6 commit b39ec6d
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 35 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jar {

targetCompatibility = sourceCompatibility = 1.8

version = "2.0.5"
version = "2.0.6"
group= "co.bugg.quickplay" // http://maven.apache.org/guides/mini/guide-naming-conventions.html
archivesBaseName = "Quickplay-1.8.9"

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/co/bugg/quickplay/Reference.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class Reference {
/**
* Version of this forge mod
*/
public static final String VERSION = "2.0.5";
public static final String VERSION = "2.0.6";
/**
* Google Analytics tracking ID
*/
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/co/bugg/quickplay/util/InstanceWatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* When online Hypixel, enabled instances of
* this class will watch for what instance the
* client is on by occasionally executing
* /whereami
* /locraw
*/
public class InstanceWatcher {
/**
Expand All @@ -27,29 +27,29 @@ public class InstanceWatcher {
*/
public boolean started = false;
/**
* How often in seconds /whereami should be executed
* How often in seconds /locraw should be executed
*/
public int whereamiFrequency;
public int locrawFrequency;

public InstanceWatcher(int frequency) {
whereamiFrequency = frequency;
locrawFrequency = frequency;
}

public int tick;

@SubscribeEvent
public void onTick(TickEvent.ClientTickEvent event) {
if(event.phase == TickEvent.Phase.START && tick++ > whereamiFrequency * 20) {
if(event.phase == TickEvent.Phase.START && tick++ > locrawFrequency * 20) {
tick = 0;
runWhereami();
runLocraw();
}
}

@SubscribeEvent
public void onWorldChange(WorldEvent.Load event) {
// Run twice, just in case first one doesn't trigger
new TickDelay(this::runWhereami, 15);
new TickDelay(this::runWhereami, 60);
new TickDelay(this::runLocraw, 15);
new TickDelay(this::runLocraw, 60);
}

/**
Expand All @@ -59,7 +59,7 @@ public void onWorldChange(WorldEvent.Load event) {
public InstanceWatcher start() {
Quickplay.INSTANCE.registerEventHandler(this);
started = true;
runWhereami();
runLocraw();
return this;
}

Expand All @@ -74,12 +74,12 @@ public InstanceWatcher stop() {
}

/**
* Send the /whereami message if possible
* Send the /locraw message if possible
* @return this
*/
public InstanceWatcher runWhereami() {
public InstanceWatcher runLocraw() {
if(Quickplay.INSTANCE.onHypixel && Quickplay.INSTANCE.enabled) {
new WhereamiWrapper((server) -> {
new LocrawWrapper((server) -> {

// Automatic lobby 1 swapper
if (Quickplay.INSTANCE.settings.lobbyOneSwap) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,49 @@
package co.bugg.quickplay.util;

import co.bugg.quickplay.Quickplay;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import net.minecraftforge.client.event.ClientChatReceivedEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Wrapper for the <code>/whereami</code> command on Hypixel and determining the client's location
* Wrapper for the <code>/locraw</code> command on Hypixel and determining the client's location
*/
public class WhereamiWrapper {
public class LocrawWrapper {
/**
* Whether this wrapper should listen for & action on chat messages
*/
boolean listening;
/**
* Whether this wrapper should cancel whereami messages it finds
* Whether this wrapper should cancel locraw messages it finds
*/
boolean cancel;
/**
* Callback when this wrapper finds a /whereami message
* Callback when this wrapper finds a /locraw message
*/
final WhereamiListenerCallback callback;
final LocrawListenerCallback callback;

/**
* Constructor
*
* @param callback Callback when this wrapper finds a /whereami message
* @param callback Callback when this wrapper finds a /locraw message
*/
public WhereamiWrapper(WhereamiListenerCallback callback) {
public LocrawWrapper(LocrawListenerCallback callback) {

Quickplay.INSTANCE.registerEventHandler(this);
this.callback = callback;
this.listening = true;
this.cancel = true;

// Send the /whereami command
Quickplay.INSTANCE.chatBuffer.push("/whereami");
// If a /whereami isn't received within 120 ticks (6 seconds), don't cancel the message
// Send the /locraw command
Quickplay.INSTANCE.chatBuffer.push("/locraw");
// If a /locraw isn't received within 120 ticks (6 seconds), don't cancel the message
new TickDelay(this::stopCancelling, 120);
// If a /whereami isn't received within 1200 ticks (60 seconds), stop listening
// If a /locraw isn't received within 1200 ticks (60 seconds), stop listening
new TickDelay(() -> stopListening(null), 1200);
}

Expand Down Expand Up @@ -69,10 +72,8 @@ public void stopListening(String instance) {
@SubscribeEvent
public void onChat(ClientChatReceivedEvent event) {
final String message = event.message.getUnformattedText();
// Regex for the /whereami response
// §bYou are currently connected to server §r§6lobby5§r
final Pattern pattern = Pattern.compile("^(?:\u00a7b)?You are currently (?:in limbo|" +
"connected to server (?:\u00a76)?([a-zA-Z]+\\d+[A-Z]?))$");
// Regex for the /locraw response
final Pattern pattern = Pattern.compile("^\\{\"server\":");
final Matcher matcher = pattern.matcher(message);

if(
Expand All @@ -87,20 +88,32 @@ public void onChat(ClientChatReceivedEvent event) {
event.setCanceled(true);
}

// Get the regex group containing the current instance
final String instance = matcher.group(1);
stopListening(instance);
try {
String instance = null;
final JsonObject locrawResponse = new Gson().fromJson(message, JsonObject.class);
// Try lobby name first -- If null, use server name.
if (locrawResponse.get("lobbyname") != null) {
instance = locrawResponse.get("lobbyname").getAsString();
}
if (instance == null && locrawResponse.get("server") != null) {
instance = locrawResponse.get("server").getAsString();
}
stopListening(instance);
} catch (JsonSyntaxException e) {
e.printStackTrace();
stopListening(null);
}
}

}

/**
* Interface for inline callbacks
* Called when a response to /whereami is received,
* Called when a response to /locraw is received,
* or after 60 seconds of no response with "null" passed
*/
@FunctionalInterface
public interface WhereamiListenerCallback {
public interface LocrawListenerCallback {

void call(String instance);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/co/bugg/quickplay/util/ServerChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public ServerChecker(ServerCheckerCallback callback) {

final String ip = getCurrentIP();
if(!ip.equals("singleplayer")) {
Pattern hypixelPattern = Pattern.compile("^(?:(?:(?:.*\\.)?hypixel\\.net)|(?:209\\.222\\.115\\.\\d{1,3}))(?::\\d{1,5})?$", Pattern.CASE_INSENSITIVE);
Pattern hypixelPattern = Pattern.compile("^(?:.*\\.)?hypixel\\.(?:net|io)(?::\\d{1,5})?$", Pattern.CASE_INSENSITIVE);
Matcher matcher = hypixelPattern.matcher(ip);

// If the current IP matches the regex above
Expand Down

0 comments on commit b39ec6d

Please sign in to comment.