aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author alemi <me@alemi.dev>2023-03-15 10:48:41 +0100
committer alemi <me@alemi.dev>2023-03-15 10:48:41 +0100
commit288c6c5d3f00cd47ad315b78361913904f678dff (patch)
tree3ba46896fa573007724d4e9d07754882b32c1130
parent330f90f821bc6499e8139c372338988e7ad0cf89 (diff)
feat: added ping and tps to infodisplay
-rw-r--r--src/main/java/ftbsc/bscv/modules/hud/InfoDisplay.java106
1 files changed, 95 insertions, 11 deletions
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<Double> history_speed = new LinkedList<>();
+ private double instant_tps = 0.0;
+ private int instant_ping = 0;
+ private Queue<Double> speed_history = new LinkedList<>();
+ private Queue<Long> tps_history = new LinkedList<>();
public final ForgeConfigSpec.ConfigValue<Boolean> logo;
public final ForgeConfigSpec.ConfigValue<Boolean> speed;
public final ForgeConfigSpec.ConfigValue<Boolean> age;
public final ForgeConfigSpec.ConfigValue<Boolean> time;
public final ForgeConfigSpec.ConfigValue<Boolean> fps;
+ public final ForgeConfigSpec.ConfigValue<Boolean> ping;
+ public final ForgeConfigSpec.ConfigValue<Boolean> tps;
// public final ForgeConfigSpec.ConfigValue<Boolean> biome;
- // public final ForgeConfigSpec.ConfigValue<Boolean> latency;
- // public final ForgeConfigSpec.ConfigValue<Boolean> tps;
// public final ForgeConfigSpec.ConfigValue<Boolean> light;
// public final ForgeConfigSpec.ConfigValue<Boolean> saturation;
// public final ForgeConfigSpec.ConfigValue<Boolean> system_time;
@@ -43,8 +48,12 @@ public class InfoDisplay extends HudModule implements ICommons {
// public final ForgeConfigSpec.ConfigValue<Boolean> effects_list;
// public final ForgeConfigSpec.ConfigValue<Boolean> item_quantity;
// public final ForgeConfigSpec.ConfigValue<Boolean> client_chunk_size;
+
public final ForgeConfigSpec.ConfigValue<Boolean> hide_effects;
+ public final ForgeConfigSpec.ConfigValue<Integer> tps_sample_size;
+ public final ForgeConfigSpec.ConfigValue<Integer> 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;
@@ -112,6 +151,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) {
if (this.hide_effects.get() && event.isCancelable()) {
@@ -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";