Skip to content

Commit 7e2b244

Browse files
committed
fully working double break and fast break. No delays for blocks that take longer than 6 ticks to break
1 parent 42991aa commit 7e2b244

2 files changed

Lines changed: 33 additions & 20 deletions

File tree

src/main/kotlin/com/lambda/interaction/managers/breaking/BreakInfo.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package com.lambda.interaction.managers.breaking
1919

2020
import com.lambda.config.blocks.BreakConfig
21+
import com.lambda.config.blocks.BreakConfig.BreakMode
2122
import com.lambda.context.SafeContext
2223
import com.lambda.interaction.construction.simulation.context.BreakContext
2324
import com.lambda.interaction.handlers.breaking.RebreakHandler
@@ -35,6 +36,7 @@ import net.minecraft.item.ItemStack
3536
import net.minecraft.network.packet.c2s.play.PlayerActionC2SPacket
3637
import net.minecraft.network.packet.c2s.play.PlayerActionC2SPacket.Action
3738
import net.minecraft.util.math.BlockPos
39+
import net.minecraft.world.attribute.EnvironmentAttributeModifier.override
3840

3941
/**
4042
* A data class that holds all the information required to process and continue a break.
@@ -66,6 +68,7 @@ data class BreakInfo(
6668
var soundsCooldown = 0f
6769
var vanillaInstantBreakable = false
6870
val rebreakable get() = !vanillaInstantBreakable && type == Primary
71+
var bypassedDelay = breakConfig.breakMode != BreakMode.OldGrim
6972

7073
enum class BreakType(
7174
override val displayName: String,
@@ -161,5 +164,7 @@ data class BreakInfo(
161164
}
162165
}
163166

167+
fun getBreakDelay() = if (!bypassedDelay) 6 else breakConfig.breakDelay
168+
164169
override fun toString() = "$type, ${context.cachedState}, ${context.blockPos}"
165170
}

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

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ object BreakManager : Manager<BreakRequest>(
154154
private var rotationRequest: RotationRequest? = null
155155
private val rotated get() = rotationRequest?.done != false
156156

157-
private var breakCooldown = 0
157+
private var breakDelay = 0
158158
var breaksThisTick = 0
159159
private var maxBreaksThisTick = 0
160160

@@ -166,19 +166,15 @@ object BreakManager : Manager<BreakRequest>(
166166
field = value
167167
}
168168

169-
const val OLD_GRIM_Y_OFFSET = 2000
169+
const val OLD_GRIM_Y_OFFSET = 955
170170

171171
override fun load(): String {
172172
super.load()
173173

174-
listen<TickEvent.Pre>({ Int.MAX_VALUE }) {
175-
if (activeInfos.isEmpty() && breaks.isEmpty()) return@listen
176-
}
177-
178174
listen<TickEvent.Post>({ Int.MIN_VALUE }) {
179175
if (primaryBreak?.breaking == false) primaryBreak = null
180176
breakInfos.forEach { it?.tickChecks() }
181-
if (breakCooldown > 0) breakCooldown--
177+
if (breakDelay > 0) breakDelay--
182178
activeRequest = null
183179
breaks = mutableListOf()
184180
breaksThisTick = 0
@@ -231,7 +227,7 @@ object BreakManager : Manager<BreakRequest>(
231227
listenUnsafe<ConnectionEvent.Connect.Pre>({ Int.MIN_VALUE }) {
232228
primaryBreak = null
233229
secondaryBreak = null
234-
breakCooldown = 0
230+
breakDelay = 0
235231
}
236232

237233
immediateRenderer("BreakManager Immediate Renderer") {
@@ -474,6 +470,7 @@ object BreakManager : Manager<BreakRequest>(
474470
private fun SafeContext.processNewBreak(request: BreakRequest): Boolean =
475471
request.runSafeAutomated {
476472
if (tickStage !in request.breakConfig.tickStageMask) return false
473+
if (breakDelay > 0) return false
477474

478475
breaks.forEach { ctx ->
479476
if (breaksThisTick >= maxBreaksThisTick) return false
@@ -505,8 +502,6 @@ object BreakManager : Manager<BreakRequest>(
505502
requestCtx: BreakContext,
506503
request: BreakRequest
507504
): BreakInfo? {
508-
if (breakCooldown > 0) return null
509-
510505
val breakInfo = BreakInfo(requestCtx, Primary, request)
511506
primaryBreak?.let { primaryInfo ->
512507
if (tickStage !in primaryInfo.breakConfig.tickStageMask) return null
@@ -622,6 +617,7 @@ object BreakManager : Manager<BreakRequest>(
622617
}
623618
}
624619
breaksThisTick++
620+
if (info.type == Primary) breakDelay = info.getBreakDelay()
625621
info.nullify()
626622
}
627623

@@ -705,7 +701,7 @@ object BreakManager : Manager<BreakRequest>(
705701

706702
if (gamemode.isCreative && world.worldBorder.contains(ctx.blockPos)) {
707703
if (!PacketLimitHandler.canSendPackets(1, PacketType.PlayerAction)) return
708-
breakCooldown = breakConfig.breakDelay
704+
breakDelay = info.getBreakDelay()
709705
lastPosStarted = ctx.blockPos
710706
onBlockBreak(info)
711707
info.startBreakPacket()
@@ -723,6 +719,13 @@ object BreakManager : Manager<BreakRequest>(
723719

724720
if (breakConfig.swapMode == BreakConfig.SwapMode.Constant && !swapped) return
725721

722+
val requiresDelayBypassing = info.type == Primary && breakConfig.breakMode == BreakMode.OldGrim && !info.bypassedDelay
723+
if (requiresDelayBypassing && PacketLimitHandler.canSendPackets(22, PacketType.PlayerAction) && info.breakingTicks >= 6) {
724+
repeat(22) { info.startBreakPacket(OLD_GRIM_Y_OFFSET) }
725+
PacketLimitHandler.sentPackets(22, PacketType.PlayerAction)
726+
info.bypassedDelay = true
727+
}
728+
726729
info.breakingTicks++
727730
val breakDelta = blockState.calcBreakDelta(ctx.blockPos)
728731
val progress = breakDelta * (info.breakingTicks - breakConfig.fudgeFactor)
@@ -750,15 +753,18 @@ object BreakManager : Manager<BreakRequest>(
750753
val swing = breakConfig.swing
751754
if (progress >= info.getBreakThreshold()) {
752755
if (info.swapInfo.swap && !swapped) return
753-
if (info.type == Primary && !PacketLimitHandler.canSendPackets(1, PacketType.PlayerAction)) return
756+
757+
if (info.type == Primary) {
758+
if (breakConfig.breakMode == BreakMode.OldGrim && info.breakingTicks < 6) return
759+
if (!PacketLimitHandler.canSendPackets(1, PacketType.PlayerAction)) return
760+
}
754761

755762
onBlockBreak(info)
756763
if (info.type == Primary) {
757764
info.stopBreakPacket()
758765
PacketLimitHandler.sentPackets(1, PacketType.PlayerAction)
759766
}
760767
if (swing.isEnabled() && swing != BreakConfig.SwingMode.Start) swingHand(breakConfig.swingType, Hand.MAIN_HAND)
761-
breakCooldown = breakConfig.breakDelay
762768
} else {
763769
if (swing == BreakConfig.SwingMode.Constant) swingHand(breakConfig.swingType, Hand.MAIN_HAND)
764770
}
@@ -809,7 +815,7 @@ object BreakManager : Manager<BreakRequest>(
809815
onBlockBreak(info)
810816
info.startBreakPacket()
811817
PacketLimitHandler.sentPackets(1, PacketType.PlayerAction)
812-
breakCooldown = breakConfig.breakDelay
818+
breakDelay = info.getBreakDelay()
813819
if (breakConfig.swing.isEnabled()) swingHand(breakConfig.swingType, Hand.MAIN_HAND)
814820
return true
815821
}
@@ -821,9 +827,12 @@ object BreakManager : Manager<BreakRequest>(
821827

822828
var packetCount = 1
823829
info.vanillaInstantBreakable = progress >= 1
824-
val oldGrim = breakConfig.breakMode == BreakMode.OldGrim && !info.vanillaInstantBreakable
825-
if (breakConfig.breakMode == BreakMode.Grim || oldGrim) packetCount++
826-
val requiresSecondStop = info.type == Secondary || (instantBreakable && !info.vanillaInstantBreakable)
830+
val isGrim = breakConfig.breakMode == BreakMode.Grim
831+
val oldGrim = breakConfig.breakMode == BreakMode.OldGrim
832+
val requiresSecondStop = instantBreakable && !info.vanillaInstantBreakable
833+
834+
if (isGrim) packetCount++
835+
else if (oldGrim) packetCount++
827836
if (requiresSecondStop) packetCount++
828837

829838
if (!PacketLimitHandler.canSendPackets(packetCount, PacketType.PlayerAction)) return false
@@ -838,8 +847,7 @@ object BreakManager : Manager<BreakRequest>(
838847

839848
if (instantBreakable) {
840849
onBlockBreak(info)
841-
val breakDelay = breakConfig.breakDelay
842-
if (!info.vanillaInstantBreakable) breakCooldown = if (breakDelay == 0) 0 else breakDelay + 1
850+
if (!info.vanillaInstantBreakable) breakDelay = info.getBreakDelay()
843851
} else {
844852
info.apply {
845853
breaking = true
@@ -849,7 +857,7 @@ object BreakManager : Manager<BreakRequest>(
849857
}
850858
}
851859

852-
if (breakConfig.breakMode == BreakMode.Grim) info.stopBreakPacket()
860+
if (isGrim) info.stopBreakPacket()
853861
info.startBreakPacket()
854862
if (oldGrim) info.startBreakPacket(OLD_GRIM_Y_OFFSET)
855863
if (requiresSecondStop) info.stopBreakPacket()

0 commit comments

Comments
 (0)