Skip to content

Commit c988675

Browse files
committed
Move rubberband analysis to module
1 parent 77277cf commit c988675

File tree

4 files changed

+58
-32
lines changed

4 files changed

+58
-32
lines changed

common/src/main/kotlin/com/lambda/command/commands/ModuleCommand.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ object ModuleCommand : LambdaCommand {
106106
literal("$name already ")
107107
literal(if (module.isEnabled) "enabled" else "disabled")
108108
}
109-
}, "")
109+
})
110110
return@runSafe success()
111111
}
112112

common/src/main/kotlin/com/lambda/interaction/PlayerPacketManager.kt

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import net.minecraft.util.math.MathHelper.square
2828
import net.minecraft.util.math.Vec3d
2929

3030
object PlayerPacketManager : Loadable {
31-
private val configurations = LimitedOrderedSet<PlayerPacketEvent.Pre>(100)
31+
val configurations = LimitedOrderedSet<PlayerPacketEvent.Pre>(100)
3232
private var sendTicks = 0
3333

3434
@JvmStatic
@@ -46,35 +46,6 @@ object PlayerPacketManager : Loadable {
4646
}
4747
}
4848

49-
init {
50-
listener<PacketEvent.Receive.Pre> { event ->
51-
if (event.packet !is PlayerPositionLookS2CPacket) return@listener
52-
53-
if (configurations.isEmpty()) {
54-
warn("Position was reverted", "Rubberband")
55-
return@listener
56-
}
57-
58-
val newPos = Vec3d(event.packet.x, event.packet.y, event.packet.z)
59-
val last = configurations.minBy {
60-
it.position distSq newPos
61-
}
62-
val delta = last.position dist newPos
63-
64-
warn(buildText {
65-
literal("Reverted position by ")
66-
color(Color.YELLOW) {
67-
literal("${configurations.reversed().indexOf(last) + 1}")
68-
}
69-
literal(" ticks (derivation: ")
70-
color(Color.YELLOW) {
71-
literal("%.3f".format(delta))
72-
}
73-
literal(")")
74-
}, "Rubberband")
75-
}
76-
}
77-
7849
private fun SafeContext.updatePlayerPackets(new: PlayerPacketEvent.Pre) {
7950
val previous = configurations.lastOrNull() ?: new
8051
configurations.add(new)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.lambda.module.modules.client
2+
3+
import com.lambda.event.events.PacketEvent
4+
import com.lambda.event.listener.SafeListener.Companion.listener
5+
import com.lambda.interaction.PlayerPacketManager
6+
import com.lambda.module.Module
7+
import com.lambda.module.tag.ModuleTag
8+
import com.lambda.util.Communication.warn
9+
import com.lambda.util.math.VecUtils.dist
10+
import com.lambda.util.math.VecUtils.distSq
11+
import com.lambda.util.text.Color
12+
import com.lambda.util.text.buildText
13+
import com.lambda.util.text.color
14+
import com.lambda.util.text.literal
15+
import net.minecraft.network.packet.s2c.play.PlayerPositionLookS2CPacket
16+
import net.minecraft.util.math.Vec3d
17+
18+
// ToDo: Should also include last packet info as HUD element and connection state. We may find a better name.
19+
object Rubberband : Module(
20+
name = "Rubberband",
21+
description = "Info about rubberbands",
22+
defaultTags = setOf(ModuleTag.CLIENT)
23+
) {
24+
private val showLastPacketInfo by setting("Show Last Packet", true)
25+
private val showConnectionState by setting("Show Connection State", true)
26+
private val showRubberbandInfo by setting("Show Rubberband Info", true)
27+
28+
init {
29+
listener<PacketEvent.Receive.Pre> { event ->
30+
if (!showRubberbandInfo) return@listener
31+
if (event.packet !is PlayerPositionLookS2CPacket) return@listener
32+
33+
if (PlayerPacketManager.configurations.isEmpty()) {
34+
this@Rubberband.warn("Position was reverted")
35+
return@listener
36+
}
37+
38+
val newPos = Vec3d(event.packet.x, event.packet.y, event.packet.z)
39+
val last = PlayerPacketManager.configurations.minBy {
40+
it.position distSq newPos
41+
}
42+
43+
this@Rubberband.warn(buildText {
44+
literal("Reverted position by ")
45+
color(Color.YELLOW) {
46+
literal("${PlayerPacketManager.configurations.reversed().indexOf(last) + 1}")
47+
}
48+
literal(" ticks (derivation: ")
49+
color(Color.YELLOW) {
50+
literal("%.3f".format(last.position dist newPos))
51+
}
52+
literal(")")
53+
})
54+
}
55+
}
56+
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ object Communication {
1414
fun Any.info(message: String, source: String = "") = log(LogLevel.INFO.text(message), LogLevel.INFO, source)
1515
fun Any.info(message: Text, source: Text = Text.empty()) = log(message, LogLevel.INFO, textSource = source)
1616
fun Any.warn(message: String, source: String = "") = log(LogLevel.WARN.text(message), LogLevel.WARN, source)
17-
fun Any.warn(message: Text, source: String = "") = log(message, LogLevel.WARN, source)
1817
fun Any.warn(message: Text, source: Text = Text.empty()) = log(message, LogLevel.WARN, textSource = source)
1918
fun Any.logError(message: String, source: String = "") = log(LogLevel.ERROR.text(message), LogLevel.ERROR, source)
2019
fun Any.logError(message: Text, source: Text = Text.empty()) = log(message, LogLevel.ERROR, textSource = source)

0 commit comments

Comments
 (0)