From 288c6c5d3f00cd47ad315b78361913904f678dff Mon Sep 17 00:00:00 2001 From: alemi Date: Wed, 15 Mar 2023 10:48:41 +0100 Subject: feat: added ping and tps to infodisplay --- .../java/ftbsc/bscv/modules/hud/InfoDisplay.java | 106 ++++++++++++++++++--- 1 file changed, 95 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/main/java/ftbsc/bscv/modules/hud/InfoDisplay.java b/src/main/java/ftbsc/bscv/modules/hud/InfoDisplay.java index ed6ae44..df48b3f 100644 --- a/src/main/java/ftbsc/bscv/modules/hud/InfoDisplay.java +++ b/src/main/java/ftbsc/bscv/modules/hud/InfoDisplay.java @@ -1,10 +1,12 @@ package ftbsc.bscv.modules.hud; import com.google.auto.service.AutoService; -import ftbsc.bscv.ICommons; import ftbsc.bscv.api.ILoadable; import ftbsc.bscv.modules.HudModule; +import ftbsc.bscv.patches.PacketPatch.PacketEvent; import ftbsc.bscv.tools.Setting; +import net.minecraft.client.network.play.NetworkPlayerInfo; +import net.minecraft.network.play.server.SUpdateTimePacket; import net.minecraft.util.math.vector.Vector3d; import net.minecraft.util.text.Color; import net.minecraft.util.text.Style; @@ -21,21 +23,24 @@ import java.util.Queue; import static ftbsc.bscv.tools.Text.TextBuilder; @AutoService(ILoadable.class) -public class InfoDisplay extends HudModule implements ICommons { +public class InfoDisplay extends HudModule { private Vector3d last_position = new Vector3d(0.0, 0.0, 0.0); private double instant_speed = 0.0; private double average_speed = 0.0; - private Queue history_speed = new LinkedList<>(); + private double instant_tps = 0.0; + private int instant_ping = 0; + private Queue speed_history = new LinkedList<>(); + private Queue tps_history = new LinkedList<>(); public final ForgeConfigSpec.ConfigValue logo; public final ForgeConfigSpec.ConfigValue speed; public final ForgeConfigSpec.ConfigValue age; public final ForgeConfigSpec.ConfigValue time; public final ForgeConfigSpec.ConfigValue fps; + public final ForgeConfigSpec.ConfigValue ping; + public final ForgeConfigSpec.ConfigValue tps; // public final ForgeConfigSpec.ConfigValue biome; - // public final ForgeConfigSpec.ConfigValue latency; - // public final ForgeConfigSpec.ConfigValue tps; // public final ForgeConfigSpec.ConfigValue light; // public final ForgeConfigSpec.ConfigValue saturation; // public final ForgeConfigSpec.ConfigValue system_time; @@ -43,8 +48,12 @@ public class InfoDisplay extends HudModule implements ICommons { // public final ForgeConfigSpec.ConfigValue effects_list; // public final ForgeConfigSpec.ConfigValue item_quantity; // public final ForgeConfigSpec.ConfigValue client_chunk_size; + public final ForgeConfigSpec.ConfigValue hide_effects; + public final ForgeConfigSpec.ConfigValue tps_sample_size; + public final ForgeConfigSpec.ConfigValue speed_sample_size; + public InfoDisplay() { super(); @@ -78,32 +87,62 @@ public class InfoDisplay extends HudModule implements ICommons { .fallback(true) .build(this); + this.ping = Setting.Bool.builder() + .name("ping") + .comment("show server latency in ms") + .fallback(true) + .build(this); + + this.tps = Setting.Bool.builder() + .name("tps") + .comment("show client-calculated server TPS") + .fallback(true) + .build(this); + this.hide_effects = Setting.Bool.builder() .name("hide-effects") .comment("hide effect icons on top right corner") .fallback(false) .build(this); + + this.tps_sample_size = Setting.Number.builder() + .min(2) + .name("tps-sample-size") + .comment("TPS samples to store (1 taken each second)") + .fallback(60) + .build(this); + + this.speed_sample_size = Setting.Number.builder() + .min(2) + .name("speed-sample-size") + .comment("instant speed samples to store (1 taken each tick)") + .fallback(100) + .build(this); } @SubscribeEvent public void onTick(TickEvent.ClientTickEvent event) { if (!this.speed.get()) return; - if (event.phase == Phase.END) return; + if (event.phase == Phase.START) return; if (MC.player != null) { this.instant_speed = this.last_position.distanceTo(MC.player.position()); this.last_position = MC.player.position(); + this.instant_ping = MC.getConnection().getPlayerInfo( + MC.player.getGameProfile().getId() + ).getLatency(); } else { this.instant_speed = 0.0; + this.instant_ping = 0; } - this.history_speed.add(this.instant_speed); - while (this.history_speed.size() >= 100) { // TODO customize this parameter - this.history_speed.remove(); + this.speed_history.offer(this.instant_speed); + while (this.speed_history.size() >= this.speed_sample_size.get()) { + this.speed_history.poll(); } double buf = 0.0; - for (double v : this.history_speed) { buf += v; } - this.average_speed = buf / this.history_speed.size(); + for (double v : this.speed_history) { buf += v; } + this.average_speed = buf / this.speed_history.size(); if (this.last_fps_string != MC.fpsString) { this.last_fps_string = MC.fpsString; @@ -111,6 +150,26 @@ public class InfoDisplay extends HudModule implements ICommons { } } + @SubscribeEvent + public void onPacket(PacketEvent.Incoming event) { + if (event.packet instanceof SUpdateTimePacket) { + this.tps_history.offer(System.currentTimeMillis()); + while (this.tps_history.size() > this.tps_sample_size.get()) { + this.tps_history.poll(); + } + double positive_time = 0.; + double last_time = 0; + for (long t : this.tps_history) { + if (last_time != 0) { + double delta = (double) (t - last_time) / 1000.; + positive_time += Math.max(delta, 1.); + } + last_time = t; + } + this.instant_tps = 20 / (positive_time / (this.tps_history.size() - 1)); + } + } + @SubscribeEvent public void onRenderOverlay(RenderGameOverlayEvent event) { if (event.getType() == ElementType.POTION_ICONS) { @@ -154,6 +213,28 @@ public class InfoDisplay extends HudModule implements ICommons { offset += MC.font.lineHeight * scale; } + if (this.ping.get()) { + TextBuilder() + .txt(this.affixed("ping: %d", this.instant_ping)) + .anchor(this.anchor.get()) + .x(this.x.get()) + .y(this.y.get() + offset) + .scale(scale) + .render(event.getMatrixStack(), event.getWindow()); + offset += MC.font.lineHeight * scale; + } + + if (this.tps.get()) { + TextBuilder() + .txt(this.affixed("tps: %.1f", this.instant_tps)) + .anchor(this.anchor.get()) + .x(this.x.get()) + .y(this.y.get() + offset) + .scale(scale) + .render(event.getMatrixStack(), event.getWindow()); + offset += MC.font.lineHeight * scale; + } + if (this.speed.get()) { TextBuilder() .txt(this.affixed("speed: %.1f [%.1f] m/s", this.instant_speed * 20.0, this.average_speed * 20.0)) @@ -191,6 +272,9 @@ public class InfoDisplay extends HudModule implements ICommons { private String last_fps_string; private String curr_fps = "0"; + // TPS utils + + // Time utils private String getTimePhase(long time) { if (time > 23000) return "Dawn"; -- cgit v1.2.3-56-ga3b1