Skip to content

Commit d5e8180

Browse files
committed
Update PacketLimiter.kt
1 parent cde1417 commit d5e8180

File tree

1 file changed

+43
-50
lines changed

1 file changed

+43
-50
lines changed

common/src/main/kotlin/com/lambda/module/modules/network/PacketLimiter.kt

Lines changed: 43 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,25 @@ import com.lambda.util.collections.LimitedDecayQueue
2929
import com.lambda.util.math.Vec2d
3030
import net.minecraft.client.gui.screen.ingame.GenericContainerScreen
3131
import net.minecraft.network.packet.c2s.common.CommonPongC2SPacket
32-
import net.minecraft.network.packet.c2s.play.ClickSlotC2SPacket
3332
import net.minecraft.network.packet.c2s.play.PlayerMoveC2SPacket.Full
3433
import net.minecraft.network.packet.c2s.play.PlayerMoveC2SPacket.LookAndOnGround
3534
import net.minecraft.network.packet.c2s.play.PlayerMoveC2SPacket.OnGroundOnly
3635
import net.minecraft.network.packet.c2s.play.PlayerMoveC2SPacket.PositionAndOnGround
3736
import net.minecraft.network.packet.c2s.play.TeleportConfirmC2SPacket
3837
import java.awt.Color
39-
import kotlin.math.floor
4038

4139
// ToDo: HUD info
4240
object PacketLimiter : Module(
4341
name = "PacketLimiter",
4442
description = "Limits the amount of packets sent to the server",
4543
defaultTags = setOf(ModuleTag.NETWORK)
4644
) {
47-
private var packetQueue = LimitedDecayQueue<PacketEvent.Send.Pre>(999, 1000)
48-
private var clickPacketQueue = LimitedDecayQueue<Long>(500, 30000)
49-
private val limit by setting("Limit", 99, 1..1000, 1, "The maximum amount of packets to send per given time interval", unit = " packets")
45+
private val page by setting("Page", Page.General)
46+
47+
private val limit by setting("Limit", 99, 1..1000, 1, "The maximum amount of packets to send per given time interval", unit = " packets") { page == Page.General }
5048
.onValueChange { _, to -> packetQueue.setSizeLimit(to) }
5149

52-
private val interval by setting("Duration", 4000L, 1L..10000L, 50L, "The interval / duration in milliseconds to limit packets for", unit = " ms")
50+
private val interval by setting("Duration", 4000L, 1L..10000L, 50L, "The interval / duration in milliseconds to limit packets for", unit = " ms") { page == Page.General }
5351
.onValueChange { _, to -> packetQueue.setDecayTime(to) }
5452

5553
private val defaultIgnorePackets = setOf(
@@ -60,68 +58,63 @@ object PacketLimiter : Module(
6058
OnGroundOnly::class,
6159
TeleportConfirmC2SPacket::class
6260
)
63-
private val limitAllPackets by setting("Limit All", false, "Limit all send packets")
64-
private val ignorePackets by setting("Ignore Packets", defaultIgnorePackets.mapNotNull { it.simpleName }, "Packets to ignore when limiting") { limitAllPackets }
65-
private val limitClickPackets by setting("Clicks limit", true, "Limits the amount of click packets you can send to prevent kicks.")
66-
private val limitClickWindowSize by setting("Click limit window size", 4f, 0.1f..10.0f, 0.1f, "Click limit window size", unit = " s") {
67-
limitClickPackets
68-
}.onValueChange { _, to -> clickPacketQueue.setDecayTime((to * 1000).toLong()) }
69-
private val limitClickRate by setting("Click limit rate", 19.3f, 0.1f..40f, 0.1f, "Click limit rate", unit = " packets/sec") {
70-
limitClickPackets
71-
}.onValueChange { _, to -> clickPacketQueue.setSizeLimit((limitClickWindowSize * to).toInt()) }
72-
private val limitClickRender by setting("Render Limit in Container", true, "Render the amount of clicks remaining in the container screen") {
73-
limitClickPackets
74-
}
7561

76-
private val clickPacketsWindowAmount: Int
77-
get() = floor(limitClickWindowSize * limitClickRate).toInt()
62+
private val limitAllPackets by setting("Limit All", false, "Limit all send packets") { page == Page.General }
63+
private val ignorePackets by setting("Ignore Packets", defaultIgnorePackets.mapNotNull { it.simpleName }, "Packets to ignore when limiting") { page == Page.General && limitAllPackets }
64+
65+
private val limitClicks by setting("Clicks Limit", true, "Limits the amount of click packets you can send to prevent kicks on certain servers.") { page == Page.Clicks }
66+
private val limitClickWindowSize by setting("Click Limit Window Size", 4.0, 0.1..10.0, 0.1, "Click limit window size", unit = " s") { page == Page.Clicks && limitClicks }
67+
.onValueChange { _, to -> clickPacketQueue.setDecayTime((to * 1000).toLong()) }
68+
private val limitClickRate by setting("Click Limit Rate", 19, 1..40, 1, "Click limit rate", unit = " packets/sec") { page == Page.Clicks && limitClicks }
69+
.onValueChange { _, to -> clickPacketQueue.setSizeLimit((limitClickWindowSize * to).toInt()) }
70+
private val limitClickRender by setting("Render Limit in Container", true, "Render the amount of clicks remaining in the container screen") { page == Page.Clicks && limitClicks }
71+
72+
private var packetQueue = LimitedDecayQueue<PacketEvent.Send.Pre>(999, 1000)
73+
private var clickPacketQueue = LimitedDecayQueue<Unit>(500, 30000)
74+
75+
private val clickPacketsWindowAmount: Double
76+
get() = limitClickWindowSize * limitClickRate
77+
7878
private val clickPacketsRemaining: Int
79-
get() = clickPacketsWindowAmount - clickPacketQueue.size
79+
get() = clickPacketsWindowAmount.toInt() - clickPacketQueue.size
8080

81-
init {
82-
onEnable {
83-
packetQueue = LimitedDecayQueue(limit, interval)
84-
}
81+
private val canSendClickPackets: Boolean
82+
get() = clickPacketQueue.size + 1 <= clickPacketsWindowAmount
8583

84+
init {
8685
listen<PacketEvent.Send.Pre>(Int.MAX_VALUE) {
8786
if (it.packet::class.simpleName in ignorePackets) return@listen
88-
89-
if (limitClickPackets && it.packet is ClickSlotC2SPacket) {
90-
if (!canSendClickPackets(1)) {
91-
it.cancel()
92-
return@listen
93-
} else {
94-
clickPacketQueue.add(System.currentTimeMillis())
95-
}
96-
}
97-
98-
// [email protected]("Packet sent: ${it.packet::class.simpleName} (${packetQueue.size} / $limit) ${Instant.now()}")
9987
if (packetQueue.add(it)) return@listen
10088

10189
it.cancel()
10290
this@PacketLimiter.info("Packet limit reached, dropping packet: ${it.packet::class.simpleName} (${packetQueue.size} / $limit)")
10391
}
10492

10593
listen<PlayerEvent.SlotClick> {
106-
if (!limitClickPackets) return@listen
107-
if (!canSendClickPackets(1)) {
108-
it.cancel()
109-
return@listen
110-
}
94+
if (limitClicks && !canSendClickPackets) it.cancel()
95+
else if (clickPacketQueue.add(Unit)) return@listen
96+
97+
this@PacketLimiter.info("Slot click limit reached, dropping ${it.action} at ${it.slot} in ${it.screenHandler::class.simpleName} (${clickPacketQueue.size} / $limitClickRate)")
11198
}
11299

113100
listen<RenderEvent.GUI.Fixed> {
114-
if (!limitClickRender) {
115-
return@listen
116-
}
117-
val sh = mc.currentScreen as? GenericContainerScreen ?: return@listen
118-
val remainingText = "Clicks Remaining: $clickPacketsRemaining"
101+
if (!limitClickRender) return@listen
102+
103+
val screen = mc.currentScreen as? GenericContainerScreen ?: return@listen
119104
val mcScale = mc.window.scaleFactor
120-
val fontScale = mcScale * 1.5
121-
val fontHeight = FontRenderer.getHeight(fontScale)
122-
FontRenderer.drawString(remainingText, Vec2d(sh.x * mcScale, sh.y * mcScale - fontHeight), Color(0x9DFFFF), fontScale, false)
105+
val fontHeight = FontRenderer.getHeight(mcScale * 1.5)
106+
val position = Vec2d(screen.x * mcScale, screen.y * mcScale - fontHeight)
107+
108+
FontRenderer.drawString("Clicks Remaining: $clickPacketsRemaining", position, Color(0x9DFFFF))
109+
}
110+
111+
onEnable {
112+
packetQueue = LimitedDecayQueue(limit, interval)
123113
}
124114
}
125115

126-
fun canSendClickPackets(packets: Int) = clickPacketQueue.size + packets <= clickPacketsWindowAmount
116+
enum class Page {
117+
General,
118+
Clicks
119+
}
127120
}

0 commit comments

Comments
 (0)