Skip to content

Commit 960f9c9

Browse files
committed
added place manager and update manager events
1 parent 9683348 commit 960f9c9

File tree

14 files changed

+319
-24
lines changed

14 files changed

+319
-24
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class BreakSettings(
3232
override val doubleBreak by c.setting("Double Break", false, "Allows breaking two blocks at once") { vis() }
3333
override val breakDelay by c.setting("Break Delay", 5, 0..5, 1, "The delay between breaking blocks", " ticks") { vis() }
3434
override val swing by c.setting("Swing Mode", SwingMode.Constant, "The times at which to swing the players hand") { vis() }
35-
override val swingType by c.setting("Swing Type", SwingType.Vanilla, "The style of swing")
35+
override val swingType by c.setting("Swing Type", SwingType.Vanilla, "The style of swing") { vis() }
3636
override val sounds by c.setting("Sounds", true, "Plays the breaking sounds") { vis() }
3737
override val particles by c.setting("Particles", true, "Renders the breaking particles") { vis() }
3838
override val breakingTexture by c.setting("Breaking Overlay", true, "Overlays the breaking texture at its different stages") { vis() }

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,5 @@ interface BuildConfig {
2929
val breakSettings: BreakSettings
3030

3131
// Placing
32-
val rotateForPlace: Boolean
33-
val placeConfirmation: Boolean
34-
val placementsPerTick: Int
32+
val placeSettings: PlaceSettings
3533
}

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package com.lambda.config.groups
1919

2020
import com.lambda.config.Configurable
2121
import com.lambda.interaction.request.breaking.BreakConfig.BreakConfirmationMode
22+
import com.lambda.interaction.request.placing.PlaceConfig
2223

2324
class BuildSettings(
2425
c: Configurable,
@@ -40,9 +41,7 @@ class BuildSettings(
4041
override val breakSettings = BreakSettings(c) { page == Page.Break && vis() }
4142

4243
// Placing
43-
override val rotateForPlace by c.setting("Rotate For Place", true, "Rotate towards block while placing") { vis() && page == Page.Place }
44-
override val placeConfirmation by c.setting("Place Confirmation", true, "Wait for block placement confirmation") { vis() && page == Page.Place }
45-
override val placementsPerTick by c.setting("Instant Places Per Tick", 1, 1..30, 1, "Maximum instant block places per tick") { vis() && page == Page.Place }
44+
override val placeSettings = PlaceSettings(c) { page == Page.Place && vis() }
4645

47-
override val interactionTimeout by c.setting("Interaction Timeout", 10, 1..30, 1, "Timeout for block breaks in ticks", unit = " ticks") { vis() && (page == Page.Place && placeConfirmation || page == Page.Break && breakSettings.breakConfirmation != BreakConfirmationMode.None) }
48-
}
46+
override val interactionTimeout by c.setting("Interaction Timeout", 10, 1..30, 1, "Timeout for block breaks in ticks", unit = " ticks") { vis() && (page == Page.Place && placeSettings.placeConfirmation != PlaceConfig.PlaceConfirmation.None || page == Page.Break && breakSettings.breakConfirmation != BreakConfirmationMode.None) }
47+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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.config.groups
19+
20+
import com.lambda.config.Configurable
21+
import com.lambda.interaction.request.Priority
22+
import com.lambda.interaction.request.placing.PlaceConfig
23+
24+
class PlaceSettings(
25+
c: Configurable,
26+
priority: Priority = 0,
27+
vis: () -> Boolean = { true }
28+
) : PlaceConfig(priority) {
29+
override val rotateForPlace by c.setting("Rotate For Place", true, "Rotate towards block while placing") { vis() }
30+
override val placeConfirmation by c.setting("Place Confirmation", PlaceConfirmation.PlaceThenAwait, "Wait for block placement confirmation") { vis() }
31+
override val placementsPerTick by c.setting("Instant Places Per Tick", 1, 1..30, 1, "Maximum instant block places per tick") { vis() }
32+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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.event.events
19+
20+
import com.lambda.event.Event
21+
22+
sealed class UpdateManagerEvent {
23+
sealed class Rotation {
24+
class Pre : Event
25+
class Post : Event
26+
}
27+
28+
sealed class Hotbar {
29+
class Pre : Event
30+
class Post : Event
31+
}
32+
33+
sealed class Break {
34+
class Pre : Event
35+
class Post : Event
36+
}
37+
38+
sealed class Place {
39+
class Pre : Event
40+
class Post : Event
41+
}
42+
}

common/src/main/kotlin/com/lambda/interaction/construction/context/PlaceContext.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,5 @@ data class PlaceContext(
7373
withState(blockState(result.blockPos), result.blockPos, sideColor, result.side)
7474
}
7575

76-
override fun shouldRotate(config: BuildConfig) = config.rotateForPlace
76+
override fun shouldRotate(config: BuildConfig) = config.placeSettings.rotateForPlace
7777
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package com.lambda.interaction.request
1919

20+
import com.lambda.event.Event
2021
import java.util.concurrent.ConcurrentHashMap
2122

2223
/**
@@ -66,4 +67,7 @@ abstract class RequestHandler<R : Request> {
6667
requestMap.clear()
6768
return prev != currentRequest
6869
}
70+
71+
protected abstract fun preEvent(): Event
72+
protected abstract fun postEvent(): Event
6973
}

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

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,17 @@ package com.lambda.interaction.request.breaking
1919

2020
import com.lambda.config.groups.BuildConfig
2121
import com.lambda.context.SafeContext
22+
import com.lambda.event.EventFlow.post
2223
import com.lambda.event.events.TickEvent
24+
import com.lambda.event.events.UpdateManagerEvent
2325
import com.lambda.event.events.WorldEvent
2426
import com.lambda.event.listener.SafeListener.Companion.listen
2527
import com.lambda.interaction.construction.context.BreakContext
2628
import com.lambda.interaction.construction.verify.TargetState
2729
import com.lambda.interaction.request.RequestHandler
2830
import com.lambda.interaction.request.breaking.BreakConfig.BreakConfirmationMode
2931
import com.lambda.interaction.request.breaking.BreakConfig.BreakMode
32+
import com.lambda.interaction.request.hotbar.HotbarConfig
3033
import com.lambda.interaction.request.hotbar.HotbarRequest
3134
import com.lambda.interaction.request.rotation.RotationConfig
3235
import com.lambda.interaction.request.rotation.RotationManager.onRotate
@@ -67,22 +70,32 @@ object BreakManager : RequestHandler<BreakRequest>() {
6770

6871
init {
6972
listen<TickEvent.Pre>(Int.MIN_VALUE) {
73+
preEvent()
74+
7075
if (isOnBreakCooldown()) {
7176
blockBreakingCooldown--
7277
updateRequest(true) { true }
78+
postEvent()
7379
return@listen
7480
}
7581

7682
var swapped = false
7783

78-
if (updateRequest(true) { true }) {
84+
//ToDo: improve instamine / non instamine integration
85+
if (updateRequest { true }) {
7986
currentRequest?.let request@ { request ->
8087
var instaBreaks = 0
8188
request.contexts
8289
.sortedBy { it.instantBreak }
8390
.forEach { requestCtx ->
8491
if (!canAccept(requestCtx)) return@forEach
85-
val breakType = handleRequestContext(requestCtx, request.onBreak, request.buildConfig, request.rotationConfig)
92+
val breakType = handleRequestContext(
93+
requestCtx,
94+
request.onBreak,
95+
request.buildConfig,
96+
request.rotationConfig,
97+
request.hotbarConfig
98+
)
8699
if (breakType == BreakType.Null) return@request
87100
if (requestCtx.instantBreak && instaBreaks < request.buildConfig.breakSettings.breaksPerTick) {
88101
breakingInfos.getOrNull(breakType.index)?.let { info ->
@@ -98,17 +111,19 @@ object BreakManager : RequestHandler<BreakRequest>() {
98111
}
99112
}
100113

101-
currentRequest?.let { request ->
102-
breakingInfos.firstOrNull()?.let { info ->
103-
if (!swapped && !request.hotbarConfig.request(HotbarRequest(info.context.hotbarIndex)).done)
104-
return@listen
114+
breakingInfos.firstOrNull()?.let { info ->
115+
if ((!swapped && !info.hotbarConfig.request(HotbarRequest(info.context.hotbarIndex)).done)
116+
|| (info.breakConfig.rotateForBreak && !info.context.rotation.done)) {
117+
postEvent()
118+
return@listen
105119
}
106120
}
107121

108122
breakingInfos.reversed().filterNotNull().forEach { info ->
109-
if (info.breakConfig.rotateForBreak && !info.context.rotation.done) return@listen
110123
updateBlockBreakingProgress(info, player.mainHandStack)
111124
}
125+
126+
postEvent()
112127
}
113128

114129
onRotate {
@@ -152,7 +167,8 @@ object BreakManager : RequestHandler<BreakRequest>() {
152167
requestCtx: BreakContext,
153168
onBreak: () -> Unit,
154169
buildConfig: BuildConfig,
155-
rotationConfig: RotationConfig
170+
rotationConfig: RotationConfig,
171+
hotbarConfig: HotbarConfig
156172
): BreakType {
157173
primaryBreakingInfo?.let { primaryInfo ->
158174
if (!primaryInfo.breakConfig.doubleBreak) return BreakType.Null
@@ -163,7 +179,8 @@ object BreakManager : RequestHandler<BreakRequest>() {
163179
BreakType.Secondary,
164180
onBreak,
165181
buildConfig.breakSettings,
166-
rotationConfig
182+
rotationConfig,
183+
hotbarConfig
167184
)
168185
return BreakType.Secondary
169186
} else {
@@ -174,7 +191,8 @@ object BreakManager : RequestHandler<BreakRequest>() {
174191
BreakType.Primary,
175192
onBreak,
176193
buildConfig.breakSettings,
177-
rotationConfig
194+
rotationConfig,
195+
hotbarConfig
178196
)
179197
return BreakType.Primary
180198
}
@@ -184,7 +202,8 @@ object BreakManager : RequestHandler<BreakRequest>() {
184202
BreakType.Primary,
185203
onBreak,
186204
buildConfig.breakSettings,
187-
rotationConfig
205+
rotationConfig,
206+
hotbarConfig
188207
)
189208
pendingInteractions.setMaxSize(buildConfig.maxPendingInteractions)
190209
pendingInteractions.setDecayTime(buildConfig.interactionTimeout * 50L)
@@ -400,7 +419,8 @@ object BreakManager : RequestHandler<BreakRequest>() {
400419
var type: BreakType,
401420
val onBreak: () -> Unit,
402421
val breakConfig: BreakConfig,
403-
val rotationConfig: RotationConfig
422+
val rotationConfig: RotationConfig,
423+
val hotbarConfig: HotbarConfig
404424
) {
405425
var breaking = false
406426
var breakingTicks = 0
@@ -444,4 +464,17 @@ object BreakManager : RequestHandler<BreakRequest>() {
444464
else -> {}
445465
}
446466
}
467+
468+
fun Any.onBreak(
469+
alwaysListen: Boolean = false,
470+
block: SafeContext.() -> Unit
471+
) = listen<UpdateManagerEvent.Break.Pre>(0, alwaysListen) { block() }
472+
473+
fun Any.onBreakPost(
474+
alwaysListen: Boolean = false,
475+
block: SafeContext.() -> Unit
476+
) = listen<UpdateManagerEvent.Break.Post>(0, alwaysListen) { block() }
477+
478+
override fun preEvent() = UpdateManagerEvent.Break.Pre().post()
479+
override fun postEvent() = UpdateManagerEvent.Break.Post().post()
447480
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import com.lambda.interaction.request.Priority
2323
import com.lambda.interaction.request.Request
2424
import com.lambda.interaction.request.hotbar.HotbarConfig
2525
import com.lambda.interaction.request.rotation.RotationConfig
26+
import com.lambda.threading.runSafe
27+
import com.lambda.util.BlockUtils.blockState
2628

2729
data class BreakRequest(
2830
val contexts: List<BreakContext>,
@@ -33,5 +35,9 @@ data class BreakRequest(
3335
val onBreak: () -> Unit
3436
) : Request(prio) {
3537
override val done: Boolean
36-
get() = false
38+
get() = runSafe {
39+
contexts.all {
40+
ctx -> ctx.targetState.matches(blockState(ctx.expectedPos), ctx.expectedPos, world)
41+
}
42+
} == true
3743
}

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

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

2020
import com.lambda.core.Loadable
21+
import com.lambda.event.EventFlow.post
2122
import com.lambda.event.events.InventoryEvent
2223
import com.lambda.event.events.TickEvent
24+
import com.lambda.event.events.UpdateManagerEvent
2325
import com.lambda.event.listener.SafeListener.Companion.listen
2426
import com.lambda.interaction.request.RequestHandler
2527
import com.lambda.threading.runSafe
@@ -59,4 +61,7 @@ object HotbarManager : RequestHandler<HotbarRequest>(), Loadable {
5961
}
6062
}
6163
}
64+
65+
override fun preEvent() = UpdateManagerEvent.Hotbar.Pre().post()
66+
override fun postEvent() = UpdateManagerEvent.Hotbar.Post().post()
6267
}

0 commit comments

Comments
 (0)