| 1 | /*
|
| 2 | * This file is part of LiquidBounce (https://github.com/CCBlueX/LiquidBounce)
|
| 3 | *
|
| 4 | * Copyright (c) 2015 - 2026 CCBlueX
|
| 5 | *
|
| 6 | * LiquidBounce is free software: you can redistribute it and/or modify
|
| 7 | * it under the terms of the GNU General Public License as published by
|
| 8 | * the Free Software Foundation, either version 3 of the License, or
|
| 9 | * (at your option) any later version.
|
| 10 | *
|
| 11 | * LiquidBounce is distributed in the hope that it will be useful,
|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 14 | * GNU General Public License for more details.
|
| 15 | *
|
| 16 | * You should have received a copy of the GNU General Public License
|
| 17 | * along with LiquidBounce. If not, see <https://www.gnu.org/licenses/>.
|
| 18 | */
|
| 19 | package net.ccbluex.liquidbounce.utils.aiming
|
| 20 |
|
| 21 | import net.ccbluex.liquidbounce.event.EventListener
|
| 22 | import net.ccbluex.liquidbounce.event.EventManager
|
| 23 | import net.ccbluex.liquidbounce.event.events.GameTickEvent
|
| 24 | import net.ccbluex.liquidbounce.event.events.MouseRotationEvent
|
| 25 | import net.ccbluex.liquidbounce.event.events.PacketEvent
|
| 26 | import net.ccbluex.liquidbounce.event.events.PlayerVelocityStrafe
|
| 27 | import net.ccbluex.liquidbounce.event.events.RotationUpdateEvent
|
| 28 | import net.ccbluex.liquidbounce.event.events.TransferOrigin
|
| 29 | import net.ccbluex.liquidbounce.event.events.WorldChangeEvent
|
| 30 | import net.ccbluex.liquidbounce.event.handler
|
| 31 | import net.ccbluex.liquidbounce.features.addon.AddonApi
|
| 32 | import net.ccbluex.liquidbounce.features.blink.BlinkManager
|
| 33 | import net.ccbluex.liquidbounce.features.module.ClientModule
|
| 34 | import net.ccbluex.liquidbounce.features.module.modules.combat.backtrack.ModuleBacktrack
|
| 35 | import net.ccbluex.liquidbounce.features.module.modules.movement.ModuleFreeze
|
| 36 | import net.ccbluex.liquidbounce.utils.aiming.data.Rotation
|
| 37 | import net.ccbluex.liquidbounce.utils.aiming.features.MovementCorrection
|
| 38 | import net.ccbluex.liquidbounce.utils.aiming.utils.RotationUtil
|
| 39 | import net.ccbluex.liquidbounce.utils.aiming.utils.setRotation
|
| 40 | import net.ccbluex.liquidbounce.utils.aiming.utils.withFixedYaw
|
| 41 | import net.ccbluex.liquidbounce.utils.client.RestrictedSingleUseAction
|
| 42 | import net.ccbluex.liquidbounce.utils.client.inGame
|
| 43 | import net.ccbluex.liquidbounce.utils.client.mc
|
| 44 | import net.ccbluex.liquidbounce.utils.client.player
|
| 45 | import net.ccbluex.liquidbounce.utils.combat.CombatManager
|
| 46 | import net.ccbluex.liquidbounce.utils.entity.lastRotation
|
| 47 | import net.ccbluex.liquidbounce.utils.entity.rotation
|
| 48 | import net.ccbluex.liquidbounce.utils.inventory.InventoryManager
|
| 49 | import net.ccbluex.liquidbounce.utils.kotlin.EventPriorityConvention
|
| 50 | import net.ccbluex.liquidbounce.utils.kotlin.EventPriorityConvention.FIRST_PRIORITY
|
| 51 | import net.ccbluex.liquidbounce.utils.kotlin.EventPriorityConvention.MODEL_STATE
|
| 52 | import net.ccbluex.liquidbounce.utils.kotlin.Priority
|
| 53 | import net.ccbluex.liquidbounce.utils.client.RequestHandler
|
| 54 | import net.minecraft.client.gui.screens.inventory.ContainerScreen
|
| 55 | import net.minecraft.network.protocol.game.ClientboundPlayerPositionPacket
|
| 56 | import net.minecraft.network.protocol.game.ClientboundPlayerRotationPacket
|
| 57 | import net.minecraft.network.protocol.game.ServerboundMovePlayerPacket
|
| 58 | import net.minecraft.network.protocol.game.ServerboundUseItemPacket
|
| 59 | import net.minecraft.util.Mth
|
| 60 | import net.minecraft.world.entity.Entity
|
| 61 | import net.minecraft.world.entity.Relative
|
| 62 |
|
| 63 | /**
|
| 64 | * A rotation manager
|
| 65 | */
|
| 66 | object RotationManager : EventListener {
|
| 67 |
|
| 68 | /**
|
| 69 | * Our final target rotation. This rotation is only used to define our current rotation.
|
| 70 | */
|
| 71 | private val rotationTarget
|
| 72 | get() = rotationTargetHandler.getActiveRequestValue()
|
| 73 | private val rotationTargetHandler = RequestHandler<RotationTarget>()
|
| 74 |
|
| 75 | @Suppress("unused")
|
| 76 | private val lifecycleListener = object : EventListener {
|
| 77 | private val worldChangeHandler = handler<WorldChangeEvent> { reset() }
|
| 78 | }
|
| 79 |
|
| 80 | val activeRotationTarget: RotationTarget?
|
| 81 | get() = rotationTarget ?: previousRotationTarget
|
| 82 | internal var previousRotationTarget: RotationTarget? = null
|
| 83 | private set
|
| 84 |
|
| 85 | /**
|
| 86 | * The rotation we want to aim at. This DOES NOT mean that the server already received this rotation.
|
| 87 | */
|
| 88 | @AddonApi
|
| 89 | var currentRotation: Rotation? = null
|
| 90 | private set(value) {
|
| 91 | previousRotation = if (value == null) {
|
| 92 | null
|
| 93 | } else {
|
| 94 | field ?: mc.player?.rotation ?: Rotation.ZERO
|
| 95 | }
|
| 96 |
|
| 97 | field = value
|
| 98 | }
|
| 99 |
|
| 100 | // Used for rotation interpolation
|
| 101 | var playerRotation: Rotation? = null
|
| 102 | private set
|
| 103 | var previousRotation: Rotation? = null
|
| 104 | private set
|
| 105 |
|
| 106 | private val fakeLagging
|
| 107 | get() = BlinkManager.isLagging || ModuleBacktrack.isLagging()
|
| 108 |
|
| 109 | private val freezing
|
| 110 | get() = ModuleFreeze.running
|
| 111 |
|
| 112 | @AddonApi
|
| 113 | val serverRotation: Rotation
|
| 114 | get() = if (fakeLagging || freezing) theoreticalServerRotation else actualServerRotation
|
| 115 |
|
| 116 | /**
|
| 117 | * Yaw used by [net.minecraft.world.entity.Entity.moveRelative] after movement correction.
|
| 118 | */
|
| 119 | val movementYaw: Float
|
| 120 | get() = resolveMovementYaw(
|
| 121 | playerYaw = player.yRot,
|
| 122 | managedYaw = currentRotation?.yaw ?: Float.NaN,
|
| 123 | movementCorrection = activeRotationTarget?.movementCorrection,
|
| 124 | )
|
| 125 |
|
| 126 | /**
|
| 127 | * The rotation that was already sent to the server and is currently active.
|
| 128 | * The value is not being written by the packets, but we gather the Rotation from the last yaw and pitch variables
|
| 129 | * from our player instance handled by the sendMovementPackets() function.
|
| 130 | */
|
| 131 | var actualServerRotation = Rotation.ZERO
|
| 132 | private set
|
| 133 |
|
| 134 | private var theoreticalServerRotation = Rotation.ZERO
|
| 135 |
|
| 136 | private fun reset() {
|
| 137 | rotationTargetHandler.clear()
|
| 138 | previousRotationTarget = null
|
| 139 | currentRotation = null
|
| 140 | playerRotation = null
|
| 141 | previousRotation = null
|
| 142 | actualServerRotation = Rotation.ZERO
|
| 143 | theoreticalServerRotation = Rotation.ZERO
|
| 144 | }
|
| 145 |
|
| 146 | /**
|
| 147 | * Applies the rotation normalization performed by the vanilla server for rotation-bearing
|
| 148 | * movement and item-use packets.
|
| 149 | *
|
| 150 | * @see net.minecraft.server.network.ServerGamePacketListenerImpl.handleMovePlayer
|
| 151 | * @see net.minecraft.server.network.ServerGamePacketListenerImpl.handleUseItem
|
| 152 | */
|
| 153 | private fun serverboundRotation(yaw: Float, pitch: Float) = Rotation(
|
| 154 | yaw = Mth.wrapDegrees(yaw),
|
| 155 | pitch = Mth.wrapDegrees(pitch).coerceIn(-90f, 90f),
|
| 156 | isNormalized = true
|
| 157 | )
|
| 158 |
|
| 159 | /**
|
| 160 | * Resolves the relative rotation flags used by vanilla player position and rotation updates.
|
| 161 | *
|
| 162 | * @see net.minecraft.world.entity.PositionMoveRotation.calculateAbsolute
|
| 163 | * @see net.minecraft.world.entity.Entity.forceSetRotation
|
| 164 | */
|
| 165 | private fun clientboundRotation(
|
| 166 | yaw: Float,
|
| 167 | pitch: Float,
|
| 168 | relativeYaw: Boolean,
|
| 169 | relativePitch: Boolean
|
| 170 | ) = Rotation(
|
| 171 | yaw = yaw + if (relativeYaw) actualServerRotation.yaw else 0f,
|
| 172 | pitch = (pitch + if (relativePitch) actualServerRotation.pitch else 0f).coerceIn(-90f, 90f),
|
| 173 | isNormalized = true
|
| 174 | )
|
| 175 |
|
| 176 | @AddonApi
|
| 177 | @Suppress("LongParameterList")
|
| 178 | fun setRotationTarget(
|
| 179 | rotation: Rotation,
|
| 180 | considerInventory: Boolean = true,
|
| 181 | valueGroup: RotationsValueGroup,
|
| 182 | priority: Priority,
|
| 183 | provider: ClientModule,
|
| 184 | whenReached: RestrictedSingleUseAction? = null
|
| 185 | ) {
|
| 186 | setRotationTarget(valueGroup.toRotationTarget(
|
| 187 | rotation, considerInventory = considerInventory, whenReached = whenReached
|
| 188 | ), priority, provider)
|
| 189 | }
|
| 190 |
|
| 191 | @AddonApi
|
| 192 | fun setRotationTarget(plan: RotationTarget, priority: Priority, provider: ClientModule) =
|
| 193 | setRotationTarget(plan, priority.priority, provider)
|
| 194 |
|
| 195 | @AddonApi
|
| 196 | fun setRotationTarget(plan: RotationTarget, priority: Int, provider: ClientModule) {
|
| 197 | if (!allowedToUpdate()) {
|
| 198 | return
|
| 199 | }
|
| 200 |
|
| 201 | rotationTargetHandler.request(
|
| 202 | RequestHandler.Request(
|
| 203 | if (plan.movementCorrection == MovementCorrection.CHANGE_LOOK) 1 else plan.ticksUntilReset,
|
| 204 | priority,
|
| 205 | provider,
|
| 206 | plan
|
| 207 | )
|
| 208 | )
|
| 209 | }
|
| 210 |
|
| 211 | /**
|
| 212 | * Checks if the rotation is allowed to be updated
|
| 213 | */
|
| 214 | fun isRotatingAllowed(rotationTarget: RotationTarget): Boolean {
|
| 215 | if (!allowedToUpdate()) {
|
| 216 | return false
|
| 217 | }
|
| 218 |
|
| 219 | if (rotationTarget.considerInventory) {
|
| 220 | if (InventoryManager.isInventoryOpen || mc.gui.screen() is ContainerScreen) {
|
| 221 | return false
|
| 222 | }
|
| 223 | }
|
| 224 |
|
| 225 | return true
|
| 226 | }
|
| 227 |
|
| 228 | /**
|
| 229 | * Update current rotation to a new rotation step
|
| 230 | */
|
| 231 | @Suppress("CognitiveComplexMethod", "NestedBlockDepth")
|
| 232 | fun update() {
|
| 233 | val playerRotation = player.rotation.also { this.playerRotation = it }
|
| 234 | val activeRotationTarget = this.activeRotationTarget ?: return
|
| 235 | val rotationTarget = this.rotationTarget
|
| 236 |
|
| 237 | // Prevents any rotation changes when inventory is opened
|
| 238 | if (isRotatingAllowed(activeRotationTarget)) {
|
| 239 | val fromRotation = currentRotation ?: playerRotation
|
| 240 | val rotation = activeRotationTarget.towards(fromRotation, rotationTarget == null)
|
| 241 | // After generating the next rotation, we need to normalize it
|
| 242 | .normalize()
|
| 243 |
|
| 244 | val diff = rotation.rotationDeltaLengthTo(playerRotation)
|
| 245 |
|
| 246 | if (rotationTarget == null && (activeRotationTarget.movementCorrection == MovementCorrection.CHANGE_LOOK
|
| 247 | || activeRotationTarget.processors.isEmpty()
|
| 248 | || diff <= activeRotationTarget.resetThreshold)) {
|
| 249 | currentRotation?.let { currentRotation ->
|
| 250 | player.yRot = player.withFixedYaw(currentRotation)
|
| 251 | player.yBob = player.yRot
|
| 252 | player.yBobO = player.yRot
|
| 253 | }
|
| 254 |
|
| 255 | currentRotation = null
|
| 256 | previousRotationTarget = null
|
| 257 | } else {
|
| 258 | currentRotation = rotation
|
| 259 | previousRotationTarget = activeRotationTarget
|
| 260 |
|
| 261 | rotationTarget?.whenReached?.invoke()
|
| 262 | }
|
| 263 | }
|
| 264 |
|
| 265 | // Update reset ticks
|
| 266 | rotationTargetHandler.tick()
|
| 267 | }
|
| 268 |
|
| 269 | fun applyChangeLookRotation(partialTicks: Float) {
|
| 270 | val activeRotationTarget = this.activeRotationTarget ?: return
|
| 271 |
|
| 272 | if (isRotatingAllowed(activeRotationTarget)
|
| 273 | && activeRotationTarget.movementCorrection == MovementCorrection.CHANGE_LOOK) {
|
| 274 | val playerRotation = playerRotation ?: return
|
| 275 | val currentRotation = currentRotation ?: return
|
| 276 |
|
| 277 | val interpolated = playerRotation.interpolateTo(currentRotation, partialTicks)
|
| 278 | player.setRotation(interpolated)
|
| 279 | }
|
| 280 | }
|
| 281 |
|
| 282 | @Suppress("unused")
|
| 283 | private val mouseMovement = handler<MouseRotationEvent> { event ->
|
| 284 | val activeRotationTarget = this.activeRotationTarget ?: return@handler
|
| 285 | if (!isRotatingAllowed(activeRotationTarget) ||
|
| 286 | activeRotationTarget.movementCorrection != MovementCorrection.CHANGE_LOOK) {
|
| 287 | return@handler
|
| 288 | }
|
| 289 |
|
| 290 | fun adjustRotation(rotation: Rotation): Rotation =
|
| 291 | RotationUtil.applyMouseTurnDelta(rotation, event.cursorDeltaX, event.cursorDeltaY)
|
| 292 |
|
| 293 | playerRotation?.let { rotation ->
|
| 294 | playerRotation = adjustRotation(rotation)
|
| 295 | }
|
| 296 |
|
| 297 | currentRotation?.let { rotation ->
|
| 298 | currentRotation = adjustRotation(rotation)
|
| 299 | }
|
| 300 | }
|
| 301 |
|
| 302 | /**
|
| 303 | * Checks if it should update the server-side rotations
|
| 304 | */
|
| 305 | private fun allowedToUpdate() = !CombatManager.shouldPauseRotation
|
| 306 |
|
| 307 | fun rotationMatchesPreviousRotation(): Boolean {
|
| 308 | val player = mc.player ?: return false
|
| 309 |
|
| 310 | currentRotation?.let {
|
| 311 | return it == previousRotation
|
| 312 | }
|
| 313 |
|
| 314 | return player.rotation == player.lastRotation
|
| 315 | }
|
| 316 |
|
| 317 | @Suppress("unused")
|
| 318 | private val velocityHandler = handler<PlayerVelocityStrafe>(priority = MODEL_STATE) { event ->
|
| 319 | if (activeRotationTarget?.movementCorrection != MovementCorrection.OFF) {
|
| 320 | val rotation = currentRotation ?: return@handler
|
| 321 |
|
| 322 | event.velocity = Entity.getInputVector(
|
| 323 | event.movementInput,
|
| 324 | event.speed,
|
| 325 | rotation.yaw
|
| 326 | )
|
| 327 | }
|
| 328 | }
|
| 329 |
|
| 330 | @Suppress("unused")
|
| 331 | private val gameTickHandler = handler<GameTickEvent>(
|
| 332 | priority = FIRST_PRIORITY
|
| 333 | ) { event ->
|
| 334 | EventManager.callEvent(RotationUpdateEvent)
|
| 335 | update()
|
| 336 | }
|
| 337 |
|
| 338 | /**
|
| 339 | * Track rotation changes
|
| 340 | *
|
| 341 | * We cannot only rely on player.lastYaw and player.lastPitch because
|
| 342 | * sometimes we update the rotation off chain (e.g. on interactItem)
|
| 343 | * and the player.lastYaw and player.lastPitch are not updated.
|
| 344 | */
|
| 345 | @Suppress("unused")
|
| 346 | val packetHandler = handler<PacketEvent>(
|
| 347 | priority = EventPriorityConvention.READ_FINAL_STATE
|
| 348 | ) { event ->
|
| 349 | val rotation = when (val packet = event.packet) {
|
| 350 | is ServerboundMovePlayerPacket -> {
|
| 351 | // If we are not changing the look, we don't need to update the rotation
|
| 352 | if (!packet.hasRot) {
|
| 353 | return@handler
|
| 354 | }
|
| 355 |
|
| 356 | serverboundRotation(packet.yRot, packet.xRot)
|
| 357 | }
|
| 358 | is ClientboundPlayerPositionPacket -> clientboundRotation(
|
| 359 | packet.change.yRot,
|
| 360 | packet.change.xRot,
|
| 361 | Relative.Y_ROT in packet.relatives,
|
| 362 | Relative.X_ROT in packet.relatives,
|
| 363 | )
|
| 364 | is ClientboundPlayerRotationPacket -> clientboundRotation(
|
| 365 | packet.yRot,
|
| 366 | packet.xRot,
|
| 367 | packet.relativeY,
|
| 368 | packet.relativeX,
|
| 369 | )
|
| 370 | is ServerboundUseItemPacket -> serverboundRotation(packet.yRot, packet.xRot)
|
| 371 | else -> return@handler
|
| 372 | }
|
| 373 |
|
| 374 | // Incoming corrections already changed the server-side player before they were sent. For
|
| 375 | // outgoing packets, only packets that pass the event pipeline can affect the server.
|
| 376 | if (event.origin == TransferOrigin.INCOMING || !event.isCancelled) {
|
| 377 | actualServerRotation = rotation
|
| 378 | }
|
| 379 | theoreticalServerRotation = rotation
|
| 380 | }
|
| 381 |
|
| 382 | override val running: Boolean
|
| 383 | get() = inGame
|
| 384 |
|
| 385 | }
|
| 386 |
|
| 387 | internal fun resolveMovementYaw(
|
| 388 | playerYaw: Float,
|
| 389 | managedYaw: Float,
|
| 390 | movementCorrection: MovementCorrection?,
|
| 391 | ): Float = if (managedYaw.isFinite() && movementCorrection != null && movementCorrection != MovementCorrection.OFF) {
|
| 392 | managedYaw
|
| 393 | } else {
|
| 394 | playerYaw
|
| 395 | }
|
| 396 |
|