Skip to content

Commit 2e59410

Browse files
Improvement: Better AutoWalk (#318)
1 parent 705bf08 commit 2e59410

2 files changed

Lines changed: 67 additions & 10 deletions

File tree

src/main/kotlin/com/lambda/module/modules/movement/AutoWalk.kt

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,85 @@
1717

1818
package com.lambda.module.modules.movement
1919

20+
import com.lambda.config.Group
21+
import com.lambda.context.SafeContext
2022
import com.lambda.event.events.MovementEvent
23+
import com.lambda.event.events.PacketEvent
2124
import com.lambda.event.listener.SafeListener.Companion.listen
25+
import com.lambda.interaction.managers.breaking.BreakManager
26+
import com.lambda.interaction.managers.interacting.InteractManager
2227
import com.lambda.module.Module
2328
import com.lambda.module.tag.ModuleTag
24-
import com.lambda.util.player.MovementUtils.forward
25-
import com.lambda.util.player.MovementUtils.strafe
29+
import com.lambda.util.math.MathUtils.toDouble
30+
import com.lambda.util.player.MovementUtils.roundedForward
31+
import com.lambda.util.player.MovementUtils.roundedStrafing
32+
import com.lambda.util.player.MovementUtils.sneaking
33+
import com.lambda.util.player.MovementUtils.sprinting
2634
import com.lambda.util.player.MovementUtils.update
27-
import net.minecraft.util.math.Vec2f
35+
import net.minecraft.network.packet.c2s.play.PlayerInteractBlockC2SPacket
2836

2937
@Suppress("unused")
3038
object AutoWalk : Module(
3139
name = "AutoWalk",
32-
description = "Automatically makes your character walk forward",
40+
description = "Automatically walks in the configured direction when certain conditions are met",
3341
tag = ModuleTag.MOVEMENT,
3442
) {
35-
val limitSpeed by setting("Limit Speed", false)
36-
val speed by setting("Speed", 0.5, 0.1..1.0, 0.05) { limitSpeed }
43+
private const val MOVEMENT_GROUP = "Movement"
44+
private const val PAUSING_GROUP = "Pausing"
45+
46+
@Group(MOVEMENT_GROUP) private val walkForward by setting("Forward", true, "Automatically walks forward")
47+
@Group(MOVEMENT_GROUP) private val walkBackward by setting("Backward", false, "Automatically walks backward")
48+
@Group(MOVEMENT_GROUP) private val strafeLeft by setting("Strafe Left", false, "Automatically strafes left")
49+
@Group(MOVEMENT_GROUP) private val strafeRight by setting("Strafe Right", false, "Automatically strafes right")
50+
@Group(MOVEMENT_GROUP) private val sneak by setting("Sneak", false, "Automatically sneaks")
51+
@Group(MOVEMENT_GROUP) private val sprint by setting("Sprint", false, "Automatically sprints")
52+
53+
@Group(PAUSING_GROUP) private val pauseWhileMining by setting("Pause While Mining", false, "Pauses walking while breaking blocks or when breaks are queued")
54+
@Group(PAUSING_GROUP) private val pauseWhilePlacing by setting("Pause While Placing", false, "Pauses walking while placing blocks or when places are queued - only works with blocks placed by a lambda module")
55+
@Group(PAUSING_GROUP) private val pauseAfterPlacing by setting("Pause After Placing", false, "Pauses walking for a period after placing a block - Works with all placing")
56+
@Group(PAUSING_GROUP) private val ticksToWaitAfterPlacing by setting("Ticks To Wait After Placing", 1, 1..20, 1, "Extends the pause after placing to this many ticks") { pauseAfterPlacing }
57+
58+
private var placeWaitTicks = 0
59+
60+
private val SafeContext.breakQueued
61+
get() = interaction.isBreakingBlock ||
62+
BreakManager.activeThisTick ||
63+
BreakManager.queuedRequest != null ||
64+
BreakManager.blockedPositions.isNotEmpty()
65+
66+
private val placeQueued
67+
get() = InteractManager.activeThisTick ||
68+
InteractManager.queuedRequest != null ||
69+
InteractManager.blockedPositions.isNotEmpty()
3770

3871
init {
72+
73+
listen<PacketEvent.Send.Pre> { event ->
74+
if (event.packet is PlayerInteractBlockC2SPacket && pauseAfterPlacing)
75+
placeWaitTicks = ticksToWaitAfterPlacing
76+
}
77+
3978
listen<MovementEvent.InputUpdate> { event ->
40-
event.input.update(forward = 1.0)
41-
if (limitSpeed) event.input.movementVector = Vec2f(event.input.strafe, event.input.forward).normalize().multiply(speed.toFloat())
79+
val waitingAfterPlacing = placeWaitTicks > 0
80+
if (placeWaitTicks > 0) placeWaitTicks--
81+
82+
if (pauseAfterPlacing && waitingAfterPlacing ||
83+
pauseWhileMining && breakQueued ||
84+
pauseWhilePlacing && placeQueued
85+
) return@listen
86+
87+
val input = event.input
88+
val forward = if (walkForward || walkBackward) walkForward.toDouble() - walkBackward.toDouble()
89+
else input.roundedForward
90+
val strafe = if (strafeLeft || strafeRight) strafeLeft.toDouble() - strafeRight.toDouble()
91+
else input.roundedStrafing
92+
93+
input.update(
94+
forward = forward,
95+
strafe = strafe,
96+
sneak = sneak || input.sneaking,
97+
sprint = sprint || input.sprinting,
98+
)
4299
}
43100
}
44-
}
101+
}

src/main/kotlin/com/lambda/util/player/MovementUtils.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ object MovementUtils {
100100
playerInput = PlayerInput(
101101
forward > 0.0,
102102
forward < 0.0,
103-
strafe < 0.0,
104103
strafe > 0.0,
104+
strafe < 0.0,
105105
jump,
106106
sneak,
107107
sprint,

0 commit comments

Comments
 (0)