Skip to content

Commit

Permalink
fix(PacketQueueManager): schedule order and packet tracking (#4783)
Browse files Browse the repository at this point in the history
  • Loading branch information
1zun4 authored Dec 2, 2024
1 parent 6cc6c5b commit 36aecf8
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,14 @@ private void hookTickEvent(CallbackInfo callbackInfo) {
EventManager.INSTANCE.callEvent(new GameTickEvent());
}

/**
* Hook game render task queue event
*/
@Inject(method = "render", at = @At("HEAD"))
private void hookRenderTaskQueue(CallbackInfo callbackInfo) {
EventManager.INSTANCE.callEvent(new GameRenderTaskQueueEvent());
}

/**
* Hook input handling
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import kotlin.reflect.KClass
*/
val ALL_EVENT_CLASSES: Array<KClass<out Event>> = arrayOf(
GameTickEvent::class,
GameRenderTaskQueueEvent::class,
BlockChangeEvent::class,
ChunkLoadEvent::class,
ChunkDeltaUpdateEvent::class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ import net.minecraft.text.Text
@Nameable("gameTick")
class GameTickEvent : Event()

/**
* We can use this event to populate the render task queue with tasks that should be
* executed in the same frame. This is useful for more responsive task execution
* and allows to also schedule tasks off-schedule.
*/
@Nameable("gameRenderTaskQueue")
class GameRenderTaskQueueEvent : Event()

@Nameable("key")
@WebSocketEvent
class KeyEvent(val key: InputUtil.Key, val action: Int) : Event()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ object ModuleBacktrack : ClientModule("Backtrack", Category.COMBAT) {
* That gets called first, then the client's packets.
*/
@Suppress("unused")
private val tickHandler = handler<GameTickEvent>(priority = 1002) {
private val handleRenderTaskQueue = handler<GameRenderTaskQueueEvent> {
if (shouldCancelPackets()) {
processPackets()
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,17 @@ object ModuleVelocity : ClientModule("Velocity", Category.COMBAT) {
}

@Suppress("unused")
private val packetHandler = sequenceHandler<PacketEvent>(priority = 1) {
val packet = it.packet
private val packetHandler = sequenceHandler<PacketEvent>(priority = 1) { event ->
val packet = event.packet

if (!it.original) {
if (!event.original) {
return@sequenceHandler
}

if (packet is EntityVelocityUpdateS2CPacket && packet.entityId == player.id || packet is ExplosionS2CPacket) {
// When delay is above 0, we will delay the velocity update
if (delay.last > 0) {
it.cancelEvent()
event.cancelEvent()

delay.random().let { ticks ->
if (ticks > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,10 +378,11 @@ object RotationManager : EventListener {
* sometimes we update the rotation off chain (e.g. on interactItem)
* and the player.lastYaw and player.lastPitch are not updated.
*/
val packetHandler = handler<PacketEvent>(priority = -1000) { event ->
val packet = event.packet

val rotation = when (packet) {
@Suppress("unused")
val packetHandler = handler<PacketEvent>(
priority = EventPriorityConvention.READ_FINAL_STATE
) { event ->
val rotation = when (val packet = event.packet) {
is PlayerMoveC2SPacket -> {
// If we are not changing the look, we don't need to update the rotation
// but, we want to handle slow start triggers
Expand All @@ -402,7 +403,6 @@ object RotationManager : EventListener {
if (!event.isCancelled) {
actualServerRotation = rotation
}

theoreticalServerRotation = rotation
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
package net.ccbluex.liquidbounce.utils.client

import net.ccbluex.liquidbounce.config.types.NamedChoice
import net.ccbluex.liquidbounce.event.events.PacketEvent
import net.ccbluex.liquidbounce.event.events.TransferOrigin
import net.ccbluex.liquidbounce.features.module.modules.combat.crystalaura.SwitchMode
import net.ccbluex.liquidbounce.utils.aiming.RotationManager
import net.ccbluex.liquidbounce.utils.block.SwingMode
import net.ccbluex.liquidbounce.utils.inventory.OFFHAND_SLOT
import net.minecraft.client.network.ClientPlayerEntity
Expand Down Expand Up @@ -134,7 +137,12 @@ fun ClientPlayerInteractionManager.interactItem(
fun handlePacket(packet: Packet<*>) =
runCatching { (packet as Packet<ClientPlayPacketListener>).apply(mc.networkHandler) }

fun sendPacketSilently(packet: Packet<*>) = mc.networkHandler?.connection?.send(packet, null)
fun sendPacketSilently(packet: Packet<*>) {
// hack fix for the packet handler not being called on Rotation Manager for tracking
val packetEvent = PacketEvent(TransferOrigin.SEND, packet, false)
RotationManager.packetHandler.handler(packetEvent)
mc.networkHandler?.connection?.send(packetEvent.packet, null)
}

enum class MovePacketType(override val choiceName: String, val generatePacket: () -> PlayerMoveC2SPacket)
: NamedChoice {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ object PacketQueueManager : EventListener {
get() = packetQueue.isNotEmpty()

@Suppress("unused")
private val flushHandler = handler<GameRenderEvent>(
priority = EventPriorityConvention.FIRST_PRIORITY
) {
private val flushHandler = handler<GameRenderTaskQueueEvent> {
if (!inGame) {
packetQueue.clear()
return@handler
Expand All @@ -87,7 +85,7 @@ object PacketQueueManager : EventListener {

@Suppress("unused")
private val packetHandler = handler<PacketEvent>(
priority = EventPriorityConvention.READ_FINAL_STATE
priority = EventPriorityConvention.FINAL_DECISION
) { event ->
// Ignore packets that are already cancelled, as they are already handled
if (event.isCancelled) {
Expand Down Expand Up @@ -186,7 +184,7 @@ object PacketQueueManager : EventListener {
}
}

fun flush(flushWhen: (PacketSnapshot) -> Boolean) {
fun flush(flushWhen: (PacketSnapshot) -> Boolean) = mc.renderTaskQueue.add(Runnable {
packetQueue.removeIf { snapshot ->
if (flushWhen(snapshot)) {
flushSnapshot(snapshot)
Expand All @@ -195,9 +193,9 @@ object PacketQueueManager : EventListener {
false
}
}
}
})

fun flush(count: Int) {
fun flush(count: Int) = mc.renderTaskQueue.add(Runnable {
// Take all packets until the counter of move packets reaches count and send them
var counter = 0

Expand All @@ -215,7 +213,7 @@ object PacketQueueManager : EventListener {
break
}
}
}
})

fun cancel() {
positions.firstOrNull().let { pos ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ object EventPriorityConvention {
*/
const val OBJECTION_AGAINST_EVERYTHING: Int = -100

/**
* Used when the event handler should be able to object anything that happened previously
*/
const val FINAL_DECISION: Int = -500

/**
* The event should be called last. It should not only be used for events that want to read the final state of the
* event
Expand Down

0 comments on commit 36aecf8

Please sign in to comment.