@@ -56,7 +56,7 @@ import com.lambda.interaction.request.breaking.BreakManager.breaks
5656import com.lambda.interaction.request.breaking.BreakManager.canAccept
5757import com.lambda.interaction.request.breaking.BreakManager.checkForCancels
5858import com.lambda.interaction.request.breaking.BreakManager.initNewBreak
59- import com.lambda.interaction.request.breaking.BreakManager.maxInstantBreaksThisTick
59+ import com.lambda.interaction.request.breaking.BreakManager.maxBreaksThisTick
6060import com.lambda.interaction.request.breaking.BreakManager.processNewBreak
6161import com.lambda.interaction.request.breaking.BreakManager.processRequest
6262import com.lambda.interaction.request.breaking.BreakManager.simulateAbandoned
@@ -94,6 +94,7 @@ import net.minecraft.util.math.BlockPos
9494import net.minecraft.util.math.Box
9595import net.minecraft.world.BlockView
9696import kotlin.math.max
97+ import kotlin.math.min
9798
9899object BreakManager : RequestHandler<BreakRequest>(
99100 0 ,
@@ -150,8 +151,8 @@ object BreakManager : RequestHandler<BreakRequest>(
150151 private val rotated get() = rotationRequest?.done != false
151152
152153 private var breakCooldown = 0
153- var instantBreaksThisTick = 0
154- private var maxInstantBreaksThisTick = 0
154+ var breaksThisTick = 0
155+ private var maxBreaksThisTick = 0
155156
156157 private var breaks = mutableListOf<BreakContext >()
157158
@@ -178,7 +179,7 @@ object BreakManager : RequestHandler<BreakRequest>(
178179 }
179180 activeRequest = null
180181 breaks = mutableListOf ()
181- instantBreaksThisTick = 0
182+ breaksThisTick = 0
182183 }
183184
184185 listen<WorldEvent .BlockUpdate .Server >(priority = Int .MIN_VALUE ) { event ->
@@ -292,7 +293,7 @@ object BreakManager : RequestHandler<BreakRequest>(
292293 * @see processRequest
293294 */
294295 override fun SafeContext.handleRequest (request : BreakRequest ) {
295- if (activeRequest != null || PlaceManager .activeThisTick || InteractionManager .activeThisTick || request.contexts.isEmpty()) return
296+ if (activeRequest != null || request.contexts.isEmpty()) return
296297
297298 activeRequest = request
298299 processRequest(request)
@@ -309,6 +310,8 @@ object BreakManager : RequestHandler<BreakRequest>(
309310 * @see updateBreakProgress
310311 */
311312 private fun SafeContext.processRequest (breakRequest : BreakRequest ? ) {
313+ if (PlaceManager .activeThisTick || InteractionManager .activeThisTick) return
314+
312315 breakRequest?.let { request ->
313316 logger.debug(" Processing request" , breakRequest)
314317 if (request.fresh) populateFrom(request)
@@ -344,15 +347,15 @@ object BreakManager : RequestHandler<BreakRequest>(
344347 if (activeRequest != null ) logger.debug(" Clearing active request" , activeRequest)
345348 activeRequest = null
346349 }
347- if (instantBreaksThisTick > 0 || activeInfos.isNotEmpty()) {
350+ if (breaksThisTick > 0 || activeInfos.isNotEmpty()) {
348351 activeThisTick = true
349352 }
350353 }
351354
352355 /* *
353356 * Filters the requests [BreakContext]s, and iterates over the [breakInfos] collection looking for matches
354357 * in positions. If a match is found, the [BreakInfo] is updated with the new context. Otherwise, the break is cancelled.
355- * The [instantBreaks] and [breaks] collections are then populated with the new appropriate contexts, and the [maxInstantBreaksThisTick ]
358+ * The [instantBreaks] and [breaks] collections are then populated with the new appropriate contexts, and the [maxBreaksThisTick ]
356359 * value is set.
357360 *
358361 * @see canAccept
@@ -363,39 +366,48 @@ object BreakManager : RequestHandler<BreakRequest>(
363366 // Sanitize the new breaks
364367 val newBreaks = request.contexts
365368 .distinctBy { it.blockPos }
369+ .filter { canAccept(it) }
366370 .toMutableList()
367371
368372 // Update the current break infos
369373 breakInfos
370374 .filterNotNull()
371375 .forEach { info ->
372- newBreaks.find { ctx -> ctx.blockPos == info.context.blockPos && canAccept(ctx) }?.let { ctx ->
373- if ((! info.updatedThisTick || info.type == RedundantSecondary ) || info.abandoned) {
374- logger.debug(" Updating info" , info, ctx)
375- if (info.type == RedundantSecondary )
376- info.request.onStart?.invoke(info.context.blockPos)
377- else if (info.abandoned) {
378- info.abandoned = false
379- info.request.onStart?.invoke(info.context.blockPos)
380- } else
381- info.request.onUpdate?.invoke(info.context.blockPos)
382-
383- info.updateInfo(ctx, request)
376+ newBreaks
377+ .find { ctx -> ctx.blockPos == info.context.blockPos }
378+ ?.let { ctx ->
379+ if ((! info.updatedThisTick || info.type == RedundantSecondary ) || info.abandoned) {
380+ logger.debug(" Updating info" , info, ctx)
381+ if (info.type == RedundantSecondary )
382+ info.request.onStart?.invoke(info.context.blockPos)
383+ else if (info.abandoned) {
384+ info.abandoned = false
385+ info.request.onStart?.invoke(info.context.blockPos)
386+ } else
387+ info.request.onUpdate?.invoke(info.context.blockPos)
388+
389+ info.updateInfo(ctx, request)
390+ }
391+ newBreaks.remove(ctx)
392+ return @forEach
384393 }
385- newBreaks.remove(ctx)
386- return @forEach
387- }
388394 }
389395
396+ val breakConfig = request.config
397+
390398 breaks = newBreaks
391399 .sortedByDescending { it.instantBreak }
400+ .take(
401+ min(
402+ breakConfig.maxPendingBreaks - pendingBreakCount,
403+ request.build.maxPendingInteractions - request.pendingInteractions.size
404+ ).coerceAtLeast(0 )
405+ )
392406 .toMutableList()
393407
394408 logger.debug(" ${breaks.size} unprocessed breaks" )
395409
396- val breakConfig = request.config
397- val pendingLimit = (breakConfig.maxPendingBreaks - pendingBreakCount).coerceAtLeast(0 )
398- maxInstantBreaksThisTick = breakConfig.breaksPerTick.coerceAtMost(pendingLimit)
410+ maxBreaksThisTick = breakConfig.breaksPerTick
399411 }
400412
401413 /* *
@@ -404,11 +416,6 @@ object BreakManager : RequestHandler<BreakRequest>(
404416 private fun SafeContext.canAccept (newCtx : BreakContext ): Boolean {
405417 if (activeInfos.none { it.context.blockPos == newCtx.blockPos } && isPosBlocked(newCtx.blockPos)) return false
406418
407- if (newCtx.instantBreak && instantBreaksThisTick > maxInstantBreaksThisTick) return false
408-
409- if (! currentStackSelection.filterStack(player.inventory.getStack(newCtx.hotbarIndex)))
410- return false
411-
412419 val blockState = blockState(newCtx.blockPos)
413420 val hardness = newCtx.cachedState.getHardness(world, newCtx.blockPos)
414421
@@ -462,7 +469,8 @@ object BreakManager : RequestHandler<BreakRequest>(
462469 */
463470 private fun SafeContext.processNewBreak (request : BreakRequest ): Boolean {
464471 breaks.forEach { ctx ->
465- if (! canAccept(ctx)) return @forEach
472+ if (breaksThisTick >= maxBreaksThisTick) return false
473+ if (! currentStackSelection.filterStack(player.inventory.getStack(ctx.hotbarIndex))) return @forEach
466474
467475 initNewBreak(ctx, request) ? : return false
468476 breaks.remove(ctx)
@@ -582,7 +590,7 @@ object BreakManager : RequestHandler<BreakRequest>(
582590 info.startPending()
583591 }
584592 }
585- instantBreaksThisTick ++
593+ breaksThisTick ++
586594 info.nullify()
587595 }
588596
@@ -808,7 +816,6 @@ object BreakManager : RequestHandler<BreakRequest>(
808816 info.vanillaInstantBreakable = progress >= 1 && swapped
809817 onBlockBreak(info)
810818 if (! info.vanillaInstantBreakable) breakCooldown = info.breakConfig.breakDelay
811- instantBreaksThisTick++
812819 } else {
813820 logger.debug(" Starting break" , info)
814821 info.apply {
0 commit comments