Skip to content

Commit 1443cb5

Browse files
committed
manual rebreak touch ups and manager stage priority
1 parent df00a6d commit 1443cb5

File tree

9 files changed

+95
-33
lines changed

9 files changed

+95
-33
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2025 Lambda
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package com.lambda.interaction.request
19+
20+
import com.lambda.util.reflections.getInstances
21+
22+
object ManagerUtils {
23+
val managers = getInstances<RequestHandler<*>>()
24+
val accumulatedManagerPriority = managers.map { it.stagePriority }.reduce { acc, priority -> acc + priority }
25+
}

common/src/main/kotlin/com/lambda/interaction/request/RequestHandler.kt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,23 @@
1818
package com.lambda.interaction.request
1919

2020
import com.lambda.context.SafeContext
21+
import com.lambda.core.Loadable
2122
import com.lambda.event.Event
2223
import com.lambda.event.events.TickEvent
2324
import com.lambda.event.listener.SafeListener.Companion.listen
25+
import com.lambda.interaction.request.ManagerUtils.accumulatedManagerPriority
2426
import com.lambda.threading.runSafe
2527

2628
/**
2729
* This class handles requests, offering specific opening times, and an option to queue a request for the
2830
* next opening if closed
2931
*/
3032
abstract class RequestHandler<R : Request>(
31-
vararg openStages: Event,
33+
val stagePriority: Int,
34+
private vararg val openStages: Event,
3235
private val onOpen: (SafeContext.() -> Unit)? = null,
3336
private val onClose: (SafeContext.() -> Unit)? = null
34-
) {
37+
) : Loadable {
3538
/**
3639
* Represents if the handler is accepting requests at any given time
3740
*/
@@ -53,7 +56,7 @@ abstract class RequestHandler<R : Request>(
5356
*/
5457
var activeThisTick = false; protected set
5558

56-
init {
59+
override fun load(): String {
5760
openStages.forEach {
5861
when (it) {
5962
is TickEvent.Pre -> openRequestsFor(it)
@@ -77,13 +80,15 @@ abstract class RequestHandler<R : Request>(
7780
listen<TickEvent.Post>(Int.MIN_VALUE) {
7881
activeThisTick = false
7982
}
83+
84+
return super.load()
8085
}
8186

8287
/**
8388
* opens the handler for requests for the duration of the given event
8489
*/
8590
private inline fun <reified T : Event> openRequestsFor(stage: T) {
86-
listen<T>(priority = Int.MAX_VALUE) {
91+
listen<T>(priority = Int.MAX_VALUE - (accumulatedManagerPriority - stagePriority)) {
8792
tickStage = stage
8893
queuedRequest?.let { request ->
8994
handleRequest(request)
@@ -94,7 +99,7 @@ abstract class RequestHandler<R : Request>(
9499
onOpen?.invoke(this)
95100
preEvent()
96101
}
97-
listen<T>(priority = Int.MIN_VALUE) {
102+
listen<T>(priority = Int.MIN_VALUE + stagePriority) {
98103
onClose?.invoke(this)
99104
acceptingRequests = false
100105
}

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

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ import net.minecraft.util.Hand
6262
import net.minecraft.util.math.BlockPos
6363

6464
object BreakManager : RequestHandler<BreakRequest>(
65+
0,
6566
TickEvent.Pre,
6667
TickEvent.Input.Pre,
6768
TickEvent.Player.Post,
@@ -93,6 +94,10 @@ object BreakManager : RequestHandler<BreakRequest>(
9394
private var instantBreaks = mutableListOf<BreakContext>()
9495

9596
var lastPosStarted: BlockPos? = null
97+
set(value) {
98+
if (value != field) ReBreakManager.clearReBreak()
99+
field = value
100+
}
96101

97102
fun Any.onBreak(
98103
alwaysListen: Boolean = false,
@@ -102,7 +107,9 @@ object BreakManager : RequestHandler<BreakRequest>(
102107
block()
103108
}
104109

105-
init {
110+
override fun load(): String {
111+
super.load()
112+
106113
listen<TickEvent.Post>(priority = Int.MIN_VALUE) {
107114
if (breakCooldown > 0) {
108115
breakCooldown--
@@ -123,7 +130,7 @@ object BreakManager : RequestHandler<BreakRequest>(
123130
breaksThisTick = 0
124131
}
125132

126-
listen<WorldEvent.BlockUpdate.Server>(priority = Int.MIN_VALUE + 1) { event ->
133+
listen<WorldEvent.BlockUpdate.Server>(priority = Int.MIN_VALUE) { event ->
127134
breakInfos
128135
.filterNotNull()
129136
.firstOrNull { it.context.expectedPos == event.pos }
@@ -147,7 +154,7 @@ object BreakManager : RequestHandler<BreakRequest>(
147154
}
148155

149156
// ToDo: Dependent on the tracked data order. When set stack is called after position it wont work
150-
listen<EntityEvent.Update>(priority = Int.MIN_VALUE + 1) {
157+
listen<EntityEvent.Update>(priority = Int.MIN_VALUE) {
151158
if (it.entity !is ItemEntity) return@listen
152159

153160
breakInfos
@@ -156,10 +163,12 @@ object BreakManager : RequestHandler<BreakRequest>(
156163
?.internalOnItemDrop(it.entity)
157164
}
158165

159-
listenUnsafe<ConnectionEvent.Connect.Pre>(priority = Int.MIN_VALUE + 1) {
166+
listenUnsafe<ConnectionEvent.Connect.Pre>(priority = Int.MIN_VALUE) {
160167
breakInfos.forEach { it?.nullify() }
161168
breakCooldown = 0
162169
}
170+
171+
return "Loaded Break Manager"
163172
}
164173

165174
/**
@@ -489,6 +498,7 @@ object BreakManager : RequestHandler<BreakRequest>(
489498
return true
490499
}
491500
breakCooldown = info.breakConfig.breakDelay
501+
lastPosStarted = ctx.expectedPos
492502
interaction.sendSequencedPacket(world) { sequence ->
493503
onBlockBreak(info)
494504
PlayerActionC2SPacket(Action.START_DESTROY_BLOCK, ctx.expectedPos, hitResult.side, sequence)
@@ -508,10 +518,9 @@ object BreakManager : RequestHandler<BreakRequest>(
508518
ReBreakManager.clearReBreak()
509519
}
510520

511-
primaryBreak?.let { primary ->
521+
return primaryBreak?.let { primary ->
512522
updateBreakProgress(primary)
513-
}
514-
return true
523+
} ?: false
515524
}
516525
is ReBreakResult.ReBroke -> {
517526
info.nullify()
@@ -523,7 +532,6 @@ object BreakManager : RequestHandler<BreakRequest>(
523532
info.nullify()
524533
return false
525534
}
526-
ReBreakManager.clearReBreak()
527535
val swing = info.breakConfig.swing
528536
if (swing.isEnabled() && swing != BreakConfig.SwingMode.End) {
529537
swingHand(info.breakConfig.swingType, Hand.MAIN_HAND)
@@ -612,6 +620,7 @@ object BreakManager : RequestHandler<BreakRequest>(
612620
if (!world.worldBorder.contains(ctx.expectedPos)) return false
613621

614622
if (gamemode.isCreative) {
623+
lastPosStarted = ctx.expectedPos
615624
interaction.sendSequencedPacket(world) { sequence: Int ->
616625
onBlockBreak(info)
617626
PlayerActionC2SPacket(Action.START_DESTROY_BLOCK, ctx.expectedPos, ctx.result.side, sequence)
@@ -621,6 +630,8 @@ object BreakManager : RequestHandler<BreakRequest>(
621630
}
622631
if (info.breaking) return false
623632

633+
lastPosStarted = ctx.expectedPos
634+
624635
val blockState = blockState(ctx.expectedPos)
625636
val notAir = !blockState.isAir
626637
if (notAir && info.breakingTicks == 0) {
@@ -644,7 +655,6 @@ object BreakManager : RequestHandler<BreakRequest>(
644655
if (info.breakConfig.breakMode == BreakMode.Packet) {
645656
info.stopBreakPacket(world, interaction)
646657
}
647-
lastPosStarted = ctx.expectedPos
648658
info.startBreakPacket(world, interaction)
649659
if (info.isSecondary || (breakDelta < 1 && breakDelta >= info.breakConfig.breakThreshold)) {
650660
info.stopBreakPacket(world, interaction)

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ object BrokenBlockHandler {
111111
pending.internalOnItemDrop(it.entity)
112112
if (pending.callbacksCompleted) {
113113
pending.stopPending()
114+
if (lastPosStarted == pending.context.expectedPos) {
115+
ReBreakManager.startReBreak(pending)
116+
}
114117
}
115118
return@listen
116119
}

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

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

2020
import com.lambda.config.groups.ReBreakSettings
21+
import com.lambda.event.events.ConnectionEvent
2122
import com.lambda.event.events.TickEvent
2223
import com.lambda.event.listener.SafeListener.Companion.listen
24+
import com.lambda.event.listener.UnsafeListener.Companion.listenUnsafe
2325
import com.lambda.interaction.construction.context.BreakContext
2426
import com.lambda.interaction.request.breaking.BrokenBlockHandler.destroyBlock
2527
import com.lambda.interaction.request.breaking.BrokenBlockHandler.isEmpty
@@ -37,11 +39,16 @@ object ReBreakManager {
3739
activeAge++
3840
}
3941
}
42+
43+
listenUnsafe<ConnectionEvent.Connect.Pre>(priority = Int.MIN_VALUE) {
44+
reBreak = null
45+
}
4046
}
4147

4248
fun startReBreak(info: BreakInfo?) {
4349
reBreak = info?.apply {
4450
type = BreakType.ReBreak
51+
breaking = true
4552
}
4653
}
4754

common/src/main/kotlin/com/lambda/interaction/request/hotbar/HotbarManager.kt

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

2020
import com.lambda.context.SafeContext
21-
import com.lambda.core.Loadable
2221
import com.lambda.event.Event
2322
import com.lambda.event.EventFlow.post
2423
import com.lambda.event.events.InventoryEvent
@@ -38,25 +37,26 @@ import com.lambda.threading.runSafe
3837
* @see InGameHudMixin.onTick
3938
*/
4039
object HotbarManager : RequestHandler<HotbarRequest>(
40+
1,
4141
TickEvent.Pre,
4242
TickEvent.Input.Pre,
4343
TickEvent.Player.Post,
4444
// ToDo: Post interact
4545
onClose = { checkResetSwap() }
46-
), Loadable {
46+
) {
4747
val serverSlot get() = runSafe {
4848
interaction.lastSelectedSlot
4949
} ?: 0
5050

51-
override fun load() = "Loaded Hotbar Manager"
52-
5351
private var swapsThisTick = 0
5452
private var maxSwapsThisTick = 0
5553
private var swapDelay = 0
5654

5755
private var activeRequest: HotbarRequest? = null
5856

59-
init {
57+
override fun load(): String {
58+
super.load()
59+
6060
listen<TickEvent.Post>(priority = Int.MIN_VALUE) {
6161
swapsThisTick = 0
6262
if (swapDelay > 0) swapDelay--
@@ -74,6 +74,8 @@ object HotbarManager : RequestHandler<HotbarRequest>(
7474
listen<InventoryEvent.HotbarSlot.Update>(priority = Int.MIN_VALUE) {
7575
it.slot = activeRequest?.slot ?: return@listen
7676
}
77+
78+
return "Loaded Hotbar Manager"
7779
}
7880

7981
override fun SafeContext.handleRequest(request: HotbarRequest) {

common/src/main/kotlin/com/lambda/interaction/request/placing/PlaceManager.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ import net.minecraft.util.math.Direction
5858
import net.minecraft.world.GameMode
5959

6060
object PlaceManager : RequestHandler<PlaceRequest>(
61+
0,
6162
TickEvent.Pre,
6263
TickEvent.Input.Pre,
6364
TickEvent.Player.Post,
@@ -85,7 +86,9 @@ object PlaceManager : RequestHandler<PlaceRequest>(
8586
block()
8687
}
8788

88-
init {
89+
override fun load(): String {
90+
super.load()
91+
8992
listen<TickEvent.Post>(priority = Int.MIN_VALUE) {
9093
activeRequest = null
9194
placementsThisTick = 0
@@ -97,6 +100,8 @@ object PlaceManager : RequestHandler<PlaceRequest>(
97100
it.input.sneaking = true
98101
}
99102
}
103+
104+
return "Loaded Place Manager"
100105
}
101106

102107
/**

common/src/main/kotlin/com/lambda/interaction/request/rotation/RotationManager.kt

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
package com.lambda.interaction.request.rotation
1919

2020
import com.lambda.Lambda.mc
21-
import com.lambda.config.groups.TickStage
2221
import com.lambda.context.SafeContext
23-
import com.lambda.core.Loadable
2422
import com.lambda.event.Event
2523
import com.lambda.event.EventFlow.post
2624
import com.lambda.event.events.ConnectionEvent
@@ -50,20 +48,19 @@ import kotlin.math.sign
5048
import kotlin.math.sin
5149

5250
object RotationManager : RequestHandler<RotationRequest>(
51+
1,
5352
TickEvent.Pre,
5453
TickEvent.Input.Pre,
5554
TickEvent.Player.Post,
5655
// ToDo: Post interact
57-
), Loadable {
56+
) {
5857
var activeRotation = Rotation.ZERO
5958
var serverRotation = Rotation.ZERO
6059
var prevServerRotation = Rotation.ZERO
6160

6261
var activeRequest: RotationRequest? = null
6362
private var changedThisTick = false
6463

65-
override fun load() = "Loaded Rotation Manager"
66-
6764
fun Any.onRotate(
6865
alwaysListen: Boolean = false,
6966
priority: Priority = 0,
@@ -72,15 +69,17 @@ object RotationManager : RequestHandler<RotationRequest>(
7269
block()
7370
}
7471

75-
init {
72+
override fun load(): String {
73+
super.load()
74+
7675
listen<TickEvent.Post>(priority = Int.MIN_VALUE) {
7776
activeRequest?.let { request ->
7877
request.age++
7978
}
8079
changedThisTick = false
8180
}
8281

83-
listen<PacketEvent.Receive.Post> { event ->
82+
listen<PacketEvent.Receive.Post>(priority = Int.MIN_VALUE) { event ->
8483
val packet = event.packet
8584
if (packet !is PlayerPositionLookS2CPacket) return@listen
8685

@@ -89,9 +88,11 @@ object RotationManager : RequestHandler<RotationRequest>(
8988
}
9089
}
9190

92-
listenUnsafe<ConnectionEvent.Connect.Pre> {
91+
listenUnsafe<ConnectionEvent.Connect.Pre>(priority = Int.MIN_VALUE) {
9392
reset(Rotation.ZERO)
9493
}
94+
95+
return "Loaded Rotation Manager"
9596
}
9697

9798
override fun SafeContext.handleRequest(request: RotationRequest) {

0 commit comments

Comments
 (0)