Skip to content

Commit e463725

Browse files
committed
selection and speed checks rather than slot comparison for new breaks
1 parent 674db53 commit e463725

File tree

5 files changed

+60
-18
lines changed

5 files changed

+60
-18
lines changed

common/src/main/kotlin/com/lambda/interaction/construction/context/BreakContext.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package com.lambda.interaction.construction.context
2020
import com.lambda.context.SafeContext
2121
import com.lambda.graphics.renderer.esp.DirectionMask
2222
import com.lambda.graphics.renderer.esp.DirectionMask.exclude
23+
import com.lambda.interaction.material.StackSelection
2324
import com.lambda.interaction.request.breaking.BreakConfig
2425
import com.lambda.interaction.request.breaking.BreakRequest
2526
import com.lambda.interaction.request.hotbar.HotbarManager
@@ -37,6 +38,7 @@ data class BreakContext(
3738
override val result: BlockHitResult,
3839
override val rotation: RotationRequest,
3940
override var hotbarIndex: Int,
41+
var itemSelection: StackSelection,
4042
var instantBreak: Boolean,
4143
override var cachedState: BlockState,
4244
val sortMode: BreakConfig.SortMode

common/src/main/kotlin/com/lambda/interaction/construction/simulation/BuildSimulator.kt

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import com.lambda.interaction.material.StackSelection.Companion.selectStack
3838
import com.lambda.interaction.material.container.ContainerManager.containerWithMaterial
3939
import com.lambda.interaction.material.container.MaterialContainer
4040
import com.lambda.interaction.request.breaking.BreakConfig
41+
import com.lambda.interaction.request.breaking.BreakManager
4142
import com.lambda.interaction.request.inventory.InventoryConfig
4243
import com.lambda.interaction.request.placing.PlaceConfig
4344
import com.lambda.interaction.request.rotating.Rotation.Companion.rotation
@@ -687,6 +688,7 @@ object BuildSimulator {
687688
hit.blockResult?.blockPos == pos
688689
}
689690

691+
// ToDo: Move this to a location where more of the context parameters can be properly set
690692
/* the player is buried inside the block */
691693
if (boxes.any { it.contains(eye) }) {
692694
currentCast?.blockResult?.let { blockHit ->
@@ -697,6 +699,7 @@ object BuildSimulator {
697699
blockHit,
698700
rotationRequest,
699701
player.inventory.selectedSlot,
702+
StackSelection.EVERYTHING.select(),
700703
instantBreakable(state, pos, breaking.breakThreshold),
701704
state,
702705
breaking.sorter
@@ -746,7 +749,15 @@ object BuildSimulator {
746749
val rotationRequest = RotationRequest(target, rotation)
747750
val instant = instantBreakable(state, pos, breaking.breakThreshold)
748751

749-
val breakContext = BreakContext(blockHit, rotationRequest, player.inventory.selectedSlot, instant, state, breaking.sorter)
752+
val breakContext = BreakContext(
753+
blockHit,
754+
rotationRequest,
755+
player.inventory.selectedSlot,
756+
StackSelection.EVERYTHING.select(),
757+
instant,
758+
state,
759+
breaking.sorter
760+
)
750761

751762
if (gamemode.isCreative) {
752763
acc.add(BreakResult.Break(pos, breakContext))
@@ -784,6 +795,15 @@ object BuildSimulator {
784795
val swapStack = swapCandidates.map { it.matchingStacks(stackSelection) }
785796
.asSequence()
786797
.flatten()
798+
.filter { newStack ->
799+
BreakManager.currentStackSelection.filterStack(newStack) &&
800+
BreakManager.currentContext?.run {
801+
val currentStack = player.inventory.getStack(hotbarIndex)
802+
val currentSpeed = cachedState.calcItemBlockBreakingDelta(player, world, blockPos, currentStack)
803+
val newSpeed = cachedState.calcItemBlockBreakingDelta(player, world, blockPos, newStack)
804+
newSpeed >= currentSpeed
805+
} != false
806+
}
787807
.let { containerStacks ->
788808
var bestStack = player.mainHandStack
789809
var bestBreakDelta = state.calcItemBlockBreakingDelta(player, world, pos, bestStack)
@@ -797,8 +817,11 @@ object BuildSimulator {
797817
bestStack
798818
}
799819

800-
breakContext.hotbarIndex = player.hotbar.indexOf(swapStack)
801-
breakContext.instantBreak = instantBreakable(state, pos, swapStack, breaking.breakThreshold)
820+
breakContext.apply {
821+
hotbarIndex = player.hotbar.indexOf(swapStack)
822+
itemSelection = stackSelection
823+
instantBreak = instantBreakable(state, pos, swapStack, breaking.breakThreshold)
824+
}
802825
acc.add(BreakResult.Break(pos, breakContext))
803826
return acc
804827
}

common/src/main/kotlin/com/lambda/interaction/request/breaking/BreakManager.kt

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ import com.lambda.interaction.construction.context.BreakContext
3636
import com.lambda.interaction.construction.result.BreakResult
3737
import com.lambda.interaction.construction.simulation.BuildSimulator.simulate
3838
import com.lambda.interaction.construction.verify.TargetState
39+
import com.lambda.interaction.material.StackSelection
40+
import com.lambda.interaction.material.StackSelection.Companion.select
3941
import com.lambda.interaction.request.ManagerUtils.isPosBlocked
4042
import com.lambda.interaction.request.PositionBlocking
4143
import com.lambda.interaction.request.RequestHandler
@@ -102,6 +104,14 @@ object BreakManager : RequestHandler<BreakRequest>(
102104
get() = breakInfos[1]
103105
set(value) { breakInfos[1] = value }
104106
private val breakInfos = arrayOfNulls<BreakInfo>(2)
107+
val currentStackSelection
108+
get() = breakInfos
109+
.lastOrNull {
110+
it?.breakConfig?.doubleBreak == true || it?.isSecondary == true
111+
}?.context?.itemSelection
112+
?: StackSelection.EVERYTHING.select()
113+
val currentContext
114+
get() = breakInfos.lastOrNull()?.context
105115

106116
private val pendingBreakCount get() = breakInfos.count { it != null } + pendingActions.size
107117
override val blockedPositions
@@ -294,7 +304,7 @@ object BreakManager : RequestHandler<BreakRequest>(
294304
} else 0
295305
if (!info.context.requestDependencies(info.request, minKeepTicks)) return@run
296306
if (tickStage !in info.breakConfig.breakStageMask) return@forEach
297-
if ((!rotated && info.isPrimary)) return@run
307+
if (!rotated && info.isPrimary) return@run
298308

299309
updateBreakProgress(info)
300310
}
@@ -323,6 +333,7 @@ object BreakManager : RequestHandler<BreakRequest>(
323333
.simulate(player.eyePos, interact, rotation, inventory, build)
324334
.asSequence()
325335
.filterIsInstance<BreakResult.Break>()
336+
.filter { canAccept(it.context) }
326337
.sorted()
327338
.let { sim ->
328339
info.updateInfo(sim.firstOrNull()?.context ?: return@forEach)
@@ -354,7 +365,7 @@ object BreakManager : RequestHandler<BreakRequest>(
354365
// Sanitize the new breaks
355366
val newBreaks = request.contexts
356367
.distinctBy { it.blockPos }
357-
.filter { ctx -> canAccept(ctx, request.config) }
368+
.filter { ctx -> canAccept(ctx) }
358369
.let { acceptable ->
359370
acceptable.firstOrNull()?.let { first ->
360371
acceptable.filter { it.hotbarIndex == first.hotbarIndex }
@@ -398,19 +409,23 @@ object BreakManager : RequestHandler<BreakRequest>(
398409
/**
399410
* @return if the break context can be accepted.
400411
*/
401-
private fun SafeContext.canAccept(ctx: BreakContext, breakConfig: BreakConfig): Boolean {
402-
if (breakInfos.none { it?.context?.blockPos == ctx.blockPos } && isPosBlocked(ctx.blockPos)) return false
412+
private fun SafeContext.canAccept(newCtx: BreakContext): Boolean {
413+
if (breakInfos.none { it?.context?.blockPos == newCtx.blockPos } && isPosBlocked(newCtx.blockPos)) return false
403414

404-
if (breakConfig.doubleBreak) {
405-
breakInfos
406-
.firstOrNull { it != null && !it.isRedundant }
407-
?.let { info ->
408-
if (ctx.hotbarIndex != info.context.hotbarIndex) return false
409-
}
410-
}
415+
breakInfos
416+
.lastOrNull { it != null && !it.isRedundant && it.breakConfig.doubleBreak }
417+
?.let { info ->
418+
val currentCtx = info.context
419+
val currentStack = player.inventory.getStack(currentCtx.hotbarIndex)
420+
val currentSpeed = currentCtx.cachedState.calcItemBlockBreakingDelta(player, world, currentCtx.blockPos, currentStack)
421+
val newStack = player.inventory.getStack(newCtx.hotbarIndex)
422+
val newSpeed = currentCtx.cachedState.calcItemBlockBreakingDelta(player, world, info.context.blockPos, newStack)
423+
if (!currentCtx.itemSelection.filterStack(newStack) || newSpeed < currentSpeed)
424+
return false
425+
}
411426

412-
val blockState = blockState(ctx.blockPos)
413-
val hardness = ctx.cachedState.getHardness(world, ctx.blockPos)
427+
val blockState = blockState(newCtx.blockPos)
428+
val hardness = newCtx.cachedState.getHardness(world, newCtx.blockPos)
414429

415430
return blockState.isNotEmpty && hardness != 600f && hardness != -1f
416431
}
@@ -469,7 +484,7 @@ object BreakManager : RequestHandler<BreakRequest>(
469484

470485
val breakInfo = BreakInfo(requestCtx, Primary, request)
471486
primaryBreak?.let { primaryInfo ->
472-
if (!breakInfo.breakConfig.doubleBreak || secondaryBreak != null) {
487+
if (!primaryInfo.breakConfig.doubleBreak || secondaryBreak != null) {
473488
if (!primaryInfo.updatedThisTick && tickStage in primaryInfo.breakConfig.breakStageMask) {
474489
primaryInfo.cancelBreak()
475490
return@let

common/src/main/kotlin/com/lambda/interaction/request/breaking/BrokenBlockHandler.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ object BrokenBlockHandler : PostActionHandler<BreakInfo>() {
5656
val loaded = world.isChunkLoaded(ChunkSectionPos.getSectionCoord(pos.x), ChunkSectionPos.getSectionCoord(pos.z))
5757
if (!loaded) return@let
5858

59-
if (!info.broken) warn("${info::class.simpleName} at ${info.context.blockPos.toShortString()} timed out")
59+
if (!info.broken) warn("${info::class.simpleName} at ${info.context.blockPos.toShortString()} timed out with cached state ${info.context.cachedState}")
6060
else if (!TaskFlowModule.ignoreItemDropWarnings) warn("${info::class.simpleName}'s item drop at ${info.context.blockPos.toShortString()} timed out")
6161

6262
if (!info.broken && info.breakConfig.breakConfirmation != BreakConfirmationMode.AwaitThenBreak) {

common/src/main/kotlin/com/lambda/module/modules/player/FastBreak.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import com.lambda.event.events.PlayerEvent
2222
import com.lambda.event.listener.SafeListener.Companion.listen
2323
import com.lambda.interaction.construction.context.BreakContext
2424
import com.lambda.interaction.construction.context.BuildContext
25+
import com.lambda.interaction.material.StackSelection.Companion.select
2526
import com.lambda.interaction.request.breaking.BreakRequest
2627
import com.lambda.interaction.request.rotating.Rotation.Companion.rotation
2728
import com.lambda.interaction.request.rotating.RotationRequest
@@ -59,6 +60,7 @@ object FastBreak : Module(
5960
hitResult,
6061
RotationRequest(lookAt(player.rotation), TaskFlowModule.rotation),
6162
player.inventory.selectedSlot,
63+
player.mainHandStack.select(),
6264
state.calcBlockBreakingDelta(player, world, pos) >= buildConfig.breaking.breakThreshold,
6365
state,
6466
buildConfig.breaking.sorter

0 commit comments

Comments
 (0)