@@ -54,10 +54,8 @@ import com.lambda.interaction.request.breaking.BreakManager.breaks
5454import com.lambda.interaction.request.breaking.BreakManager.canAccept
5555import com.lambda.interaction.request.breaking.BreakManager.checkForCancels
5656import com.lambda.interaction.request.breaking.BreakManager.initNewBreak
57- import com.lambda.interaction.request.breaking.BreakManager.instantBreaks
5857import com.lambda.interaction.request.breaking.BreakManager.maxBreaksThisTick
59- import com.lambda.interaction.request.breaking.BreakManager.performInstantBreaks
60- import com.lambda.interaction.request.breaking.BreakManager.processNewBreaks
58+ import com.lambda.interaction.request.breaking.BreakManager.processNewBreak
6159import com.lambda.interaction.request.breaking.BreakManager.processRequest
6260import com.lambda.interaction.request.breaking.BreakManager.simulateAbandoned
6361import com.lambda.interaction.request.breaking.BreakManager.updateBreakProgress
@@ -94,7 +92,6 @@ import net.minecraft.util.math.Box
9492import net.minecraft.world.BlockView
9593import kotlin.math.max
9694
97- // ToDo: Fix root cause of breaks becoming redundant while the actual state is empty
9895object BreakManager : RequestHandler<BreakRequest>(
9996 0 ,
10097 TickEvent .Pre ,
@@ -155,7 +152,6 @@ object BreakManager : RequestHandler<BreakRequest>(
155152 private var maxBreaksThisTick = 0
156153
157154 private var breaks = mutableListOf<BreakContext >()
158- private var instantBreaks = mutableListOf<BreakContext >()
159155
160156 var lastPosStarted: BlockPos ? = null
161157 set(value) {
@@ -185,7 +181,6 @@ object BreakManager : RequestHandler<BreakRequest>(
185181 }
186182 activeRequest = null
187183 breaks = mutableListOf ()
188- instantBreaks = mutableListOf ()
189184 breaksThisTick = 0
190185 }
191186
@@ -313,37 +308,45 @@ object BreakManager : RequestHandler<BreakRequest>(
313308 * or the player needs to swap to a different hotbar slot.
314309 *
315310 * @see performInstantBreaks
316- * @see processNewBreaks
311+ * @see processNewBreak
317312 * @see updateBreakProgress
318313 */
319314 private fun SafeContext.processRequest (breakRequest : BreakRequest ? ) {
320315 breakRequest?.let { request ->
321316 if (request.fresh) populateFrom(request)
322317 }
323318
324- repeat(2 ) {
325- breakRequest?.let { request ->
326- if (performInstantBreaks(request)) {
327- processNewBreaks(request)
328- }
329- }
319+ var noNew = false
320+ var noProgression = false
321+
322+ while (true ) {
323+ noNew = breakRequest?.let { request ->
324+ ! processNewBreak(request)
325+ } != false
330326
331327 // Reversed so that the breaking order feels natural to the user as the primary break is always the
332328 // last break to be started
333- if (handlePreProcessing()) {
329+ noProgression = if (handlePreProcessing()) {
334330 activeInfos
335- .filter { it.updatedThisTick }
331+ .filter { it.updatedThisTick && it.shouldProgress }
336332 .asReversed()
337- .forEach { info ->
338- if (info.shouldProgress)
339- updateBreakProgress(info)
333+ .run {
334+ if (isEmpty()) {
335+ true
336+ } else {
337+ forEach { info ->
338+ updateBreakProgress(info)
339+ }
340+ false
341+ }
340342 }
341- }
342- }
343+ } else true
343344
344- if (instantBreaks.isEmpty() && breaks.isEmpty()) {
345- activeRequest = null
345+ if (noNew && noProgression) break
346346 }
347+
348+ if (breaks.isEmpty()) activeRequest = null
349+
347350 if (breaksThisTick > 0 || activeInfos.isNotEmpty()) {
348351 activeThisTick = true
349352 }
@@ -384,12 +387,8 @@ object BreakManager : RequestHandler<BreakRequest>(
384387 }
385388 }
386389
387- instantBreaks = newBreaks
388- .filter { it.instantBreak }
389- .toMutableList()
390-
391390 breaks = newBreaks
392- .filter { ! it.instantBreak }
391+ .sortedByDescending { it.instantBreak }
393392 .toMutableList()
394393
395394 val breakConfig = request.config
@@ -451,50 +450,22 @@ object BreakManager : RequestHandler<BreakRequest>(
451450 return true
452451 }
453452
454- /* *
455- * Attempts to break as many [BreakContext]'s as possible from the [instantBreaks] collection within this tick.
456- *
457- * @return false if a break could not be performed.
458- */
459- private fun SafeContext.performInstantBreaks (request : BreakRequest ): Boolean {
460- val iterator = instantBreaks.iterator()
461- while (iterator.hasNext()) {
462- if (breaksThisTick + 1 > maxBreaksThisTick) return false
463-
464- val ctx = iterator.next()
465-
466- if (! canAccept(ctx)) continue
467-
468- rotationRequest = if (request.config.rotateForBreak) ctx.rotation.submit(false ) else null
469- if (! rotated || tickStage !in request.config.breakStageMask) return false
470-
471- val breakInfo = initNewBreak(ctx, request) ? : return false
472- if (! handlePreProcessing()) return false
473-
474- updateBreakProgress(breakInfo)
475- iterator.remove()
476- }
477- return true
478- }
479-
480453 /* *
481454 * Attempts to start breaking as many [BreakContext]'s from the [breaks] collection as possible.
482455 *
483456 * @return false if a context cannot be started or the maximum active breaks has been reached.
484457 *
485458 * @see initNewBreak
486459 */
487- private fun SafeContext.processNewBreaks (request : BreakRequest ): Boolean {
488- val iterator = breaks.iterator()
489- while (iterator.hasNext()) {
490- val ctx = iterator.next()
491-
492- if (! canAccept(ctx)) continue
460+ private fun SafeContext.processNewBreak (request : BreakRequest ): Boolean {
461+ breaks.forEach { ctx ->
462+ if (! canAccept(ctx)) return @forEach
493463
494464 initNewBreak(ctx, request) ? : return false
495- iterator.remove()
465+ breaks.remove(ctx)
466+ return true
496467 }
497- return true
468+ return false
498469 }
499470
500471 /* *
@@ -517,10 +488,7 @@ object BreakManager : RequestHandler<BreakRequest>(
517488 } else return null
518489 }
519490
520- if (! primaryInfo.breaking) {
521- secondaryBreak = breakInfo.apply { type = Secondary }
522- return secondaryBreak
523- }
491+ if (! primaryInfo.breaking) return null
524492
525493 secondaryBreak = primaryInfo.apply { type = Secondary }
526494 secondaryBreak?.stopBreakPacket(world, interaction)
0 commit comments