diff --git a/src/main/java/com/lambda/mixin/entity/ClientPlayerEntityMixin.java b/src/main/java/com/lambda/mixin/entity/ClientPlayerEntityMixin.java index 136c9d2d4..f0edaaba9 100644 --- a/src/main/java/com/lambda/mixin/entity/ClientPlayerEntityMixin.java +++ b/src/main/java/com/lambda/mixin/entity/ClientPlayerEntityMixin.java @@ -27,6 +27,7 @@ import com.lambda.module.modules.render.ViewModel; import com.llamalad7.mixinextras.injector.ModifyExpressionValue; import com.llamalad7.mixinextras.injector.ModifyReturnValue; +import com.llamalad7.mixinextras.injector.v2.WrapWithCondition; import com.llamalad7.mixinextras.injector.wrapmethod.WrapMethod; import com.llamalad7.mixinextras.injector.wrapoperation.Operation; import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; @@ -153,6 +154,12 @@ boolean modifyIsSprinting(boolean original) { return EventFlow.post(new MovementEvent.Sprint(original)).getSprint(); } + // Bounce glides without forward input, which would stop the sprint every tick + @WrapWithCondition(method = "tickMovement", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/ClientPlayerEntity;setSprinting(Z)V")) + private boolean wrapSetSprinting(ClientPlayerEntity instance, boolean sprinting) { + return sprinting || !ElytraFly.getBounceMode().keepsSprint(instance); + } + @ModifyReturnValue(method = "isSneaking", at = @At("RETURN")) boolean injectSneakingInput(boolean original) { ClientPlayerEntity self = (ClientPlayerEntity) (Object) this; diff --git a/src/main/java/com/lambda/mixin/entity/EntityMixin.java b/src/main/java/com/lambda/mixin/entity/EntityMixin.java index 99fb142f6..bdce83c27 100644 --- a/src/main/java/com/lambda/mixin/entity/EntityMixin.java +++ b/src/main/java/com/lambda/mixin/entity/EntityMixin.java @@ -175,17 +175,6 @@ private boolean wrapSetPitch(Entity instance, float yaw) { return RotationManager.getLockPitch() == null; } - @ModifyReturnValue(method = "isSprinting()Z", at = @At("RETURN")) - private boolean injectIsSprinting(boolean original) { - var player = getMc().player; - if ((Object) this != getMc().player) return original; - - if (ElytraFly.getBounceMode().isEnabled() && player.isGliding()) - return true; - - return original; - } - @ModifyReturnValue(method = "getPose", at = @At("RETURN")) private EntityPose injectGetPose(EntityPose original) { var player = getMc().player; diff --git a/src/main/kotlin/com/lambda/module/modules/movement/elytrafly/ElytraFlyMode.kt b/src/main/kotlin/com/lambda/module/modules/movement/elytrafly/ElytraFlyMode.kt index e178d4e71..c289e7e55 100644 --- a/src/main/kotlin/com/lambda/module/modules/movement/elytrafly/ElytraFlyMode.kt +++ b/src/main/kotlin/com/lambda/module/modules/movement/elytrafly/ElytraFlyMode.kt @@ -137,7 +137,7 @@ abstract class ElytraFlyMode( fakeGliding = true } - protected fun SafeContext.startFlyPacket() = + protected open fun SafeContext.startFlyPacket() = connection.sendPacket(ClientCommandC2SPacket(player, ClientCommandC2SPacket.Mode.START_FALL_FLYING)) open fun isGliding() = runSafe { player.getFlag(Entity.GLIDING_FLAG_INDEX) || (fakeFly && fakeGliding) } == true diff --git a/src/main/kotlin/com/lambda/module/modules/movement/elytrafly/ObstaclePassingMode.kt b/src/main/kotlin/com/lambda/module/modules/movement/elytrafly/ObstaclePassingMode.kt index 36f5cfe17..4ef52496e 100644 --- a/src/main/kotlin/com/lambda/module/modules/movement/elytrafly/ObstaclePassingMode.kt +++ b/src/main/kotlin/com/lambda/module/modules/movement/elytrafly/ObstaclePassingMode.kt @@ -60,9 +60,10 @@ abstract class ObstaclePassingMode( } onFlag { if (!passerConfig.passObstacles || !passerConfig.walkWhenFlagged) return@onFlag - val snappedDir = getSnappedDir() - val closestLinePoint = player.pos.findClosestPointOnLine(snappedDir) runGameScheduled { + if (!movedFromStart()) return@runGameScheduled + val snappedDir = getSnappedDir() + val closestLinePoint = player.pos.findClosestPointOnLine(snappedDir) val delta = snappedDir.multiply(passerConfig.obstacleLookAhead.toDouble()) val pathToPoint = closestLinePoint.add(delta) pathToValidPoint(pathToPoint, snappedDir) @@ -82,16 +83,16 @@ abstract class ObstaclePassingMode( if (!passerConfig.passObstacles) return false val playerPos = player.pos - val validDistanceFromStart = Vec3d(playerPos.x, startPos.y, playerPos.z) dist startPos > 0.1 - if (!validDistanceFromStart) return false + if (!movedFromStart()) return false val snappedDir = getSnappedDir() val closestLinePoint = playerPos.findClosestPointOnLine(snappedDir) passingToPos?.let { passingTo -> if (passingTo distSq startPos < playerPos distSq startPos) { - val atClosestPointBlockPos = closestLinePoint.blockPos == player.blockPos - if (!atClosestPointBlockPos) pathToValidPoint(closestLinePoint, snappedDir, false) + // Baritone stops beside a GoalGetToBlock's block, so count beside as there + val atClosestPoint = GoalGetToBlock(closestLinePoint.blockPos).isInGoal(player.blockPos) + if (!atClosestPoint) pathToValidPoint(closestLinePoint, snappedDir, false) else { BaritoneHandler.cancel() passingToPos = null @@ -135,6 +136,8 @@ abstract class ObstaclePassingMode( return false } + protected fun SafeContext.movedFromStart() = Vec3d(player.x, startPos.y, player.z) dist startPos > 0.1 + protected fun SafeContext.getSnappedDir(): Vec3d { val travelDiff = player.pos.subtract(startPos).normalize().let { Vec3d(it.x, 0.0, it.z) } return lockYawToStep(travelDiff) diff --git a/src/main/kotlin/com/lambda/module/modules/movement/elytrafly/modes/BounceElytraFly.kt b/src/main/kotlin/com/lambda/module/modules/movement/elytrafly/modes/BounceElytraFly.kt index 178bd19cb..107100c65 100644 --- a/src/main/kotlin/com/lambda/module/modules/movement/elytrafly/modes/BounceElytraFly.kt +++ b/src/main/kotlin/com/lambda/module/modules/movement/elytrafly/modes/BounceElytraFly.kt @@ -35,24 +35,25 @@ import com.lambda.module.modules.movement.elytrafly.ElytraFly.fakeFly import com.lambda.module.modules.movement.elytrafly.ObstaclePassingMode import com.lambda.module.modules.movement.elytrafly.PasserSettings import com.lambda.threading.runSafe -import com.lambda.util.PacketUtils.handlePacketSilently import com.lambda.util.PacketUtils.sendPacketSilently import com.lambda.util.SpeedUnit import com.lambda.util.TickTimer -import com.lambda.util.math.dist import com.lambda.util.math.distSq import com.lambda.util.math.minus import com.lambda.util.player.PlayerUtils.canStartGliding import com.lambda.util.player.PlayerUtils.canTakeoff import net.minecraft.client.input.KeyboardInput.getMovementMultiplier +import net.minecraft.client.network.ClientPlayerEntity import net.minecraft.entity.Entity import net.minecraft.network.packet.Packet -import net.minecraft.network.packet.s2c.common.CommonPingS2CPacket +import net.minecraft.network.packet.c2s.play.ClientCommandC2SPacket +import net.minecraft.network.packet.c2s.play.PlayerInputC2SPacket +import net.minecraft.network.packet.c2s.play.PlayerMoveC2SPacket +import net.minecraft.network.packet.c2s.play.TeleportConfirmC2SPacket import net.minecraft.util.PlayerInput import net.minecraft.util.math.Vec2f import net.minecraft.util.math.Vec3d import java.util.* -import java.util.concurrent.ConcurrentLinkedQueue import kotlin.math.abs import kotlin.math.cos import kotlin.math.pow @@ -75,6 +76,7 @@ class BounceElytraFly( private val minimizePackets by c.setting("Minimize Packets", true, "Shrinks the amount of start fly packets sent to the server as much as possible") private val fakeLag by c.setting("Fake Lag", true, "Emulates the player lagging to allow flying in 1x2 tunnels") .onValueChange { _, to -> if (!to) flushPackets() } + private val maxHold by c.setting("Max Hold", 10, 1..100, 1, "How long Fake Lag can hold packets", "ticks") { fakeLag } @Group(Y_MOTION_GROUP) val yMotionSetting by c.setting("Y Motion", false, "Cancels the players y velocity to aid speed") @Group(Y_MOTION_GROUP) val onlyOnDiagonal: Boolean by c.setting("Only On Diagonal", true, "Only use y motion when the player is flying on a non-axial angle") { yMotionSetting } @@ -100,14 +102,18 @@ class BounceElytraFly( private var wantJump = false private var prevGliding: Boolean? = null private val pauseTimer = TickTimer() - private val pingPackets = ConcurrentLinkedQueue() + private val holdTimer = TickTimer() + // Holds pongs too, so Grim gets them in order with the moves private val sendPacketQueue = LinkedList>() + private var answeringSetback = false private var sneakLeft = false private var sneakRight = false private var interrupting = false private val flightPaused - get() = !pauseTimer.hasSurpassed(flagPause) || interrupting || BaritoneHandler.isActive + get() = !pauseTimer.hasSurpassed(flagPause) || bouncingStopped + private val bouncingStopped + get() = interrupting || BaritoneHandler.isActive private val SafeContext.queuePackets get() = fakeLag && player.isGliding && (!yMotionSetting || !onYMotionAngle) && @@ -123,6 +129,8 @@ class BounceElytraFly( listen { pauseTimer.tick() wantJump = false + // A sprint doesn't start by itself while gliding + if (keepsSprint(player) && !player.isSprinting) player.isSprinting = true if (autoPitch) { rotationRequest { @@ -137,9 +145,8 @@ class BounceElytraFly( if (handlePassingObstacles()) return@listen if (yMotionSetting && strictYMotionRange && onYMotionAngle) run yMotionCorrection@ { + if (!movedFromStart()) return@yMotionCorrection val playerPos = player.pos - val validDistanceFromStart = Vec3d(playerPos.x, startPos.y, playerPos.z) dist startPos > 0.1 - if (!validDistanceFromStart) return@yMotionCorrection val snappedDir = getSnappedDir() val closestLinePoint = playerPos.findClosestPointOnLine(snappedDir) @@ -159,7 +166,12 @@ class BounceElytraFly( } } - if (flightPaused) return@listen + if (bouncingStopped) return@listen + if (flightPaused) { + // Staying on the ground after a landing ends the glide + if (player.isOnGround && (player.isGliding || (takeoff && player.canTakeoff))) wantJump = jump + return@listen + } if (!player.isGliding) { if (takeoff && player.canTakeoff) { @@ -190,6 +202,8 @@ class BounceElytraFly( listen({ -100 }) { interrupting = false + if (sendPacketQueue.isNotEmpty()) holdTimer.tick() + if (holdTimer.hasSurpassed(maxHold)) flushPackets() } listen { event -> @@ -216,24 +230,24 @@ class BounceElytraFly( return@listen } if (!wantJump) return@listen - if (flightPaused) return@listen + if (bouncingStopped) return@listen input.jump() } listen({ 1 }) { event -> - if (queuePackets) { - sendPacketQueue.add(event.packet) + // Keep-alives are answered from the network thread, and the queue is the game thread's + if (!mc.isOnThread) return@listen + val packet = event.packet + // The server takes no moves until it has a setback's confirm and the move after it + if (packet is TeleportConfirmC2SPacket) answeringSetback = true + if (queuePackets && !answeringSetback) { + sendPacketQueue.add(packet) event.cancel() return@listen } flushPackets() - } - listen({ 1 }) { event -> - if (event.packet is CommonPingS2CPacket && queuePackets) { - pingPackets.add(event.packet) - event.cancel() - } + if (packet is PlayerMoveC2SPacket) answeringSetback = false } onFlag { pauseTimer.reset() } @@ -241,6 +255,7 @@ class BounceElytraFly( onDisable { wantJump = false prevGliding = false + answeringSetback = false flushPackets() sneakLeft = false sneakRight = false @@ -248,16 +263,34 @@ class BounceElytraFly( } private fun SafeContext.flushPackets() { + holdTimer.reset() while (sendPacketQueue.isNotEmpty()) { val packet = sendPacketQueue.poll() connection.sendPacketSilently(packet) } - while (pingPackets.isNotEmpty()) { - val packet = pingPackets.poll() - connection.handlePacketSilently(packet) - } } + // Sent like pressing jump mid-air, which Grim checks for + override fun SafeContext.startFlyPacket() { + if (player.lastPlayerInput.jump) sendJumpKey(false) + connection.sendPacket(ClientCommandC2SPacket(player, ClientCommandC2SPacket.Mode.START_FALL_FLYING)) + sendJumpKey(true) + // Kept down into the next move, as a press would be + wantJump = true + // Now, as the flags the server echoes for a later sprint can end the next glide + if (keepsSprint(player) && !player.isSprinting) player.isSprinting = true + } + + private fun SafeContext.sendJumpKey(down: Boolean) { + val last = player.lastPlayerInput + val input = PlayerInput(last.forward, last.backward, last.left, last.right, down, last.sneak, last.sprint) + connection.sendPacket(PlayerInputC2SPacket(input)) + player.lastPlayerInput = input + } + + fun keepsSprint(player: ClientPlayerEntity) = + isEnabled && player.isGliding && player.canSprintOrFly() + fun getModifiedVelocity(original: Vec3d) = runSafe { if (!yMotion) return@runSafe original diff --git a/src/main/resources/lambda.accesswidener b/src/main/resources/lambda.accesswidener index c446edbb0..1acc244f0 100644 --- a/src/main/resources/lambda.accesswidener +++ b/src/main/resources/lambda.accesswidener @@ -49,6 +49,7 @@ transitive-accessible method net/minecraft/entity/LivingEntity applyArmorToDamag transitive-accessible method net/minecraft/entity/Entity isAlwaysInvulnerableTo (Lnet/minecraft/entity/damage/DamageSource;)Z transitive-accessible field net/minecraft/entity/player/PlayerInventory equipment Lnet/minecraft/entity/EntityEquipment; transitive-accessible method net/minecraft/client/network/ClientPlayerEntity applyMovementSpeedFactors (Lnet/minecraft/util/math/Vec2f;)Lnet/minecraft/util/math/Vec2f; +transitive-accessible field net/minecraft/client/network/ClientPlayerEntity lastPlayerInput Lnet/minecraft/util/PlayerInput; transitive-accessible field net/minecraft/entity/EntityEquipment map Ljava/util/EnumMap; transitive-accessible method net/minecraft/entity/LivingEntity getHandSwingDuration ()I transitive-accessible method net/minecraft/client/network/ClientPlayerInteractionManager syncSelectedSlot ()V @@ -63,6 +64,7 @@ transitive-accessible field net/minecraft/entity/Entity FLAGS Lnet/minecraft/ent transitive-accessible field net/minecraft/block/entity/BeaconBlockEntity level I transitive-accessible method net/minecraft/entity/LivingEntity canGlide ()Z transitive-accessible method net/minecraft/entity/player/PlayerEntity canGlide ()Z +transitive-accessible method net/minecraft/entity/player/PlayerEntity canSprintOrFly ()Z # Camera transitive-accessible method net/minecraft/client/render/Camera setPos (DDD)V