Skip to content

Commit 39d6b05

Browse files
committed
move stack selection checks to after simulation and improve processing
1 parent 11e39ab commit 39d6b05

File tree

7 files changed

+25
-34
lines changed

7 files changed

+25
-34
lines changed

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

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -130,19 +130,15 @@ class InteractSim private constructor(simInfo: InteractSimInfo)
130130
buildConfig.pointSelection.select(validHits)?.let { checkedHit ->
131131
val hitResult = checkedHit.hit.blockResult ?: return
132132

133-
val swapStack = getSwapStack(item)
134-
135133
if (!placing) {
136-
if (swapStack == null) return
137-
138134
val interactContext = InteractContext(
139135
hitResult,
140136
rotationRequest { rotation(checkedHit.rotation) },
141-
swapStack.inventoryIndex,
137+
getSwapStack(item, supervisorScope)?.inventoryIndex ?: return,
142138
pos,
143139
state,
144140
expectedState,
145-
false,
141+
preProcessing.info,
146142
fakePlayer.isSneaking,
147143
true,
148144
this@InteractSim
@@ -188,21 +184,14 @@ class InteractSim private constructor(simInfo: InteractSimInfo)
188184
lookInDirection(PlaceDirection.fromRotation(rotatePlaceTest.rotation))
189185
else rotatePlaceTest.rotation
190186

191-
if (swapStack == null) return
192-
if (!swapStack.item.isEnabled(world.enabledFeatures)) {
193-
result(InteractResult.BlockFeatureDisabled(pos, swapStack))
194-
supervisorScope.cancel()
195-
return
196-
}
197-
198187
val interactContext = InteractContext(
199188
hitResult,
200189
rotationRequest { rotation(rotationRequest) },
201-
swapStack.inventoryIndex,
190+
getSwapStack(item, supervisorScope)?.inventoryIndex ?: return,
202191
pos,
203192
state,
204193
rotatePlaceTest.resultState,
205-
true,
194+
preProcessing.info,
206195
fakePlayer.isSneaking,
207196
rotatePlaceTest.currentDirIsValid,
208197
this@InteractSim
@@ -214,7 +203,12 @@ class InteractSim private constructor(simInfo: InteractSimInfo)
214203
return
215204
}
216205

217-
private fun AutomatedSafeContext.getSwapStack(item: Item?): ItemStack? {
206+
private fun AutomatedSafeContext.getSwapStack(item: Item?, supervisorScope: CoroutineScope): ItemStack? {
207+
if (item?.isEnabled(world.enabledFeatures) == false) {
208+
result(InteractResult.BlockFeatureDisabled(pos, item))
209+
supervisorScope.cancel()
210+
return null
211+
}
218212
val stackSelection = item?.select()
219213
?: StackSelection.selectStack(0, sorter = compareByDescending { it.inventoryIndex == player.inventory.selectedSlot })
220214
val containerSelection = selectContainer { ofAnyType(MaterialContainer.Rank.Hotbar) }
@@ -309,9 +303,8 @@ class InteractSim private constructor(simInfo: InteractSimInfo)
309303
(pos.y - (hitbox.maxY - hitbox.minY)).floorToInt(),
310304
pos.y
311305
)
312-
}
313-
.flatten()
314-
.forEach { support ->
306+
}.flatten()
307+
.forEach { support ->
315308
sim(support, blockState(support), TargetState.Empty)
316309
}
317310
}

src/main/kotlin/com/lambda/interaction/construction/simulation/context/InteractContext.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package com.lambda.interaction.construction.simulation.context
1919

2020
import com.lambda.context.Automated
2121
import com.lambda.graphics.mc.TransientRegionESP
22+
import com.lambda.interaction.construction.simulation.processing.PreProcessingInfo
2223
import com.lambda.interaction.managers.hotbar.HotbarRequest
2324
import com.lambda.interaction.managers.interacting.InteractRequest
2425
import com.lambda.interaction.managers.rotating.RotationRequest
@@ -35,7 +36,7 @@ data class InteractContext(
3536
override val blockPos: BlockPos,
3637
override var cachedState: BlockState,
3738
override val expectedState: BlockState,
38-
val placing: Boolean,
39+
val preProcessingInfo: PreProcessingInfo,
3940
val sneak: Boolean,
4041
val currentDirIsValid: Boolean = false,
4142
private val automated: Automated

src/main/kotlin/com/lambda/interaction/construction/simulation/processing/ProcessorRegistry.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import com.lambda.context.SafeContext
2222
import com.lambda.core.Loadable
2323
import com.lambda.interaction.construction.simulation.SimDsl
2424
import com.lambda.interaction.construction.verify.TargetState
25+
import com.lambda.util.BlockUtils.matches
2526
import com.lambda.util.reflections.getInstances
2627
import net.minecraft.block.BlockState
2728
import net.minecraft.item.ItemStack
@@ -162,12 +163,12 @@ object ProcessorRegistry : Loadable {
162163
with(processor) { preProcess(state, expectedState) }
163164
}
164165
} else {
165-
val postProcessable = propertyPostProcessors.any { processor ->
166-
processor.acceptsState(state, expectedState).also {
166+
propertyPostProcessors.forEach { processor ->
167+
if (processor.acceptsState(state, expectedState)) {
167168
with(processor) { preProcess(state, expectedState) }
168169
}
169170
}
170-
if (!postProcessable) return@run null
171+
if (!state.matches(targetState, ignore)) return@run null
171172
}
172173
}
173174
complete()

src/main/kotlin/com/lambda/interaction/construction/simulation/processing/preprocessors/property/placement/post/StandardInteractPreProcessor.kt renamed to src/main/kotlin/com/lambda/interaction/construction/simulation/processing/preprocessors/property/placement/post/StandardInteractPostProcessor.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ import com.lambda.interaction.construction.simulation.processing.ProcessorRegist
2222
import com.lambda.interaction.construction.simulation.processing.PropertyPostProcessor
2323
import net.minecraft.block.BlockState
2424

25-
object StandardInteractPreProcessor : PropertyPostProcessor {
25+
// Collected using reflections and then accessed from a collection in ProcessorRegistry
26+
@Suppress("unused")
27+
object StandardInteractPostProcessor : PropertyPostProcessor {
2628
override fun acceptsState(state: BlockState, targetState: BlockState) =
2729
standardInteractProperties.any {
2830
it in targetState && state.get(it) != targetState.get(it)

src/main/kotlin/com/lambda/interaction/construction/simulation/result/results/InteractResult.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package com.lambda.interaction.construction.simulation.result.results
1919

2020
import baritone.api.pathing.goals.GoalBlock
2121
import baritone.api.pathing.goals.GoalInverted
22-
import com.lambda.graphics.esp.ShapeScope
2322
import com.lambda.graphics.mc.TransientRegionESP
2423
import com.lambda.interaction.construction.simulation.context.InteractContext
2524
import com.lambda.interaction.construction.simulation.result.BuildResult
@@ -30,8 +29,8 @@ import com.lambda.interaction.construction.simulation.result.Navigable
3029
import com.lambda.interaction.construction.simulation.result.Rank
3130
import net.minecraft.block.BlockState
3231
import net.minecraft.entity.Entity
32+
import net.minecraft.item.Item
3333
import net.minecraft.item.ItemPlacementContext
34-
import net.minecraft.item.ItemStack
3534
import net.minecraft.util.math.BlockPos
3635
import net.minecraft.util.math.Box
3736
import net.minecraft.util.math.Direction
@@ -168,7 +167,7 @@ sealed class InteractResult : BuildResult() {
168167
*/
169168
data class BlockFeatureDisabled(
170169
override val pos: BlockPos,
171-
val itemStack: ItemStack,
170+
val item: Item,
172171
) : InteractResult() {
173172
override val rank = Rank.PlaceBlockFeatureDisabled
174173
}

src/main/kotlin/com/lambda/interaction/managers/interacting/InteractManager.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ object InteractManager : Manager<InteractRequest>(
148148
if (!validSneak(player)) return
149149
if (tickStage !in interactConfig.tickStageMask) return
150150

151-
val actionResult = if (ctx.placing) placeBlock(ctx, request, Hand.MAIN_HAND)
151+
val actionResult = if (ctx.preProcessingInfo.placing) placeBlock(ctx, request, Hand.MAIN_HAND)
152152
else interaction.interactBlock(player, Hand.MAIN_HAND, ctx.hitResult)
153153
if (actionResult.isAccepted && interactConfig.swing) {
154154
swingHand(interactConfig.swingType, Hand.MAIN_HAND)

src/main/kotlin/com/lambda/interaction/managers/interacting/InteractedBlockHandler.kt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import com.lambda.config.AutomationConfig.Companion.DEFAULT
2121
import com.lambda.config.AutomationConfig.Companion.DEFAULT.managerDebugLogs
2222
import com.lambda.event.events.WorldEvent
2323
import com.lambda.event.listener.SafeListener.Companion.listen
24-
import com.lambda.interaction.construction.simulation.processing.ProcessorRegistry
2524
import com.lambda.interaction.managers.PostActionHandler
2625
import com.lambda.interaction.managers.interacting.InteractManager.placeSound
2726
import com.lambda.threading.runSafe
@@ -49,11 +48,7 @@ object InteractedBlockHandler : PostActionHandler<InteractInfo>() {
4948
.firstOrNull { it.context.blockPos == event.pos }
5049
?.let { pending ->
5150
if (!pending.context.expectedState.matches(event.newState)) {
52-
if (pending.context.cachedState.matches(
53-
event.newState,
54-
ProcessorRegistry.postProcessedProperties
55-
)
56-
) {
51+
if (pending.context.cachedState.matches(event.newState, pending.context.preProcessingInfo.ignore)) {
5752
pending.context.cachedState = event.newState
5853
return@listen
5954
}

0 commit comments

Comments
 (0)