Skip to content

Commit 7631fa4

Browse files
committed
Rework ServerTPS
1 parent ac67df8 commit 7631fa4

File tree

6 files changed

+36
-42
lines changed

6 files changed

+36
-42
lines changed

common/src/main/kotlin/com/lambda/module/hud/TPS.kt

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,20 @@ package com.lambda.module.hud
1919

2020
import com.lambda.module.HudModule
2121
import com.lambda.module.tag.ModuleTag
22-
import com.lambda.util.TpsClock.normalizedTickRate
22+
import com.lambda.util.NamedEnum
23+
import com.lambda.util.ServerTPS.averageMSPerTick
2324
import com.lambda.util.math.MathUtils.format
2425

2526
object TPS : HudModule(
2627
name = "TPS",
2728
description = "Display the server's tick rate",
2829
defaultTags = setOf(ModuleTag.CLIENT, ModuleTag.NETWORK),
2930
) {
30-
private val format by setting("Tick format", TickFormat.Tick)
31+
private val format by setting("Tick format", TickFormat.TPS)
3132

32-
private val text: String get() = "${format.string}: ${format.output().format(2)}"
33+
private val text: String get() = "${format.displayName}: ${format.output().format(2)}${format.unit}"
3334

34-
// TODO: Replace by LambdaAtlas height cache
35+
// TODO: Replace by LambdaAtlas height cache and actually build a proper text with highlighted parameters
3536

3637
override val height: Double get() = 20.0
3738
override val width: Double get() = 50.0
@@ -42,10 +43,15 @@ object TPS : HudModule(
4243
}
4344
}
4445

45-
private enum class TickFormat(val output: () -> Double, val string: String) {
46-
Tick({ normalizedTickRate * 20 }, "TPS"),
47-
Milliseconds({ normalizedTickRate * 50 }, "MSPS"),
48-
Normalized({ normalizedTickRate }, "TPSN"),
49-
Percentage({ normalizedTickRate * 100 }, "TPS%")
46+
@Suppress("unused")
47+
private enum class TickFormat(
48+
val output: () -> Double,
49+
override val displayName: String,
50+
val unit: String = ""
51+
) : NamedEnum {
52+
TPS({ 1000 / averageMSPerTick }, "TPS"),
53+
MSPT({ averageMSPerTick }, "MSPT", " ms"),
54+
Normalized({ 50 / averageMSPerTick }, "TPS"),
55+
Percentage({ 5000 / averageMSPerTick }, "TPS", "%")
5056
}
5157
}

common/src/main/kotlin/com/lambda/module/modules/movement/Jesus.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import com.lambda.event.events.WorldEvent
2525
import com.lambda.event.listener.SafeListener.Companion.listen
2626
import com.lambda.module.Module
2727
import com.lambda.module.tag.ModuleTag
28-
import com.lambda.util.Nameable
28+
import com.lambda.util.NamedEnum
2929
import com.lambda.util.math.MathUtils.toInt
3030
import com.lambda.util.math.VecUtils.minus
3131
import com.lambda.util.player.MovementUtils.isInputting
@@ -53,7 +53,7 @@ object Jesus : Module(
5353
private var goUp = true
5454
private var swimmingTicks = 0
5555

56-
enum class Mode(override val displayName: String, val collision: Boolean) : Nameable.NamedEnum {
56+
enum class Mode(override val displayName: String, val collision: Boolean) : NamedEnum {
5757
NCP("NCP", true),
5858
NCP_DOLPHIN("NCP Dolphin", false),
5959
NCP_NEW("NCP New", true)

common/src/main/kotlin/com/lambda/module/modules/movement/Speed.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import com.lambda.interaction.rotation.RotationContext
2828
import com.lambda.interaction.rotation.RotationMode
2929
import com.lambda.module.Module
3030
import com.lambda.module.tag.ModuleTag
31-
import com.lambda.util.Nameable
31+
import com.lambda.util.NamedEnum
3232
import com.lambda.util.extension.contains
3333
import com.lambda.util.player.MovementUtils.addSpeed
3434
import com.lambda.util.player.MovementUtils.calcMoveYaw
@@ -85,7 +85,7 @@ object Speed : Module(
8585
private var ncpSpeed = NCP_BASE_SPEED
8686
private var lastDistance = 0.0
8787

88-
enum class Mode(override val displayName: String) : Nameable.NamedEnum {
88+
enum class Mode(override val displayName: String) : NamedEnum {
8989
GRIM_STRAFE("Grim Strafe"),
9090
NCP_STRAFE("NCP Strafe"),
9191
}

common/src/main/kotlin/com/lambda/util/Nameable.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ package com.lambda.util
2222
*/
2323
interface Nameable {
2424
val name: String
25+
}
2526

26-
interface NamedEnum {
27-
val displayName: String
28-
}
27+
interface NamedEnum {
28+
val displayName: String
2929
}

common/src/main/kotlin/com/lambda/util/TpsClock.kt renamed to common/src/main/kotlin/com/lambda/util/ServerTPS.kt

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,39 +23,28 @@ import com.lambda.event.listener.SafeListener.Companion.listen
2323
import com.lambda.util.collections.LimitedDecayQueue
2424
import net.minecraft.network.packet.s2c.play.WorldTimeUpdateS2CPacket
2525

26-
object TpsClock {
27-
private val tickHistory = LimitedDecayQueue<Double>(120, 5000)
26+
object ServerTPS {
27+
// Server sends exactly one world time update every 20 server ticks (one per second).
28+
private val updateHistory = LimitedDecayQueue<Long>(61, 60000)
2829
private var lastUpdate = 0L
2930

30-
/**
31-
* Returns the average tick rate normalized between 0 and 1
32-
*/
33-
val normalizedTickRate: Double
34-
get() {
35-
val average = tickHistory.average()
36-
return if (average.isNaN()) 1.0 else average
37-
}
38-
39-
/**
40-
* Returns the average tick rate normalized multiplied by 20
41-
*/
42-
val tickRate: Double get() = normalizedTickRate * 20
31+
val averageMSPerTick: Double
32+
get() = (if (updateHistory.isEmpty()) 1000.0 else updateHistory.average()) / 20
4333

4434
init {
4535
listen<PacketEvent.Receive.Pre>(priority = 10000) {
4636
if (it.packet !is WorldTimeUpdateS2CPacket) return@listen
47-
37+
val currentTime = System.currentTimeMillis()
4838

4939
if (lastUpdate != 0L) {
50-
val timeElapsed = (System.nanoTime() - lastUpdate) / 1E9
51-
tickHistory.add((1 / timeElapsed).coerceIn(0.0, 1.0))
40+
updateHistory.add(currentTime - lastUpdate)
5241
}
5342

54-
lastUpdate = System.nanoTime()
43+
lastUpdate = currentTime
5544
}
5645

5746
listen<ConnectionEvent.Connect.Post> {
58-
tickHistory.clear()
47+
updateHistory.clear()
5948
lastUpdate = 0
6049
}
6150
}

common/src/main/kotlin/com/lambda/util/extension/Enum.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@
1717

1818
package com.lambda.util.extension
1919

20-
import com.lambda.util.Nameable
20+
import com.lambda.util.NamedEnum
21+
import com.lambda.util.StringUtils.capitalize
2122

2223
val Enum<*>.displayValue
2324
get() =
24-
(this as? Nameable.NamedEnum)?.displayName ?: name.split('_').joinToString(" ") { low ->
25-
low.lowercase().replaceFirstChar {
26-
if (it.isLowerCase()) it.titlecase() else it.toString()
27-
}
28-
}
25+
(this as? NamedEnum)?.displayName ?: name.split('_').joinToString(" ") { low ->
26+
low.capitalize()
27+
}

0 commit comments

Comments
 (0)