Skip to content

Commit 982e6fb

Browse files
committed
more request creation functions for break and place requests. Omitted improving interact request as i plan to merge the interaction and place managers into one
1 parent af56457 commit 982e6fb

File tree

4 files changed

+98
-53
lines changed

4 files changed

+98
-53
lines changed

src/main/kotlin/com/lambda/interaction/request/breaking/BreakRequest.kt

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,15 @@
1818
package com.lambda.interaction.request.breaking
1919

2020
import com.lambda.context.Automated
21+
import com.lambda.context.AutomatedSafeContext
2122
import com.lambda.context.SafeContext
2223
import com.lambda.interaction.construction.context.BreakContext
2324
import com.lambda.interaction.construction.context.BuildContext
25+
import com.lambda.interaction.construction.result.BuildResult
26+
import com.lambda.interaction.construction.result.Dependent
27+
import com.lambda.interaction.construction.result.results.BreakResult
28+
import com.lambda.interaction.construction.simulation.BuildSimulator.simulate
29+
import com.lambda.interaction.construction.verify.TargetState
2430
import com.lambda.interaction.request.LogContext
2531
import com.lambda.interaction.request.LogContext.Companion.LogContextBuilder
2632
import com.lambda.interaction.request.Request
@@ -134,13 +140,40 @@ data class BreakRequest private constructor(
134140
companion object {
135141
var requestCount = 0
136142

143+
@BreakRequestDsl
144+
fun AutomatedSafeContext.breakRequest(
145+
positions: Collection<BlockPos>,
146+
pendingInteractions: MutableCollection<BuildContext>,
147+
nowOrNothing: Boolean = false,
148+
builder: (BreakRequestBuilder.() -> Unit)? = null
149+
) = positions
150+
.associateWith { TargetState.Empty }
151+
.simulate()
152+
.breakRequest(pendingInteractions, nowOrNothing, builder)
153+
154+
@BreakRequestDsl
155+
context(automated: Automated)
156+
fun Collection<BuildResult>.breakRequest(
157+
pendingInteractions: MutableCollection<BuildContext>,
158+
nowOrNothing: Boolean = false,
159+
builder: (BreakRequestBuilder.() -> Unit)? = null
160+
) = asSequence()
161+
.map { if (it is Dependent) it.lastDependency else it }
162+
.filterIsInstance<BreakResult.Break>()
163+
.map { it.context }
164+
.toSet()
165+
.takeIf { it.isNotEmpty() }
166+
?.let { automated.breakRequest(it, pendingInteractions, nowOrNothing, builder) }
167+
137168
@BreakRequestDsl
138169
fun Automated.breakRequest(
139170
contexts: Collection<BreakContext>,
140171
pendingInteractions: MutableCollection<BuildContext>,
141172
nowOrNothing: Boolean = false,
142173
builder: (BreakRequestBuilder.() -> Unit)? = null
143-
) = BreakRequestBuilder(contexts, pendingInteractions, nowOrNothing, this).apply { builder?.invoke(this) }.build()
174+
) = BreakRequestBuilder(
175+
contexts, pendingInteractions, nowOrNothing, this
176+
).apply { builder?.invoke(this) }.build()
144177

145178
@BreakRequestDsl
146179
private fun BreakRequestBuilder.build(): BreakRequest = request

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

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ import com.lambda.context.Automated
2121
import com.lambda.context.SafeContext
2222
import com.lambda.interaction.construction.context.BuildContext
2323
import com.lambda.interaction.construction.context.PlaceContext
24+
import com.lambda.interaction.construction.result.BuildResult
25+
import com.lambda.interaction.construction.result.Dependent
26+
import com.lambda.interaction.construction.result.results.PlaceResult
2427
import com.lambda.interaction.request.LogContext
2528
import com.lambda.interaction.request.LogContext.Companion.LogContextBuilder
2629
import com.lambda.interaction.request.Request
@@ -29,16 +32,17 @@ import com.lambda.util.BlockUtils.blockState
2932
import com.lambda.util.BlockUtils.matches
3033
import net.minecraft.util.math.BlockPos
3134

32-
data class PlaceRequest(
35+
data class PlaceRequest private constructor(
3336
val contexts: Collection<PlaceContext>,
3437
val pendingInteractions: MutableCollection<BuildContext>,
3538
private val automated: Automated,
3639
override val nowOrNothing: Boolean = false,
37-
val onPlace: (SafeContext.(BlockPos) -> Unit)? = null
3840
) : Request(), LogContext, Automated by automated {
3941
override val requestId = ++requestCount
4042
override val tickStageMask get() = placeConfig.tickStageMask
4143

44+
var onPlace: (SafeContext.(BlockPos) -> Unit)? = null
45+
4246
override val done: Boolean
4347
get() = runSafe {
4448
contexts.all { it.expectedState.matches(blockState(it.blockPos)) }
@@ -54,7 +58,50 @@ data class PlaceRequest(
5458
}
5559
}
5660

61+
@DslMarker
62+
annotation class PlaceRequestDsl
63+
64+
@PlaceRequestDsl
65+
class PlaceRequestBuilder(
66+
contexts: Collection<PlaceContext>,
67+
pendingInteractions: MutableCollection<BuildContext>,
68+
nowOrNothing: Boolean,
69+
automated: Automated
70+
) {
71+
val request = PlaceRequest(contexts, pendingInteractions, automated, nowOrNothing)
72+
73+
@PlaceRequestDsl
74+
fun onPlace(callback: SafeContext.(BlockPos) -> Unit) {
75+
request.onPlace = callback
76+
}
77+
}
78+
5779
companion object {
5880
var requestCount = 0
81+
82+
@PlaceRequestDsl
83+
context(automated: Automated)
84+
fun Collection<BuildResult>.placeRequest(
85+
pendingInteractions: MutableCollection<BuildContext>,
86+
nowOrNothing: Boolean = false,
87+
builder: (PlaceRequestBuilder.() -> Unit)? = null
88+
) = asSequence()
89+
.map { if (it is Dependent) it.lastDependency else it }
90+
.filterIsInstance<PlaceResult.Place>()
91+
.map { it.context }
92+
.toSet()
93+
.takeIf { it.isNotEmpty() }
94+
?.let { automated.placeRequest(it, pendingInteractions, nowOrNothing, builder) }
95+
96+
@PlaceRequestDsl
97+
fun Automated.placeRequest(
98+
contexts: Collection<PlaceContext>,
99+
pendingInteractions: MutableCollection<BuildContext>,
100+
nowOrNothing: Boolean = false,
101+
builder: (PlaceRequestBuilder.() -> Unit)? = null
102+
) = PlaceRequestBuilder(contexts, pendingInteractions, nowOrNothing, this).apply { builder?.invoke(this) }.build()
103+
104+
@PlaceRequestDsl
105+
fun PlaceRequestBuilder.build() = request
59106
}
60107
}

src/main/kotlin/com/lambda/module/modules/player/FastBreak.kt

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ import com.lambda.event.events.PlayerEvent
2323
import com.lambda.event.events.TickEvent
2424
import com.lambda.event.listener.SafeListener.Companion.listen
2525
import com.lambda.interaction.construction.context.BuildContext
26-
import com.lambda.interaction.construction.result.results.BreakResult
27-
import com.lambda.interaction.construction.simulation.BuildSimulator.simulate
28-
import com.lambda.interaction.construction.verify.TargetState
2926
import com.lambda.interaction.request.breaking.BreakRequest.Companion.breakRequest
3027
import com.lambda.module.Module
3128
import com.lambda.module.tag.ModuleTag
@@ -77,17 +74,9 @@ object FastBreak : Module(
7774
listen<PlayerEvent.Attack.Block> { it.cancel() }
7875
listen<PlayerEvent.Breaking.Update> { event ->
7976
event.cancel()
80-
81-
val breakContexts =
82-
runSafeAutomated {
83-
buildMap { put(event.pos, TargetState.Empty) }
84-
.simulate()
85-
.filterIsInstance<BreakResult.Break>()
86-
.map { it.context }
87-
.takeIf { it.isNotEmpty() }
88-
} ?: return@listen
89-
90-
breakRequest(breakContexts, pendingInteractions).submit()
77+
runSafeAutomated {
78+
breakRequest(listOf(event.pos), pendingInteractions)?.submit()
79+
}
9180
}
9281
}
9382
}

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

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ import com.lambda.interaction.construction.verify.TargetState
5050
import com.lambda.interaction.request.breaking.BreakRequest.Companion.breakRequest
5151
import com.lambda.interaction.request.interacting.InteractRequest
5252
import com.lambda.interaction.request.inventory.InventoryRequest.Companion.inventoryRequest
53-
import com.lambda.interaction.request.placing.PlaceRequest
53+
import com.lambda.interaction.request.placing.PlaceRequest.Companion.placeRequest
5454
import com.lambda.task.Task
5555
import com.lambda.task.tasks.EatTask.Companion.eat
5656
import com.lambda.threading.runSafeAutomated
@@ -168,32 +168,19 @@ class BuildTask private constructor(
168168
is Contextual -> {
169169
if (atMaxPendingInteractions) return
170170
when (result) {
171-
is BreakResult.Break -> {
172-
val breakResults = allResults
173-
.map { if (it is Dependent) it.lastDependency else it }
174-
.filterIsInstance<BreakResult.Break>()
175-
.map { it.context }
176-
177-
breakRequest(breakResults, pendingInteractions) {
171+
is BreakResult.Break ->
172+
allResults.breakRequest(pendingInteractions) {
178173
onStop { breaks++ }
179174
onItemDrop?.let { onItemDrop ->
180175
onItemDrop { onItemDrop(it) }
181176
}
182-
}.submit(queueIfMismatchedStage = true)
183-
return
184-
}
185-
is PlaceResult.Place -> {
186-
val placeResults = allResults
187-
.map { if (it is Dependent) it.lastDependency else it }
188-
.filterIsInstance<PlaceResult.Place>()
189-
.map { it.context }
177+
}?.submit()
178+
179+
is PlaceResult.Place ->
180+
allResults.placeRequest(pendingInteractions, false) {
181+
onPlace { placements++ }
182+
}?.submit()
190183

191-
PlaceRequest(
192-
placeResults,
193-
pendingInteractions,
194-
this@BuildTask
195-
) { placements++ }.submit(queueIfMismatchedStage = true)
196-
}
197184
is InteractResult.Interact -> {
198185
val interactResults = allResults
199186
.map { if (it is Dependent) it.lastDependency else it }
@@ -258,7 +245,7 @@ class BuildTask private constructor(
258245
@Ta5kBuilder
259246
fun Automated.build(
260247
finishOnDone: Boolean = true,
261-
collectDrops: Boolean = DEFAULT.buildConfig.collectDrops,
248+
collectDrops: Boolean = buildConfig.collectDrops,
262249
lifeMaintenance: Boolean = false,
263250
blueprint: () -> Blueprint
264251
) = BuildTask(blueprint(), finishOnDone, collectDrops, lifeMaintenance, this)
@@ -267,15 +254,15 @@ class BuildTask private constructor(
267254
context(automated: Automated)
268255
fun Structure.build(
269256
finishOnDone: Boolean = true,
270-
collectDrops: Boolean = DEFAULT.buildConfig.collectDrops,
257+
collectDrops: Boolean = automated.buildConfig.collectDrops,
271258
lifeMaintenance: Boolean = false
272259
) = BuildTask(toBlueprint(), finishOnDone, collectDrops, lifeMaintenance, automated)
273260

274261
@Ta5kBuilder
275262
context(automated: Automated)
276263
fun Blueprint.build(
277264
finishOnDone: Boolean = true,
278-
collectDrops: Boolean = DEFAULT.buildConfig.collectDrops,
265+
collectDrops: Boolean = automated.buildConfig.collectDrops,
279266
lifeMaintenance: Boolean = false
280267
) = BuildTask(this, finishOnDone, collectDrops, lifeMaintenance, automated)
281268

@@ -289,16 +276,5 @@ class BuildTask private constructor(
289276
blockPos.toStructure(TargetState.Air).toBlueprint(),
290277
finishOnDone, collectDrops, lifeMaintenance, this
291278
)
292-
293-
@Ta5kBuilder
294-
fun Automated.breakBlock(
295-
blockPos: BlockPos,
296-
finishOnDone: Boolean = true,
297-
collectDrops: Boolean = DEFAULT.buildConfig.collectDrops,
298-
lifeMaintenance: Boolean = false
299-
) = BuildTask(
300-
blockPos.toStructure(TargetState.Air).toBlueprint(),
301-
finishOnDone, collectDrops, lifeMaintenance, this
302-
)
303279
}
304280
}

0 commit comments

Comments
 (0)