Skip to content

Commit 23aee7e

Browse files
committed
simulate break contexts for each break info independently in the break manager to keep up to date information
1 parent 49f0127 commit 23aee7e

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

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

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ import com.lambda.event.events.UpdateManagerEvent
2828
import com.lambda.event.events.WorldEvent
2929
import com.lambda.event.listener.SafeListener.Companion.listen
3030
import com.lambda.event.listener.UnsafeListener.Companion.listenUnsafe
31+
import com.lambda.interaction.construction.blueprint.Blueprint.Companion.toStructure
32+
import com.lambda.interaction.construction.blueprint.StaticBlueprint.Companion.toBlueprint
3133
import com.lambda.interaction.construction.context.BreakContext
34+
import com.lambda.interaction.construction.result.BreakResult
35+
import com.lambda.interaction.construction.simulation.BuildSimulator.simulate
3236
import com.lambda.interaction.construction.verify.TargetState
3337
import com.lambda.interaction.request.PositionBlocking
3438
import com.lambda.interaction.request.Priority
@@ -51,6 +55,7 @@ import com.lambda.util.player.gamemode
5155
import com.lambda.util.player.swingHand
5256
import net.minecraft.block.BlockState
5357
import net.minecraft.block.OperatorBlock
58+
import net.minecraft.client.network.ClientPlayerEntity
5459
import net.minecraft.client.sound.PositionedSoundInstance
5560
import net.minecraft.client.sound.SoundInstance
5661
import net.minecraft.client.world.ClientWorld
@@ -84,8 +89,7 @@ object BreakManager : RequestHandler<BreakRequest>(), PositionBlocking {
8489
get() = breakingInfos.mapNotNull { it?.context?.expectedPos } + pendingBreaks.map { it.context.expectedPos }
8590

8691
private var rotation: RotationRequest? = null
87-
private val validRotation
88-
get() = rotation?.done ?: true
92+
private var validRotation = false
8993

9094
private var blockBreakingCooldown = 0
9195

@@ -109,6 +113,10 @@ object BreakManager : RequestHandler<BreakRequest>(), PositionBlocking {
109113
listen<TickEvent.Pre>(priority = Int.MIN_VALUE + 1) {
110114
preEvent()
111115

116+
breakingInfos.forEach {
117+
it?.simulate(player)
118+
}
119+
112120
if (updateRequest()) currentRequest?.let request@ { request ->
113121
if (isOnBreakCooldown()) {
114122
blockBreakingCooldown--
@@ -149,12 +157,11 @@ object BreakManager : RequestHandler<BreakRequest>(), PositionBlocking {
149157
}
150158

151159
requestRotate()
152-
if (!validRotation) {
160+
if (!validRotation && rotation != null) {
153161
postEvent()
154162
return@listen
155163
}
156164

157-
// ToDo: dynamically update hotbarIndex as contexts are persistent and don't get updated by new requests each tick
158165
// Reversed so that the breaking order feels natural to the user as the primary break has to
159166
// be started after the secondary
160167
breakingInfos
@@ -169,6 +176,10 @@ object BreakManager : RequestHandler<BreakRequest>(), PositionBlocking {
169176
postEvent()
170177
}
171178

179+
listen<UpdateManagerEvent.Rotation.Post>(priority = Int.MIN_VALUE) {
180+
validRotation = rotation?.done ?: true
181+
}
182+
172183
listen<WorldEvent.BlockUpdate.Server> { event ->
173184
pendingBreaks
174185
.firstOrNull { it.context.expectedPos == event.pos }
@@ -253,8 +264,7 @@ object BreakManager : RequestHandler<BreakRequest>(), PositionBlocking {
253264

254265
private fun requestRotate() {
255266
rotation = breakingInfos
256-
.filterNotNull()
257-
.firstOrNull { it.breakConfig.rotateForBreak }
267+
.firstOrNull { it?.breakConfig?.rotateForBreak == true }
258268
?.let { info ->
259269
info.rotationConfig.request(RotationRequest(info.context.rotation, info.rotationConfig))
260270
}
@@ -497,13 +507,18 @@ object BreakManager : RequestHandler<BreakRequest>(), PositionBlocking {
497507
}
498508

499509
data class BreakInfo(
500-
val context: BreakContext,
510+
var context: BreakContext,
501511
var type: BreakType,
502512
val request: BreakRequest
503513
) {
514+
// I hate this...
504515
val breakConfig = request.buildConfig.breakSettings
505516
val rotationConfig = request.rotationConfig
517+
private val interactionConfig = request.interactionConfig
518+
private val buildConfig = request.buildConfig
519+
private val inventoryConfig = request.inventoryConfig
506520
private val hotbarConfig = request.hotbarConfig
521+
507522
val pendingInteractionsList = request.pendingInteractionsList
508523
private val onBreak = request.onBreak
509524
private val onItemDrop = request.onItemDrop
@@ -543,6 +558,19 @@ object BreakManager : RequestHandler<BreakRequest>(), PositionBlocking {
543558
fun requestHotbarSwap() =
544559
hotbarConfig.request(HotbarRequest(context.hotbarIndex)).done
545560

561+
fun simulate(player: ClientPlayerEntity) {
562+
val result = context.expectedPos
563+
.toStructure(context.targetState)
564+
.toBlueprint()
565+
.simulate(player.eyePos, interactionConfig, rotationConfig, inventoryConfig, buildConfig)
566+
.firstOrNull()
567+
?: return
568+
569+
if (result is BreakResult.Break) {
570+
context = result.context
571+
}
572+
}
573+
546574
fun getBreakTextureProgress(player: PlayerEntity, world: ClientWorld): Int {
547575
val breakDelta = context.checkedState.calcItemBlockBreakingDelta(player, world, context.expectedPos, player.mainHandStack)
548576
val progress = (breakDelta * breakingTicks) / breakConfig.breakThreshold

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

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

2020
import com.lambda.config.groups.BuildConfig
21+
import com.lambda.config.groups.InteractionConfig
22+
import com.lambda.config.groups.InventoryConfig
2123
import com.lambda.interaction.construction.context.BreakContext
2224
import com.lambda.interaction.construction.context.BuildContext
2325
import com.lambda.interaction.request.Priority
@@ -32,6 +34,8 @@ data class BreakRequest(
3234
val contexts: Collection<BreakContext>,
3335
val buildConfig: BuildConfig,
3436
val rotationConfig: RotationConfig,
37+
val interactionConfig: InteractionConfig,
38+
val inventoryConfig: InventoryConfig,
3539
val hotbarConfig: HotbarConfig,
3640
val prio: Priority = 0,
3741
val pendingInteractionsList: MutableCollection<BuildContext>,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ class BuildTask @Ta5kBuilder constructor(
152152
if (instantBreakResults.isNotEmpty()) {
153153
build.breakSettings.request(
154154
BreakRequest(
155-
instantBreakResults.map { it.context }, build, rotation, hotbar,
155+
instantBreakResults.map { it.context }, build, rotation, interact, inventory, hotbar,
156156
pendingInteractionsList = pendingInteractions,
157157
onBreak = { breaks++ },
158158
onItemDrop = onItemDrop
@@ -163,7 +163,7 @@ class BuildTask @Ta5kBuilder constructor(
163163
}
164164

165165
val request = BreakRequest(
166-
breakResults.map { it.context }, build, rotation, hotbar,
166+
breakResults.map { it.context }, build, rotation, interact, inventory, hotbar,
167167
pendingInteractionsList = pendingInteractions,
168168
onBreak = { breaks++ },
169169
onItemDrop = onItemDrop

0 commit comments

Comments
 (0)