Skip to content

Commit 37786be

Browse files
committed
swap mode setting
1 parent 4b6f410 commit 37786be

File tree

6 files changed

+41
-9
lines changed

6 files changed

+41
-9
lines changed

common/src/main/kotlin/com/lambda/config/groups/BreakSettings.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,24 @@ class BreakSettings(
3737
// General
3838
override val breakMode by c.setting("Break Mode", BreakMode.Packet) { vis() && page == Page.General }
3939
override val sorter by c.setting("Sorter", SortMode.Closest, "The order in which breaks are performed") { vis() && page == Page.General }
40-
override val breakThreshold by c.setting("Break Threshold", 0.70f, 0.1f..1.0f, 0.01f, "The break amount at which the block is considered broken") { vis() && page == Page.General }
4140
override val reBreak by c.setting("ReBreak", true, "Re-breaks blocks after they've been broken once") { vis() && page == Page.General }
4241

4342
// Double break
4443
override val doubleBreak by c.setting("Double Break", true, "Allows breaking two blocks at once") { vis() && page == Page.General }
4544
override val unsafeCancels by c.setting("Unsafe Cancels", true, "Allows cancelling block breaking even if the server might continue breaking sever side, potentially causing unexpected state changes") { vis() && page == Page.General }
4645

4746
// Fixes / Delays
47+
override val breakThreshold by c.setting("Break Threshold", 0.70f, 0.1f..1.0f, 0.01f, "The break amount at which the block is considered broken") { vis() && page == Page.General }
4848
override val fudgeFactor by c.setting("Fudge Factor", 1, 0..5, 1, "The amount of ticks to give double, aka secondary breaks extra for the server to recognise the break") { vis() && page == Page.General }
4949
// override val desyncFix by c.setting("Desync Fix", false, "Predicts if the players breaking will be slowed next tick as block break packets are processed using the players next position") { vis() && page == Page.General }
5050
override val breakDelay by c.setting("Break Delay", 0, 0..6, 1, "The delay between breaking blocks", " ticks") { vis() && page == Page.General }
5151

5252
// Timing
5353
override val breakStageMask by c.setting("Break Stage Mask", setOf(TickEvent.Input.Post, TickEvent.Player.Post), "The sub-tick timing at which break actions can be performed") { vis() && page == Page.General }
5454

55+
// Swap
56+
override val swapMode by c.setting("Swap Mode", BreakConfig.SwapMode.End, "Decides when to swap to the best suited tool when breaking a block") { vis() && page == Page.General }
57+
5558
// Swing
5659
override val swing by c.setting("Swing Mode", SwingMode.Constant, "The times at which to swing the players hand") { vis() && page == Page.General }
5760
override val swingType by c.setting("Break Swing Type", BuildConfig.SwingType.Vanilla, "The style of swing") { vis() && page == Page.General && swing != SwingMode.None }

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ data class BreakContext(
7777
withState(cachedState, blockPos, sideColor, result.side)
7878
}
7979

80-
fun requestDependencies(breakRequest: BreakRequest, minKeepTicks: Int = 0): Boolean =
80+
fun requestSwap(breakRequest: BreakRequest, minKeepTicks: Int = 0): Boolean =
8181
HotbarRequest(
8282
hotbarIndex,
8383
breakRequest.hotbar,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ object BuildSimulator {
814814
breakContext.apply {
815815
hotbarIndex = player.hotbar.indexOf(swapStack)
816816
itemSelection = stackSelection
817-
instantBreak = instantBreakable(state, pos, swapStack, breaking.breakThreshold)
817+
instantBreak = instantBreakable(state, pos, if (breaking.swapMode.isEnabled()) swapStack else player.mainHandStack, breaking.breakThreshold)
818818
}
819819
acc.add(BreakResult.Break(pos, breakContext))
820820
return acc

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ interface BreakConfig : RequestConfig {
3939

4040
val breakStageMask: Set<Event>
4141

42+
val swapMode: SwapMode
43+
4244
val swing: SwingMode
4345
val swingType: BuildConfig.SwingType
4446

@@ -90,6 +92,16 @@ interface BreakConfig : RequestConfig {
9092
Random
9193
}
9294

95+
enum class SwapMode {
96+
None,
97+
Start,
98+
End,
99+
StartAndEnd,
100+
Constant;
101+
102+
fun isEnabled() = this != None
103+
}
104+
93105
enum class SwingMode {
94106
Constant,
95107
StartAndEnd,

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import net.minecraft.entity.ItemEntity
2727
import net.minecraft.entity.player.PlayerEntity
2828
import net.minecraft.network.packet.c2s.play.PlayerActionC2SPacket
2929
import net.minecraft.network.packet.c2s.play.PlayerActionC2SPacket.Action
30+
import net.minecraft.world.WorldView
3031

3132
data class BreakInfo(
3233
override var context: BreakContext,
@@ -93,6 +94,21 @@ data class BreakInfo(
9394
item = null
9495
}
9596

97+
fun shouldSwap(player: ClientPlayerEntity, world: WorldView): Boolean {
98+
val item = player.inventory.getStack(context.hotbarIndex)
99+
val breakDelta = context.cachedState.calcItemBlockBreakingDelta(player, world, context.blockPos, item)
100+
val breakProgress = breakDelta * ((breakingTicks + 1) - breakConfig.fudgeFactor).let {
101+
if (isSecondary) it + 1 else it
102+
}
103+
return when (breakConfig.swapMode) {
104+
BreakConfig.SwapMode.None -> false
105+
BreakConfig.SwapMode.Start -> !breaking
106+
BreakConfig.SwapMode.End -> breakProgress >= getBreakThreshold()
107+
BreakConfig.SwapMode.StartAndEnd -> !breaking || breakProgress >= getBreakThreshold()
108+
BreakConfig.SwapMode.Constant -> true
109+
}
110+
}
111+
96112
fun setBreakingTextureStage(
97113
player: ClientPlayerEntity,
98114
world: ClientWorld,

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ object BreakManager : RequestHandler<BreakRequest>(
217217
world,
218218
info.context.blockPos,
219219
info.breakConfig,
220-
if (!info.isRedundant) activeStack else null
220+
if (!info.isRedundant && info.breakConfig.swapMode.isEnabled()) activeStack else null
221221
).toDouble()
222222
val currentDelta = info.breakingTicks * breakDelta
223223

@@ -298,6 +298,8 @@ object BreakManager : RequestHandler<BreakRequest>(
298298
}
299299
}
300300
.also {
301+
if (breakInfos.none { it?.shouldSwap(player, world) == true }) return@also
302+
301303
it.firstOrNull()?.let { info ->
302304
secondaryBreak?.let { secondary ->
303305
val breakDelta = secondary.context.cachedState.calcBreakDelta(
@@ -307,9 +309,9 @@ object BreakManager : RequestHandler<BreakRequest>(
307309
secondary.breakConfig,
308310
player.inventory.getStack(secondary.context.hotbarIndex)
309311
)
310-
val breakAmount = breakDelta * (secondary.breakingTicks + 1)
312+
val breakAmount = breakDelta * ((secondary.breakingTicks - secondary.breakConfig.fudgeFactor) + 1)
311313
val minKeepTicks = if (breakAmount >= 1.0f) 1 else 0
312-
if (!info.context.requestDependencies(info.request, minKeepTicks)) {
314+
if (!info.context.requestSwap(info.request, minKeepTicks)) {
313315
secondary.serverBreakTicks = 0
314316
return@run
315317
}
@@ -318,7 +320,7 @@ object BreakManager : RequestHandler<BreakRequest>(
318320
}
319321
return@also
320322
}
321-
if (!info.context.requestDependencies(info.request, 0)) return@run
323+
if (!info.context.requestSwap(info.request, 0)) return@run
322324
}
323325
}
324326
.asReversed()
@@ -458,13 +460,12 @@ object BreakManager : RequestHandler<BreakRequest>(
458460

459461
if (!canAccept(ctx)) continue
460462

461-
if (!ctx.requestDependencies(request)) return false
463+
if (request.build.breaking.swapMode.isEnabled() && !ctx.requestSwap(request)) return false
462464
rotationRequest = if (request.config.rotateForBreak) ctx.rotation.submit(false) else null
463465
if (!rotated || tickStage !in request.config.breakStageMask) return false
464466

465467
val breakInfo = initNewBreak(ctx, request) ?: return false
466468
updateBreakProgress(breakInfo)
467-
breaksThisTick++
468469
iterator.remove()
469470
}
470471
return true

0 commit comments

Comments
 (0)