From 6f07debe5cf457498be69219d99f0e94091e25f3 Mon Sep 17 00:00:00 2001 From: mg95 <66237187+themg95@users.noreply.github.com> Date: Tue, 8 Oct 2024 12:38:32 +0300 Subject: [PATCH 1/3] Estimate tps and not hard-code it. --- .../mixin/ServerWorldMixin.java | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/common/src/main/java/io/github/steveplays28/stevesrealisticsleep/mixin/ServerWorldMixin.java b/common/src/main/java/io/github/steveplays28/stevesrealisticsleep/mixin/ServerWorldMixin.java index a7d475a..cb47f65 100644 --- a/common/src/main/java/io/github/steveplays28/stevesrealisticsleep/mixin/ServerWorldMixin.java +++ b/common/src/main/java/io/github/steveplays28/stevesrealisticsleep/mixin/ServerWorldMixin.java @@ -109,9 +109,25 @@ protected ServerWorldMixin(MutableWorldProperties properties, RegistryKey private boolean stevesrealisticsleep$shouldSkipWeather = false; @Unique private int stevesrealisticsleep$consecutiveSleepTicks = 0; + @Unique + private int stevesrealisticsleep$ticksSinceLastChecked = 0; + @Unique + private long stevesrealisticsleep$previousTime = System.currentTimeMillis(); + @Unique + private double stevesrealisticsleep$estimatedTps = 20.0; @Inject(method = "tick", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/GameRules;getInt(Lnet/minecraft/world/GameRules$Key;)I")) public void stevesrealisticsleep$tick(BooleanSupplier shouldKeepTicking, CallbackInfo ci) { + if (stevesrealisticsleep$ticksSinceLastChecked >= 10) { + long currentTime = System.currentTimeMillis(); + stevesrealisticsleep$estimatedTps = (double) stevesrealisticsleep$ticksSinceLastChecked / (currentTime - stevesrealisticsleep$previousTime) * 1000; + stevesrealisticsleep$ticksSinceLastChecked = 0; + stevesrealisticsleep$previousTime = currentTime; + System.out.println(stevesrealisticsleep$estimatedTps); + } + stevesrealisticsleep$ticksSinceLastChecked += 1; + + // Calculate seconds until awake int sleepingPlayerCount = sleepManager.getSleeping(); int playerCount = getPlayers().size(); @@ -120,8 +136,7 @@ protected ServerWorldMixin(MutableWorldProperties properties, RegistryKey stevesrealisticsleep$timeStepPerTick ); int timeOfDay = StevesRealisticSleepApi.getTimeOfDay(this); - // TODO: Don't assume the TPS is 20 - int secondsUntilAwake = Math.abs(SleepMathUtil.calculateSecondsUntilAwake(timeOfDay, stevesrealisticsleep$timeStepPerTick, 20)); + int secondsUntilAwake = Math.abs(SleepMathUtil.calculateSecondsUntilAwake(timeOfDay, stevesrealisticsleep$timeStepPerTick, stevesrealisticsleep$estimatedTps)); // Check if the night has (almost) ended and the weather should be skipped if (secondsUntilAwake <= 2 && stevesrealisticsleep$shouldSkipWeather) { From 999c99c56ffc50f0f3ee74c958f016fb6cfbb802 Mon Sep 17 00:00:00 2001 From: mg95 <66237187+themg95@users.noreply.github.com> Date: Sat, 4 Jan 2025 12:18:32 +0300 Subject: [PATCH 2/3] Rename ticksSinceLastChecked to be more descriptive --- .../stevesrealisticsleep/mixin/ServerWorldMixin.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/common/src/main/java/io/github/steveplays28/stevesrealisticsleep/mixin/ServerWorldMixin.java b/common/src/main/java/io/github/steveplays28/stevesrealisticsleep/mixin/ServerWorldMixin.java index cb47f65..4803663 100644 --- a/common/src/main/java/io/github/steveplays28/stevesrealisticsleep/mixin/ServerWorldMixin.java +++ b/common/src/main/java/io/github/steveplays28/stevesrealisticsleep/mixin/ServerWorldMixin.java @@ -110,7 +110,7 @@ protected ServerWorldMixin(MutableWorldProperties properties, RegistryKey @Unique private int stevesrealisticsleep$consecutiveSleepTicks = 0; @Unique - private int stevesrealisticsleep$ticksSinceLastChecked = 0; + private int stevesrealisticsleep$ticksSinceLastTpsCheck = 0; @Unique private long stevesrealisticsleep$previousTime = System.currentTimeMillis(); @Unique @@ -118,14 +118,14 @@ protected ServerWorldMixin(MutableWorldProperties properties, RegistryKey @Inject(method = "tick", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/GameRules;getInt(Lnet/minecraft/world/GameRules$Key;)I")) public void stevesrealisticsleep$tick(BooleanSupplier shouldKeepTicking, CallbackInfo ci) { - if (stevesrealisticsleep$ticksSinceLastChecked >= 10) { + if (stevesrealisticsleep$ticksSinceLastTpsCheck >= 10) { long currentTime = System.currentTimeMillis(); - stevesrealisticsleep$estimatedTps = (double) stevesrealisticsleep$ticksSinceLastChecked / (currentTime - stevesrealisticsleep$previousTime) * 1000; - stevesrealisticsleep$ticksSinceLastChecked = 0; + stevesrealisticsleep$estimatedTps = (double) stevesrealisticsleep$ticksSinceLastTpsCheck / (currentTime - stevesrealisticsleep$previousTime) * 1000; + stevesrealisticsleep$ticksSinceLastTpsCheck = 0; stevesrealisticsleep$previousTime = currentTime; System.out.println(stevesrealisticsleep$estimatedTps); } - stevesrealisticsleep$ticksSinceLastChecked += 1; + stevesrealisticsleep$ticksSinceLastTpsCheck += 1; // Calculate seconds until awake From e3f7deea8ffbec96dba4b4b20e856d79d884081b Mon Sep 17 00:00:00 2001 From: mg95 <66237187+themg95@users.noreply.github.com> Date: Sat, 4 Jan 2025 12:19:10 +0300 Subject: [PATCH 3/3] Remove leftover debug info --- .../stevesrealisticsleep/mixin/ServerWorldMixin.java | 1 - 1 file changed, 1 deletion(-) diff --git a/common/src/main/java/io/github/steveplays28/stevesrealisticsleep/mixin/ServerWorldMixin.java b/common/src/main/java/io/github/steveplays28/stevesrealisticsleep/mixin/ServerWorldMixin.java index 4803663..01c5e13 100644 --- a/common/src/main/java/io/github/steveplays28/stevesrealisticsleep/mixin/ServerWorldMixin.java +++ b/common/src/main/java/io/github/steveplays28/stevesrealisticsleep/mixin/ServerWorldMixin.java @@ -123,7 +123,6 @@ protected ServerWorldMixin(MutableWorldProperties properties, RegistryKey stevesrealisticsleep$estimatedTps = (double) stevesrealisticsleep$ticksSinceLastTpsCheck / (currentTime - stevesrealisticsleep$previousTime) * 1000; stevesrealisticsleep$ticksSinceLastTpsCheck = 0; stevesrealisticsleep$previousTime = currentTime; - System.out.println(stevesrealisticsleep$estimatedTps); } stevesrealisticsleep$ticksSinceLastTpsCheck += 1;