Skip to content

Commit 2566433

Browse files
committed
use coroutines for build simulation
1 parent e0fbd3a commit 2566433

File tree

1 file changed

+32
-25
lines changed

1 file changed

+32
-25
lines changed

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

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,7 @@ import com.lambda.util.BlockUtils.blockState
5959
import com.lambda.util.BlockUtils.calcItemBlockBreakingDelta
6060
import com.lambda.util.BlockUtils.hasFluid
6161
import com.lambda.util.BlockUtils.instantBreakable
62-
import com.lambda.util.BlockUtils.isEmpty
6362
import com.lambda.util.BlockUtils.isNotEmpty
64-
import com.lambda.util.Communication.info
6563
import com.lambda.util.Communication.warn
6664
import com.lambda.util.math.distSq
6765
import com.lambda.util.math.vec3d
@@ -70,6 +68,10 @@ import com.lambda.util.player.copyPlayer
7068
import com.lambda.util.player.gamemode
7169
import com.lambda.util.world.WorldUtils.isLoaded
7270
import com.lambda.util.world.raycast.RayCastUtils.blockResult
71+
import kotlinx.coroutines.Dispatchers
72+
import kotlinx.coroutines.async
73+
import kotlinx.coroutines.awaitAll
74+
import kotlinx.coroutines.runBlocking
7375
import net.minecraft.block.BlockState
7476
import net.minecraft.block.FallingBlock
7577
import net.minecraft.block.OperatorBlock
@@ -86,7 +88,6 @@ import net.minecraft.item.Item
8688
import net.minecraft.item.ItemPlacementContext
8789
import net.minecraft.item.ItemStack
8890
import net.minecraft.item.ItemUsageContext
89-
import net.minecraft.item.Items
9091
import net.minecraft.registry.tag.ItemTags.DIAMOND_TOOL_MATERIALS
9192
import net.minecraft.registry.tag.ItemTags.GOLD_TOOL_MATERIALS
9293
import net.minecraft.registry.tag.ItemTags.IRON_TOOL_MATERIALS
@@ -109,28 +110,34 @@ object BuildSimulator {
109110
rotation: RotationConfig = TaskFlowModule.rotation,
110111
inventory: InventoryConfig = TaskFlowModule.inventory,
111112
build: BuildConfig = TaskFlowModule.build,
112-
) = runSafe {
113-
structure.entries.flatMap { (pos, target) ->
114-
val preProcessing = target.getProcessingInfo(pos) ?: return@flatMap emptySet()
115-
checkRequirements(pos, target, build).let {
116-
if (it.isEmpty()) return@let
117-
return@flatMap it
118-
}
119-
checkPostProcessResults(pos, eye, preProcessing, target, interactionConfig, build.placing, rotation, inventory).let {
120-
if (it.isEmpty()) return@let
121-
return@flatMap it
122-
}
123-
checkPlaceResults(pos, eye, preProcessing, target, build.placing, interactionConfig, rotation, inventory).let {
124-
if (it.isEmpty()) return@let
125-
return@flatMap it
126-
}
127-
checkBreakResults(pos, eye, preProcessing, build.breaking, interactionConfig, rotation, inventory, build).let {
128-
if (it.isEmpty()) return@let
129-
return@flatMap it
130-
}
131-
warn("Nothing matched $pos $target")
132-
emptySet()
133-
}.toSet()
113+
): Set<BuildResult> = runSafe {
114+
runBlocking(Dispatchers.Default) {
115+
structure.entries
116+
.map { (pos, target) ->
117+
async {
118+
val preProcessing = target.getProcessingInfo(pos) ?: return@async emptySet()
119+
120+
checkRequirements(pos, target, build).let { results ->
121+
if (results.isNotEmpty()) return@async results
122+
}
123+
checkPostProcessResults(pos, eye, preProcessing, target, interactionConfig, build.placing, rotation, inventory).let { results ->
124+
if (results.isNotEmpty()) return@async results
125+
}
126+
checkPlaceResults(pos, eye, preProcessing, target, build.placing, interactionConfig, rotation, inventory).let { results ->
127+
if (results.isNotEmpty()) return@async results
128+
}
129+
checkBreakResults(pos, eye, preProcessing, build.breaking, interactionConfig, rotation, inventory, build).let { results ->
130+
if (results.isNotEmpty()) return@async results
131+
}
132+
133+
warn("Nothing matched $pos $target")
134+
emptySet()
135+
}
136+
}
137+
.awaitAll()
138+
.flatMap { it }
139+
.toSet()
140+
}
134141
} ?: emptySet()
135142

136143
private fun SafeContext.checkRequirements(

0 commit comments

Comments
 (0)