1
mirror of https://github.com/Shiewk/Widgets.git synced 2026-04-28 11:34:17 +02:00

Show TPS as loading when TPS estimation is still loading

This commit is contained in:
Shy
2024-12-07 12:39:44 +01:00
parent fe904c7000
commit b5c55e5ea3
2 changed files with 34 additions and 14 deletions
@@ -6,6 +6,7 @@ import de.shiewk.widgets.widgets.*;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientEntityEvents;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
@@ -46,6 +47,13 @@ public class WidgetsModClient implements ClientModInitializer {
)
);
ClientEntityEvents.ENTITY_LOAD.register((entity, clientWorld) -> {
if (entity == MinecraftClient.getInstance().player){
// player switched world
TPSWidget.worldChanged();
}
});
WidgetManager.register(new FPSWidget(Identifier.of(WidgetsMod.MOD_ID, "fps")));
WidgetManager.register(new ClockWidget(Identifier.of(WidgetsMod.MOD_ID, "clock")));
WidgetManager.register(new CoordinatesWidget(Identifier.of(WidgetsMod.MOD_ID, "coordinates")));
@@ -23,9 +23,14 @@ public class TPSWidget extends BasicTextWidget {
private static final long[] lastUpdates = new long[5];
private static int updatePointer = 0;
private static int updatesSinceWorldChange = 0;
private boolean dynamicColor = true;
public static void worldChanged(){
updatesSinceWorldChange = 0;
}
public static void worldTimeUpdated(long nanoTime) {
MinecraftClient client = MinecraftClient.getInstance();
if (client.isInSingleplayer()){
@@ -35,12 +40,13 @@ public class TPSWidget extends BasicTextWidget {
float tps = 1000f / server.getAverageTickTime();
float targetTickRate = tickManager.getTickRate();
if (tickManager.isSprinting()){
INSTANCE.updateTPS(tps, targetTickRate);
INSTANCE.updateTPS(tps, targetTickRate, true);
} else {
INSTANCE.updateTPS(Math.min(tps, targetTickRate), targetTickRate);
INSTANCE.updateTPS(Math.min(tps, targetTickRate), targetTickRate, true);
}
}
} else {
updatesSinceWorldChange++;
lastUpdates[updatePointer] = nanoTime;
updatePointer++;
if (updatePointer >= lastUpdates.length) updatePointer = 0;
@@ -55,24 +61,30 @@ public class TPSWidget extends BasicTextWidget {
float mspt = avgDifference / 20000000f;
float ticksPerSecond = 1000f / mspt;
boolean loadingFinished = updatesSinceWorldChange > 5;
if (client.world != null) {
INSTANCE.updateTPS(ticksPerSecond, client.world.getTickManager().getTickRate());
INSTANCE.updateTPS(ticksPerSecond, client.world.getTickManager().getTickRate(), loadingFinished);
} else {
INSTANCE.updateTPS(ticksPerSecond, 20);
INSTANCE.updateTPS(ticksPerSecond, 20, loadingFinished);
}
}
}
private void updateTPS(float tps, float targetTickRate) {
tps = Math.round(tps * 10f) / 10f;
this.renderText = Text.literal(Text.translatable("widgets.widgets.tps.tps", tps).getString());
if (dynamicColor){
if (tps >= targetTickRate * 0.995){
this.textColor = 0x00ff00;
} else if (tps >= targetTickRate * 0.745){
this.textColor = 0xffff00;
} else {
this.textColor = 0xff0000;
private void updateTPS(float tps, float targetTickRate, boolean loadingFinished) {
if (!loadingFinished){
this.renderText = Text.literal(Text.translatable("widgets.widgets.tps.tps", "???").getString());
if (dynamicColor) this.textColor = 0x00ff00;
} else {
tps = Math.round(tps * 10f) / 10f;
this.renderText = Text.literal(Text.translatable("widgets.widgets.tps.tps", tps).getString());
if (dynamicColor){
if (tps >= targetTickRate * 0.990){
this.textColor = 0x00ff00;
} else if (tps >= targetTickRate * 0.740){
this.textColor = 0xffff00;
} else {
this.textColor = 0xff0000;
}
}
}
}