added webhelper and added offline check to close #722

This commit is contained in:
Bella 2020-04-19 15:14:06 -04:00
parent 62b7b5a17a
commit 65914fa66e
No known key found for this signature in database
GPG Key ID: DBD4A6030080C8B3
2 changed files with 45 additions and 1 deletions

View File

@ -8,13 +8,17 @@ import me.zeroeightsix.kami.module.Module;
import me.zeroeightsix.kami.setting.Setting;
import me.zeroeightsix.kami.setting.Settings;
import me.zeroeightsix.kami.util.Wrapper;
import net.minecraft.client.gui.GuiChat;
import static me.zeroeightsix.kami.gui.kami.DisplayGuiScreen.getScale;
import static me.zeroeightsix.kami.util.MathsUtils.round;
import static me.zeroeightsix.kami.util.WebHelper.isDown;
/**
* @author dominikaaaa
* Thanks Brady and cooker and leij for helping me not be completely retarded
*
* Updated by dominikaaaa on 19/04/20
*/
@Module.Info(
name = "LagNotifier",
@ -24,11 +28,20 @@ import static me.zeroeightsix.kami.util.MathsUtils.round;
public class LagNotifier extends Module {
private Setting<Double> timeout = register(Settings.doubleBuilder().withName("Timeout").withValue(1.0).withMinimum(0.0).withMaximum(10.0).build());
private long serverLastUpdated;
String text = "Server Not Responding! ";
@Override
public void onRender() {
if (mc.currentScreen != null && !(mc.currentScreen instanceof GuiChat)) return;
if (!(timeout.getValue() * 1000L <= System.currentTimeMillis() - serverLastUpdated)) return;
String text = "Server Not Responding! " + timeDifference() + "s";
if (shouldPing()) {
if (isDown("google.com", 80, 1000)) {
text = "Your internet is offline! ";
} else {
text = "Server Not Responding! ";
}
}
text = text.replaceAll("! .*", "! " + timeDifference() + "s");
FontRenderer renderer = Wrapper.getFontRenderer();
int divider = getScale();
@ -42,4 +55,14 @@ public class LagNotifier extends Module {
private double timeDifference() {
return round((System.currentTimeMillis() - serverLastUpdated) / 1000d, 1);
}
private static long startTime = 0;
private boolean shouldPing() {
if (startTime == 0) startTime = System.currentTimeMillis();
if (startTime + 1000 <= System.currentTimeMillis()) { // 1 second
startTime = System.currentTimeMillis();
return true;
}
return false;
}
}

View File

@ -0,0 +1,21 @@
package me.zeroeightsix.kami.util;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
/**
* @author balusc (StackOverflow ID 157882)
*
* https://stackoverflow.com/questions/3584210/preferred-java-way-to-ping-an-http-url-for-availability#3584332
*/
public class WebHelper {
public static boolean isDown(String host, int port, int timeout) {
try (Socket socket = new Socket()) {
socket.connect(new InetSocketAddress(host, port), timeout);
return false;
} catch (IOException e) {
return true; // Either timeout or unreachable or failed DNS lookup.
}
}
}