Skip to content

Commit 49e13c1

Browse files
committed
Hopefully improve compatibility with other mods that require rotation
1 parent b2060a2 commit 49e13c1

File tree

4 files changed

+46
-74
lines changed

4 files changed

+46
-74
lines changed

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

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import com.lambda.module.modules.player.PortalGui;
3030
import com.lambda.module.modules.render.ViewModel;
3131
import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
32-
import com.llamalad7.mixinextras.injector.v2.WrapWithCondition;
3332
import com.llamalad7.mixinextras.injector.wrapmethod.WrapMethod;
3433
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
3534
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
@@ -38,11 +37,14 @@
3837
import net.minecraft.client.gui.screen.Screen;
3938
import net.minecraft.client.input.Input;
4039
import net.minecraft.client.network.AbstractClientPlayerEntity;
40+
import net.minecraft.client.network.ClientPlayNetworkHandler;
4141
import net.minecraft.client.network.ClientPlayerEntity;
4242
import net.minecraft.client.world.ClientWorld;
4343
import net.minecraft.entity.MovementType;
44+
import net.minecraft.network.packet.Packet;
4445
import net.minecraft.util.Hand;
4546
import net.minecraft.util.math.Vec3d;
47+
import org.objectweb.asm.Opcodes;
4648
import org.spongepowered.asm.mixin.Final;
4749
import org.spongepowered.asm.mixin.Mixin;
4850
import org.spongepowered.asm.mixin.Shadow;
@@ -57,7 +59,6 @@
5759
public abstract class ClientPlayerEntityMixin extends AbstractClientPlayerEntity {
5860
@Shadow public Input input;
5961
@Shadow @Final protected MinecraftClient client;
60-
@Shadow private boolean autoJumpEnabled;
6162

6263
public ClientPlayerEntityMixin(ClientWorld world, GameProfile profile) {
6364
super(world, profile);
@@ -83,17 +84,37 @@ private void injectTickMovement(CallbackInfo ci) {
8384
if (NoJumpCooldown.INSTANCE.isEnabled() || (ElytraFly.INSTANCE.isEnabled() && ElytraFly.getMode() == ElytraFly.FlyMode.Bounce)) jumpingCooldown = 0;
8485
}
8586

86-
@Inject(method = "sendMovementPackets", at = @At("HEAD"))
87-
private void injectSendMovementPackets(CallbackInfo ci) {
88-
PlayerPacketHandler.sendPlayerPackets();
89-
autoJumpEnabled = Lambda.getMc().options.getAutoJump().getValue();
87+
@ModifyExpressionValue(method = "sendMovementPackets", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/ClientPlayerEntity;getYaw()F"))
88+
private float modifyGetYaw(float original) {
89+
final var rot = RotationManager.getHeadYaw();
90+
return rot != null ? rot : original;
91+
}
92+
93+
@ModifyExpressionValue(method = "sendMovementPackets", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/ClientPlayerEntity;getPitch()F"))
94+
private float modifyGetPitch(float original) {
95+
final var rot = RotationManager.getHeadPitch();
96+
return rot != null ? rot : original;
9097
}
91-
92-
@WrapWithCondition(method = "sendMovementPackets", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/ClientPlayerEntity;sendSprintingPacket()V"))
93-
private boolean wrapSendSprintingPackets(ClientPlayerEntity instance) { return false; }
9498

95-
@ModifyExpressionValue(method = "sendMovementPackets", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/ClientPlayerEntity;isCamera()Z"))
96-
private boolean wrapIsCamera(boolean original) { return false; }
99+
@ModifyExpressionValue(method = "sendMovementPackets", at = @At(value = "FIELD", target = "Lnet/minecraft/client/network/ClientPlayerEntity;lastYawClient:F", opcode = Opcodes.GETFIELD))
100+
private float modifyLastYawClient(float original) {
101+
return RotationManager.getServerRotation().getYawF();
102+
}
103+
104+
@ModifyExpressionValue(method = "sendMovementPackets", at = @At(value = "FIELD", target = "Lnet/minecraft/client/network/ClientPlayerEntity;lastPitchClient:F", opcode = Opcodes.GETFIELD))
105+
private float modifyLastPitchClient(float original) {
106+
return RotationManager.getServerRotation().getPitchF();
107+
}
108+
109+
@WrapOperation(method = "sendMovementPackets", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/ClientPlayNetworkHandler;sendPacket(Lnet/minecraft/network/packet/Packet;)V"))
110+
private void wrapSendPacket(ClientPlayNetworkHandler instance, Packet packet, Operation<Void> original) {
111+
PlayerPacketHandler.sendPlayerPackets(packet);
112+
}
113+
114+
@Inject(method = "sendMovementPackets", at = @At("TAIL"))
115+
private void injectSendMovementPackets(CallbackInfo ci) {
116+
RotationManager.onRotationSend();
117+
}
97118

98119
@ModifyExpressionValue(method = "tickMovement", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/ClientPlayerEntity;isSprinting()Z"))
99120
boolean isSprinting(boolean original) {

src/main/kotlin/com/lambda/event/events/PlayerPacketEvent.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ sealed class PlayerPacketEvent {
3333
var isCollidingHorizontally: Boolean,
3434
) : ICancellable by Cancellable()
3535

36-
class Post : Event
37-
3836
data class Send(
3937
val packet: PlayerMoveC2SPacket,
4038
) : ICancellable by Cancellable()
39+
40+
class Post : Event
4141
}

src/main/kotlin/com/lambda/interaction/PlayerPacketHandler.kt

Lines changed: 9 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,20 @@ import com.lambda.context.SafeContext
2121
import com.lambda.event.EventFlow.post
2222
import com.lambda.event.EventFlow.postChecked
2323
import com.lambda.event.events.PlayerPacketEvent
24-
import com.lambda.interaction.managers.rotating.Rotation
2524
import com.lambda.interaction.managers.rotating.RotationManager
2625
import com.lambda.threading.runSafe
2726
import com.lambda.util.collections.LimitedOrderedSet
28-
import com.lambda.util.math.approximate
29-
import net.minecraft.network.packet.c2s.play.ClientCommandC2SPacket
27+
import net.minecraft.network.packet.Packet
3028
import net.minecraft.network.packet.c2s.play.PlayerMoveC2SPacket.Full
3129
import net.minecraft.network.packet.c2s.play.PlayerMoveC2SPacket.LookAndOnGround
3230
import net.minecraft.network.packet.c2s.play.PlayerMoveC2SPacket.OnGroundOnly
3331
import net.minecraft.network.packet.c2s.play.PlayerMoveC2SPacket.PositionAndOnGround
34-
import net.minecraft.util.math.Vec3d
3532

3633
object PlayerPacketHandler {
3734
val configurations = LimitedOrderedSet<PlayerPacketEvent.Pre>(100)
3835

39-
var lastPosition: Vec3d = Vec3d.ZERO
40-
var lastRotation = Rotation.ZERO
41-
var lastSprint = false
42-
var lastOnGround = false
43-
var lastHorizontalCollision = false
44-
45-
private var sendTicks = 0
46-
4736
@JvmStatic
48-
fun sendPlayerPackets() {
37+
fun sendPlayerPackets(packet: Packet<*>) {
4938
runSafe {
5039
PlayerPacketEvent.Pre(
5140
player.pos,
@@ -54,68 +43,30 @@ object PlayerPacketHandler {
5443
player.isSprinting,
5544
player.horizontalCollision,
5645
).post {
57-
updatePlayerPackets(this)
46+
updatePlayerPackets(this, packet)
5847
}
5948
}
6049
}
6150

62-
private fun SafeContext.updatePlayerPackets(new: PlayerPacketEvent.Pre) {
51+
private fun SafeContext.updatePlayerPackets(new: PlayerPacketEvent.Pre, packet: Packet<*>) {
6352
configurations.add(new)
6453

65-
reportSprint(lastSprint, new.isSprinting)
66-
67-
if (mc.cameraEntity != player) return
68-
69-
val position = new.position
7054
val (yaw, pitch) = new.rotation
71-
val onGround = new.onGround
72-
val isCollidingHorizontally = new.isCollidingHorizontally
7355

74-
val updatePosition = position.approximate(lastPosition) || ++sendTicks >= 20
75-
val updateRotation = lastRotation.yaw != yaw || lastRotation.pitch != pitch
76-
77-
when {
78-
updatePosition && updateRotation -> Full(position, yaw.toFloat(), pitch.toFloat(), onGround, isCollidingHorizontally)
79-
updatePosition -> PositionAndOnGround(position, onGround, isCollidingHorizontally)
80-
updateRotation -> LookAndOnGround(yaw.toFloat(), pitch.toFloat(), onGround, isCollidingHorizontally)
81-
lastOnGround != onGround || lastHorizontalCollision != isCollidingHorizontally -> OnGroundOnly(onGround, isCollidingHorizontally)
56+
when (packet) {
57+
is Full -> Full(new.position, yaw.toFloat(), pitch.toFloat(), new.onGround, new.isCollidingHorizontally)
58+
is PositionAndOnGround -> PositionAndOnGround(new.position, new.onGround, new.isCollidingHorizontally)
59+
is LookAndOnGround -> LookAndOnGround(yaw.toFloat(), pitch.toFloat(), new.onGround, new.isCollidingHorizontally)
60+
is OnGroundOnly -> OnGroundOnly(new.onGround, new.isCollidingHorizontally)
8261
else -> null
8362
}?.let {
8463
PlayerPacketEvent.Send(it).postChecked {
8564
connection.sendPacket(this.packet)
86-
87-
if (updatePosition) {
88-
lastPosition = position
89-
sendTicks = 0
90-
}
91-
92-
if (updateRotation) {
93-
lastRotation = new.rotation
94-
}
95-
96-
lastOnGround = onGround
97-
lastHorizontalCollision = isCollidingHorizontally
9865
}
9966
}
10067

101-
// Update the server rotation in RotationManager
102-
RotationManager.onRotationSend()
103-
10468
PlayerPacketEvent.Post().post()
10569
}
106-
107-
fun SafeContext.reportSprint(previous: Boolean, new: Boolean) {
108-
if (previous == new) return
109-
110-
val state = if (new) {
111-
ClientCommandC2SPacket.Mode.START_SPRINTING
112-
} else {
113-
ClientCommandC2SPacket.Mode.STOP_SPRINTING
114-
}
115-
116-
connection.sendPacket(ClientCommandC2SPacket(player, state))
117-
lastSprint = new
118-
}
11970
}
12071

12172

src/main/kotlin/com/lambda/interaction/managers/rotating/RotationManager.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ object RotationManager : Manager<RotationRequest>(
6868
val requests = mutableListOf<IRotationRequest?>(null, null)
6969

7070
private var usingBaritoneRotation = false
71-
var activeRotation = Rotation.ZERO
72-
var serverRotation = Rotation.ZERO
71+
@JvmStatic var activeRotation = Rotation.ZERO
72+
@JvmStatic var serverRotation = Rotation.ZERO
7373
@JvmStatic var prevServerRotation = Rotation.ZERO
7474

7575
private var changedThisTick = false
@@ -293,7 +293,7 @@ object RotationManager : Manager<RotationRequest>(
293293
private fun multiplier(positive: Boolean, negative: Boolean) =
294294
((if (positive) 1 else 0) - (if (negative) 1 else 0)).toFloat()
295295

296-
fun onRotationSend() {
296+
@JvmStatic fun onRotationSend() {
297297
prevServerRotation = serverRotation
298298
serverRotation = activeRotation
299299

0 commit comments

Comments
 (0)