Skip to content

Commit ecbd48c

Browse files
committed
use supervisor scopes' feature to only return after all child jobs are done
1 parent 9bc9b11 commit ecbd48c

File tree

4 files changed

+49
-51
lines changed

4 files changed

+49
-51
lines changed

src/main/kotlin/com/lambda/interaction/construction/simulation/BuildSimulator.kt

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,40 +29,45 @@ import com.lambda.interaction.construction.simulation.checks.RequirementChecks.c
2929
import com.lambda.util.BlockUtils.blockState
3030
import io.ktor.util.collections.*
3131
import kotlinx.coroutines.Dispatchers
32-
import kotlinx.coroutines.joinAll
3332
import kotlinx.coroutines.launch
3433
import kotlinx.coroutines.runBlocking
34+
import kotlinx.coroutines.supervisorScope
3535
import net.minecraft.util.math.Vec3d
3636

3737
object BuildSimulator : SimChecker<PostSimResult>() {
3838
context(automatedSafeContext: AutomatedSafeContext)
39-
fun Blueprint.simulate(pov: Vec3d = automatedSafeContext.player.eyePos): Set<BuildResult> = runBlocking(Dispatchers.Default) {
40-
val concurrentSet = ConcurrentSet<BuildResult>()
41-
with(automatedSafeContext) {
42-
structure.entries
43-
.map { (pos, targetState) ->
44-
launch {
45-
val simInfo = simInfo(
46-
pos,
47-
blockState(pos),
48-
targetState,
49-
pov,
50-
concurrentSet
51-
) ?: return@launch
52-
with(simInfo) {
53-
with(null) {
54-
if (checkRequirements() ||
55-
checkPostProcessing() ||
56-
checkBreaks() ||
57-
checkPlacements()) return@launch
58-
else result(PostSimResult.NoMatch(pos))
39+
fun Blueprint.simulate(
40+
pov: Vec3d = automatedSafeContext.player.eyePos
41+
): Set<BuildResult> =
42+
runBlocking(Dispatchers.Default) {
43+
supervisorScope {
44+
val concurrentSet = ConcurrentSet<BuildResult>()
45+
46+
with(automatedSafeContext) {
47+
structure.entries.forEach { (pos, targetState) ->
48+
launch {
49+
val simInfo = simInfo(
50+
pos,
51+
blockState(pos),
52+
targetState,
53+
pov,
54+
concurrentSet
55+
) ?: return@launch
56+
57+
with(simInfo) {
58+
with(null) {
59+
if (checkRequirements() ||
60+
checkPostProcessing() ||
61+
checkBreaks() ||
62+
checkPlacements()) return@launch
63+
else result(PostSimResult.NoMatch(pos))
64+
}
5965
}
6066
}
6167
}
62-
}.joinAll()
63-
}
68+
}
6469

65-
return@runBlocking concurrentSet
66-
}
70+
concurrentSet
71+
}
72+
}
6773
}
68-

src/main/kotlin/com/lambda/interaction/construction/simulation/SimChecker.kt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,8 @@ import com.lambda.util.math.distSq
2929
import com.lambda.util.math.vec3d
3030
import com.lambda.util.world.raycast.RayCastUtils.blockResult
3131
import io.ktor.util.collections.*
32-
import kotlinx.coroutines.Dispatchers
33-
import kotlinx.coroutines.joinAll
3432
import kotlinx.coroutines.launch
35-
import kotlinx.coroutines.withContext
33+
import kotlinx.coroutines.supervisorScope
3634
import net.minecraft.util.hit.BlockHitResult
3735
import net.minecraft.util.math.BlockPos
3836
import net.minecraft.util.math.Direction
@@ -82,8 +80,8 @@ abstract class SimChecker<T : BuildResult> {
8280
val validHits = ConcurrentSet<CheckedHit>()
8381
val misses = ConcurrentSet<Vec3d>()
8482

85-
withContext(Dispatchers.Default) {
86-
boxes.map { box ->
83+
supervisorScope {
84+
boxes.forEach { box ->
8785
launch {
8886
val sides = if (buildConfig.checkSideVisibility || buildConfig.strictRayCast) {
8987
sides.intersect(box.getVisibleSurfaces(pov))
@@ -110,7 +108,7 @@ abstract class SimChecker<T : BuildResult> {
110108
validHits.add(checked)
111109
}
112110
}
113-
}.joinAll()
111+
}
114112
}
115113

116114
if (validHits.isEmpty()) {

src/main/kotlin/com/lambda/interaction/construction/simulation/checks/PlaceChecker.kt

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,9 @@ import com.lambda.util.player.MovementUtils.sneaking
5050
import com.lambda.util.player.copyPlayer
5151
import com.lambda.util.world.raycast.RayCastUtils.blockResult
5252
import kotlinx.coroutines.CoroutineScope
53-
import kotlinx.coroutines.Dispatchers
5453
import kotlinx.coroutines.cancel
55-
import kotlinx.coroutines.joinAll
5654
import kotlinx.coroutines.launch
5755
import kotlinx.coroutines.supervisorScope
58-
import kotlinx.coroutines.withContext
5956
import net.minecraft.block.BlockState
6057
import net.minecraft.block.pattern.CachedBlockPosition
6158
import net.minecraft.client.network.ClientPlayerEntity
@@ -83,24 +80,20 @@ class PlaceChecker @SimCheckerDsl private constructor(simInfo: SimInfo)
8380
}
8481
}
8582

86-
private suspend fun AutomatedSafeContext.checkPlacements(): Boolean {
83+
private suspend fun AutomatedSafeContext.checkPlacements(): Boolean =
8784
supervisorScope {
88-
withContext(Dispatchers.Default) {
89-
preProcessing.sides.map { side ->
90-
launch {
91-
val neighborPos = pos.offset(side)
92-
val neighborSide = side.opposite
93-
if (!placeConfig.airPlace.isEnabled)
94-
testBlock(neighborPos, neighborSide, this@supervisorScope)
95-
testBlock(pos, side, this@supervisorScope)
96-
}
97-
}.joinAll()
85+
preProcessing.sides.forEach { side ->
86+
launch {
87+
val neighborPos = pos.offset(side)
88+
val neighborSide = side.opposite
89+
if (!placeConfig.airPlace.isEnabled)
90+
testBlock(neighborPos, neighborSide, this@supervisorScope)
91+
testBlock(pos, side, this@supervisorScope)
92+
}
9893
}
94+
true
9995
}
10096

101-
return true
102-
}
103-
10497
private suspend fun AutomatedSafeContext.testBlock(pos: BlockPos, side: Direction, supervisorScope: CoroutineScope) {
10598
if (!world.worldBorder.contains(pos)) return
10699

@@ -240,7 +233,7 @@ class PlaceChecker @SimCheckerDsl private constructor(simInfo: SimInfo)
240233

241234
private fun AutomatedSafeContext.testPlaceState(context: ItemPlacementContext): PlaceTest {
242235
val resultState = context.stack.blockItem.getPlacementState(context) ?: run {
243-
result(PlaceResult.BlockedByEntity(pos))
236+
result(PlaceResult.BlockedByEntity(pos, emptyList()))
244237
return PlaceTest(state, PlaceTestResult.BlockedByEntity)
245238
}
246239

src/main/kotlin/com/lambda/interaction/construction/simulation/checks/PostProcessingChecker.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ import com.lambda.interaction.construction.result.Dependable
2424
import com.lambda.interaction.construction.result.results.GenericResult
2525
import com.lambda.interaction.construction.result.results.InteractResult
2626
import com.lambda.interaction.construction.simulation.ISimInfo
27+
import com.lambda.interaction.construction.simulation.ISimInfo.Companion.simInfo
2728
import com.lambda.interaction.construction.simulation.SimChecker
2829
import com.lambda.interaction.construction.simulation.SimCheckerDsl
2930
import com.lambda.interaction.construction.simulation.SimInfo
31+
import com.lambda.interaction.construction.simulation.checks.PlaceChecker.Companion.checkPlacements
3032
import com.lambda.interaction.construction.verify.TargetState
3133
import com.lambda.interaction.material.ContainerSelection.Companion.selectContainer
3234
import com.lambda.interaction.material.StackSelection.Companion.select
@@ -98,7 +100,7 @@ class PostProcessingChecker @SimCheckerDsl private constructor(simInfo: SimInfo)
98100
simInteraction(expectedState)
99101
}
100102

101-
Properties.SLAB_TYPE -> return false
103+
Properties.SLAB_TYPE -> simInfo()?.checkPlacements()
102104
}
103105
}
104106

0 commit comments

Comments
 (0)