Skip to content

Commit

Permalink
PacketUtils methods cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
MukjepScarlet committed Dec 20, 2024
1 parent d2689ab commit f90c812
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ object Backtrack : Module("Backtrack", Category.COMBAT, hideModule = false) {
get() = synchronized(packetQueue) { packetQueue.isEmpty() }

val areQueuedPacketsEmpty
get() = PacketUtils.isEmpty()
get() = PacketUtils.isQueueEmpty()

val onPacket = handler<PacketEvent> { event ->
val packet = event.packet
Expand Down Expand Up @@ -472,7 +472,7 @@ object Backtrack : Module("Backtrack", Category.COMBAT, hideModule = false) {
synchronized(packetQueue) {
packetQueue.removeAll { (packet, timestamp) ->
if (timestamp <= System.currentTimeMillis() - supposedDelay) {
schedulePacketProcess(packet)
PacketUtils.schedulePacketProcess(packet)
true
} else false
}
Expand All @@ -494,7 +494,7 @@ object Backtrack : Module("Backtrack", Category.COMBAT, hideModule = false) {
synchronized(packetQueue) {
packetQueue.removeAll { (packet, timestamp) ->
if (timestamp <= time) {
schedulePacketProcess(packet)
PacketUtils.schedulePacketProcess(packet)
true
} else false
}
Expand Down Expand Up @@ -533,7 +533,7 @@ object Backtrack : Module("Backtrack", Category.COMBAT, hideModule = false) {
synchronized(packetQueue) {
packetQueue.removeAll {
if (handlePackets) {
schedulePacketProcess(it.packet)
PacketUtils.schedulePacketProcess(it.packet)
}

true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import net.ccbluex.liquidbounce.ui.client.hud.element.elements.Notification
import net.ccbluex.liquidbounce.utils.attack.EntityUtils.isLookingOnEntities
import net.ccbluex.liquidbounce.utils.attack.EntityUtils.isSelected
import net.ccbluex.liquidbounce.utils.client.BlinkUtils
import net.ccbluex.liquidbounce.utils.client.PacketUtils
import net.ccbluex.liquidbounce.utils.client.chat
import net.ccbluex.liquidbounce.utils.client.schedulePacketProcess
import net.ccbluex.liquidbounce.utils.extensions.*
import net.ccbluex.liquidbounce.utils.kotlin.RandomUtils.nextFloat
import net.ccbluex.liquidbounce.utils.kotlin.RandomUtils.nextInt
Expand Down Expand Up @@ -279,7 +279,8 @@ object TimerRange : Module("TimerRange", Category.COMBAT, hideModule = false) {
val onMotion = handler<MotionEvent> { event ->
if (blink && event.eventState == EventState.POST) {
synchronized(packetsReceived) {
schedulePacketProcess(packetsReceived)
PacketUtils.schedulePacketProcess(packetsReceived)
Unit
}
packetsReceived.clear()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,9 @@ import net.ccbluex.liquidbounce.features.module.modules.exploit.Disabler
import net.ccbluex.liquidbounce.features.module.modules.movement.Speed
import net.ccbluex.liquidbounce.utils.attack.EntityUtils.isLookingOnEntities
import net.ccbluex.liquidbounce.utils.attack.EntityUtils.isSelected
import net.ccbluex.liquidbounce.utils.client.*
import net.ccbluex.liquidbounce.utils.client.PacketUtils.sendPacket
import net.ccbluex.liquidbounce.utils.client.PacketUtils.sendPackets
import net.ccbluex.liquidbounce.utils.client.realMotionX
import net.ccbluex.liquidbounce.utils.client.realMotionY
import net.ccbluex.liquidbounce.utils.client.realMotionZ
import net.ccbluex.liquidbounce.utils.client.schedulePacketProcess
import net.ccbluex.liquidbounce.utils.extensions.*
import net.ccbluex.liquidbounce.utils.kotlin.RandomUtils.nextInt
import net.ccbluex.liquidbounce.utils.movement.MovementUtils.isOnGround
Expand Down Expand Up @@ -690,7 +687,7 @@ object Velocity : Module("Velocity", Category.COMBAT, hideModule = false) {
synchronized(packets) {
packets.entries.removeAll { (packet, timestamp) ->
if (velocity || timestamp <= System.currentTimeMillis() - spoofDelay) {
schedulePacketProcess(packet)
PacketUtils.schedulePacketProcess(packet)
true
} else false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import net.ccbluex.liquidbounce.event.handler
import net.ccbluex.liquidbounce.features.module.Category
import net.ccbluex.liquidbounce.features.module.Module
import net.ccbluex.liquidbounce.features.module.modules.combat.Velocity
import net.ccbluex.liquidbounce.utils.client.schedulePacketProcess
import net.ccbluex.liquidbounce.utils.client.PacketUtils
import net.minecraft.network.Packet
import net.minecraft.network.play.client.C0CPacketInput
import net.minecraft.network.play.server.*
Expand Down Expand Up @@ -76,7 +76,7 @@ object PingSpoof : Module("PingSpoof", Category.EXPLOIT, hideModule = false) {
synchronized(packetQueue) {
packetQueue.entries.removeAll { (packet, timestamp) ->
if (all || timestamp <= (System.currentTimeMillis() - spoofDelay)) {
schedulePacketProcess(packet)
PacketUtils.schedulePacketProcess(packet)
true
} else false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import net.ccbluex.liquidbounce.features.module.modules.movement.Fly.stopOnLandi
import net.ccbluex.liquidbounce.features.module.modules.movement.Fly.stopOnNoMove
import net.ccbluex.liquidbounce.features.module.modules.movement.Fly.timerSlowed
import net.ccbluex.liquidbounce.features.module.modules.movement.flymodes.FlyMode
import net.ccbluex.liquidbounce.utils.client.PacketUtils
import net.ccbluex.liquidbounce.utils.client.PacketUtils.sendPackets
import net.ccbluex.liquidbounce.utils.client.chat
import net.ccbluex.liquidbounce.utils.client.schedulePacketProcess
import net.ccbluex.liquidbounce.utils.extensions.isMoving
import net.ccbluex.liquidbounce.utils.extensions.tryJump
import net.ccbluex.liquidbounce.utils.movement.MovementUtils.strafe
Expand Down Expand Up @@ -204,7 +204,8 @@ object BlocksMC2 : FlyMode("BlocksMC2"), Listenable {

private fun blink() {
synchronized(packetsReceived) {
schedulePacketProcess(packetsReceived)
PacketUtils.schedulePacketProcess(packetsReceived)
Unit
}
synchronized(packets) {
sendPackets(*packets.toTypedArray(), triggerEvents = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ object BlinkUtils : MinecraftInstance, Listenable {
if (sent == true && receive == false) {
if (event.eventType == EventState.RECEIVE) {
synchronized(packetsReceived) {
schedulePacketProcess(packetsReceived)
PacketUtils.schedulePacketProcess(packetsReceived)
Unit
}
packetsReceived.clear()
}
Expand Down Expand Up @@ -119,7 +120,7 @@ object BlinkUtils : MinecraftInstance, Listenable {

fun syncSent() {
synchronized(packetsReceived) {
schedulePacketProcess(packetsReceived)
PacketUtils.schedulePacketProcess(packetsReceived)
packetsReceived.clear()
}
}
Expand Down Expand Up @@ -163,7 +164,8 @@ object BlinkUtils : MinecraftInstance, Listenable {

fun unblink() {
synchronized(packetsReceived) {
schedulePacketProcess(packetsReceived)
PacketUtils.schedulePacketProcess(packetsReceived)
Unit
}
synchronized(packets) {
sendPackets(*packets.toTypedArray(), triggerEvents = false)
Expand Down
33 changes: 14 additions & 19 deletions src/main/java/net/ccbluex/liquidbounce/utils/client/PacketUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,28 @@ import net.minecraft.network.play.client.C03PacketPlayer
import net.minecraft.network.play.server.*
import net.minecraft.util.BlockPos
import net.minecraft.util.Vec3
import java.util.concurrent.locks.ReentrantReadWriteLock
import java.util.concurrent.locks.ReentrantLock
import kotlin.collections.ArrayDeque
import kotlin.concurrent.read
import kotlin.concurrent.withLock
import kotlin.concurrent.write
import kotlin.math.roundToInt

object PacketUtils : MinecraftInstance, Listenable {

private val queuedPackets = ArrayDeque<Packet<*>>()
private val queueLock = ReentrantReadWriteLock()
private val queueLock = ReentrantLock()

fun addAll(elements: Collection<Packet<*>>): Boolean = queueLock.write { queuedPackets.addAll(elements) }

fun add(element: Packet<*>): Boolean = queueLock.write { queuedPackets.add(element) }
fun schedulePacketProcess(elements: Collection<Packet<*>>): Boolean = queueLock.withLock {
queuedPackets.addAll(elements)
}

fun isEmpty(): Boolean = queueLock.read { queuedPackets.isEmpty() }
fun schedulePacketProcess(element: Packet<*>): Boolean = queueLock.withLock {
queuedPackets.add(element)
}

val size: Int get() = queueLock.read { queuedPackets.size }
fun isQueueEmpty(): Boolean = queueLock.withLock {
queuedPackets.isEmpty()
}

val onTick = handler<GameTickEvent>(priority = 2) {
for (entity in mc.theWorld.loadedEntityList) {
Expand Down Expand Up @@ -91,7 +95,7 @@ object PacketUtils : MinecraftInstance, Listenable {
return@handler
}

queueLock.write {
queueLock.withLock {
queuedPackets.removeEach { packet ->
handlePacket(packet)
val packetEvent = PacketEvent(packet, EventState.RECEIVE)
Expand All @@ -105,7 +109,7 @@ object PacketUtils : MinecraftInstance, Listenable {

val onWorld = handler<WorldEvent>(priority = -1) { event ->
if (event.worldClient == null) {
queueLock.write {
queueLock.withLock {
queuedPackets.clear()
}
}
Expand Down Expand Up @@ -244,12 +248,3 @@ var C03PacketPlayer.pos
y = value.yCoord
z = value.zCoord
}


fun schedulePacketProcess(packet: Packet<*>) {
PacketUtils.add(packet)
}

fun schedulePacketProcess(packets: Collection<Packet<*>>) {
PacketUtils.addAll(packets)
}

0 comments on commit f90c812

Please sign in to comment.