Skip to content

Commit 2900b09

Browse files
committed
moved build task logic from on rotate to on tick pre
1 parent b007333 commit 2900b09

File tree

2 files changed

+47
-117
lines changed

2 files changed

+47
-117
lines changed

common/src/main/kotlin/com/lambda/interaction/request/placing/PlaceRequest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ data class PlaceRequest(
3333
val rotationConfig: RotationConfig,
3434
val hotbarConfig: HotbarConfig,
3535
val interactionConfig: InteractionConfig,
36-
val prio: Priority,
36+
val prio: Priority = 0,
3737
val onPlace: () -> Unit
3838
) : Request(prio) {
3939
override val done: Boolean

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

Lines changed: 46 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,14 @@ import com.lambda.config.groups.InventoryConfig
2525
import com.lambda.interaction.request.rotation.RotationConfig
2626
import com.lambda.context.SafeContext
2727
import com.lambda.event.events.EntityEvent
28-
import com.lambda.event.events.MovementEvent
2928
import com.lambda.event.events.TickEvent
30-
import com.lambda.event.events.WorldEvent
3129
import com.lambda.event.listener.SafeListener.Companion.listen
32-
import com.lambda.interaction.request.rotation.RotationManager.onRotate
3330
import com.lambda.interaction.construction.blueprint.Blueprint
3431
import com.lambda.interaction.construction.blueprint.Blueprint.Companion.toStructure
3532
import com.lambda.interaction.construction.blueprint.PropagatingBlueprint
3633
import com.lambda.interaction.construction.blueprint.TickingBlueprint
3734
import com.lambda.interaction.construction.blueprint.StaticBlueprint.Companion.toBlueprint
38-
import com.lambda.interaction.construction.context.BreakContext
3935
import com.lambda.interaction.construction.context.BuildContext
40-
import com.lambda.interaction.construction.context.PlaceContext
4136
import com.lambda.interaction.construction.result.*
4237
import com.lambda.interaction.construction.simulation.BuildGoal
4338
import com.lambda.interaction.construction.simulation.BuildSimulator.simulate
@@ -46,7 +41,7 @@ import com.lambda.interaction.construction.verify.TargetState
4641
import com.lambda.interaction.material.transfer.TransactionExecutor.Companion.transfer
4742
import com.lambda.interaction.request.breaking.BreakRequest
4843
import com.lambda.interaction.request.hotbar.HotbarConfig
49-
import com.lambda.interaction.request.hotbar.HotbarRequest
44+
import com.lambda.interaction.request.placing.PlaceRequest
5045
import com.lambda.module.modules.client.TaskFlowModule
5146
import com.lambda.task.Task
5247
import com.lambda.util.BaritoneUtils
@@ -99,59 +94,7 @@ class BuildTask @Ta5kBuilder constructor(
9994

10095
init {
10196
listen<TickEvent.Pre> {
102-
currentInteraction?.let { context ->
103-
// TaskFlowModule.drawables = listOf(context)
104-
if (context.shouldRotate(build) && !context.rotation.done) return@let
105-
if (!hotbar.request(HotbarRequest(context.hotbarIndex)).done) return@let
106-
when (context) {
107-
is PlaceContext -> {
108-
if (context.sneak && !player.isSneaking) return@let
109-
placeBlock(Hand.MAIN_HAND, context)
110-
}
111-
}
112-
}
113-
114-
dropsToCollect.firstOrNull()?.let { itemDrop ->
115-
if (!world.entities.contains(itemDrop)) {
116-
dropsToCollect.remove(itemDrop)
117-
BaritoneUtils.cancel()
118-
return@listen
119-
}
120-
121-
val noInventorySpace = player.hotbarAndStorage.none { it.isEmpty }
122-
if (noInventorySpace) {
123-
val stackToThrow = player.currentScreenHandler.inventorySlots.firstOrNull {
124-
it.stack.item.block in TaskFlowModule.inventory.disposables
125-
} ?: run {
126-
failure("No item in inventory to throw but inventory is full and cant pick up item drop")
127-
return@listen
128-
}
129-
transfer(player.currentScreenHandler) {
130-
throwStack(stackToThrow.id)
131-
}.execute(this@BuildTask)
132-
return@listen
133-
}
134-
135-
BaritoneUtils.setGoalAndPath(GoalBlock(itemDrop.blockPos))
136-
}
137-
}
138-
139-
listen<TickEvent.Post> {
140-
(blueprint as? TickingBlueprint)?.tick()
141-
142-
if (finishOnDone && blueprint.structure.isEmpty()) {
143-
failure("Structure is empty")
144-
return@listen
145-
}
146-
}
147-
148-
onRotate {
149-
if (collectDrops && dropsToCollect.isNotEmpty()) return@onRotate
150-
151-
// val sim = blueprint.simulation(interact, rotation, inventory)
152-
// BlockPos.iterateOutwards(player.blockPos, 5, 5, 5).forEach { pos ->
153-
// sim.simulate(pos.toFastVec())
154-
// }
97+
if (collectDrops()) return@listen
15598

15699
// ToDo: Simulate for each pair player positions that work
157100
val results = blueprint.simulate(player.eyePos, interact, rotation, inventory, build)
@@ -168,31 +111,31 @@ class BuildTask @Ta5kBuilder constructor(
168111

169112
instantResults.firstOrNull()?.let {
170113
build.breakSettings.request(BreakRequest(instantResults.map { it.context }, build, rotation, hotbar) { breaks++ })
171-
return@onRotate
114+
return@listen
172115
}
173116
}
174117

175-
val resultsNotBlocked= results.filterNot { result ->
118+
val resultsNotBlocked = results.filterNot { result ->
176119
result.blockPos in pendingInteractions.map { it.expectedPos }
177120
}.sorted()
178-
val bestResult = resultsNotBlocked.firstOrNull() ?: return@onRotate
121+
val bestResult = resultsNotBlocked.firstOrNull() ?: return@listen
179122
when (bestResult) {
180123
is BuildResult.Done,
181124
is BuildResult.Ignored,
182125
is BuildResult.Unbreakable,
183126
is BuildResult.Restricted,
184127
is BuildResult.NoPermission -> {
185-
if (pendingInteractions.isNotEmpty()) return@onRotate
128+
if (pendingInteractions.isNotEmpty()) return@listen
186129
if (blueprint is PropagatingBlueprint) {
187130
blueprint.next()
188-
return@onRotate
131+
return@listen
189132
}
190133
if (finishOnDone) success()
191134
}
192135

193136
is BuildResult.NotVisible,
194137
is PlaceResult.NoIntegrity -> {
195-
if (!build.pathing) return@onRotate
138+
if (!build.pathing) return@listen
196139
val sim = blueprint.simulation(interact, rotation, inventory, build)
197140
val goal = BuildGoal(sim, player.blockPos)
198141
BaritoneUtils.setGoalAndPath(goal)
@@ -203,14 +146,17 @@ class BuildTask @Ta5kBuilder constructor(
203146
}
204147

205148
is BuildResult.Contextual -> {
206-
if (pendingInteractions.size >= build.maxPendingInteractions) return@onRotate
207-
208-
currentInteraction = bestResult.context
209-
if (bestResult !is BreakResult.Break) return@onRotate
210-
211-
val contexts = resultsNotBlocked.filterIsInstance<BreakResult.Break>().take(2).map { it.context }
212-
val request = BreakRequest(contexts, build, rotation, hotbar) { breaks++ }
213-
build.breakSettings.request(request)
149+
if (pendingInteractions.size >= build.maxPendingInteractions) return@listen
150+
val breakContexts = resultsNotBlocked.filterIsInstance<BreakResult.Break>().map { it.context }
151+
if (breakContexts.isNotEmpty()) {
152+
val request = BreakRequest(breakContexts, build, rotation, hotbar) { breaks++ }
153+
build.breakSettings.request(request)
154+
return@listen
155+
}
156+
if (bestResult !is PlaceResult.Place) return@listen
157+
build.placeSettings.request(
158+
PlaceRequest(bestResult.context, build, rotation, hotbar, interact) { placements++ }
159+
)
214160
}
215161

216162
is Resolvable -> {
@@ -219,38 +165,14 @@ class BuildTask @Ta5kBuilder constructor(
219165
bestResult.resolve().execute(this@BuildTask)
220166
}
221167
}
222-
223-
currentInteraction?.let { currentInteraction ->
224-
if (currentInteraction is BreakContext) return@let
225-
if (!currentInteraction.shouldRotate(build)) return@onRotate
226-
val rotateTo = currentInteraction.rotation
227-
rotation.request(rotateTo)
228-
}
229-
}
230-
231-
listen<MovementEvent.InputUpdate> {
232-
val context = currentInteraction ?: return@listen
233-
if (context !is PlaceContext) return@listen
234-
if (context.sneak) it.input.sneaking = true
235168
}
236169

237-
// listen<WorldEvent.BlockUpdate.Client> { event ->
238-
// val context = currentInteraction ?: return@listen
239-
// if (context.expectedPos != event.pos) return@listen
240-
// currentInteraction = null
241-
// pendingInteractions.add(context)
242-
// }
170+
listen<TickEvent.Post> {
171+
(blueprint as? TickingBlueprint)?.tick()
243172

244-
listen<WorldEvent.BlockUpdate.Server>(alwaysListen = true) { event ->
245-
pendingInteractions.firstOrNull { it.expectedPos == event.pos }?.let { ctx ->
246-
pendingInteractions.remove(ctx)
247-
if (!ctx.targetState.matches(event.newState, event.pos, world)) {
248-
this@BuildTask.warn("Update at ${event.pos.toShortString()} was rejected with ${event.newState} instead of ${ctx.targetState}")
249-
return@let
250-
}
251-
when (ctx) {
252-
is PlaceContext -> placements++
253-
}
173+
if (finishOnDone && blueprint.structure.isEmpty()) {
174+
failure("Structure is empty")
175+
return@listen
254176
}
255177
}
256178

@@ -268,23 +190,31 @@ class BuildTask @Ta5kBuilder constructor(
268190
}
269191
}
270192

271-
private fun SafeContext.placeBlock(hand: Hand, ctx: PlaceContext) {
272-
val actionResult = interaction.interactBlock(
273-
player, hand, ctx.result
274-
)
275-
276-
if (actionResult.isAccepted) {
277-
if (actionResult.shouldSwingHand() && interact.swingHand) {
278-
player.swingHand(hand)
193+
private fun SafeContext.collectDrops() =
194+
dropsToCollect.firstOrNull()?.let { itemDrop ->
195+
if (!world.entities.contains(itemDrop)) {
196+
dropsToCollect.remove(itemDrop)
197+
BaritoneUtils.cancel()
198+
return true
279199
}
280200

281-
if (!player.getStackInHand(hand).isEmpty && interaction.hasCreativeInventory()) {
282-
mc.gameRenderer.firstPersonRenderer.resetEquipProgress(hand)
201+
val noInventorySpace = player.hotbarAndStorage.none { it.isEmpty }
202+
if (noInventorySpace) {
203+
val stackToThrow = player.currentScreenHandler.inventorySlots.firstOrNull {
204+
it.stack.item.block in TaskFlowModule.inventory.disposables
205+
} ?: run {
206+
failure("No item in inventory to throw but inventory is full and cant pick up item drop")
207+
return true
208+
}
209+
transfer(player.currentScreenHandler) {
210+
throwStack(stackToThrow.id)
211+
}.execute(this@BuildTask)
212+
return true
283213
}
284-
} else {
285-
warn("Internal interaction failed with $actionResult")
286-
}
287-
}
214+
215+
BaritoneUtils.setGoalAndPath(GoalBlock(itemDrop.blockPos))
216+
return true
217+
} ?: false
288218

289219
companion object {
290220
@Ta5kBuilder

0 commit comments

Comments
 (0)