Skip to content

Commit

Permalink
Use Minecraft's smoothing, keep camera movement when in screens
Browse files Browse the repository at this point in the history
  • Loading branch information
landon-wills committed Oct 10, 2024
1 parent 9fc21d4 commit edabc16
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions src/client/java/ca/landonjw/mixin/client/MouseHandlerMixin.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
package ca.landonjw.mixin.client;

import ca.landonjw.Rollable;
import ca.landonjw.math.Smoother;
import com.llamalad7.mixinextras.injector.v2.WrapWithCondition;
import com.llamalad7.mixinextras.sugar.Local;
import net.minecraft.client.Minecraft;
import net.minecraft.client.MouseHandler;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.util.SmoothDouble;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(MouseHandler.class)
public class MouseHandlerMixin {

@Shadow @Final private Minecraft minecraft;

@Unique
Smoother pitchSmoother = new Smoother(30);
SmoothDouble pitchSmoother = new SmoothDouble();
@Unique
Smoother rollSmoother = new Smoother(30);
SmoothDouble rollSmoother = new SmoothDouble();

@WrapWithCondition(
method = "turnPlayer",
Expand All @@ -29,17 +32,32 @@ public class MouseHandlerMixin {
target = "Lnet/minecraft/client/player/LocalPlayer;turn(DD)V"
)
)
public boolean open_camera$modifyRotation(LocalPlayer player, double cursorDeltaX, double cursorDeltaY) {
public boolean open_camera$modifyRotation(LocalPlayer player, double cursorDeltaX, double cursorDeltaY, @Local(argsOnly = true) double d) {
if (!(player instanceof Rollable rollable)) return true;

rollable.updateOrientation(() -> {
var pitch = pitchSmoother.smooth((float) (cursorDeltaY * 0.15f), 1);
var roll = rollSmoother.smooth((float) (cursorDeltaX * 0.15f), 1);
var pitch = pitchSmoother.getNewDeltaValue(cursorDeltaY * 0.15f, d);
var roll = rollSmoother.getNewDeltaValue(cursorDeltaX * 0.15f, d);

// This has side effects so either remove return or make a duplicate
return rollable.rotate(0.0F, pitch, roll).getOrientation();
return rollable.rotate(0.0F, (float)pitch, (float)roll).getOrientation();
});
return false;
}

@Inject(method = "handleAccumulatedMovement", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/MouseHandler;isMouseGrabbed()Z", ordinal = 0))
private void open_camera$maintainMovementWhenInScreens(CallbackInfo ci, @Local(ordinal = 1) double e) {
if (minecraft.player == null) return;
if (!(minecraft.player instanceof Rollable rollable)) return;
if (minecraft.isPaused()) return;

rollable.updateOrientation(() -> {
var pitch = pitchSmoother.getNewDeltaValue(0, e);
var roll = rollSmoother.getNewDeltaValue(0, e);

// This has side effects so either remove return or make a duplicate
return rollable.rotate(0.0F, (float)pitch, (float)roll).getOrientation();
});
}

}

0 comments on commit edabc16

Please sign in to comment.