feat: added ping and tps to infodisplay

This commit is contained in:
əlemi 2023-03-15 10:48:41 +01:00
parent 330f90f821
commit 288c6c5d3f
Signed by: alemi
GPG key ID: A4895B84D311642C

View file

@ -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;
@ -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";