diff --git a/src/main/java/me/zeroeightsix/kami/module/modules/player/LagNotifier.java b/src/main/java/me/zeroeightsix/kami/module/modules/player/LagNotifier.java index 1d50bc7a1..174d78d07 100644 --- a/src/main/java/me/zeroeightsix/kami/module/modules/player/LagNotifier.java +++ b/src/main/java/me/zeroeightsix/kami/module/modules/player/LagNotifier.java @@ -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 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; + } } diff --git a/src/main/java/me/zeroeightsix/kami/util/WebHelper.java b/src/main/java/me/zeroeightsix/kami/util/WebHelper.java new file mode 100644 index 000000000..2c4b5d8d4 --- /dev/null +++ b/src/main/java/me/zeroeightsix/kami/util/WebHelper.java @@ -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. + } + } +}