1717
1818package com.lambda.interaction.request.breaking
1919
20- import com.lambda.config.groups.BuildConfig
2120import com.lambda.context.SafeContext
2221import com.lambda.event.events.TickEvent
2322import com.lambda.event.events.WorldEvent
2423import com.lambda.event.listener.SafeListener.Companion.listen
2524import com.lambda.interaction.construction.context.BreakContext
26- import com.lambda.interaction.request.RequestConfig
2725import com.lambda.interaction.request.RequestHandler
2826import com.lambda.interaction.request.breaking.BreakConfig.BreakConfirmationMode
2927import com.lambda.interaction.request.breaking.BreakConfig.BreakMode
28+ import com.lambda.interaction.request.rotation.RotationManager
29+ import com.lambda.interaction.request.rotation.RotationManager.onRotate
3030import com.lambda.module.modules.client.TaskFlowModule
3131import com.lambda.util.BlockUtils.blockState
3232import com.lambda.util.BlockUtils.calcItemBlockBreakingDelta
@@ -44,44 +44,47 @@ import net.minecraft.network.packet.c2s.play.PlayerActionC2SPacket
4444import net.minecraft.sound.SoundCategory
4545
4646object BreakManager : RequestHandler<BreakRequest>() {
47- var buildConfig: BuildConfig = TaskFlowModule .build
48- var primaryBreakingInfo: BreakInfo ?
47+ private var primaryBreakingInfo: BreakInfo ?
4948 get() = breakingInfos[0 ]
5049 set(value) { breakingInfos[0 ] = value }
51- var secondaryBreakingInfo: BreakInfo ?
50+ private var secondaryBreakingInfo: BreakInfo ?
5251 get() = breakingInfos[1 ]
5352 set(value) { breakingInfos[1 ] = value }
54- val breakingInfos = arrayOfNulls<BreakInfo >(2 )
53+ private val breakingInfos = arrayOfNulls<BreakInfo >(2 )
5554
56- val pendingInteractions = LimitedDecayQueue <BreakInfo >(
57- buildConfig. maxPendingInteractions, buildConfig .interactionTimeout * 50L
55+ private val pendingInteractions = LimitedDecayQueue <BreakInfo >(
56+ TaskFlowModule .build. maxPendingInteractions, TaskFlowModule .build .interactionTimeout * 50L
5857 ) { info(" ${it::class .simpleName} at ${it.context.expectedPos.toShortString()} timed out" ); it.nullify() }
5958
6059 init {
61- listen<TickEvent .Pre >(Int .MIN_VALUE ) {
60+ listen<TickEvent .Pre >(Int .MIN_VALUE , alwaysListen = true ) {
6261 if (updateRequest(true ) { true }) {
6362 currentRequest?.let { request ->
6463 request.contexts.forEach { requestCtx ->
6564 if (requestCtx == null ) return @forEach
6665 if (! canAccept(requestCtx)) return @forEach
6766
68- primaryBreakingInfo?.let { primaryInfo ->
69- if (! buildConfig .breakSettings.doubleBreak) return @let
70- if (primaryInfo.startedWithSecondary) return @let
67+ primaryBreakingInfo?.let primaryInfo@ { primaryInfo ->
68+ if (! primaryInfo .breakSettings.doubleBreak) return @primaryInfo
69+ if (primaryInfo.startedWithSecondary) return @primaryInfo
7170 secondaryBreakingInfo = BreakInfo .SecondaryBreakInfo (requestCtx, request.onBreak)
7271 } ? : run {
7372 primaryBreakingInfo = BreakInfo .PrimaryBreakInfo (requestCtx, request.onBreak)
73+ pendingInteractions.setMaxSize(requestCtx.buildConfig.maxPendingInteractions)
74+ pendingInteractions.setDecayTime(requestCtx.buildConfig.interactionTimeout * 50L )
7475 }
7576 }
7677 }
7778 }
7879
79- for (it in breakingInfos.reversed()) {
80+ for (info in breakingInfos.reversed()) {
8081 if (interaction.blockBreakingCooldown > 0 ) {
8182 interaction.blockBreakingCooldown--
8283 break
8384 }
84- it?.let { info ->
85+
86+ info?.let { info ->
87+ if (info.breakSettings.rotateForBreak && ! info.context.rotation.done) return @listen
8588 if (pendingInteractions.contains(info)) return @let
8689 updateBlockBreakingProgress(info, player.mainHandStack)
8790 if (info is BreakInfo .SecondaryBreakInfo )
@@ -90,13 +93,26 @@ object BreakManager : RequestHandler<BreakRequest>() {
9093 }
9194 }
9295
96+ onRotate {
97+ run forLoop@ {
98+ breakingInfos.forEach { info ->
99+ info?.let { info ->
100+ if (! info.breakSettings.rotateForBreak) return @let
101+ val rotate = info.context.rotation
102+ RotationManager .registerRequest(info.context.rotationConfig, rotate)
103+ return @forLoop
104+ }
105+ }
106+ }
107+ }
108+
93109 listen<WorldEvent .BlockUpdate .Server >(alwaysListen = true ) { event ->
94110 var breakBlock = false
95111 val info = pendingInteractions
96112 .firstOrNull { it.context.expectedPos == event.pos }
97- ?.also {
98- pendingInteractions.remove(it )
99- if (buildConfig .breakSettings.breakConfirmation == BreakConfirmationMode .AwaitThenBreak )
113+ ?.also { pending ->
114+ pendingInteractions.remove(pending )
115+ if (pending .breakSettings.breakConfirmation == BreakConfirmationMode .AwaitThenBreak )
100116 breakBlock = true
101117 }
102118 ? : breakingInfos
@@ -109,7 +125,7 @@ object BreakManager : RequestHandler<BreakRequest>() {
109125 info.nullify()
110126
111127 if (! info.context.targetState.matches(event.newState, event.pos, world)) {
112- this @BreakManager.warn(" Update at ${event.pos.toShortString()} was rejected with ${event.newState} instead of ${info.context.targetState} " )
128+ this @BreakManager.warn(" Break at ${event.pos.toShortString()} was rejected with ${event.newState} instead of ${info.context.targetState} " )
113129 return @listen
114130 }
115131 if (breakBlock) {
@@ -119,19 +135,10 @@ object BreakManager : RequestHandler<BreakRequest>() {
119135 }
120136 }
121137
122- fun canAccept (ctx : BreakContext ) =
138+ private fun canAccept (ctx : BreakContext ) =
123139 pendingInteractions.none { it.context.expectedPos == ctx.expectedPos }
124140 && breakingInfos.none { info -> info?.context?.expectedPos == ctx.expectedPos }
125141
126- override fun updateRequest (
127- keepIfNull : Boolean ,
128- filter : (Map .Entry <RequestConfig <BreakRequest >, BreakRequest >) -> Boolean
129- ): Boolean {
130- val updatedCurrentRequest = super .updateRequest(keepIfNull, filter)
131- buildConfig = currentRequest?.primaryContext?.buildConfig ? : TaskFlowModule .build
132- return updatedCurrentRequest
133- }
134-
135142 private fun SafeContext.updateBlockBreakingProgress (info : BreakInfo , item : ItemStack ): Boolean {
136143 val ctx = info.context
137144 val hitResult = ctx.result
@@ -216,7 +223,7 @@ object BreakManager : RequestHandler<BreakRequest>() {
216223 onBlockBreak(info)
217224 PlayerActionC2SPacket (PlayerActionC2SPacket .Action .START_DESTROY_BLOCK , ctx.expectedPos, ctx.result.side, sequence)
218225 }
219- interaction.blockBreakingCooldown = buildConfig .breakSettings.breakDelay
226+ interaction.blockBreakingCooldown = info .breakSettings.breakDelay
220227 return true
221228 }
222229 if (info.breaking) return false
@@ -262,7 +269,7 @@ object BreakManager : RequestHandler<BreakRequest>() {
262269 }
263270
264271 private fun SafeContext.onBlockBreak (info : BreakInfo ) {
265- when (info.context.buildConfig. breakSettings.breakConfirmation) {
272+ when (info.breakSettings.breakConfirmation) {
266273 BreakConfirmationMode .None -> {
267274 destroyBlock(info)
268275 info.onBreak()
@@ -318,6 +325,8 @@ object BreakManager : RequestHandler<BreakRequest>() {
318325 var breakingTicks = 0
319326 var soundsCooldown = 0.0f
320327 var startedWithSecondary = false
328+ val buildConfig = context.buildConfig
329+ val breakSettings = buildConfig.breakSettings
321330
322331 fun getBreakTextureProgress (player : PlayerEntity , world : ClientWorld ): Int {
323332 val breakDelta = context.checkedState.calcItemBlockBreakingDelta(
@@ -327,18 +336,17 @@ object BreakManager : RequestHandler<BreakRequest>() {
327336 player.mainHandStack
328337 )
329338
330- val progress = (breakDelta * breakingTicks) / buildConfig. breakSettings.breakThreshold
339+ val progress = (breakDelta * breakingTicks) / breakSettings.breakThreshold
331340 return if (progress > 0.0f ) (progress * 10.0f ).toInt() else - 1
332341 }
333342
334- open fun getBreakThreshold () =
335- context.buildConfig.breakSettings.breakThreshold
343+ open fun getBreakThreshold () = breakSettings.breakThreshold
336344
337345 open fun nullify () {}
338346
339347 class PrimaryBreakInfo (
340348 ctx : BreakContext ,
341- onBreak : () -> Unit
349+ onBreak : () -> Unit ,
342350 ) : BreakInfo(ctx, onBreak) {
343351 override fun nullify () {
344352 primaryBreakingInfo = null
@@ -347,7 +355,7 @@ object BreakManager : RequestHandler<BreakRequest>() {
347355
348356 class SecondaryBreakInfo (
349357 ctx : BreakContext ,
350- onBreak : () -> Unit
358+ onBreak : () -> Unit ,
351359 ) : BreakInfo(ctx, onBreak) {
352360 override fun getBreakThreshold () =
353361 1.0f
0 commit comments