@@ -33,6 +33,7 @@ import com.lambda.interaction.material.ContainerSelection.Companion.selectContai
3333import com.lambda.interaction.material.StackSelection.Companion.select
3434import com.lambda.interaction.material.container.ContainerManager.containerWithMaterial
3535import com.lambda.interaction.material.container.MaterialContainer
36+ import com.lambda.interaction.request.rotating.Rotation
3637import com.lambda.interaction.request.rotating.Rotation.Companion.rotation
3738import com.lambda.interaction.request.rotating.Rotation.Companion.rotationTo
3839import com.lambda.interaction.request.rotating.RotationManager
@@ -78,9 +79,6 @@ class PlaceChecker @SimCheckerDsl private constructor(simInfo: SimInfo)
7879 : SimChecker <PlaceResult >(), Dependable ,
7980 ISimInfo by simInfo
8081{
81- lateinit var resultState: BlockState
82- var rot = RotationManager .serverRotation
83-
8482 private val swapStack by lazy {
8583 runSafeAutomated {
8684 val optimalStack = targetState.getStack(pos)
@@ -104,8 +102,6 @@ class PlaceChecker @SimCheckerDsl private constructor(simInfo: SimInfo)
104102 }
105103 private val blockItem get() = swapStack.blockItem
106104
107- private var currentDirIsValid = false
108-
109105 override fun asDependent (buildResult : BuildResult ) =
110106 PlaceResult .Dependency (pos, buildResult)
111107
@@ -120,8 +116,6 @@ class PlaceChecker @SimCheckerDsl private constructor(simInfo: SimInfo)
120116 }
121117
122118 private suspend fun AutomatedSafeContext.checkPlacements (): Boolean {
123- if (targetState.isEmpty()) return false
124-
125119 supervisorScope {
126120 withContext(Dispatchers .Default ) {
127121 preProcessing.sides.map { side ->
@@ -155,7 +149,7 @@ class PlaceChecker @SimCheckerDsl private constructor(simInfo: SimInfo)
155149 val misses = mutableSetOf<Vec3d >()
156150 val reachSq = buildConfig.interactReach.pow(2 )
157151
158- // ToDo: For each hand and sneak or not?
152+ // ToDo: For each hand
159153 val fakePlayer = copyPlayer(player).apply {
160154 this .rotation = RotationManager .serverRotation
161155 if (testBlockState.block::class in BlockUtils .interactionBlocks) {
@@ -249,22 +243,23 @@ class PlaceChecker @SimCheckerDsl private constructor(simInfo: SimInfo)
249243 return
250244 }
251245
252- if (! simRotation(fakePlayer, checkedHit, context)) return
246+ val rotatePlaceTest = simRotatePlace(fakePlayer, checkedHit, context) ? : return
247+ if (! rotatePlaceTest.isValid) return
253248
254249 val rotationRequest = if (placeConfig.axisRotate) {
255- lookInDirection(PlaceDirection .fromRotation(rot ))
256- } else lookAt(rot , 0.001 )
250+ lookInDirection(PlaceDirection .fromRotation(rotatePlaceTest.rotation ))
251+ } else lookAt(rotatePlaceTest.rotation , 0.001 )
257252
258253 val placeContext = PlaceContext (
259254 hitResult,
260255 RotationRequest (rotationRequest, this @PlaceChecker),
261256 swapStack.inventoryIndex,
262257 pos,
263258 state,
264- resultState,
259+ rotatePlaceTest.placeTest. resultState,
265260 fakePlayer.isSneaking,
266261 false ,
267- currentDirIsValid,
262+ rotatePlaceTest. currentDirIsValid,
268263 this @PlaceChecker
269264 )
270265
@@ -274,61 +269,64 @@ class PlaceChecker @SimCheckerDsl private constructor(simInfo: SimInfo)
274269 return
275270 }
276271
277- private fun SafeContext.simRotation (
272+ private fun SafeContext.simRotatePlace (
278273 fakePlayer : ClientPlayerEntity ,
279274 checkedHit : CheckedHit ,
280275 context : ItemPlacementContext
281- ): Boolean {
282- currentDirIsValid = if (testPlaceState(context) != PlaceTestResult .Success ) {
283- if (! placeConfig.rotateForPlace) return false
284- else false
285- } else true
276+ ): RotatePlaceTest ? {
277+ val currentDirIsValid = testPlaceState(context).let { currentDirTest ->
278+ if (currentDirTest.testResult != PlaceTestResult .Success ) {
279+ if (! placeConfig.rotateForPlace)
280+ return RotatePlaceTest (currentDirTest, false , fakePlayer.rotation)
281+ else false
282+ } else true
283+ }
286284
287285 if (! placeConfig.axisRotate) {
288286 fakePlayer.rotation = checkedHit.targetRotation
289- if (testPlaceState(context) != PlaceTestResult .Success ) return false
290- rot = fakePlayer.rotation
291- return true
287+ return RotatePlaceTest (testPlaceState(context), currentDirIsValid, fakePlayer.rotation)
292288 }
293289
294290 fakePlayer.rotation = player.rotation
295- if ( testPlaceState(context) == PlaceTestResult . Success ) {
296- rot = fakePlayer.rotation
297- return true
291+ testPlaceState(context). let { playerRotTest ->
292+ if (playerRotTest.testResult == PlaceTestResult . Success )
293+ return RotatePlaceTest (playerRotTest, currentDirIsValid, fakePlayer.rotation)
298294 }
299295
300296 PlaceDirection .entries.asReversed().forEachIndexed direction@{ index, direction ->
301297 fakePlayer.rotation = direction.rotation
302- when (testPlaceState(context)) {
298+ val axisRotateTest = testPlaceState(context)
299+ when (axisRotateTest.testResult) {
303300 PlaceTestResult .BlockedByEntity -> return @direction
304301
305302 PlaceTestResult .NoIntegrity -> {
306303 if (index != PlaceDirection .entries.lastIndex) return @direction
307- return false
304+ return RotatePlaceTest (axisRotateTest, currentDirIsValid, fakePlayer.rotation)
308305 }
309306
310- else -> {
311- rot = fakePlayer.rotation
312- return true
313- }
307+ else -> return RotatePlaceTest (axisRotateTest, currentDirIsValid, fakePlayer.rotation)
314308 }
315309 }
316310
317- return true
311+ return null
318312 }
319313
320- private fun SafeContext.testPlaceState (context : ItemPlacementContext ): PlaceTestResult {
321- resultState = blockItem.getPlacementState(context) ? : run {
314+ private fun SafeContext.testPlaceState (context : ItemPlacementContext ): PlaceTest {
315+ val resultState = blockItem.getPlacementState(context) ? : run {
322316 result(PlaceResult .BlockedByEntity (pos))
323- return PlaceTestResult .BlockedByEntity
317+ return PlaceTest (state, PlaceTestResult .BlockedByEntity )
324318 }
325319
326320 return if (! targetState.matches(resultState, pos, preProcessing.ignore)) {
327321 result(PlaceResult .NoIntegrity (pos, resultState, context, (targetState as ? TargetState .State )?.blockState))
328- PlaceTestResult .NoIntegrity
329- } else PlaceTestResult .Success
322+ PlaceTest (resultState, PlaceTestResult .NoIntegrity )
323+ } else PlaceTest (resultState, PlaceTestResult .Success )
330324 }
331325
326+ private data class RotatePlaceTest (val placeTest : PlaceTest , val currentDirIsValid : Boolean , val rotation : Rotation ) {
327+ val isValid = placeTest.testResult == PlaceTestResult .Success
328+ }
329+ private data class PlaceTest (val resultState : BlockState , val testResult : PlaceTestResult )
332330 private enum class PlaceTestResult {
333331 Success ,
334332 BlockedByEntity ,
0 commit comments