@@ -33,44 +33,53 @@ import net.minecraft.network.packet.c2s.play.TeleportConfirmC2SPacket
3333
3434// ToDo: HUD info
3535object PacketLimiter : Module(
36- name = "PacketLimiter",
37- description = "Limits the amount of packets sent to the server",
38- tag = ModuleTag.NETWORK,
36+ name = "PacketLimiter",
37+ description = "Limits the amount of packets sent to the server",
38+ tag = ModuleTag.NETWORK,
3939) {
40- private var packetQueue = LimitedDecayQueue<PacketEvent.Send.Pre>(99, 1000)
41- private val limit by setting("Limit", 99, 1..100, 1, "The maximum amount of packets to send per given time interval", unit = " packets")
42- .onValueChange { _, to -> packetQueue.setSizeLimit(to) }
40+ private var packetQueueMap = mutableMapOf<String, LimitedDecayQueue<PacketEvent.Send.Pre>>()
41+ private val globalQueue = LimitedDecayQueue<PacketEvent.Send.Pre>(1, 1)
4342
44- private val interval by setting("Duration", 4000L, 1L..10000L, 50L, "The interval / duration in milliseconds to limit packets for", unit = " ms")
45- .onValueChange { _, to -> packetQueue.setDecayTime(to) }
43+ private val limit by setting("Limit per packet", 99, 1..100, 1, "The maximum amount of packets to send per given time interval", unit = " packets")
44+ .onValueChange { _, to -> packetQueueMap.forEach { (t, u) -> u.setSizeLimit(to) } }
45+ private val globalLimit by setting("Global Limit", 1000, 100..5000, 50, "The maximum amount of packets to send overall per given time interval", unit = " packets")
46+ .onValueChange { _, to -> globalQueue.setSizeLimit(to) }
4647
47- private val defaultIgnorePackets = setOf(
48- nameOf<CommonPongC2SPacket>(),
49- nameOf<PositionAndOnGround>(),
50- nameOf<Full>(),
51- nameOf<LookAndOnGround>(),
52- nameOf<OnGroundOnly>(),
53- nameOf<TeleportConfirmC2SPacket>()
54- )
48+ private val interval by setting("Duration", 4000L, 1L..10000L, 50L, "The interval / duration in milliseconds to limit packets for", unit = " ms")
49+ .onValueChange { _, to -> packetQueueMap.forEach { (t, u) -> u.setDecayTime(to) }; globalQueue.setDecayTime(to) }
5550
56- inline fun <reified T> nameOf(): String = T::class.className
51+ private val defaultIgnorePackets = setOf(
52+ nameOf<CommonPongC2SPacket>(),
53+ nameOf<PositionAndOnGround>(),
54+ nameOf<Full>(),
55+ nameOf<LookAndOnGround>(),
56+ nameOf<OnGroundOnly>(),
57+ nameOf<TeleportConfirmC2SPacket>()
58+ )
5759
58- // ToDo: Find a way to have a list of serverbound packets
59- private val ignorePackets by setting("Ignore Packets", defaultIgnorePackets, description = "Packets to ignore when limiting")
60+ inline fun <reified T> nameOf(): String = T::class.className
6061
61- init {
62- onEnable {
63- packetQueue = LimitedDecayQueue(limit, interval)
64- }
62+ // ToDo: Find a way to have a list of serverbound packets
63+ private val ignorePackets by setting("Ignore Packets", defaultIgnorePackets, description = "Packets to ignore when limiting")
6564
66- listen<PacketEvent.Send.Pre>(Int.MAX_VALUE) {
67- if (it.packet::class.java.name in ignorePackets) return@listen
65+ init {
66+ onEnable {
67+ packetQueueMap.clear()
68+ globalQueue.setDecayTime(interval)
69+ globalQueue.setSizeLimit(globalLimit)
70+ }
6871
69- // this@PacketLimiter.info("Packet sent: ${it.packet::class.simpleName} (${packetQueue.size} / $limit) ${Instant.now()}")
70- if (packetQueue.add(it) ) return@listen
72+ listen<PacketEvent.Send.Pre>(Int.MAX_VALUE) {
73+ if (it.packet::class.java.name in ignorePackets ) return@listen
7174
72- it.cancel()
73- this@PacketLimiter.info("Packet limit reached, dropping packet: ${it.packet::class.simpleName} (${packetQueue.size} / $limit)")
74- }
75- }
75+ // this@PacketLimiter.info("Packet sent: ${it.packet::class.simpleName} (${packetQueue.size} / $limit) ${Instant.now()}")
76+ val queue = packetQueueMap.getOrPut(it.packet::class.java.name) {
77+ LimitedDecayQueue(limit, interval)
78+ }
79+ if (queue.add(it)) return@listen
80+
81+ it.cancel()
82+ this@PacketLimiter.info("Packet limit reached, dropping packet: ${it.packet::class.simpleName} (${queue.size} / $limit)")
83+ }
84+ }
7685}
0 commit comments