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..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 @@ -109,9 +109,24 @@ protected ServerWorldMixin(MutableWorldProperties properties, RegistryKey private boolean stevesrealisticsleep$shouldSkipWeather = false; @Unique private int stevesrealisticsleep$consecutiveSleepTicks = 0; + @Unique + private int stevesrealisticsleep$ticksSinceLastTpsCheck = 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$ticksSinceLastTpsCheck >= 10) { + long currentTime = System.currentTimeMillis(); + stevesrealisticsleep$estimatedTps = (double) stevesrealisticsleep$ticksSinceLastTpsCheck / (currentTime - stevesrealisticsleep$previousTime) * 1000; + stevesrealisticsleep$ticksSinceLastTpsCheck = 0; + stevesrealisticsleep$previousTime = currentTime; + } + stevesrealisticsleep$ticksSinceLastTpsCheck += 1; + + // Calculate seconds until awake int sleepingPlayerCount = sleepManager.getSleeping(); int playerCount = getPlayers().size(); @@ -120,8 +135,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) {