Skip to content

Commit 295d0f0

Browse files
committed
added desyncFix setting, although its functionality is broken until player simulation is progressed
1 parent 341acb7 commit 295d0f0

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class BreakSettings(
3535
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)
3636
override val doubleBreak by c.setting("Double Break", true, "Allows breaking two blocks at once", visibility = vis)
3737
override val fudgeFactor by c.setting("Fudge Factor", 2, 0..5, 1, "The amount of ticks to give double, aka secondary breaks extra for the server to recognise the break", visibility = vis)
38+
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", visibility = vis)
3839
override val breakDelay by c.setting("Break Delay", 0, 0..6, 1, "The delay between breaking blocks", " ticks", visibility = vis)
3940
override val breakStageMask by c.setting("Break Stage Mask", setOf(TickEvent.Pre, TickEvent.Input.Pre, TickEvent.Input.Post, TickEvent.Player.Post), "The sub-tick timing at which break actions can be performed", visibility = vis)
4041
override val swing by c.setting("Swing Mode", SwingMode.Constant, "The times at which to swing the players hand", visibility = vis)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ abstract class BreakConfig(
3333
abstract val breakThreshold: Float
3434
abstract val doubleBreak: Boolean
3535
abstract val fudgeFactor: Int
36+
abstract val desyncFix: Boolean
3637
abstract val breakDelay: Int
3738
abstract val breakStageMask: Set<Event>
3839
abstract val swing: SwingMode

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

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,21 @@ import com.lambda.util.Communication.warn
5656
import com.lambda.util.item.ItemUtils.block
5757
import com.lambda.util.math.lerp
5858
import com.lambda.util.player.gamemode
59+
import com.lambda.util.player.prediction.buildPlayerPrediction
5960
import com.lambda.util.player.swingHand
61+
import net.minecraft.block.BlockState
62+
import net.minecraft.client.network.ClientPlayerEntity
6063
import net.minecraft.client.sound.PositionedSoundInstance
6164
import net.minecraft.client.sound.SoundInstance
65+
import net.minecraft.enchantment.EnchantmentHelper
6266
import net.minecraft.entity.ItemEntity
67+
import net.minecraft.item.ItemStack
68+
import net.minecraft.registry.tag.FluidTags
6369
import net.minecraft.sound.SoundCategory
6470
import net.minecraft.util.Hand
6571
import net.minecraft.util.math.BlockPos
6672
import net.minecraft.util.math.Box
73+
import net.minecraft.world.BlockView
6774

6875
object BreakManager : RequestHandler<BreakRequest>(
6976
0,
@@ -180,10 +187,11 @@ object BreakManager : RequestHandler<BreakRequest>(
180187
.forEach { info ->
181188
val config = info.breakConfig
182189
if (!config.renders) return@listen
183-
val breakDelta = info.context.checkedState.calcItemBlockBreakingDelta(
190+
val breakDelta = info.context.checkedState.calcBreakDelta(
184191
player,
185192
world,
186193
info.context.expectedPos,
194+
info.breakConfig,
187195
player.inventory.getStack(info.context.hotbarIndex)
188196
)
189197
val progress = (info.breakingTicks * breakDelta).let {
@@ -268,10 +276,11 @@ object BreakManager : RequestHandler<BreakRequest>(
268276
.forEach { info ->
269277
if (info.updatedProgressThisTick) return@forEach
270278
val minKeepTicks = if (info.isSecondary) {
271-
val breakDelta = info.context.checkedState.calcItemBlockBreakingDelta(
279+
val breakDelta = info.context.checkedState.calcBreakDelta(
272280
player,
273281
world,
274282
info.context.expectedPos,
283+
info.breakConfig,
275284
player.inventory.getStack(info.context.hotbarIndex)
276285
)
277286
val breakAmount = breakDelta * (info.breakingTicks + 1)
@@ -605,10 +614,11 @@ object BreakManager : RequestHandler<BreakRequest>(
605614
}
606615

607616
info.breakingTicks++
608-
val progress = blockState.calcBlockBreakingDelta(
617+
val progress = blockState.calcBreakDelta(
609618
player,
610619
world,
611-
ctx.expectedPos
620+
ctx.expectedPos,
621+
config
612622
) * (info.breakingTicks - config.fudgeFactor)
613623

614624
val overBreakThreshold = progress >= info.getBreakThreshold()
@@ -694,7 +704,7 @@ object BreakManager : RequestHandler<BreakRequest>(
694704
blockState.onBlockBreakStart(world, ctx.expectedPos, player)
695705
}
696706

697-
val breakDelta = blockState.calcBlockBreakingDelta(player, world, ctx.expectedPos)
707+
val breakDelta = blockState.calcBreakDelta(player, world, ctx.expectedPos, info.breakConfig)
698708
if (notEmpty && breakDelta >= info.getBreakThreshold()) {
699709
onBlockBreak(info)
700710
} else {
@@ -722,6 +732,31 @@ object BreakManager : RequestHandler<BreakRequest>(
722732
return true
723733
}
724734

735+
private fun BlockState.calcBreakDelta(
736+
player: ClientPlayerEntity,
737+
world: BlockView,
738+
pos: BlockPos,
739+
config: BreakConfig,
740+
item: ItemStack? = null
741+
) = runSafe {
742+
var delta = calcItemBlockBreakingDelta(player, world, pos, item ?: player.inventory.mainHandStack)
743+
// This setting requires some fixes / improvements in the player movement prediction to work properly. Currently, its broken
744+
if (config.desyncFix) {
745+
val nextTickPrediction = buildPlayerPrediction().next()
746+
if (player.isOnGround && !nextTickPrediction.onGround) {
747+
delta /= 5.0f
748+
}
749+
750+
val affectedThisTick = player.isSubmergedIn(FluidTags.WATER) && !EnchantmentHelper.hasAquaAffinity(player)
751+
val simulatedPlayer = nextTickPrediction.predictionEntity.player
752+
val affectedNextTick = simulatedPlayer.isSubmergedIn(FluidTags.WATER) && !EnchantmentHelper.hasAquaAffinity(simulatedPlayer)
753+
if (!affectedThisTick && affectedNextTick) {
754+
delta /= 5.0f
755+
}
756+
}
757+
delta
758+
} ?: 0f
759+
725760
/**
726761
* @return if the [ItemEntity] matches the [BreakInfo]'s expected item drop.
727762
*/

0 commit comments

Comments
 (0)