Skip to content

Commit 3aecab9

Browse files
committed
rely on hotbar settings for server swap
1 parent e8893d5 commit 3aecab9

File tree

5 files changed

+31
-66
lines changed

5 files changed

+31
-66
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ class BreakSettings(
5151
// Fixes / Delays
5252
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", visibility = vis).group(groupPath, Group.General)
5353
override val fudgeFactor by c.setting("Fudge Factor", 1, 0..5, 1, "The number of ticks to add to the break time, usually to account for server lag", visibility = vis).group(groupPath, Group.General)
54-
override val serverSwapTicks by c.setting("Server Swap", 2, 0..5, 1, "The number of ticks to give the server time to recognize the player attributes on the swapped item", " tick(s)", visibility = vis).group(groupPath, Group.General)
5554
// 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 }
5655
override val breakDelay by c.setting("Break Delay", 0, 0..6, 1, "The delay between breaking blocks", " tick(s)", visibility = vis).group(groupPath, Group.General)
5756

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ interface BreakConfig : RequestConfig {
3535

3636
val breakThreshold: Float
3737
val fudgeFactor: Int
38-
val serverSwapTicks: Int
3938
//ToDo: Needs a more advanced player simulation implementation to predict the next ticks onGround / submerged status
4039
// abstract val desyncFix: Boolean
4140
val breakDelay: Int

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

Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -143,18 +143,12 @@ object BreakManager : RequestHandler<BreakRequest>(
143143

144144
private var activeRequest: BreakRequest? = null
145145

146+
private var hotbarRequest: HotbarRequest? = null
147+
val swapped get() = hotbarRequest?.done != false
148+
146149
private var rotationRequest: RotationRequest? = null
147150
private val rotated get() = rotationRequest?.done != false
148151

149-
var currentStack: ItemStack = ItemStack.EMPTY
150-
set(value) {
151-
if (value != field)
152-
heldTicks = 0
153-
swappedThisTick = true
154-
field = value
155-
}
156-
var heldTicks = 0
157-
var swappedThisTick = false
158152
private var breakCooldown = 0
159153
var instantBreaksThisTick = 0
160154
private var maxInstantBreaksThisTick = 0
@@ -178,11 +172,6 @@ object BreakManager : RequestHandler<BreakRequest>(
178172
}
179173

180174
listen<TickEvent.Post>(priority = Int.MIN_VALUE) {
181-
if (!swappedThisTick) {
182-
currentStack = player.mainHandStack
183-
heldTicks++
184-
}
185-
swappedThisTick = false
186175
if (breakCooldown > 0) {
187176
breakCooldown--
188177
}
@@ -332,21 +321,18 @@ object BreakManager : RequestHandler<BreakRequest>(
332321

333322
// Reversed so that the breaking order feels natural to the user as the primary break is always the
334323
// last break to be started
335-
noProgression = if (handlePreProcessing()) {
324+
handlePreProcessing()
325+
noProgression =
336326
activeInfos
337327
.filter { it.updatedThisTick && it.shouldProgress }
338328
.asReversed()
339329
.run {
340-
if (isEmpty()) {
341-
true
342-
} else {
343-
forEach { info ->
344-
updateBreakProgress(info)
345-
}
330+
if (isEmpty()) true
331+
else {
332+
forEach { updateBreakProgress(it) }
346333
false
347334
}
348335
}
349-
} else true
350336

351337
if (noNew && noProgression) break
352338
}
@@ -426,7 +412,7 @@ object BreakManager : RequestHandler<BreakRequest>(
426412
return blockState.isNotEmpty && hardness != 600f && hardness != -1f
427413
}
428414

429-
private fun SafeContext.handlePreProcessing(): Boolean {
415+
private fun SafeContext.handlePreProcessing() {
430416
activeInfos
431417
.filter { it.updatedThisTick }
432418
.let { infos ->
@@ -437,35 +423,30 @@ object BreakManager : RequestHandler<BreakRequest>(
437423
rotation.submit(false)
438424
}
439425

440-
if (activeInfos.isEmpty()) return true
426+
if (activeInfos.isEmpty()) return
441427

442428
infos.forEach { it.updatePreProcessing(player, world) }
443429

444430
infos.firstOrNull()?.let { info ->
445431
infos.lastOrNull { it.swapInfo.swap && it.shouldProgress }?.let { last ->
446432
val minSwapTicks = max(info.swapInfo.minKeepTicks, last.swapInfo.minKeepTicks)
447-
val hotbarRequest = with(info) {
433+
hotbarRequest = with(info) {
448434
HotbarRequest(
449435
context.hotbarIndex,
450436
request.hotbar,
451-
request.hotbar.keepTicks.coerceAtLeast(minSwapTicks)
437+
request.hotbar.keepTicks.coerceAtLeast(minSwapTicks),
438+
request.hotbar.swapPause - 1
452439
).submit(false)
453440
}
454441
logger.debug("Submitted hotbar request", hotbarRequest)
455-
if (!hotbarRequest.done) {
456-
logger.warning("Hotbar request failed", hotbarRequest)
457-
return false
458-
}
459-
if (minSwapTicks > 0) {
460-
val alreadySwapped = swappedThisTick
461-
currentStack = info.swapStack
462-
if (!alreadySwapped) heldTicks++
463-
}
442+
return
464443
}
465444
}
466445
}
467446

468-
return true
447+
hotbarRequest = null
448+
449+
return
469450
}
470451

471452
/**
@@ -687,6 +668,8 @@ object BreakManager : RequestHandler<BreakRequest>(
687668
return true
688669
}
689670

671+
if (config.swapMode == BreakConfig.SwapMode.Constant && !swapped) return true
672+
690673
val hitResult = ctx.result
691674

692675
if (gamemode.isCreative && world.worldBorder.contains(ctx.blockPos)) {
@@ -739,7 +722,7 @@ object BreakManager : RequestHandler<BreakRequest>(
739722
}
740723

741724
val swing = config.swing
742-
if (progress >= info.getBreakThreshold() && info.swapInfo.validSwap) {
725+
if (progress >= info.getBreakThreshold() && swapped) {
743726
logger.success("Breaking", info)
744727
if (info.type == Primary) {
745728
onBlockBreak(info)
@@ -778,7 +761,7 @@ object BreakManager : RequestHandler<BreakRequest>(
778761
}
779762

780763
primaryBreak?.let { primary ->
781-
if (!handlePreProcessing()) return false
764+
handlePreProcessing()
782765
updateBreakProgress(primary)
783766
}
784767
return true
@@ -817,11 +800,12 @@ object BreakManager : RequestHandler<BreakRequest>(
817800

818801
val progress = blockState.calcBreakDelta(player, world, ctx.blockPos, info.breakConfig)
819802

820-
val instantBreakable = progress >= info.getBreakThreshold() && info.swapInfo.validSwap
821-
info.vanillaInstantBreakable = progress >= 1 && info.swapInfo.validSwap
803+
if (!swapped) return true
822804

805+
val instantBreakable = progress >= info.getBreakThreshold() && swapped
823806
if (instantBreakable) {
824807
logger.success("Instant breaking", info)
808+
info.vanillaInstantBreakable = progress >= 1 && swapped
825809
onBlockBreak(info)
826810
if (!info.vanillaInstantBreakable) breakCooldown = info.breakConfig.breakDelay
827811
instantBreaksThisTick++

src/main/kotlin/com/lambda/interaction/request/breaking/RebreakHandler.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ object RebreakHandler {
8989
val context = reBreak.context
9090
val breakDelta = context.cachedState.calcBreakDelta(player, world, context.blockPos, reBreak.breakConfig)
9191
val breakTicks = reBreak.breakingTicks - reBreak.breakConfig.fudgeFactor
92-
return@runSafe if (breakTicks * breakDelta >= reBreak.getBreakThreshold() && reBreak.swapInfo.validSwap) {
92+
return@runSafe if (breakTicks * breakDelta >= reBreak.getBreakThreshold() && BreakManager.swapped) {
9393
if (reBreak.breakConfig.breakConfirmation != BreakConfig.BreakConfirmationMode.AwaitThenBreak) {
9494
destroyBlock(reBreak)
9595
}

src/main/kotlin/com/lambda/interaction/request/breaking/SwapInfo.kt

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,38 +17,26 @@
1717

1818
package com.lambda.interaction.request.breaking
1919

20-
import com.lambda.Lambda.mc
2120
import com.lambda.interaction.request.LogContext
2221
import com.lambda.interaction.request.LogContext.Companion.LogContextBuilder
2322
import com.lambda.interaction.request.breaking.BreakInfo.BreakType.Primary
24-
import com.lambda.interaction.request.breaking.BreakInfo.BreakType.Rebreak
2523
import com.lambda.interaction.request.breaking.BreakManager.calcBreakDelta
26-
import com.lambda.interaction.request.breaking.BreakManager.currentStack
24+
import com.lambda.interaction.request.hotbar.HotbarConfig
2725
import com.lambda.module.modules.client.TaskFlowModule
2826
import net.minecraft.client.network.ClientPlayerEntity
29-
import net.minecraft.item.ItemStack
3027
import net.minecraft.world.BlockView
3128

3229
data class SwapInfo(
3330
private val type: BreakInfo.BreakType,
34-
private val breakConfig: BreakConfig = TaskFlowModule.build.breaking,
31+
private val hotbarConfig: HotbarConfig = TaskFlowModule.hotbar,
3532
val swap: Boolean = false,
3633
val minKeepTicks: Int = 0,
3734
) : LogContext {
38-
val validSwap
39-
get() = run {
40-
val serverSwapTicks = if (type == Primary || type == Rebreak) breakConfig.serverSwapTicks
41-
else breakConfig.serverSwapTicks.coerceAtLeast(3)
42-
43-
(mc.player?.mainHandStack?.heldTicks ?: return false) >= serverSwapTicks
44-
}
45-
4635
override fun getLogContextBuilder(): LogContextBuilder.() -> Unit = {
4736
group("Swap Info") {
4837
value("Type", type)
4938
value("Swap", swap)
5039
value("Min Keep Ticks", minKeepTicks)
51-
value("Valid Swap", validSwap)
5240
}
5341
}
5442

@@ -72,11 +60,11 @@ data class SwapInfo(
7260

7361
val minKeepTicks = run {
7462
if (type == Primary) {
75-
val swapTickProgress = breakDelta * (breakTicks + breakConfig.serverSwapTicks - 1).coerceAtLeast(1)
76-
if (swapTickProgress >= threshold) 1
63+
val swapTickProgress = breakDelta * (breakTicks + request.hotbar.swapPause - 1).coerceAtLeast(1)
64+
if (swapTickProgress >= threshold && request.hotbar.swapPause > 0) 1
7765
else 0
7866
} else {
79-
val serverSwapTicks = breakConfig.serverSwapTicks.coerceAtLeast(3)
67+
val serverSwapTicks = request.hotbar.swapPause.coerceAtLeast(3)
8068
val swapTickProgress = breakDelta * (breakTicks + serverSwapTicks - 1).coerceAtLeast(1)
8169
if (swapTickProgress >= threshold) 1
8270
else 0
@@ -95,12 +83,7 @@ data class SwapInfo(
9583
BreakConfig.SwapMode.Constant -> true
9684
}
9785

98-
return SwapInfo(info.type, breakConfig, swap, minKeepTicks)
86+
return SwapInfo(info.type, request.hotbar, swap, minKeepTicks)
9987
}
100-
101-
private val ItemStack.heldTicks
102-
get() = if (this == currentStack)
103-
BreakManager.heldTicks
104-
else 0
10588
}
10689
}

0 commit comments

Comments
 (0)