Skip to content

Commit 18c373b

Browse files
committed
Refactor rotation handling and placement logic
Simplified and streamlined rotation management by consolidating `onUpdate` and `onReceive` callbacks. Improved code readability and consistency in `BuildTask` and `BreakBlock` tasks by refining placement and rotation operations. Removed unused comments and ensured proper alignment with updated rotation conventions.
1 parent 653e71c commit 18c373b

File tree

3 files changed

+33
-33
lines changed

3 files changed

+33
-33
lines changed

common/src/main/kotlin/com/lambda/interaction/RotationManager.kt

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ object RotationManager : Loadable {
5656
priority: Int = 0,
5757
alwaysListen: Boolean = false,
5858
onUpdate: SafeContext.(lastContext: RotationContext?) -> RotationContext?,
59-
onReceive: SafeContext.() -> Unit = {}
59+
onReceive: SafeContext.(context: RotationContext) -> Unit = {}
6060
) {
6161
var lastCtx: RotationContext? = null
6262

@@ -71,8 +71,8 @@ object RotationManager : Loadable {
7171
}
7272

7373
listen<RotationEvent.Post> { event ->
74-
if (event.context == lastCtx && event.context.isValid) {
75-
onReceive()
74+
if (event.context == lastCtx) {
75+
onReceive(event.context)
7676
}
7777
}
7878
}
@@ -123,20 +123,19 @@ object RotationManager : Loadable {
123123
currentRotation = currentContext?.let { context ->
124124
val rotationTo = if (keepTicks >= 0) context.rotation else player.rotation
125125

126-
rotationTo
127-
// var speedMultiplier = (context.config as? RotationSettings)?.speedMultiplier ?: 1.0
128-
// if (keepTicks < 0) speedMultiplier = 1.0
129-
//
130-
// val turnSpeed = context.config.turnSpeed * speedMultiplier
131-
//
132-
// currentRotation
133-
// .slerp(rotationTo, turnSpeed)
134-
// .fixSensitivity(prevRotation)
135-
// .apply {
136-
// if (context.config.rotationMode != RotationMode.LOCK) return@apply
137-
// player.yaw = this.yawF
138-
// player.pitch = this.pitchF
139-
// }
126+
var speedMultiplier = (context.config as? RotationSettings)?.speedMultiplier ?: 1.0
127+
if (keepTicks < 0) speedMultiplier = 1.0
128+
129+
val turnSpeed = context.config.turnSpeed * speedMultiplier
130+
131+
currentRotation
132+
.slerp(rotationTo, turnSpeed)
133+
.fixSensitivity(prevRotation)
134+
.apply {
135+
if (context.config.rotationMode != RotationMode.LOCK) return@apply
136+
player.yaw = this.yawF
137+
player.pitch = this.pitchF
138+
}
140139
} ?: player.rotation
141140
}
142141

common/src/main/kotlin/com/lambda/task/tasks/BreakBlock.kt

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import com.lambda.event.events.RotationEvent
2525
import com.lambda.event.events.TickEvent
2626
import com.lambda.event.events.WorldEvent
2727
import com.lambda.event.listener.SafeListener.Companion.listen
28+
import com.lambda.interaction.RotationManager.requestRotation
2829
import com.lambda.interaction.construction.context.BreakContext
2930
import com.lambda.interaction.visibilty.VisibilityChecker.lookAtBlock
3031
import com.lambda.module.modules.client.TaskFlowModule
@@ -71,6 +72,7 @@ class BreakBlock @Ta5kBuilder constructor(
7172
success(null)
7273
return
7374
}
75+
7476
beginState = blockState
7577

7678
if (!rotate || ctx.instantBreak) {
@@ -79,18 +81,17 @@ class BreakBlock @Ta5kBuilder constructor(
7981
}
8082

8183
init {
82-
listen<RotationEvent.Update> { event ->
83-
if (state != State.BREAKING) return@listen
84-
if (!rotate || ctx.instantBreak) return@listen
85-
event.context = lookAtBlock(blockPos, rotation, interact, sides)
86-
}
87-
88-
listen<RotationEvent.Post> {
89-
if (state != State.BREAKING) return@listen
90-
if (!rotate || ctx.instantBreak) return@listen
91-
92-
isValid = it.context.isValid
93-
}
84+
requestRotation(
85+
onUpdate = {
86+
if (state != State.BREAKING) return@requestRotation null
87+
if (!rotate || ctx.instantBreak) return@requestRotation null
88+
89+
lookAtBlock(blockPos, rotation, interact, sides)
90+
},
91+
onReceive = { context ->
92+
isValid = context.isValid
93+
}
94+
)
9495

9596
listen<TickEvent.Pre> {
9697
drop?.let { itemDrop ->

common/src/main/kotlin/com/lambda/task/tasks/BuildTask.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ class BuildTask @Ta5kBuilder constructor(
8181
timeout
8282
}
8383

84-
currentPlacement?.let {
85-
if (currentPlacement?.rotation?.isValid != true) return@listen
84+
currentPlacement?.let { context ->
85+
if (!context.rotation.isValid) return@listen
8686
if (inScope++ < 1) return@listen // ToDo: Should not be needed but timings are wrong
87-
it.place(interact.swingHand)
88-
pendingPlacements.add(it)
87+
context.place(interact.swingHand)
88+
pendingPlacements.add(context)
8989
currentPlacement = null
9090
inScope = 0
9191
}

0 commit comments

Comments
 (0)