Skip to content

Commit c536583

Browse files
committed
Rotation System fixes:
force sensitivity-fix the rotation; head rotating(third person); movement redirection;
1 parent 1f6e108 commit c536583

File tree

8 files changed

+92
-40
lines changed

8 files changed

+92
-40
lines changed

common/src/main/java/com/lambda/mixin/entity/ClientPlayerEntityMixin.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,8 @@ boolean onSlowDown(ClientPlayerEntity entity) {
5757

5858
@Redirect(method = "tickMovement", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/input/Input;tick(ZF)V"))
5959
void processMovement(Input input, boolean slowDown, float slowDownFactor) {
60-
if (EventFlow.post(new MovementEvent.InputUpdate(input, slowDown, slowDownFactor)).isCanceled()) return;
61-
6260
input.tick(slowDown, slowDownFactor);
61+
EventFlow.post(new MovementEvent.InputUpdate(input, slowDown, slowDownFactor));
6362
}
6463

6564
@Inject(method = "sendMovementPackets", at = @At(value = "HEAD"), cancellable = true)

common/src/main/java/com/lambda/mixin/entity/PlayerEntityMixin.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
import com.lambda.event.EventFlow;
44
import com.lambda.event.events.MovementEvent;
5+
import com.lambda.interaction.RotationManager;
6+
import net.minecraft.client.MinecraftClient;
57
import net.minecraft.entity.player.PlayerEntity;
68
import org.spongepowered.asm.mixin.Mixin;
79
import org.spongepowered.asm.mixin.injection.At;
810
import org.spongepowered.asm.mixin.injection.Inject;
11+
import org.spongepowered.asm.mixin.injection.Redirect;
912
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
1013

1114
@Mixin(PlayerEntity.class)
@@ -15,4 +18,14 @@ private void injectSafeWalk(CallbackInfoReturnable<Boolean> cir) {
1518
MovementEvent.ClipAtLedge event = new MovementEvent.ClipAtLedge(((PlayerEntity) (Object) this).isSneaking());
1619
cir.setReturnValue(EventFlow.post(event).getClip());
1720
}
21+
22+
@Redirect(method = "tickNewAi", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/PlayerEntity;getYaw()F"))
23+
private float injectHeadYaw(PlayerEntity instance) {
24+
if ((Object) this != MinecraftClient.getInstance().player) {
25+
return instance.getYaw();
26+
}
27+
28+
Float yaw = RotationManager.getRenderYaw();
29+
return (yaw != null) ? yaw : instance.getYaw();
30+
}
1831
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.lambda.mixin.render;
2+
3+
import com.lambda.Lambda;
4+
import com.lambda.interaction.RotationManager;
5+
import net.minecraft.client.render.VertexConsumerProvider;
6+
import net.minecraft.client.render.entity.LivingEntityRenderer;
7+
import net.minecraft.client.util.math.MatrixStack;
8+
import net.minecraft.entity.LivingEntity;
9+
import net.minecraft.util.math.MathHelper;
10+
import org.spongepowered.asm.mixin.Mixin;
11+
import org.spongepowered.asm.mixin.Unique;
12+
import org.spongepowered.asm.mixin.injection.At;
13+
import org.spongepowered.asm.mixin.injection.Inject;
14+
import org.spongepowered.asm.mixin.injection.Redirect;
15+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
16+
17+
import java.util.Objects;
18+
19+
@Mixin(LivingEntityRenderer.class)
20+
public class LivingEntityRendererMixin<T extends LivingEntity> {
21+
@Unique
22+
private Float lambda$pitch = null;
23+
24+
@Inject(method = "render(Lnet/minecraft/entity/LivingEntity;FFLnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumerProvider;I)V", at = @At("HEAD"))
25+
private void injectRender(T livingEntity, float f, float g, MatrixStack matrixStack, VertexConsumerProvider vertexConsumerProvider, int i, CallbackInfo ci) {
26+
Float rotationPitch = RotationManager.getRenderPitch();
27+
28+
this.lambda$pitch = null;
29+
30+
if (livingEntity != Lambda.getMc().player || rotationPitch == null) {
31+
return;
32+
}
33+
34+
this.lambda$pitch = rotationPitch;
35+
}
36+
37+
@Redirect(method = "render(Lnet/minecraft/entity/LivingEntity;FFLnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumerProvider;I)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/util/math/MathHelper;lerp(FFF)F", ordinal = 0))
38+
private float injectRotationPitch(float g, float f, float s) {
39+
return Objects.requireNonNullElseGet(lambda$pitch, () -> MathHelper.lerp(g, f, s));
40+
}
41+
}

common/src/main/kotlin/com/lambda/event/events/MovementEvent.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ import net.minecraft.client.input.Input
88
abstract class MovementEvent : Event {
99
class Pre : MovementEvent()
1010
class Post : MovementEvent()
11+
1112
class InputUpdate(
1213
val input: Input,
1314
val slowDown: Boolean,
1415
val slowDownFactor: Float,
15-
) : MovementEvent(), ICancellable by Cancellable()
16+
) : MovementEvent()
1617

1718
class ClipAtLedge(
1819
var clip: Boolean,

common/src/main/kotlin/com/lambda/interaction/PlayerPacketManager.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import com.lambda.context.SafeContext
55
import com.lambda.event.EventFlow.post
66
import com.lambda.event.EventFlow.postChecked
77
import com.lambda.event.events.PlayerPacketEvent
8+
import com.lambda.interaction.rotation.Rotation.Companion.fixSensitivity
89
import com.lambda.threading.runSafe
910
import com.lambda.util.collections.LimitedOrderedSet
1011
import com.lambda.util.math.VecUtils.approximate
@@ -46,10 +47,12 @@ object PlayerPacketManager : Loadable {
4647

4748
val rotation = new.rotation
4849
val position = new.position
49-
RotationManager.currentRotation = rotation
5050
val (yaw, pitch) = rotation.float
5151
val onGround = new.onGround
5252

53+
// Fix sensitivity for absolutely any outgoing angle
54+
RotationManager.currentRotation = rotation.fixSensitivity(RotationManager.prevRotation)
55+
5356
if (player.hasVehicle()) {
5457
connection.sendPacket(
5558
Full(

common/src/main/kotlin/com/lambda/interaction/RotationManager.kt

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package com.lambda.interaction
22

3+
import baritone.utils.PlayerMovementInput
34
import com.lambda.Lambda.mc
45
import com.lambda.core.Loadable
56
import com.lambda.config.RotationSettings
7+
import com.lambda.context.SafeContext
68
import com.lambda.event.EventFlow.post
79
import com.lambda.event.events.*
810
import com.lambda.event.listener.SafeListener.Companion.listener
911
import com.lambda.event.listener.UnsafeListener.Companion.unsafeListener
1012
import com.lambda.interaction.rotation.Rotation
1113
import com.lambda.interaction.rotation.Rotation.Companion.angleDifference
12-
import com.lambda.interaction.rotation.Rotation.Companion.fixSensitivity
1314
import com.lambda.interaction.rotation.Rotation.Companion.slerp
1415
import com.lambda.interaction.rotation.Rotation.Companion.lerp
1516
import com.lambda.interaction.rotation.RotationContext
@@ -22,8 +23,6 @@ import com.lambda.util.math.MathUtils.toRadian
2223
import com.lambda.util.math.Vec2d
2324
import com.lambda.util.primitives.extension.partialTicks
2425
import com.lambda.util.primitives.extension.rotation
25-
import net.minecraft.client.input.KeyboardInput
26-
import net.minecraft.enchantment.EnchantmentHelper
2726
import net.minecraft.network.packet.s2c.play.PlayerPositionLookS2CPacket
2827
import net.minecraft.util.math.MathHelper
2928
import kotlin.math.roundToInt
@@ -88,25 +87,22 @@ object RotationManager : Loadable {
8887
keepTicks = request.config.keepTicks
8988
}
9089

91-
currentContext?.let { current ->
92-
val rotationTo = if (keepTicks >= 0) current.rotation else currentRotation
90+
currentRotation = currentContext?.let { context ->
91+
val rotationTo = if (keepTicks >= 0) context.rotation else player.rotation
9392

94-
var speedMultiplier = (current.config as? RotationSettings)?.speedMultiplier ?: 1.0
93+
var speedMultiplier = (context.config as? RotationSettings)?.speedMultiplier ?: 1.0
9594
if (keepTicks < 0) speedMultiplier = 1.0
9695

97-
val turnSpeed = current.config.turnSpeed * speedMultiplier
98-
99-
val interpolation = prevRotation.slerp(rotationTo, turnSpeed)
100-
101-
currentRotation = interpolation.fixSensitivity(prevRotation)
96+
val turnSpeed = context.config.turnSpeed * speedMultiplier
10297

103-
if (current.config.rotationMode == RotationMode.LOCK) {
104-
player.yaw = currentRotation.yaw.toFloat()
105-
player.pitch = currentRotation.pitch.toFloat()
106-
}
107-
} ?: run {
108-
currentRotation = player.rotation
109-
}
98+
prevRotation
99+
.slerp(rotationTo, turnSpeed)
100+
.apply {
101+
if (context.config.rotationMode != RotationMode.LOCK) return@apply
102+
player.yaw = this.yawF
103+
player.pitch = this.pitchF
104+
}
105+
} ?: player.rotation
110106
}
111107

112108
private fun reset(rotation: Rotation) {
@@ -171,7 +167,7 @@ object RotationManager : Loadable {
171167
}
172168

173169
listener<MovementEvent.InputUpdate> {
174-
processPlayerMovement()
170+
processPlayerMovement(it)
175171
}
176172
}
177173

@@ -180,29 +176,27 @@ object RotationManager : Loadable {
180176
baritoneContext = RotationContext(Rotation(yaw, pitch), Baritone.rotation)
181177
}
182178

183-
@JvmStatic
184-
fun processPlayerMovement() = runSafe {
185-
val config = currentContext?.config ?: return@runSafe
179+
private fun SafeContext.processPlayerMovement(event: MovementEvent.InputUpdate) {
180+
val config = currentContext?.config ?: return
186181

187-
val input = player.input
188-
val handledByBaritone = input !is KeyboardInput
182+
// No changes are needed, when we don't modify the yaw used to move the player
183+
if (config.rotationMode == RotationMode.SILENT) return
184+
185+
val input = event.input
186+
val handledByBaritone = input is PlayerMovementInput
189187

190188
// Sign it to remove previous speed modifier
191189
val signForward = sign(input.movementForward)
192190
val signStrafe = sign(input.movementSideways)
193191

194192
// No changes are needed when no inputs are pressed
195-
if (signForward == 0f && signStrafe == 0f) return@runSafe
193+
if (signForward == 0f && signStrafe == 0f) return
196194

197195
// Movement speed modifier
198-
val multiplier = if (!player.shouldSlowDown()) 1f else
199-
(0.3f + EnchantmentHelper.getSwiftSneakSpeedBoost(player)).coerceIn(0f, 1f)
200-
201-
// No changes are needed, when we don't modify the yaw used to move the player
202-
if (config.rotationMode == RotationMode.SILENT) return@runSafe
196+
val multiplier = if (event.slowDown) event.slowDownFactor else 1f
203197

204198
val modifyMovement = config.rotationMode == RotationMode.SYNC || handledByBaritone
205-
if (!modifyMovement) return@runSafe
199+
if (!modifyMovement) return
206200

207201
val playerYaw = player.yaw.toDouble()
208202
val baritoneYaw = if (handledByBaritone) baritoneContext?.rotation?.yaw else null
@@ -222,10 +216,12 @@ object RotationManager : Loadable {
222216
val newZ = signForward * cosDelta + signStrafe * sinDelta
223217

224218
// Apply new movement
225-
input.movementSideways = newX.roundToInt().toFloat() * multiplier
226-
input.movementForward = newZ.roundToInt().toFloat() * multiplier
219+
input.apply {
220+
movementSideways = newX.roundToInt().toFloat() * multiplier
221+
movementForward = newZ.roundToInt().toFloat() * multiplier
222+
}
227223

228-
baritoneYaw ?: return@runSafe
224+
baritoneYaw ?: return
229225

230226
// Makes baritone movement safe
231227
// when yaw difference is too big to compensate it by modifying keyboard input

common/src/main/kotlin/com/lambda/module/modules/player/Freecam.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,6 @@ object Freecam : Module(
8383
listener<MovementEvent.InputUpdate> { event ->
8484
// Don't block baritone from working
8585
if (player.input !is PlayerMovementInput) {
86-
event.cancel()
87-
8886
// Reset actual input
8987
player.input.cancel()
9088
}

common/src/main/resources/lambda.mixins.common.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"render.DebugHudMixin",
2222
"render.GameRendererMixin",
2323
"render.LightmapTextureManagerMixin",
24+
"render.LivingEntityRendererMixin",
2425
"render.RenderTickCounterMixin",
2526
"render.WorldRendererMixin"
2627
],

0 commit comments

Comments
 (0)