1717
1818package com.lambda.interaction.request.breaking
1919
20+ import com.lambda.context.SafeContext
2021import com.lambda.interaction.construction.context.BreakContext
2122import com.lambda.interaction.request.ActionInfo
2223import com.lambda.interaction.request.LogContext
@@ -26,10 +27,10 @@ import com.lambda.interaction.request.breaking.BreakInfo.BreakType.Rebreak
2627import com.lambda.interaction.request.breaking.BreakInfo.BreakType.RedundantSecondary
2728import com.lambda.interaction.request.breaking.BreakInfo.BreakType.Secondary
2829import com.lambda.interaction.request.breaking.BreakManager.calcBreakDelta
30+ import com.lambda.threading.runSafeAutomated
2931import com.lambda.util.Describable
3032import com.lambda.util.NamedEnum
3133import net.minecraft.client.network.ClientPlayerEntity
32- import net.minecraft.client.network.ClientPlayerInteractionManager
3334import net.minecraft.client.world.ClientWorld
3435import net.minecraft.entity.ItemEntity
3536import net.minecraft.item.ItemStack
@@ -112,19 +113,19 @@ data class BreakInfo(
112113 progressedThisTick = false
113114 }
114115
116+ context(safeContext: SafeContext )
115117 fun setBreakingTextureStage (
116118 player : ClientPlayerEntity ,
117119 world : ClientWorld ,
118- stage : Int = getBreakTextureProgress(player, world)
119- ) {
120- world.setBlockBreakingInfo(player.id, context.blockPos, stage)
121- }
120+ stage : Int = getBreakTextureProgress()
121+ ) = world.setBlockBreakingInfo(player.id, context.blockPos, stage)
122122
123- private fun getBreakTextureProgress ( player : ClientPlayerEntity , world : ClientWorld ): Int {
124- val swapMode = breakConfig.swapMode
123+ context(safeContext : SafeContext )
124+ private fun getBreakTextureProgress (): Int = with (safeContext) {
125125 val item =
126- if (swapMode.isEnabled() && swapMode != BreakConfig .SwapMode .Start ) swapStack else player.mainHandStack
127- val breakDelta = context.cachedState.calcBreakDelta(player, world, context.blockPos, breakConfig, item)
126+ if (breakConfig.swapMode.isEnabled() && breakConfig.swapMode != BreakConfig .SwapMode .Start ) swapStack
127+ else player.mainHandStack
128+ val breakDelta = request.runSafeAutomated { context.cachedState.calcBreakDelta(context.blockPos, item) }
128129 val progress = (breakDelta * breakingTicks) / (getBreakThreshold() + (breakDelta * breakConfig.fudgeFactor))
129130 return if (progress > 0.0f ) (progress * 10.0f ).toInt().coerceAtMost(9 ) else - 1
130131 }
@@ -136,23 +137,29 @@ data class BreakInfo(
136137 else -> 1.0f
137138 }
138139
139- fun startBreakPacket (world : ClientWorld , interaction : ClientPlayerInteractionManager ) =
140- breakPacket(Action .START_DESTROY_BLOCK , world, interaction)
141-
142- fun stopBreakPacket (world : ClientWorld , interaction : ClientPlayerInteractionManager ) =
143- breakPacket(Action .STOP_DESTROY_BLOCK , world, interaction)
144-
145- fun abortBreakPacket (world : ClientWorld , interaction : ClientPlayerInteractionManager ) =
146- breakPacket(Action .ABORT_DESTROY_BLOCK , world, interaction)
147-
148- private fun breakPacket (action : Action , world : ClientWorld , interaction : ClientPlayerInteractionManager ) =
149- interaction.sendSequencedPacket(world) { sequence: Int ->
150- PlayerActionC2SPacket (
151- action,
152- context.blockPos,
153- context.result.side,
154- sequence
155- )
140+ context(_: SafeContext )
141+ fun startBreakPacket () =
142+ breakPacket(Action .START_DESTROY_BLOCK )
143+
144+ context(_: SafeContext )
145+ fun stopBreakPacket () =
146+ breakPacket(Action .STOP_DESTROY_BLOCK )
147+
148+ context(_: SafeContext )
149+ fun abortBreakPacket () =
150+ breakPacket(Action .ABORT_DESTROY_BLOCK )
151+
152+ context(safeContext: SafeContext )
153+ private fun breakPacket (action : Action ) =
154+ with (safeContext) {
155+ interaction.sendSequencedPacket(world) { sequence: Int ->
156+ PlayerActionC2SPacket (
157+ action,
158+ context.blockPos,
159+ context.result.side,
160+ sequence
161+ )
162+ }
156163 }
157164
158165 override fun getLogContextBuilder (): LogContextBuilder .() -> Unit = {
0 commit comments