Skip to content

Commit e0a95b2

Browse files
committed
packemine refactor and rebreak fixes
1 parent 77101de commit e0a95b2

File tree

5 files changed

+51
-59
lines changed

5 files changed

+51
-59
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ data class BreakInfo(
6262

6363
@Synchronized
6464
fun internalOnBreak() {
65-
broken = true
65+
if (!isReBreaking) broken = true
6666
request.onBreak?.invoke(context.expectedPos)
6767
item?.let { item ->
6868
request.onItemDrop?.invoke(item)
@@ -71,8 +71,8 @@ data class BreakInfo(
7171

7272
@Synchronized
7373
fun internalOnItemDrop(item: ItemEntity) {
74-
this.item = item
75-
if (broken) {
74+
if (!isReBreaking) this.item = item
75+
if (broken || isReBreaking) {
7676
request.onItemDrop?.invoke(item)
7777
}
7878
}

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

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,12 @@ object BreakManager : RequestHandler<BreakRequest>(
129129
}
130130

131131
listen<WorldEvent.BlockUpdate.Server>(priority = Int.MIN_VALUE) { event ->
132+
if (event.pos == ReBreakManager.reBreak?.context?.expectedPos) return@listen
133+
132134
breakInfos
133135
.filterNotNull()
134136
.firstOrNull { it.context.expectedPos == event.pos }
135137
?.let { info ->
136-
if (event.pos == ReBreakManager.reBreak?.context?.expectedPos) return@listen
137-
138138
// if not broken
139139
if (!isBroken(info.context.checkedState, event.newState)) {
140140
this@BreakManager.warn("Break at ${event.pos.toShortString()} was rejected with ${event.newState} instead of ${info.context.checkedState.brokenState}")
@@ -203,8 +203,7 @@ object BreakManager : RequestHandler<BreakRequest>(
203203

204204
if (request.fresh) populateFrom(request)
205205

206-
if (!atMaxBreakInfos(request.build.breaking)) run processNewBreaks@ {
207-
if (!performInstantBreaks(request)) return@processNewBreaks
206+
if (performInstantBreaks(request)) {
208207
processNewBreaks(request)
209208
}
210209

@@ -215,7 +214,7 @@ object BreakManager : RequestHandler<BreakRequest>(
215214
.filterNotNull()
216215
.filter { !it.isRedundant }
217216
.also {
218-
rotationRequest = it.firstOrNull { it.breakConfig.rotateForBreak }
217+
rotationRequest = it.firstOrNull { info -> info.breakConfig.rotateForBreak }
219218
?.let { info ->
220219
val rotation = info.context.rotation
221220
if (instantBreaks.isEmpty()) info.request.rotation.request(rotation, false) else rotation
@@ -251,7 +250,7 @@ object BreakManager : RequestHandler<BreakRequest>(
251250
private fun SafeContext.populateFrom(request: BreakRequest) {
252251
// Sanitize the new breaks
253252
val newBreaks = request.contexts
254-
.filter { ctx -> canAccept(ctx) }
253+
.filter { ctx -> canAccept(ctx, request.build.breaking) }
255254
.toMutableList()
256255

257256
// Update the current break infos or cancel if abandoned
@@ -281,14 +280,16 @@ object BreakManager : RequestHandler<BreakRequest>(
281280
/**
282281
* @return if the break context can be accepted.
283282
*/
284-
private fun SafeContext.canAccept(ctx: BreakContext): Boolean {
283+
private fun SafeContext.canAccept(ctx: BreakContext, breakConfig: BreakConfig): Boolean {
285284
if (pendingBreaks.any { it.context.expectedPos == ctx.expectedPos }) return false
286285

287-
breakInfos
288-
.firstOrNull { it != null && !it.isRedundant }
289-
?.let { info ->
290-
if (ctx.hotbarIndex != info.context.hotbarIndex) return false
291-
}
286+
if (breakConfig.doubleBreak) {
287+
breakInfos
288+
.firstOrNull { it != null && !it.isRedundant }
289+
?.let { info ->
290+
if (ctx.hotbarIndex != info.context.hotbarIndex) return false
291+
}
292+
}
292293

293294
return !blockState(ctx.expectedPos).isAir
294295
}
@@ -449,27 +450,23 @@ object BreakManager : RequestHandler<BreakRequest>(
449450
if (isPrimary) {
450451
abortBreakPacket(world, interaction)
451452
nullify()
452-
return@runSafe
453-
}
454-
if (isSecondary && breakConfig.unsafeCancels) {
453+
} else if (isSecondary && breakConfig.unsafeCancels) {
455454
makeRedundant()
456455
}
456+
457+
internalOnCancel()
457458
}
458459

459460
/**
460461
* Nullifies the break. If the block is not broken, the [BreakInfo.internalOnCancel] callback gets triggered
461462
*/
462-
private fun BreakInfo.nullify() {
463-
type.nullify()
464-
if (!broken && !pending && !isReBreaking && !isRedundant) internalOnCancel()
465-
}
463+
private fun BreakInfo.nullify() = type.nullify()
466464

467465
/**
468466
* Makes the [BreakInfo] redundant and triggers the [BreakInfo.internalOnCancel] callback
469467
*/
470468
private fun BreakInfo.makeRedundant() {
471469
type = BreakType.RedundantSecondary
472-
internalOnCancel()
473470
}
474471

475472
/**
@@ -494,15 +491,15 @@ object BreakManager : RequestHandler<BreakRequest>(
494491
val ctx = info.context
495492
val hitResult = ctx.result
496493

497-
if (gamemode.isCreative && world.worldBorder.contains(ctx.expectedPos)) {
494+
if (gamemode.isCreative && world.worldBorder.contains(ctx.expectedPos) && info.breaking) {
498495
if (info.isRedundant) {
499496
onBlockBreak(info)
500497
return true
501498
}
502499
breakCooldown = info.breakConfig.breakDelay
503500
lastPosStarted = ctx.expectedPos
501+
onBlockBreak(info)
504502
interaction.sendSequencedPacket(world) { sequence ->
505-
onBlockBreak(info)
506503
PlayerActionC2SPacket(Action.START_DESTROY_BLOCK, ctx.expectedPos, hitResult.side, sequence)
507504
}
508505
val swing = info.breakConfig.swing
@@ -535,6 +532,7 @@ object BreakManager : RequestHandler<BreakRequest>(
535532
}
536533
if (!startBreaking(info)) {
537534
info.nullify()
535+
info.internalOnCancel()
538536
return false
539537
}
540538
val swing = info.breakConfig.swing
@@ -547,6 +545,7 @@ object BreakManager : RequestHandler<BreakRequest>(
547545
val blockState = blockState(ctx.expectedPos)
548546
if (blockState.isAir) {
549547
info.nullify()
548+
info.internalOnCancel()
550549
return false
551550
}
552551

@@ -595,8 +594,8 @@ object BreakManager : RequestHandler<BreakRequest>(
595594
val swing = info.breakConfig.swing
596595
if (overBreakThreshold) {
597596
if (info.isPrimary) {
597+
onBlockBreak(info)
598598
interaction.sendSequencedPacket(world) { sequence ->
599-
onBlockBreak(info)
600599
PlayerActionC2SPacket(Action.STOP_DESTROY_BLOCK, ctx.expectedPos, hitResult.side, sequence)
601600
}
602601
} else {
@@ -626,8 +625,8 @@ object BreakManager : RequestHandler<BreakRequest>(
626625

627626
if (gamemode.isCreative) {
628627
lastPosStarted = ctx.expectedPos
628+
onBlockBreak(info)
629629
interaction.sendSequencedPacket(world) { sequence: Int ->
630-
onBlockBreak(info)
631630
PlayerActionC2SPacket(Action.START_DESTROY_BLOCK, ctx.expectedPos, ctx.result.side, sequence)
632631
}
633632
breakCooldown = info.breakConfig.breakDelay

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,10 @@ object BrokenBlockHandler {
8181
if (!isBroken(pending.context.checkedState, event.newState)) {
8282
if (!pending.isReBreaking) {
8383
this@BrokenBlockHandler.warn("Broken block at ${event.pos.toShortString()} was rejected with ${event.newState} instead of ${pending.context.checkedState.brokenState}")
84+
pending.stopPending()
85+
} else {
86+
pending.context.checkedState = event.newState
8487
}
85-
pending.stopPending()
8688
return@listen
8789
}
8890

@@ -141,8 +143,6 @@ object BrokenBlockHandler {
141143
if (!isReBreaking) {
142144
pendingBreaks.remove(this)
143145
pendingInteractions.remove(context)
144-
} else {
145-
resetCallbacks()
146146
}
147147
}
148148

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ object ReBreakManager {
5050
reBreak = info.apply {
5151
type = BreakType.ReBreak
5252
breaking = true
53+
resetCallbacks()
5354
}
5455
info.request.onReBreakStart?.invoke(info.context.expectedPos)
5556
}
@@ -62,11 +63,10 @@ object ReBreakManager {
6263
runSafe {
6364
val info = reBreak ?: return@runSafe ReBreakResult.Ignored
6465

65-
if (info.updatedThisTick) return@runSafe ReBreakResult.ReBroke
66-
6766
if (info.context.expectedPos != ctx.expectedPos || !info.breakConfig.reBreak.mode.isEnabled()) {
6867
return@runSafe ReBreakResult.Ignored
6968
}
69+
if (info.updatedThisTick) return@runSafe ReBreakResult.ReBroke
7070
info.updateInfo(ctx, breakRequest)
7171

7272
val context = info.context

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

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -62,51 +62,45 @@ object PacketMine : Module(
6262
private val breakingPositions = arrayOfNulls<BlockPos>(2)
6363
private var reBreakPos: BlockPos? = null
6464

65-
private var requestedThisTick = false
65+
private var attackedThisTick = false
6666

6767
init {
6868
listen<TickEvent.Post> {
69-
requestedThisTick = false
69+
attackedThisTick = false
7070
}
7171

7272
//ToDo: run on every tick stage
7373
listen<TickEvent.Pre> {
7474
val reBreakMode = breakConfig.reBreak.mode
7575
if (reBreakMode != ReBreakSettings.Mode.Auto && reBreakMode != ReBreakSettings.Mode.AutoConstant) return@listen
7676
val reBreak = reBreakPos ?: return@listen
77-
requestBreakManager(listOf(reBreak))
77+
requestBreakManager(reBreak)
7878
}
7979

8080
listen<PlayerEvent.Attack.Block> { it.cancel() }
8181
listen<PlayerEvent.Breaking.Update> { event ->
8282
event.cancel()
8383
if (breakingPositions.any { it == event.pos }) return@listen
84-
if (breakConfig.doubleBreak && breakingPositions[1] == null) {
85-
breakingPositions[1] = breakingPositions[0]
86-
}
87-
breakingPositions[0] = null
88-
sendBreakRequest(event.pos)
89-
requestedThisTick = true
84+
val secondary = if (breakConfig.doubleBreak) {
85+
breakingPositions[1] ?: breakingPositions[0]
86+
} else null
87+
requestBreakManager(event.pos, secondary)
88+
attackedThisTick = true
9089
}
9190

9291
listen<TickEvent.Input.Post> {
93-
if (!requestedThisTick) sendBreakRequest()
92+
if (!attackedThisTick) requestBreakManager(*breakingPositions.toList().toTypedArray())
9493
}
9594
}
9695

97-
private fun SafeContext.sendBreakRequest(hitPos: BlockPos? = null) {
98-
val requestPositions = arrayListOf(*breakingPositions.filterNotNull().toTypedArray())
99-
hitPos?.let { pos ->
100-
requestPositions.add(pos)
101-
}
102-
103-
if (requestPositions.isNotEmpty()) requestBreakManager(requestPositions)
104-
}
105-
106-
private fun SafeContext.requestBreakManager(requestPositions: List<BlockPos>) {
96+
private fun SafeContext.requestBreakManager(vararg requestPositions: BlockPos?) {
97+
if (requestPositions.isEmpty()) return
10798
val request = BreakRequest(
108-
breakContexts(requestPositions), build, rotation, hotbar, pendingInteractions = pendingInteractionsList,
99+
breakContexts(requestPositions.filterNotNull()), build, rotation, hotbar, pendingInteractions = pendingInteractionsList,
109100
onAccept = {
101+
if (breakConfig.doubleBreak && breakingPositions[1] == null) {
102+
breakingPositions[1] = breakingPositions[0]
103+
}
110104
breakingPositions[0] = it
111105
reBreakPos = null
112106
},
@@ -123,16 +117,15 @@ object PacketMine : Module(
123117
}
124118

125119
private fun nullifyBreakPos(pos: BlockPos, includeReBreak: Boolean = false) {
126-
if (includeReBreak && pos == reBreakPos) {
127-
reBreakPos = null
128-
return
129-
}
130120
breakingPositions.forEachIndexed { index, breakPos ->
131121
if (breakPos == pos) {
132122
breakingPositions[index] = null
133-
return
134123
}
135124
}
125+
if (includeReBreak && pos == reBreakPos) {
126+
reBreakPos = null
127+
return
128+
}
136129
}
137130

138131
private fun SafeContext.breakContexts(breakPositions: Collection<BlockPos>) =
@@ -152,4 +145,4 @@ object PacketMine : Module(
152145
enum class Page {
153146
Build, Rotation, Interaction, Inventory, Hotbar
154147
}
155-
}
148+
}

0 commit comments

Comments
 (0)