@@ -28,7 +28,11 @@ import com.lambda.event.events.UpdateManagerEvent
2828import com.lambda.event.events.WorldEvent
2929import com.lambda.event.listener.SafeListener.Companion.listen
3030import com.lambda.event.listener.UnsafeListener.Companion.listenUnsafe
31+ import com.lambda.interaction.construction.blueprint.Blueprint.Companion.toStructure
32+ import com.lambda.interaction.construction.blueprint.StaticBlueprint.Companion.toBlueprint
3133import com.lambda.interaction.construction.context.BreakContext
34+ import com.lambda.interaction.construction.result.BreakResult
35+ import com.lambda.interaction.construction.simulation.BuildSimulator.simulate
3236import com.lambda.interaction.construction.verify.TargetState
3337import com.lambda.interaction.request.PositionBlocking
3438import com.lambda.interaction.request.Priority
@@ -51,6 +55,7 @@ import com.lambda.util.player.gamemode
5155import com.lambda.util.player.swingHand
5256import net.minecraft.block.BlockState
5357import net.minecraft.block.OperatorBlock
58+ import net.minecraft.client.network.ClientPlayerEntity
5459import net.minecraft.client.sound.PositionedSoundInstance
5560import net.minecraft.client.sound.SoundInstance
5661import net.minecraft.client.world.ClientWorld
@@ -84,8 +89,7 @@ object BreakManager : RequestHandler<BreakRequest>(), PositionBlocking {
8489 get() = breakingInfos.mapNotNull { it?.context?.expectedPos } + pendingBreaks.map { it.context.expectedPos }
8590
8691 private var rotation: RotationRequest ? = null
87- private val validRotation
88- get() = rotation?.done ? : true
92+ private var validRotation = false
8993
9094 private var blockBreakingCooldown = 0
9195
@@ -109,6 +113,10 @@ object BreakManager : RequestHandler<BreakRequest>(), PositionBlocking {
109113 listen<TickEvent .Pre >(priority = Int .MIN_VALUE + 1 ) {
110114 preEvent()
111115
116+ breakingInfos.forEach {
117+ it?.simulate(player)
118+ }
119+
112120 if (updateRequest()) currentRequest?.let request@ { request ->
113121 if (isOnBreakCooldown()) {
114122 blockBreakingCooldown--
@@ -149,12 +157,11 @@ object BreakManager : RequestHandler<BreakRequest>(), PositionBlocking {
149157 }
150158
151159 requestRotate()
152- if (! validRotation) {
160+ if (! validRotation && rotation != null ) {
153161 postEvent()
154162 return @listen
155163 }
156164
157- // ToDo: dynamically update hotbarIndex as contexts are persistent and don't get updated by new requests each tick
158165 // Reversed so that the breaking order feels natural to the user as the primary break has to
159166 // be started after the secondary
160167 breakingInfos
@@ -169,6 +176,10 @@ object BreakManager : RequestHandler<BreakRequest>(), PositionBlocking {
169176 postEvent()
170177 }
171178
179+ listen<UpdateManagerEvent .Rotation .Post >(priority = Int .MIN_VALUE ) {
180+ validRotation = rotation?.done ? : true
181+ }
182+
172183 listen<WorldEvent .BlockUpdate .Server > { event ->
173184 pendingBreaks
174185 .firstOrNull { it.context.expectedPos == event.pos }
@@ -253,8 +264,7 @@ object BreakManager : RequestHandler<BreakRequest>(), PositionBlocking {
253264
254265 private fun requestRotate () {
255266 rotation = breakingInfos
256- .filterNotNull()
257- .firstOrNull { it.breakConfig.rotateForBreak }
267+ .firstOrNull { it?.breakConfig?.rotateForBreak == true }
258268 ?.let { info ->
259269 info.rotationConfig.request(RotationRequest (info.context.rotation, info.rotationConfig))
260270 }
@@ -497,13 +507,18 @@ object BreakManager : RequestHandler<BreakRequest>(), PositionBlocking {
497507 }
498508
499509 data class BreakInfo (
500- val context : BreakContext ,
510+ var context : BreakContext ,
501511 var type : BreakType ,
502512 val request : BreakRequest
503513 ) {
514+ // I hate this...
504515 val breakConfig = request.buildConfig.breakSettings
505516 val rotationConfig = request.rotationConfig
517+ private val interactionConfig = request.interactionConfig
518+ private val buildConfig = request.buildConfig
519+ private val inventoryConfig = request.inventoryConfig
506520 private val hotbarConfig = request.hotbarConfig
521+
507522 val pendingInteractionsList = request.pendingInteractionsList
508523 private val onBreak = request.onBreak
509524 private val onItemDrop = request.onItemDrop
@@ -543,6 +558,19 @@ object BreakManager : RequestHandler<BreakRequest>(), PositionBlocking {
543558 fun requestHotbarSwap () =
544559 hotbarConfig.request(HotbarRequest (context.hotbarIndex)).done
545560
561+ fun simulate (player : ClientPlayerEntity ) {
562+ val result = context.expectedPos
563+ .toStructure(context.targetState)
564+ .toBlueprint()
565+ .simulate(player.eyePos, interactionConfig, rotationConfig, inventoryConfig, buildConfig)
566+ .firstOrNull()
567+ ? : return
568+
569+ if (result is BreakResult .Break ) {
570+ context = result.context
571+ }
572+ }
573+
546574 fun getBreakTextureProgress (player : PlayerEntity , world : ClientWorld ): Int {
547575 val breakDelta = context.checkedState.calcItemBlockBreakingDelta(player, world, context.expectedPos, player.mainHandStack)
548576 val progress = (breakDelta * breakingTicks) / breakConfig.breakThreshold
0 commit comments