Skip to content

Commit 2e54ef1

Browse files
committed
move context collection actions into the managers and improve activity checks from other managers
1 parent 26b5371 commit 2e54ef1

File tree

5 files changed

+59
-46
lines changed

5 files changed

+59
-46
lines changed

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

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ import com.lambda.interaction.request.breaking.BreakManager.breaks
5656
import com.lambda.interaction.request.breaking.BreakManager.canAccept
5757
import com.lambda.interaction.request.breaking.BreakManager.checkForCancels
5858
import com.lambda.interaction.request.breaking.BreakManager.initNewBreak
59-
import com.lambda.interaction.request.breaking.BreakManager.maxInstantBreaksThisTick
59+
import com.lambda.interaction.request.breaking.BreakManager.maxBreaksThisTick
6060
import com.lambda.interaction.request.breaking.BreakManager.processNewBreak
6161
import com.lambda.interaction.request.breaking.BreakManager.processRequest
6262
import com.lambda.interaction.request.breaking.BreakManager.simulateAbandoned
@@ -94,6 +94,7 @@ import net.minecraft.util.math.BlockPos
9494
import net.minecraft.util.math.Box
9595
import net.minecraft.world.BlockView
9696
import kotlin.math.max
97+
import kotlin.math.min
9798

9899
object BreakManager : RequestHandler<BreakRequest>(
99100
0,
@@ -150,8 +151,8 @@ object BreakManager : RequestHandler<BreakRequest>(
150151
private val rotated get() = rotationRequest?.done != false
151152

152153
private var breakCooldown = 0
153-
var instantBreaksThisTick = 0
154-
private var maxInstantBreaksThisTick = 0
154+
var breaksThisTick = 0
155+
private var maxBreaksThisTick = 0
155156

156157
private var breaks = mutableListOf<BreakContext>()
157158

@@ -178,7 +179,7 @@ object BreakManager : RequestHandler<BreakRequest>(
178179
}
179180
activeRequest = null
180181
breaks = mutableListOf()
181-
instantBreaksThisTick = 0
182+
breaksThisTick = 0
182183
}
183184

184185
listen<WorldEvent.BlockUpdate.Server>(priority = Int.MIN_VALUE) { event ->
@@ -292,7 +293,7 @@ object BreakManager : RequestHandler<BreakRequest>(
292293
* @see processRequest
293294
*/
294295
override fun SafeContext.handleRequest(request: BreakRequest) {
295-
if (activeRequest != null || PlaceManager.activeThisTick || InteractionManager.activeThisTick || request.contexts.isEmpty()) return
296+
if (activeRequest != null || request.contexts.isEmpty()) return
296297

297298
activeRequest = request
298299
processRequest(request)
@@ -309,6 +310,8 @@ object BreakManager : RequestHandler<BreakRequest>(
309310
* @see updateBreakProgress
310311
*/
311312
private fun SafeContext.processRequest(breakRequest: BreakRequest?) {
313+
if (PlaceManager.activeThisTick || InteractionManager.activeThisTick) return
314+
312315
breakRequest?.let { request ->
313316
logger.debug("Processing request", breakRequest)
314317
if (request.fresh) populateFrom(request)
@@ -344,15 +347,15 @@ object BreakManager : RequestHandler<BreakRequest>(
344347
if (activeRequest != null) logger.debug("Clearing active request", activeRequest)
345348
activeRequest = null
346349
}
347-
if (instantBreaksThisTick > 0 || activeInfos.isNotEmpty()) {
350+
if (breaksThisTick > 0 || activeInfos.isNotEmpty()) {
348351
activeThisTick = true
349352
}
350353
}
351354

352355
/**
353356
* Filters the requests [BreakContext]s, and iterates over the [breakInfos] collection looking for matches
354357
* in positions. If a match is found, the [BreakInfo] is updated with the new context. Otherwise, the break is cancelled.
355-
* The [instantBreaks] and [breaks] collections are then populated with the new appropriate contexts, and the [maxInstantBreaksThisTick]
358+
* The [instantBreaks] and [breaks] collections are then populated with the new appropriate contexts, and the [maxBreaksThisTick]
356359
* value is set.
357360
*
358361
* @see canAccept
@@ -363,39 +366,48 @@ object BreakManager : RequestHandler<BreakRequest>(
363366
// Sanitize the new breaks
364367
val newBreaks = request.contexts
365368
.distinctBy { it.blockPos }
369+
.filter { canAccept(it) }
366370
.toMutableList()
367371

368372
// Update the current break infos
369373
breakInfos
370374
.filterNotNull()
371375
.forEach { info ->
372-
newBreaks.find { ctx -> ctx.blockPos == info.context.blockPos && canAccept(ctx) }?.let { ctx ->
373-
if ((!info.updatedThisTick || info.type == RedundantSecondary) || info.abandoned) {
374-
logger.debug("Updating info", info, ctx)
375-
if (info.type == RedundantSecondary)
376-
info.request.onStart?.invoke(info.context.blockPos)
377-
else if (info.abandoned) {
378-
info.abandoned = false
379-
info.request.onStart?.invoke(info.context.blockPos)
380-
} else
381-
info.request.onUpdate?.invoke(info.context.blockPos)
382-
383-
info.updateInfo(ctx, request)
376+
newBreaks
377+
.find { ctx -> ctx.blockPos == info.context.blockPos }
378+
?.let { ctx ->
379+
if ((!info.updatedThisTick || info.type == RedundantSecondary) || info.abandoned) {
380+
logger.debug("Updating info", info, ctx)
381+
if (info.type == RedundantSecondary)
382+
info.request.onStart?.invoke(info.context.blockPos)
383+
else if (info.abandoned) {
384+
info.abandoned = false
385+
info.request.onStart?.invoke(info.context.blockPos)
386+
} else
387+
info.request.onUpdate?.invoke(info.context.blockPos)
388+
389+
info.updateInfo(ctx, request)
390+
}
391+
newBreaks.remove(ctx)
392+
return@forEach
384393
}
385-
newBreaks.remove(ctx)
386-
return@forEach
387-
}
388394
}
389395

396+
val breakConfig = request.config
397+
390398
breaks = newBreaks
391399
.sortedByDescending { it.instantBreak }
400+
.take(
401+
min(
402+
breakConfig.maxPendingBreaks - pendingBreakCount,
403+
request.build.maxPendingInteractions - request.pendingInteractions.size
404+
).coerceAtLeast(0)
405+
)
392406
.toMutableList()
393407

394408
logger.debug("${breaks.size} unprocessed breaks")
395409

396-
val breakConfig = request.config
397-
val pendingLimit = (breakConfig.maxPendingBreaks - pendingBreakCount).coerceAtLeast(0)
398-
maxInstantBreaksThisTick = breakConfig.breaksPerTick.coerceAtMost(pendingLimit)
410+
maxBreaksThisTick = breakConfig.breaksPerTick
399411
}
400412

401413
/**
@@ -404,11 +416,6 @@ object BreakManager : RequestHandler<BreakRequest>(
404416
private fun SafeContext.canAccept(newCtx: BreakContext): Boolean {
405417
if (activeInfos.none { it.context.blockPos == newCtx.blockPos } && isPosBlocked(newCtx.blockPos)) return false
406418

407-
if (newCtx.instantBreak && instantBreaksThisTick > maxInstantBreaksThisTick) return false
408-
409-
if (!currentStackSelection.filterStack(player.inventory.getStack(newCtx.hotbarIndex)))
410-
return false
411-
412419
val blockState = blockState(newCtx.blockPos)
413420
val hardness = newCtx.cachedState.getHardness(world, newCtx.blockPos)
414421

@@ -462,7 +469,8 @@ object BreakManager : RequestHandler<BreakRequest>(
462469
*/
463470
private fun SafeContext.processNewBreak(request: BreakRequest): Boolean {
464471
breaks.forEach { ctx ->
465-
if (!canAccept(ctx)) return@forEach
472+
if (breaksThisTick >= maxBreaksThisTick) return false
473+
if (!currentStackSelection.filterStack(player.inventory.getStack(ctx.hotbarIndex))) return@forEach
466474

467475
initNewBreak(ctx, request) ?: return false
468476
breaks.remove(ctx)
@@ -582,7 +590,7 @@ object BreakManager : RequestHandler<BreakRequest>(
582590
info.startPending()
583591
}
584592
}
585-
instantBreaksThisTick++
593+
breaksThisTick++
586594
info.nullify()
587595
}
588596

@@ -808,7 +816,6 @@ object BreakManager : RequestHandler<BreakRequest>(
808816
info.vanillaInstantBreakable = progress >= 1 && swapped
809817
onBlockBreak(info)
810818
if (!info.vanillaInstantBreakable) breakCooldown = info.breakConfig.breakDelay
811-
instantBreaksThisTick++
812819
} else {
813820
logger.debug("Starting break", info)
814821
info.apply {

src/main/kotlin/com/lambda/interaction/request/breaking/RebreakHandler.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ object RebreakHandler {
9797
if (reBreak.breakConfig.swing.isEnabled()) {
9898
swingHand(reBreak.breakConfig.swingType, Hand.MAIN_HAND)
9999
}
100-
BreakManager.instantBreaksThisTick++
100+
BreakManager.breaksThisTick++
101101
RebreakResult.Rebroke
102102
} else {
103103
RebreakResult.StillBreaking(reBreak)

src/main/kotlin/com/lambda/interaction/request/interacting/InteractionManager.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,16 @@ object InteractionManager : RequestHandler<InteractRequest>(
9090
}
9191

9292
override fun SafeContext.handleRequest(request: InteractRequest) {
93-
if (activeRequest != null || BreakManager.activeThisTick || PlaceManager.activeThisTick) return
93+
if (activeRequest != null || request.contexts.isEmpty()) return
9494

9595
activeRequest = request
9696
processRequest(request)
9797
if (interactionsThisTick > 0) activeThisTick = true
9898
}
9999

100100
fun SafeContext.processRequest(request: InteractRequest) {
101+
if (BreakManager.activeThisTick || PlaceManager.activeThisTick) return
102+
101103
logger.debug("Processing request", request)
102104

103105
if (request.fresh) populateFrom(request)
@@ -139,13 +141,14 @@ object InteractionManager : RequestHandler<InteractRequest>(
139141
logger.debug("Populating from request", request)
140142
setPendingConfigs(request.build)
141143
potentialInteractions = request.contexts
144+
.distinctBy { it.blockPos }
142145
.filter { !isPosBlocked(it.blockPos) }
146+
.take((request.build.maxPendingInteractions - pendingActions.size).coerceAtLeast(0))
143147
.toMutableList()
144148

145149
logger.debug("${potentialInteractions.size} potential interactions")
146150

147-
val pendingLimit = (request.build.maxPendingInteractions - pendingActions.size).coerceAtLeast(0)
148-
maxInteractionsThisTick = (request.build.interactionsPerTick.coerceAtMost(pendingLimit))
151+
maxInteractionsThisTick = request.build.interactionsPerTick
149152
}
150153

151154
override fun preEvent() = UpdateManagerEvent.Interact.post()

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ import net.minecraft.util.hit.BlockHitResult
6565
import net.minecraft.util.math.BlockPos
6666
import net.minecraft.util.math.Direction
6767
import net.minecraft.world.GameMode
68+
import kotlin.math.min
6869

6970
object PlaceManager : RequestHandler<PlaceRequest>(
7071
0,
@@ -124,7 +125,7 @@ object PlaceManager : RequestHandler<PlaceRequest>(
124125
* @see processRequest
125126
*/
126127
override fun SafeContext.handleRequest(request: PlaceRequest) {
127-
if (activeRequest != null || BreakManager.activeThisTick || InteractionManager.activeThisTick) return
128+
if (activeRequest != null || request.contexts.isEmpty()) return
128129

129130
activeRequest = request
130131
processRequest(request)
@@ -142,6 +143,8 @@ object PlaceManager : RequestHandler<PlaceRequest>(
142143
* @see placeBlock
143144
*/
144145
fun SafeContext.processRequest(request: PlaceRequest) {
146+
if (BreakManager.activeThisTick || InteractionManager.activeThisTick) return
147+
145148
logger.debug("Processing request", request)
146149

147150
if (request.fresh) populateFrom(request)
@@ -185,12 +188,18 @@ object PlaceManager : RequestHandler<PlaceRequest>(
185188
logger.debug("Populating from request", request)
186189
setPendingConfigs(request.build)
187190
potentialPlacements = request.contexts
191+
.distinctBy { it.blockPos }
188192
.filter { !isPosBlocked(it.blockPos) }
193+
.take(
194+
min(
195+
request.maxPendingPlacements - pendingActions.size,
196+
request.build.maxPendingInteractions - request.pendingInteractions.size
197+
).coerceAtLeast(0)
198+
)
189199
.toMutableList()
190200
logger.debug("${potentialPlacements.size} potential placements")
191201

192-
val pendingLimit = (request.maxPendingPlacements - pendingActions.size).coerceAtLeast(0)
193-
maxPlacementsThisTick = (request.placementsPerTick.coerceAtMost(pendingLimit))
202+
maxPlacementsThisTick = request.placementsPerTick
194203
}
195204

196205
/**

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,6 @@ class BuildTask private constructor(
158158
is BreakResult.Break -> {
159159
val breakResults = resultsNotBlocked
160160
.filterIsInstance<BreakResult.Break>()
161-
.distinctBy { it.blockPos }
162-
.take(emptyPendingInteractionSlots)
163161
.map { it.context }
164162

165163
breakRequest(
@@ -175,17 +173,13 @@ class BuildTask private constructor(
175173
is PlaceResult.Place -> {
176174
val placeResults = resultsNotBlocked
177175
.filterIsInstance<PlaceResult.Place>()
178-
.distinctBy { it.blockPos }
179-
.take(emptyPendingInteractionSlots)
180176
.map { it.context }
181177

182178
PlaceRequest(placeResults, pendingInteractions, build, hotbar, rotation) { placements++ }.submit()
183179
}
184180
is InteractResult.Interact -> {
185181
val interactResults = resultsNotBlocked
186182
.filterIsInstance<InteractResult.Interact>()
187-
.distinctBy { it.blockPos }
188-
.take(emptyPendingInteractionSlots)
189183
.map { it.context }
190184

191185
InteractRequest(interactResults, null, pendingInteractions, build.interacting, build, hotbar, rotation).submit()

0 commit comments

Comments
 (0)