Skip to content

Commit 49009c9

Browse files
committed
Freecam fixes:
Wrong sensitivity; HitResult "NPE"; Rotation may override other modules
1 parent edf227f commit 49009c9

File tree

2 files changed

+42
-34
lines changed

2 files changed

+42
-34
lines changed

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

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@ import com.lambda.event.events.RenderEvent
99
import com.lambda.event.events.RotationEvent
1010
import com.lambda.event.listener.SafeListener.Companion.listener
1111
import com.lambda.interaction.rotation.Rotation
12-
import com.lambda.interaction.rotation.Rotation.Companion.slerp
1312
import com.lambda.interaction.rotation.Rotation.Companion.rotationTo
1413
import com.lambda.interaction.rotation.RotationContext
1514
import com.lambda.interaction.rotation.RotationMode
1615
import com.lambda.module.Module
1716
import com.lambda.module.tag.ModuleTag
1817
import com.lambda.util.KeyCode
1918
import com.lambda.util.player.MovementUtils.cancel
19+
import com.lambda.util.player.MovementUtils.verticalMovement
2020
import com.lambda.util.primitives.extension.interpolate
21+
import com.lambda.util.primitives.extension.partialTicks
2122
import com.lambda.util.primitives.extension.rotation
22-
import com.lambda.util.world.raycast.RayCastMask
23-
import com.lambda.util.world.raycast.RayCastUtils.rayCast
23+
import com.lambda.util.world.raycast.RayCastUtils.orMiss
24+
import com.lambda.util.world.raycast.RayCastUtils.orNull
2425
import net.minecraft.client.input.KeyboardInput
2526
import net.minecraft.entity.Entity
2627
import net.minecraft.util.math.Vec3d
27-
import kotlin.math.pow
2828

2929
object Freecam : Module(
3030
name = "Freecam",
@@ -35,46 +35,46 @@ object Freecam : Module(
3535
private val speed by setting("Speed", 0.5f, 0.1f..1.0f, 0.1f)
3636
private val sprint by setting("Sprint Multiplier", 3.0f, 0.1f..10.0f, 0.1f, description = "Set below 1.0 to fly slower on sprint.")
3737
private val rotateToTarget by setting("Rotate to target", true)
38+
private val reach by setting("Reach", 4.0, 1.0..100.0, 0.1)
3839

3940
private val rotationConfig = RotationSettings(this).apply {
4041
rotationMode = RotationMode.LOCK
4142
}
4243

4344
private var prevPosition: Vec3d = Vec3d.ZERO
44-
@JvmStatic var position: Vec3d = Vec3d.ZERO
45-
private val interpolatedPosition: Vec3d
46-
get() = prevPosition.interpolate(position, mc.tickDelta.toDouble())
47-
48-
private var previousRotation: Rotation = Rotation.ZERO
49-
@JvmStatic var rotation: Rotation = Rotation.ZERO
50-
private val interpolatedRotation: Rotation
51-
get() = previousRotation.slerp(rotation, mc.tickDelta.toDouble())
45+
private var position: Vec3d = Vec3d.ZERO
46+
private val interpolatedPosition: Vec3d get() =
47+
prevPosition.interpolate(position, mc.partialTicks)
5248

49+
private var rotation: Rotation = Rotation.ZERO
5350
private var velocity: Vec3d = Vec3d.ZERO
5451

5552
@JvmStatic fun updateCam() {
56-
mc.gameRenderer.camera.apply {
57-
setRotation(interpolatedRotation.yaw.toFloat(), interpolatedRotation.pitch.toFloat())
58-
setPos(interpolatedPosition.x, interpolatedPosition.y, interpolatedPosition.z)
53+
mc.gameRenderer.apply {
54+
camera.setRotation(rotation.yawF, rotation.pitchF)
55+
camera.setPos(interpolatedPosition.x, interpolatedPosition.y, interpolatedPosition.z)
5956
}
6057
}
6158

6259
@JvmStatic fun updateRotation(deltaYaw: Double, deltaPitch: Double) {
63-
previousRotation = rotation
64-
val factor = (mc.options.mouseSensitivity.value * 0.6 + 0.2).pow(3)
65-
rotation = rotation.withDelta(deltaYaw * factor, deltaPitch * factor)
60+
rotation = rotation.withDelta(deltaYaw * SENSITIVITY_FACTOR, deltaPitch * SENSITIVITY_FACTOR)
6661
}
6762

63+
/**
64+
* @see net.minecraft.entity.Entity.changeLookDirection
65+
*/
66+
private const val SENSITIVITY_FACTOR = 0.15
67+
6868
init {
6969
onEnable {
7070
position = player.eyePos
7171
rotation = player.rotation
7272
velocity = Vec3d.ZERO
7373
}
7474

75-
listener<RotationEvent.Pre> {
75+
listener<RotationEvent.Pre>(Int.MAX_VALUE) {
7676
if (!rotateToTarget) return@listener
77-
val target = mc.crosshairTarget ?: return@listener
77+
val target = mc.crosshairTarget?.orNull ?: return@listener
7878

7979
val rotation = player.eyePos.rotationTo(target.pos)
8080
it.context = RotationContext(rotation, rotationConfig)
@@ -90,30 +90,31 @@ object Freecam : Module(
9090
}
9191

9292
// Create new input for freecam
93-
val input = KeyboardInput(mc.options)
94-
input.tick(event.slowDown, event.slowDownFactor)
95-
var y = 0.0
96-
if (input.jumping) y++
97-
if (input.sneaking) y--
98-
val inputVec = Vec3d(input.movementSideways.toDouble(), y, input.movementForward.toDouble())
93+
val input = KeyboardInput(mc.options).apply {
94+
tick(false, 1f)
95+
}
96+
97+
val inputVec = Vec3d(
98+
input.movementSideways.toDouble(),
99+
verticalMovement.toDouble(),
100+
input.movementForward.toDouble()
101+
)
102+
99103
val endSpeed = speed * if (mc.options.sprintKey.isPressed) sprint else 1.0f
100104
val velocityDelta = Entity.movementInputToVelocity(inputVec, endSpeed, rotation.yawF)
101105

102-
// move freecam
106+
// Move freecam
103107
velocity = velocity.add(velocityDelta).multiply(0.6)
104108
prevPosition = position
105109
position = position.add(velocity)
106110
}
107111

108112
listener<RenderEvent.UpdateTarget> {
109113
it.cancel()
110-
mc.crosshairTarget = rayCast(
111-
interpolatedPosition,
112-
interpolatedRotation.vector,
113-
interaction.reachDistance.toDouble(),
114-
RayCastMask.BOTH,
115-
true
116-
)
114+
115+
mc.crosshairTarget = rotation
116+
.rayCast(reach, eye = interpolatedPosition)
117+
.orMiss // Can't be null (otherwise mc will spam "Null returned as 'hitResult', this shouldn't happen!")
117118
}
118119

119120
listener<ConnectionEvent.Disconnect> {

common/src/main/kotlin/com/lambda/util/world/raycast/RayCastUtils.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.lambda.util.world.raycast
22

3+
import com.lambda.Lambda.mc
34
import com.lambda.context.SafeContext
45
import com.lambda.interaction.rotation.Rotation
56
import com.lambda.threading.runSafe
@@ -66,4 +67,10 @@ object RayCastUtils {
6667
if (type == HitResult.Type.MISS) return null
6768
return this as? BlockHitResult
6869
}
70+
71+
val HitResult.orNull get() = entityResult ?: blockResult
72+
73+
val HitResult?.orMiss get() = this ?: object : HitResult(mc.player?.eyePos ?: Vec3d.ZERO) {
74+
override fun getType() = Type.MISS
75+
}
6976
}

0 commit comments

Comments
 (0)