Skip to content

Commit

Permalink
Clean up the structure of the AntiSpeedMitigation.kt file
Browse files Browse the repository at this point in the history
  • Loading branch information
yogwoggf committed Apr 25, 2024
1 parent 37cba66 commit 1fcfa5b
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions src/main/kotlin/com/puffy/mitigations/AntiSpeedMitigation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ const val MAXIMUM_SUSPICIOUS_MAGNITUDE_TICKS = 25
// Used if minecraft reports a XYZ before the player loads (usually garbage)
const val FALSE_MAGNITUDE_THRESHOLD = 100
private const val MITIGATION_NAME = "Speed"
private const val SERVER_VELOCITY_THRESHOLD = 3

class AntiSpeedMitigation(val player: ServerPlayerEntity) {
private var movementMagnitudes: MutableList<Double> = mutableListOf()
private var magnitudeWindow: MutableList<Double> = mutableListOf()
private val averageMagnitude: Double
get() = magnitudeWindow.average()

private var lastX: Double = 0.0
private var lastY: Double = 0.0
private var lastZ: Double = 0.0
Expand All @@ -42,7 +44,7 @@ class AntiSpeedMitigation(val player: ServerPlayerEntity) {
firstIteration = true
}

updateMagnitudeWithCurrentPlayerMovement()
addCurrentMagnitudeToWindow()
updateLastPositionWithCurrentPosition()
updateSuspiciousTicksCount()
}
Expand All @@ -53,29 +55,21 @@ class AntiSpeedMitigation(val player: ServerPlayerEntity) {
suspiciousMagnitudeTicks > MAXIMUM_SUSPICIOUS_MAGNITUDE_TICKS

private fun updateSuspiciousTicksCount() {
if (getAverageMagnitude() >= MAXIMUM_AVERAGE_MAGNITUDE) {
if (!averageMagnitude.isNaN() && averageMagnitude >= MAXIMUM_AVERAGE_MAGNITUDE) {
suspiciousMagnitudeTicks++
} else {
suspiciousMagnitudeTicks = 0
}
}

private fun getAverageMagnitude(): Double {
if (movementMagnitudes.isEmpty()) {
return 0.0
}

return movementMagnitudes.average()
}

private fun updateMagnitudeWithCurrentPlayerMovement() {
private fun addCurrentMagnitudeToWindow() {
val magnitude = calculateMagnitudeForPlayer()
if (magnitude >= FALSE_MAGNITUDE_THRESHOLD) {
return
}

movementMagnitudes.trimToLength(SLIDING_WINDOW_LENGTH_TICKS)
movementMagnitudes.add(magnitude)
magnitudeWindow.trimToLength(SLIDING_WINDOW_LENGTH_TICKS)
magnitudeWindow.add(magnitude)
}

private fun calculateMagnitudeForPlayer(): Double {
Expand Down

0 comments on commit 1fcfa5b

Please sign in to comment.