Skip to content

Commit ba5b7d6

Browse files
committed
refactors and build task cleanup / small fixes
1 parent f577f0d commit ba5b7d6

File tree

4 files changed

+64
-47
lines changed

4 files changed

+64
-47
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import com.lambda.context.SafeContext
2323
import com.lambda.graphics.renderer.esp.DirectionMask
2424
import com.lambda.graphics.renderer.esp.DirectionMask.exclude
2525
import com.lambda.interaction.construction.verify.TargetState
26+
import com.lambda.interaction.request.rotation.RotationConfig
2627
import com.lambda.interaction.request.rotation.RotationRequest
2728
import com.lambda.util.world.raycast.RayCastUtils.distanceTo
2829
import net.minecraft.block.BlockState
@@ -44,6 +45,7 @@ data class BreakContext(
4445
override var hotbarIndex: Int,
4546
val instantBreak: Boolean,
4647
val buildConfig: BuildConfig,
48+
val rotationConfig: RotationConfig,
4749
val inventoryConfig: InventoryConfig
4850
) : BuildContext {
4951
private val baseColor = Color(222, 0, 0, 25)

common/src/main/kotlin/com/lambda/interaction/construction/simulation/BuildSimulator.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ object BuildSimulator {
412412
player.inventory.selectedSlot,
413413
instantBreakable(state, pos),
414414
build,
415+
rotation,
415416
inventory
416417
)
417418
acc.add(BreakResult.Break(pos, breakContext))
@@ -460,7 +461,7 @@ object BuildSimulator {
460461
val instant = instantBreakable(state, pos)
461462

462463
val breakContext = BreakContext(
463-
eye, blockHit, request, state, targetState, player.inventory.selectedSlot, instant, build, inventory
464+
eye, blockHit, request, state, targetState, player.inventory.selectedSlot, instant, build, rotation, inventory
464465
)
465466

466467
if (player.isCreative) {

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

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@
1717

1818
package com.lambda.interaction.request.breaking
1919

20-
import com.lambda.config.groups.BuildConfig
2120
import com.lambda.context.SafeContext
2221
import com.lambda.event.events.TickEvent
2322
import com.lambda.event.events.WorldEvent
2423
import com.lambda.event.listener.SafeListener.Companion.listen
2524
import com.lambda.interaction.construction.context.BreakContext
26-
import com.lambda.interaction.request.RequestConfig
2725
import com.lambda.interaction.request.RequestHandler
2826
import com.lambda.interaction.request.breaking.BreakConfig.BreakConfirmationMode
2927
import com.lambda.interaction.request.breaking.BreakConfig.BreakMode
28+
import com.lambda.interaction.request.rotation.RotationManager
29+
import com.lambda.interaction.request.rotation.RotationManager.onRotate
3030
import com.lambda.module.modules.client.TaskFlowModule
3131
import com.lambda.util.BlockUtils.blockState
3232
import com.lambda.util.BlockUtils.calcItemBlockBreakingDelta
@@ -44,44 +44,47 @@ import net.minecraft.network.packet.c2s.play.PlayerActionC2SPacket
4444
import net.minecraft.sound.SoundCategory
4545

4646
object BreakManager : RequestHandler<BreakRequest>() {
47-
var buildConfig: BuildConfig = TaskFlowModule.build
48-
var primaryBreakingInfo: BreakInfo?
47+
private var primaryBreakingInfo: BreakInfo?
4948
get() = breakingInfos[0]
5049
set(value) { breakingInfos[0] = value }
51-
var secondaryBreakingInfo: BreakInfo?
50+
private var secondaryBreakingInfo: BreakInfo?
5251
get() = breakingInfos[1]
5352
set(value) { breakingInfos[1] = value }
54-
val breakingInfos = arrayOfNulls<BreakInfo>(2)
53+
private val breakingInfos = arrayOfNulls<BreakInfo>(2)
5554

56-
val pendingInteractions = LimitedDecayQueue<BreakInfo>(
57-
buildConfig.maxPendingInteractions, buildConfig.interactionTimeout * 50L
55+
private val pendingInteractions = LimitedDecayQueue<BreakInfo>(
56+
TaskFlowModule.build.maxPendingInteractions, TaskFlowModule.build.interactionTimeout * 50L
5857
) { info("${it::class.simpleName} at ${it.context.expectedPos.toShortString()} timed out"); it.nullify() }
5958

6059
init {
61-
listen<TickEvent.Pre>(Int.MIN_VALUE) {
60+
listen<TickEvent.Pre>(Int.MIN_VALUE, alwaysListen = true) {
6261
if (updateRequest(true) { true }) {
6362
currentRequest?.let { request ->
6463
request.contexts.forEach { requestCtx ->
6564
if (requestCtx == null) return@forEach
6665
if (!canAccept(requestCtx)) return@forEach
6766

68-
primaryBreakingInfo?.let { primaryInfo ->
69-
if (!buildConfig.breakSettings.doubleBreak) return@let
70-
if (primaryInfo.startedWithSecondary) return@let
67+
primaryBreakingInfo?.let primaryInfo@ { primaryInfo ->
68+
if (!primaryInfo.breakSettings.doubleBreak) return@primaryInfo
69+
if (primaryInfo.startedWithSecondary) return@primaryInfo
7170
secondaryBreakingInfo = BreakInfo.SecondaryBreakInfo(requestCtx, request.onBreak)
7271
} ?: run {
7372
primaryBreakingInfo = BreakInfo.PrimaryBreakInfo(requestCtx, request.onBreak)
73+
pendingInteractions.setMaxSize(requestCtx.buildConfig.maxPendingInteractions)
74+
pendingInteractions.setDecayTime(requestCtx.buildConfig.interactionTimeout * 50L)
7475
}
7576
}
7677
}
7778
}
7879

79-
for (it in breakingInfos.reversed()) {
80+
for (info in breakingInfos.reversed()) {
8081
if (interaction.blockBreakingCooldown > 0) {
8182
interaction.blockBreakingCooldown--
8283
break
8384
}
84-
it?.let { info ->
85+
86+
info?.let { info ->
87+
if (info.breakSettings.rotateForBreak && !info.context.rotation.done) return@listen
8588
if (pendingInteractions.contains(info)) return@let
8689
updateBlockBreakingProgress(info, player.mainHandStack)
8790
if (info is BreakInfo.SecondaryBreakInfo)
@@ -90,13 +93,26 @@ object BreakManager : RequestHandler<BreakRequest>() {
9093
}
9194
}
9295

96+
onRotate {
97+
run forLoop@ {
98+
breakingInfos.forEach { info ->
99+
info?.let { info ->
100+
if (!info.breakSettings.rotateForBreak) return@let
101+
val rotate = info.context.rotation
102+
RotationManager.registerRequest(info.context.rotationConfig, rotate)
103+
return@forLoop
104+
}
105+
}
106+
}
107+
}
108+
93109
listen<WorldEvent.BlockUpdate.Server>(alwaysListen = true) { event ->
94110
var breakBlock = false
95111
val info = pendingInteractions
96112
.firstOrNull { it.context.expectedPos == event.pos }
97-
?.also {
98-
pendingInteractions.remove(it)
99-
if (buildConfig.breakSettings.breakConfirmation == BreakConfirmationMode.AwaitThenBreak)
113+
?.also { pending ->
114+
pendingInteractions.remove(pending)
115+
if (pending.breakSettings.breakConfirmation == BreakConfirmationMode.AwaitThenBreak)
100116
breakBlock = true
101117
}
102118
?: breakingInfos
@@ -109,7 +125,7 @@ object BreakManager : RequestHandler<BreakRequest>() {
109125
info.nullify()
110126

111127
if (!info.context.targetState.matches(event.newState, event.pos, world)) {
112-
this@BreakManager.warn("Update at ${event.pos.toShortString()} was rejected with ${event.newState} instead of ${info.context.targetState}")
128+
this@BreakManager.warn("Break at ${event.pos.toShortString()} was rejected with ${event.newState} instead of ${info.context.targetState}")
113129
return@listen
114130
}
115131
if (breakBlock) {
@@ -119,19 +135,10 @@ object BreakManager : RequestHandler<BreakRequest>() {
119135
}
120136
}
121137

122-
fun canAccept(ctx: BreakContext) =
138+
private fun canAccept(ctx: BreakContext) =
123139
pendingInteractions.none { it.context.expectedPos == ctx.expectedPos }
124140
&& breakingInfos.none { info -> info?.context?.expectedPos == ctx.expectedPos }
125141

126-
override fun updateRequest(
127-
keepIfNull: Boolean,
128-
filter: (Map.Entry<RequestConfig<BreakRequest>, BreakRequest>) -> Boolean
129-
): Boolean {
130-
val updatedCurrentRequest = super.updateRequest(keepIfNull, filter)
131-
buildConfig = currentRequest?.primaryContext?.buildConfig ?: TaskFlowModule.build
132-
return updatedCurrentRequest
133-
}
134-
135142
private fun SafeContext.updateBlockBreakingProgress(info: BreakInfo, item: ItemStack): Boolean {
136143
val ctx = info.context
137144
val hitResult = ctx.result
@@ -216,7 +223,7 @@ object BreakManager : RequestHandler<BreakRequest>() {
216223
onBlockBreak(info)
217224
PlayerActionC2SPacket(PlayerActionC2SPacket.Action.START_DESTROY_BLOCK, ctx.expectedPos, ctx.result.side, sequence)
218225
}
219-
interaction.blockBreakingCooldown = buildConfig.breakSettings.breakDelay
226+
interaction.blockBreakingCooldown = info.breakSettings.breakDelay
220227
return true
221228
}
222229
if (info.breaking) return false
@@ -262,7 +269,7 @@ object BreakManager : RequestHandler<BreakRequest>() {
262269
}
263270

264271
private fun SafeContext.onBlockBreak(info: BreakInfo) {
265-
when (info.context.buildConfig.breakSettings.breakConfirmation) {
272+
when (info.breakSettings.breakConfirmation) {
266273
BreakConfirmationMode.None -> {
267274
destroyBlock(info)
268275
info.onBreak()
@@ -318,6 +325,8 @@ object BreakManager : RequestHandler<BreakRequest>() {
318325
var breakingTicks = 0
319326
var soundsCooldown = 0.0f
320327
var startedWithSecondary = false
328+
val buildConfig = context.buildConfig
329+
val breakSettings = buildConfig.breakSettings
321330

322331
fun getBreakTextureProgress(player: PlayerEntity, world: ClientWorld): Int {
323332
val breakDelta = context.checkedState.calcItemBlockBreakingDelta(
@@ -327,18 +336,17 @@ object BreakManager : RequestHandler<BreakRequest>() {
327336
player.mainHandStack
328337
)
329338

330-
val progress = (breakDelta * breakingTicks) / buildConfig.breakSettings.breakThreshold
339+
val progress = (breakDelta * breakingTicks) / breakSettings.breakThreshold
331340
return if (progress > 0.0f) (progress * 10.0f).toInt() else -1
332341
}
333342

334-
open fun getBreakThreshold() =
335-
context.buildConfig.breakSettings.breakThreshold
343+
open fun getBreakThreshold() = breakSettings.breakThreshold
336344

337345
open fun nullify() {}
338346

339347
class PrimaryBreakInfo(
340348
ctx: BreakContext,
341-
onBreak: () -> Unit
349+
onBreak: () -> Unit,
342350
) : BreakInfo(ctx, onBreak) {
343351
override fun nullify() {
344352
primaryBreakingInfo = null
@@ -347,7 +355,7 @@ object BreakManager : RequestHandler<BreakRequest>() {
347355

348356
class SecondaryBreakInfo(
349357
ctx: BreakContext,
350-
onBreak: () -> Unit
358+
onBreak: () -> Unit,
351359
) : BreakInfo(ctx, onBreak) {
352360
override fun getBreakThreshold() =
353361
1.0f

common/src/main/kotlin/com/lambda/task/tasks/BuildTask.kt

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,7 @@ class BuildTask @Ta5kBuilder constructor(
8181
private var currentInteraction: BuildContext? = null
8282
private val instantBreaks = mutableSetOf<BreakContext>()
8383

84-
var breaking = false
85-
private var breakingTicks = 0
86-
private var soundsCooldown = 0.0f
84+
var breakRequest: BreakRequest? = null
8785

8886
private var placements = 0
8987
private var breaks = 0
@@ -105,6 +103,14 @@ class BuildTask @Ta5kBuilder constructor(
105103

106104
init {
107105
listen<TickEvent.Pre> {
106+
if (instantBreaks.isNotEmpty()) {
107+
instantBreaks.forEach { context ->
108+
BreakManager.registerRequest(build.breakSettings, BreakRequest(context) { breaks++ })
109+
}
110+
instantBreaks.clear()
111+
return@listen
112+
}
113+
108114
currentInteraction?.let { context ->
109115
// TaskFlowModule.drawables = listOf(context)
110116
if (context.shouldRotate(build) && !context.rotation.done) return@let
@@ -116,10 +122,6 @@ class BuildTask @Ta5kBuilder constructor(
116122
}
117123
}
118124
}
119-
instantBreaks.forEach { context ->
120-
BreakManager.registerRequest(build.breakSettings, BreakRequest(context) { breaks++ })
121-
}
122-
instantBreaks.clear()
123125

124126
dropsToCollect.firstOrNull()?.let { itemDrop ->
125127
if (!world.entities.contains(itemDrop)) {
@@ -215,6 +217,7 @@ class BuildTask @Ta5kBuilder constructor(
215217
if (pendingInteractions.size >= build.maxPendingInteractions) return@onRotate
216218

217219
currentInteraction = bestResult.context
220+
if (instantBreaks.isNotEmpty()) return@onRotate
218221
if (bestResult !is BreakResult.Break) return@onRotate
219222

220223
val breakRequest = BreakRequest(
@@ -223,6 +226,7 @@ class BuildTask @Ta5kBuilder constructor(
223226
prio = 0
224227
) { breaks++ }
225228
BreakManager.registerRequest(build.breakSettings, breakRequest)
229+
this@BuildTask.breakRequest = breakRequest
226230
}
227231

228232
is Resolvable -> {
@@ -232,10 +236,12 @@ class BuildTask @Ta5kBuilder constructor(
232236
}
233237
}
234238

235-
if (!build.rotateForPlace) return@onRotate
236-
val rotateTo = currentInteraction?.rotation ?: return@onRotate
237-
238-
rotation.request(rotateTo)
239+
currentInteraction?.let { currentInteraction ->
240+
if (currentInteraction is BreakContext) return@let
241+
if (!currentInteraction.shouldRotate(build)) return@onRotate
242+
val rotateTo = currentInteraction.rotation
243+
rotation.request(rotateTo)
244+
}
239245
}
240246

241247
listen<MovementEvent.InputUpdate> {

0 commit comments

Comments
 (0)