Skip to content

Commit e68b215

Browse files
committed
Refactor task resolution and task nesting complexity
Simplifies the task execution flow by introducing a `Resolvable` interface to standardize task resolution. Removes redundant task nesting and pending logic, streamlining parent task management and improving readability and maintainability.
1 parent 5cfdbac commit e68b215

File tree

8 files changed

+88
-101
lines changed

8 files changed

+88
-101
lines changed

common/src/main/kotlin/com/lambda/interaction/construction/result/BreakResult.kt

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ import com.lambda.context.SafeContext
2323
import com.lambda.interaction.construction.context.BreakContext
2424
import com.lambda.interaction.material.ContainerManager.findBestAvailableTool
2525
import com.lambda.interaction.material.ContainerManager.transfer
26+
import com.lambda.interaction.material.MaterialContainer
2627
import com.lambda.interaction.material.StackSelection.Companion.select
2728
import com.lambda.interaction.material.StackSelection.Companion.selectStack
2829
import com.lambda.interaction.material.container.MainHandContainer
30+
import com.lambda.task.Task
2931
import com.lambda.task.tasks.BreakBlock
3032
import net.minecraft.block.BlockState
3133
import net.minecraft.item.Item
@@ -42,18 +44,14 @@ sealed class BreakResult : BuildResult() {
4244
data class Break(
4345
override val blockPos: BlockPos,
4446
val context: BreakContext
45-
) : Drawable, BreakResult() {
47+
) : Drawable, Resolvable, BreakResult() {
4648
override val rank = Rank.BREAK_SUCCESS
4749
private val color = Color(222, 0, 0, 100)
4850

4951
var collectDrop = false
5052
override val pausesParent get() = collectDrop
5153

52-
override fun SafeContext.onStart() {
53-
BreakBlock(context, collectDrop).finally {
54-
success()
55-
}.execute(this@Break)
56-
}
54+
override fun resolve() = BreakBlock(context, collectDrop)
5755

5856
override fun SafeContext.buildRenderer() {
5957
withPos(context.expectedPos, color, context.result.side)
@@ -79,10 +77,6 @@ sealed class BreakResult : BuildResult() {
7977
override val rank = Rank.BREAK_NOT_EXPOSED
8078
private val color = Color(46, 0, 0, 30)
8179

82-
override fun SafeContext.onStart() {
83-
failure("Block is not exposed to air.")
84-
}
85-
8680
override fun SafeContext.buildRenderer() {
8781
withPos(blockPos, color, side)
8882
}
@@ -104,24 +98,20 @@ sealed class BreakResult : BuildResult() {
10498
override val blockPos: BlockPos,
10599
val blockState: BlockState,
106100
val badItem: Item
107-
) : Drawable, BreakResult() {
101+
) : Drawable, Resolvable, BreakResult() {
108102
override val rank = Rank.BREAK_ITEM_CANT_MINE
109103
private val color = Color(255, 0, 0, 100)
110104

111105
override val pausesParent get() = true
112106

113-
override fun SafeContext.onStart() {
107+
override fun resolve() =
114108
findBestAvailableTool(blockState)
115109
?.select()
116110
?.transfer(MainHandContainer)
117-
?.finally {
118-
success()
119-
}?.execute(this@ItemCantMine) ?: run {
120-
selectStack {
121-
isItem(badItem).not()
122-
}.transfer(MainHandContainer)?.execute(this@ItemCantMine) ?: failure("No item found or space")
123-
}
124-
}
111+
?: selectStack {
112+
isItem(badItem).not()
113+
}.transfer(MainHandContainer)
114+
?: MaterialContainer.Nothing()
125115

126116
override fun SafeContext.buildRenderer() {
127117
withPos(blockPos, color)

common/src/main/kotlin/com/lambda/interaction/construction/result/BuildResult.kt

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ import baritone.api.pathing.goals.GoalNear
2222
import com.lambda.context.SafeContext
2323
import com.lambda.interaction.construction.context.BuildContext
2424
import com.lambda.interaction.material.ContainerManager.transfer
25+
import com.lambda.interaction.material.MaterialContainer
2526
import com.lambda.interaction.material.StackSelection.Companion.select
2627
import com.lambda.interaction.material.container.MainHandContainer
2728
import com.lambda.task.Task
2829
import com.lambda.util.BlockUtils.blockState
30+
import com.lambda.util.Nameable
2931
import net.minecraft.block.BlockState
3032
import net.minecraft.item.Item
3133
import net.minecraft.item.ItemStack
@@ -35,7 +37,7 @@ import net.minecraft.util.math.Direction
3537
import net.minecraft.util.math.Vec3d
3638
import java.awt.Color
3739

38-
abstract class BuildResult : ComparableResult<Rank>, Task<Unit>() {
40+
abstract class BuildResult : ComparableResult<Rank>, Nameable {
3941
abstract val blockPos: BlockPos
4042
open val pausesParent = false
4143
override val name: String get() = "${this::class.simpleName} at ${blockPos.toShortString()}"
@@ -191,20 +193,15 @@ abstract class BuildResult : ComparableResult<Rank>, Task<Unit>() {
191193
val context: BuildContext,
192194
val neededItem: Item,
193195
val currentItem: ItemStack,
194-
) : Drawable, BuildResult() {
196+
) : Drawable, Resolvable, BuildResult() {
195197
override val name: String get() = "Wrong item ($currentItem) for ${blockPos.toShortString()} need ${neededItem.name.string}"
196198
override val rank = Rank.WRONG_ITEM
197199
private val color = Color(3, 252, 169, 100)
198200

199201
override val pausesParent get() = true
200202

201-
override fun SafeContext.onStart() {
202-
neededItem.select()
203-
.transfer(MainHandContainer)
204-
?.finally {
205-
success()
206-
}?.execute(this@WrongItem) ?: failure("Item ${neededItem.name.string} not found")
207-
}
203+
override fun resolve() = neededItem.select()
204+
.transfer(MainHandContainer) ?: MaterialContainer.Nothing()
208205

209206
override fun SafeContext.buildRenderer() {
210207
if (blockPos.blockState(world).isAir) {
@@ -231,20 +228,15 @@ abstract class BuildResult : ComparableResult<Rank>, Task<Unit>() {
231228
override val blockPos: BlockPos,
232229
val context: BuildContext,
233230
val neededStack: ItemStack
234-
) : Drawable, BuildResult() {
231+
) : Drawable, Resolvable, BuildResult() {
235232
override val name: String get() = "Wrong stack for $blockPos need $neededStack."
236233
override val rank = Rank.WRONG_ITEM
237234
private val color = Color(3, 252, 169, 100)
238235

239236
override val pausesParent get() = true
240237

241-
override fun SafeContext.onStart() {
242-
neededStack.select()
243-
.transfer(MainHandContainer)
244-
?.finally {
245-
success()
246-
}?.execute(this@WrongStack) ?: failure("Stack ${neededStack.name.string} not found")
247-
}
238+
override fun resolve() =
239+
neededStack.select().transfer(MainHandContainer) ?: MaterialContainer.Nothing()
248240

249241
override fun SafeContext.buildRenderer() {
250242
if (blockPos.blockState(world).isAir) {

common/src/main/kotlin/com/lambda/interaction/construction/result/PlaceResult.kt

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import baritone.api.pathing.goals.GoalBlock
2121
import baritone.api.pathing.goals.GoalInverted
2222
import com.lambda.context.SafeContext
2323
import com.lambda.interaction.construction.context.PlaceContext
24+
import com.lambda.task.Task
2425
import com.lambda.task.tasks.BuildTask.Companion.breakBlock
2526
import com.lambda.task.tasks.PlaceBlock
2627
import net.minecraft.block.BlockState
@@ -44,15 +45,11 @@ sealed class PlaceResult : BuildResult() {
4445
data class Place(
4546
override val blockPos: BlockPos,
4647
val context: PlaceContext
47-
) : Drawable, PlaceResult() {
48+
) : Drawable, Resolvable, PlaceResult() {
4849
override val rank = Rank.PLACE_SUCCESS
4950
private val color = Color(35, 188, 254, 100)
5051

51-
override fun SafeContext.onStart() {
52-
PlaceBlock(context).finally {
53-
success()
54-
}.execute(this@Place)
55-
}
52+
override fun resolve() = PlaceBlock(context)
5653

5754
override fun SafeContext.buildRenderer() {
5855
val hitPos = context.result.blockPos
@@ -105,14 +102,10 @@ sealed class PlaceResult : BuildResult() {
105102
data class CantReplace(
106103
override val blockPos: BlockPos,
107104
val simulated: ItemPlacementContext
108-
) : PlaceResult() {
105+
) : Resolvable, PlaceResult() {
109106
override val rank = Rank.PLACE_CANT_REPLACE
110107

111-
override fun SafeContext.onStart() {
112-
breakBlock(blockPos).finally {
113-
success()
114-
}.execute(this@CantReplace)
115-
}
108+
override fun resolve() = breakBlock(blockPos)
116109
}
117110

118111
/**
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2024 Lambda
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package com.lambda.interaction.construction.result
19+
20+
import com.lambda.task.Task
21+
22+
interface Resolvable {
23+
fun resolve(): Task<*>
24+
}

common/src/main/kotlin/com/lambda/module/modules/client/TaskFlowModule.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ package com.lambda.module.modules.client
2020
import com.lambda.config.groups.BuildSettings
2121
import com.lambda.config.groups.InteractionSettings
2222
import com.lambda.config.groups.RotationSettings
23+
import com.lambda.event.events.RenderEvent
24+
import com.lambda.event.listener.SafeListener.Companion.listen
25+
import com.lambda.interaction.construction.result.Drawable
2326
import com.lambda.module.Module
2427
import com.lambda.module.tag.ModuleTag
2528
import com.lambda.util.BlockUtils.allSigns
@@ -63,4 +66,15 @@ object TaskFlowModule : Module(
6366
Properties.WEST
6467
)
6568
// val ignoredTags by setting("Ignored Tags", defaultIgnoreTags)
69+
70+
@Volatile
71+
var drawables = listOf<Drawable>()
72+
73+
init {
74+
listen<RenderEvent.StaticESP> {
75+
drawables.toList().forEach { res ->
76+
with(res) { buildRenderer() }
77+
}
78+
}
79+
}
6680
}

common/src/main/kotlin/com/lambda/task/Task.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ abstract class Task<Result> : Nameable {
4141
val isCompleted get() = state == State.COMPLETED
4242
val size: Int get() = subTasks.sumOf { it.size } + 1
4343

44-
open var alwaysListening = false
44+
open var unpausable = false
4545

4646
private var nextTask: TaskGenerator<Result>? = null
4747
private var nextTaskOrNull: TaskGeneratorOrNull<Result>? = null
@@ -113,9 +113,9 @@ abstract class Task<Result> : Nameable {
113113
owner.subTasks.add(this)
114114
parent = owner
115115
LOG.info("${owner.name} started $name")
116-
if (!alwaysListening || pauseParent) {
116+
if (!unpausable || pauseParent) {
117117
LOG.info("$name deactivating parent ${owner.name}")
118-
owner.deactivate()
118+
if (owner !is TaskFlow) owner.deactivate()
119119
}
120120
runSafe { runCatching { onStart() }.onFailure { failure(it) } }
121121
startListening()
@@ -145,8 +145,8 @@ abstract class Task<Result> : Nameable {
145145

146146
@Ta5kBuilder
147147
fun deactivate() {
148-
if (this is TaskFlow) return
149148
if (state != State.RUNNING) return
149+
if (unpausable) return
150150
state = State.PAUSED
151151
stopListening()
152152
}
@@ -161,7 +161,8 @@ abstract class Task<Result> : Nameable {
161161
nextTaskOrNull = null
162162
parent?.let { owner -> task?.execute(owner) }
163163
} ?: run {
164-
onFinish?.invoke(this, result) ?: println("No more tasks to run")
164+
onFinish?.invoke(this, result)
165+
parent?.activate()
165166
onFinish = null
166167
}
167168
}
@@ -324,7 +325,7 @@ abstract class Task<Result> : Nameable {
324325
appendLine("${" ".repeat(level * 4)}${task.name}" + if (task !is TaskFlow) " [${task.state.display}]" else "")
325326
// if (task.state == State.COMPLETED || task.state == State.CANCELLED) return
326327
task.subTasks.forEach {
327-
// if (task is TaskFlow && (it.state == State.COMPLETED || it.state == State.CANCELLED)) return@forEach
328+
if (task is TaskFlow && (it.state == State.COMPLETED || it.state == State.CANCELLED)) return@forEach
328329
appendTaskTree(it, level + 1)
329330
}
330331
}

0 commit comments

Comments
 (0)