Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.lambda.event.events.MovementEvent;
import com.lambda.interaction.PlayerPacketManager;
import com.lambda.interaction.RotationManager;
import net.minecraft.client.input.Input;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.entity.MovementType;
import net.minecraft.util.math.Vec3d;
Expand All @@ -26,6 +27,8 @@ public abstract class ClientPlayerEntityMixin extends EntityMixin {

@Shadow private boolean autoJumpEnabled;

@Shadow public Input input;

@Inject(method = "move", at = @At("HEAD"), cancellable = true)
void onMove(MovementType movementType, Vec3d movement, CallbackInfo ci) {
ClientPlayerEntity self = (ClientPlayerEntity) (Object) this;
Expand All @@ -49,12 +52,14 @@ void onMove(MovementType movementType, Vec3d movement, CallbackInfo ci) {
@Redirect(method = "tickMovement", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/ClientPlayerEntity;isUsingItem()Z"))
boolean onSlowDown(ClientPlayerEntity entity) {
if (EventFlow.post(new MovementEvent.SlowDown()).isCanceled()) return false;
return this.isUsingItem();
return isUsingItem();
}

@Inject(method = "tickMovement", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/input/Input;tick(ZF)V", shift = At.Shift.AFTER))
void processMovement(CallbackInfo ci) {
RotationManager.BaritoneProcessor.processPlayerMovement();
@Redirect(method = "tickMovement", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/input/Input;tick(ZF)V"))
void processMovement(Input input, boolean slowDown, float slowDownFactor) {
if (EventFlow.post(new MovementEvent.InputUpdate(input, slowDown, slowDownFactor)).isCanceled()) return;

input.tick(slowDown, slowDownFactor);
}

@Inject(method = "sendMovementPackets", at = @At(value = "HEAD"), cancellable = true)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.lambda.mixin.input;

import com.lambda.event.EventFlow;
import com.lambda.event.events.InputEvent;
import net.minecraft.client.input.KeyboardInput;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(KeyboardInput.class)
public class KeyboardInputMixin {
@Inject(method = "tick", at = @At("TAIL"))
private void onTick(boolean slowDown, float slowDownFactor, CallbackInfo ci) {
EventFlow.post(new InputEvent());
}
}
21 changes: 21 additions & 0 deletions common/src/main/java/com/lambda/mixin/input/MouseMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.lambda.mixin.input;

import com.lambda.module.modules.player.Freecam;
import net.minecraft.client.Mouse;
import net.minecraft.client.network.ClientPlayerEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

@Mixin(Mouse.class)
public class MouseMixin {
@Redirect(method = "updateMouse", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/ClientPlayerEntity;changeLookDirection(DD)V"))
private void updateMouseChangeLookDirection(ClientPlayerEntity player, double cursorDeltaX, double cursorDeltaY) {
if (Freecam.INSTANCE.isEnabled()) {
Freecam.updateRotation(cursorDeltaX, cursorDeltaY);
return;
}

player.changeLookDirection(cursorDeltaX, cursorDeltaY);
}
}
27 changes: 27 additions & 0 deletions common/src/main/java/com/lambda/mixin/render/CameraMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.lambda.mixin.render;

import com.lambda.module.modules.player.Freecam;
import net.minecraft.entity.Entity;
import net.minecraft.world.BlockView;
import org.spongepowered.asm.mixin.Mixin;
import net.minecraft.client.render.Camera;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(Camera.class)
public class CameraMixin {
@Inject(method = "update", at = @At("TAIL"))
private void onUpdate(
BlockView area,
Entity focusedEntity,
boolean thirdPerson,
boolean inverseView,
float tickDelta,
CallbackInfo ci
) {
if (!Freecam.INSTANCE.isEnabled()) return;

Freecam.updateCam();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.lambda.mixin.render;

import com.lambda.module.modules.player.Freecam;
import net.minecraft.client.render.GameRenderer;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(GameRenderer.class)
public class GameRendererMixin {
@Inject(method = "updateTargetedEntity", at = @At("HEAD"), cancellable = true)
private void updateTargetedEntityInvoke(float tickDelta, CallbackInfo info) {
if (!Freecam.INSTANCE.isEnabled()) return;

info.cancel();
Freecam.updateTarget();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.lambda.mixin.render;

import com.lambda.module.modules.player.Freecam;
import net.minecraft.client.render.Camera;
import net.minecraft.client.render.WorldRenderer;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import org.spongepowered.asm.mixin.injection.Redirect;

@Mixin(WorldRenderer.class)
public class WorldRendererMixin {
@Redirect(method = "render", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/render/Camera;isThirdPerson()Z"))
private boolean renderIsThirdPerson(Camera camera) {
return Freecam.INSTANCE.isEnabled() || camera.isThirdPerson();
}

@ModifyArg(method = "render", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/render/WorldRenderer;setupTerrain(Lnet/minecraft/client/render/Camera;Lnet/minecraft/client/render/Frustum;ZZ)V"), index = 3)
private boolean renderSetupTerrainModifyArg(boolean spectator) {
return Freecam.INSTANCE.isEnabled() || spectator;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class RotationSettings(
c: Configurable,
vis: () -> Boolean = { true }
) : IRotationConfig {
override val rotationMode by c.setting("Mode", RotationMode.LOCK, "SILENT - server-side rotation, SYNC - server-side rotation; client-side movement, LOCK - Lock camera", vis)
override var rotationMode by c.setting("Mode", RotationMode.SYNC, "SILENT - server-side rotation, SYNC - server-side rotation; client-side movement, LOCK - Lock camera", vis)
override val keepTicks by c.setting("Keep Rotation", 3, 1..10, 1, "Ticks to keep rotation", vis)
override val resetTicks by c.setting("Reset Rotation", 3, 1..10, 1, "Ticks before rotation is reset", vis)

Expand Down
5 changes: 5 additions & 0 deletions common/src/main/kotlin/com/lambda/event/events/InputEvent.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.lambda.event.events

import com.lambda.event.Event

class InputEvent : Event
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ package com.lambda.event.events
import com.lambda.event.Event
import com.lambda.event.cancellable.Cancellable
import com.lambda.event.cancellable.ICancellable
import net.minecraft.client.input.Input

abstract class MovementEvent : Event {
class Pre : MovementEvent()
class Post : MovementEvent()
class InputUpdate(
val input: Input,
val slowDown: Boolean,
val slowDownFactor: Float
) : MovementEvent(), ICancellable by Cancellable()
class ClipAtLedge : MovementEvent(), ICancellable by Cancellable()
class Jump(var height: Double) : MovementEvent(), ICancellable by Cancellable()
class SlowDown : Event, ICancellable by Cancellable()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ abstract class RotationEvent : Event {
init {
// Always check if baritone wants to rotate as well
RotationManager.BaritoneProcessor.baritoneContext?.let { context ->
requests.add(RotationRequest(-1, context.config, context.rotation))
requests.add(RotationRequest(context.config, context.rotation, -1))
}
}

Expand Down
28 changes: 13 additions & 15 deletions common/src/main/kotlin/com/lambda/interaction/RotationManager.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package com.lambda.interaction

import com.lambda.Lambda.LOG
import com.lambda.Lambda.mc
import com.lambda.Loadable
import com.lambda.config.RotationSettings
import com.lambda.event.EventFlow
import com.lambda.event.events.ConnectionEvent
import com.lambda.event.events.PacketEvent
import com.lambda.event.events.RotationEvent
import com.lambda.event.events.TickEvent
import com.lambda.event.events.*
import com.lambda.event.listener.SafeListener.Companion.listener
import com.lambda.event.listener.UnsafeListener.Companion.unsafeListener
import com.lambda.interaction.rotation.*
Expand All @@ -18,7 +14,6 @@ import com.lambda.interaction.rotation.Rotation.Companion.interpolate
import com.lambda.module.modules.client.Baritone
import com.lambda.threading.runOnGameThread
import com.lambda.threading.runSafe
import com.lambda.util.Communication.info
import com.lambda.util.math.MathUtils.lerp
import com.lambda.util.math.MathUtils.toRadian
import com.lambda.util.math.Vec2d
Expand Down Expand Up @@ -50,15 +45,14 @@ object RotationManager : Loadable {

@JvmStatic
fun updateInterpolated() = runSafe {
// if (currentRequest == null) return@runSafe
// val interpolation = interpolate(prevRotation, currentRotation, mc.tickDelta.toDouble())
//
if (currentRequest == null) return@runSafe
if (currentRequest?.config?.rotationMode != RotationMode.LOCK) return@runSafe
val interpolation = prevRotation.interpolate(currentRotation, mc.tickDelta.toDouble())

// val rot = interpolation.fixSensitivity(prevRotation)
//
// if (currentRequest?.config?.rotationMode == RotationMode.LOCK) {
// player.yaw = rot.yaw.toFloat()
// player.pitch = rot.pitch.toFloat()
// }

player.yaw = interpolation.yaw.toFloat()
player.pitch = interpolation.pitch.toFloat()
}

init {
Expand Down Expand Up @@ -105,7 +99,7 @@ object RotationManager : Loadable {

val turnSpeed = context.config.turnSpeed * speedMultiplier

val interpolation = interpolate(prevRotation, rotationTo, turnSpeed)
val interpolation = prevRotation.interpolate(rotationTo, turnSpeed)

currentRotation = interpolation.fixSensitivity(prevRotation)

Expand Down Expand Up @@ -183,6 +177,10 @@ object RotationManager : Loadable {
listener<TickEvent.Post> {
baritoneContext = null
}

listener<MovementEvent.InputUpdate> {
processPlayerMovement()
}
}

@JvmStatic
Expand Down
21 changes: 12 additions & 9 deletions common/src/main/kotlin/com/lambda/interaction/rotation/Rotation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,24 @@ import kotlin.math.*
data class Rotation(val yaw: Double, val pitch: Double) {
constructor(yaw: Float, pitch: Float) : this(yaw.toDouble(), pitch.toDouble())

private val rotationVector: Vec3d get() {
val vector: Vec3d get() {
val yawRad = -yaw.toRadian()
val pitchRad = pitch.toRadian()

return Vec3d(sin(yawRad), -1.0, cos(yawRad))
.multiply(Vec3d(cos(pitchRad), sin(pitchRad), cos(pitchRad)))
}

fun withDelta(yaw: Double = 0.0, pitch: Double = 0.0) =
Rotation(this.yaw + yaw, (this.pitch + pitch).coerceIn(-90.0, 90.0))

fun rayCast(
reach: Double,
mask: RayCastMask = RayCastMask.BOTH,
eye: Vec3d? = null,
fluids: Boolean = false
) = runSafe {
rayCast(eye ?: player.eyePos, rotationVector, reach, mask, fluids)
rayCast(eye ?: player.eyePos, vector, reach, mask, fluids)
}

val Direction.yaw: Float
Expand All @@ -46,17 +49,17 @@ data class Rotation(val yaw: Double, val pitch: Double) {

private fun wrap(deg: Double) = MathHelper.wrapDegrees(deg)

fun interpolate(a: Rotation, b: Rotation, speed: Double): Rotation {
val yawDiff = wrap(b.yaw - a.yaw)
val pitchDiff = wrap(b.pitch - a.pitch)
fun Rotation.interpolate(other: Rotation, delta: Double): Rotation {
val yawDiff = wrap(other.yaw - yaw)
val pitchDiff = wrap(other.pitch - pitch)

val diff = hypot(yawDiff, pitchDiff)

val yawSpeed = abs(yawDiff / diff) * speed
val pitchSpeed = abs(pitchDiff / diff) * speed
val yawSpeed = abs(yawDiff / diff) * delta
val pitchSpeed = abs(pitchDiff / diff) * delta

val yaw = a.yaw + yawDiff.coerceIn(-yawSpeed, yawSpeed)
val pitch = a.pitch + pitchDiff.coerceIn(-pitchSpeed, pitchSpeed)
val yaw = yaw + yawDiff.coerceIn(-yawSpeed, yawSpeed)
val pitch = pitch + pitchDiff.coerceIn(-pitchSpeed, pitchSpeed)

return Rotation(yaw, pitch)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.lambda.interaction.rotation

data class RotationRequest(
val priority: Int = 1,
val config: IRotationConfig,
val rotation: Rotation,
var isPending: Boolean = true
val priority: Int = 0,
) : Comparable<RotationRequest> {
var isPending: Boolean = true

override fun compareTo(other: RotationRequest) =
priority.compareTo(other.priority)
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ object VisibilityChecker {

// Rotate to selected point
closestRotation?.let { rotation ->
return RotationRequest(priority, rotationConfig, rotation)
return RotationRequest(rotationConfig, rotation, priority)
}

return null
}

private fun stay(priority: Int = 0, config: IRotationConfig) =
RotationRequest(priority, config, RotationManager.currentRotation)
RotationRequest(config, RotationManager.currentRotation, priority)

inline fun SafeContext.scanVisibleSurfaces(box: Box, sides: Set<Direction>, resolution: Int, check: (Vec3d) -> Unit) {
val shrunk = box.expand(-0.005)
Expand Down
2 changes: 1 addition & 1 deletion common/src/main/kotlin/com/lambda/module/Module.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import com.lambda.util.Nameable
* If a module does not need to be activated by a key (like [ClickGUI]),
* the default [keybind] should not be set (using [KeyCode.Unbound]).
*
* [Module]s are [Configurable] with [settings] (see [AbstractSetting] for all setting types).
* [Module]s are [Configurable]s with [settings] (see [AbstractSetting] for all setting types).
* For example, a [BooleanSetting] and a [DoubleSetting] can be defined like this:
* ```kotlin
* private val foo by setting("Foo", true)
Expand Down
Loading