Skip to content

Commit 26b8c78

Browse files
committed
now or nothing requests
1 parent 91f75b3 commit 26b8c78

File tree

16 files changed

+147
-99
lines changed

16 files changed

+147
-99
lines changed

src/main/java/com/lambda/mixin/entity/PlayerInventoryMixin.java

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,19 @@
1818
package com.lambda.mixin.entity;
1919

2020
import com.lambda.interaction.request.hotbar.HotbarManager;
21-
import com.lambda.interaction.request.hotbar.HotbarRequest;
2221
import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
2322
import net.minecraft.entity.player.PlayerInventory;
2423
import org.objectweb.asm.Opcodes;
2524
import org.spongepowered.asm.mixin.Mixin;
2625
import org.spongepowered.asm.mixin.injection.At;
27-
import org.spongepowered.asm.mixin.injection.Inject;
28-
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
2926

3027
@Mixin(PlayerInventory.class)
3128
public class PlayerInventoryMixin {
32-
@SuppressWarnings({"MixinAnnotationTarget", "UnresolvedMixinReference"})
29+
@SuppressWarnings({"MixinAnnotationTarget"})
3330
@ModifyExpressionValue(method = "*", at = @At(value = "FIELD", target = "Lnet/minecraft/entity/player/PlayerInventory;selectedSlot:I", opcode = Opcodes.GETFIELD))
3431
private int modifySelectedSlot(int original) {
35-
final HotbarRequest hotbarRequest = HotbarManager.INSTANCE.getActiveRequest();
36-
if (hotbarRequest == null) return original;
37-
return hotbarRequest.getSlot();
38-
}
39-
40-
@Inject(method = "getSelectedSlot", at = @At("HEAD"), cancellable = true)
41-
private void redirectGetSelectedSlot(CallbackInfoReturnable<Integer> cir) {
42-
final HotbarRequest hotbarRequest = HotbarManager.INSTANCE.getActiveRequest();
43-
if (hotbarRequest == null) return;
44-
cir.setReturnValue(hotbarRequest.getSlot());
32+
final int hotbarSlot = HotbarManager.getActiveSlot();
33+
if (hotbarSlot == -1) return original;
34+
return hotbarSlot;
4535
}
4636
}

src/main/kotlin/com/lambda/context/AutomationConfig.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ object AutomationConfig : Configurable(LambdaConfig), Automated {
6060
override val eatConfig = EatSettings(this, Group.Eat)
6161

6262
val avoidDesync by setting("Avoid Desync", true, "Cancels incoming inventory update packets if they match previous actions").group(Group.Debug)
63-
val maxDesyncCache by setting("Max Desync Cache", 30, 1..30, 1, "Maximum cached previous inventory actions") { avoidDesync }.group(Group.Debug)
6463
val desyncTimeout by setting("Desync Timeout", 30, 1..30, 1, unit = " ticks", description = "Time to store previous inventory actions before dropping the cache") { avoidDesync }.group(Group.Debug)
6564
val showAllEntries by setting("Show All Entries", false, "Show all entries in the task tree").group(Group.Debug)
6665
val shrinkFactor by setting("Shrink Factor", 0.001, 0.0..1.0, 0.001).group(Group.Debug)

src/main/kotlin/com/lambda/interaction/request/Request.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ abstract class Request : Automated {
3030
abstract val requestId: Int
3131
var fresh = true
3232

33+
abstract val nowOrNothing: Boolean
34+
3335
abstract val done: Boolean
3436

3537
abstract fun submit(queueIfClosed: Boolean = true): Request

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

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -307,15 +307,21 @@ object BreakManager : RequestHandler<BreakRequest>(
307307

308308
/**
309309
* Attempts to accept and process the request, if there is not already an [activeRequest] and the
310-
* [BreakRequest.contexts] collection is not empty.
310+
* [BreakRequest.contexts] collection is not empty. If nowOrNothing is true, the request is cleared
311+
* after the first process.
311312
*
312313
* @see processRequest
313314
*/
314315
override fun AutomatedSafeContext.handleRequest(request: BreakRequest) {
315316
if (activeRequest != null || request.contexts.isEmpty()) return
317+
if (PlaceManager.activeThisTick || InteractionManager.activeThisTick) return
316318

317319
activeRequest = request
318320
processRequest(request)
321+
if (request.nowOrNothing) {
322+
activeRequest = null
323+
breaks = mutableListOf()
324+
}
319325
}
320326

321327
/**
@@ -328,8 +334,6 @@ object BreakManager : RequestHandler<BreakRequest>(
328334
* @see updateBreakProgress
329335
*/
330336
private fun SafeContext.processRequest(request: BreakRequest?) {
331-
if (PlaceManager.activeThisTick || InteractionManager.activeThisTick) return
332-
333337
request?.let { request ->
334338
logger.debug("Processing request", request)
335339
if (request.fresh) populateFrom(request)
@@ -385,7 +389,7 @@ object BreakManager : RequestHandler<BreakRequest>(
385389
// Sanitize the new breaks
386390
val newBreaks = request.contexts
387391
.distinctBy { it.blockPos }
388-
.filter { canAccept(it) }
392+
.filter { canAccept(it) && (!request.nowOrNothing || it.instantBreak) }
389393
.toMutableList()
390394

391395
// Update the current break infos
@@ -705,11 +709,7 @@ object BreakManager : RequestHandler<BreakRequest>(
705709
info.progressedThisTick = true
706710

707711
if (!info.breaking) {
708-
if (breakConfig.swapMode.isEnabled() &&
709-
(breakConfig.swapMode != BreakConfig.SwapMode.End ||
710-
info.context.instantBreak ||
711-
info.rebreakPotential.isPossible()) &&
712-
!swapped) return
712+
if (info.swapInfo.swap && !swapped) return
713713
if (!startBreaking(info)) {
714714
info.nullify()
715715
info.request.onCancel?.invoke(this, ctx.blockPos)
@@ -736,7 +736,7 @@ object BreakManager : RequestHandler<BreakRequest>(
736736
return
737737
}
738738

739-
if (breakConfig.swapMode.isEnabled() && breakConfig.swapMode == BreakConfig.SwapMode.Constant && !swapped) return
739+
if (breakConfig.swapMode == BreakConfig.SwapMode.Constant && !swapped) return
740740

741741
info.breakingTicks++
742742
val breakDelta = blockState.calcBreakDelta(ctx.blockPos)
@@ -770,7 +770,7 @@ object BreakManager : RequestHandler<BreakRequest>(
770770

771771
val swing = breakConfig.swing
772772
if (progress >= info.getBreakThreshold()) {
773-
if (breakConfig.swapMode.isEnabled() && breakConfig.swapMode != BreakConfig.SwapMode.Start && !swapped) return
773+
if (info.swapInfo.swap && !swapped) return
774774

775775
logger.success("Breaking", info)
776776
onBlockBreak(info)
@@ -782,8 +782,6 @@ object BreakManager : RequestHandler<BreakRequest>(
782782
if (swing == BreakConfig.SwingMode.Constant)
783783
swingHand(breakConfig.swingType, Hand.MAIN_HAND)
784784
}
785-
786-
return
787785
}
788786

789787
/**

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ import net.minecraft.util.math.BlockPos
4747
data class BreakRequest private constructor(
4848
val contexts: Collection<BreakContext>,
4949
val pendingInteractions: MutableCollection<BuildContext>,
50-
private val automated: Automated
50+
private val automated: Automated,
51+
override val nowOrNothing: Boolean = false
5152
) : Request(), LogContext, Automated by automated {
5253
override val requestId = ++requestCount
5354

@@ -88,9 +89,10 @@ data class BreakRequest private constructor(
8889
class BreakRequestBuilder(
8990
contexts: Collection<BreakContext>,
9091
pendingInteractions: MutableCollection<BuildContext>,
92+
nowOrNothing: Boolean,
9193
automated: Automated
9294
) {
93-
val request = BreakRequest(contexts, pendingInteractions, automated)
95+
val request = BreakRequest(contexts, pendingInteractions, automated, nowOrNothing)
9496

9597
@BreakRequestDsl
9698
fun onStart(callback: SafeContext.(BlockPos) -> Unit) {
@@ -135,8 +137,9 @@ data class BreakRequest private constructor(
135137
fun Automated.breakRequest(
136138
contexts: Collection<BreakContext>,
137139
pendingInteractions: MutableCollection<BuildContext>,
140+
nowOrNothing: Boolean = false,
138141
builder: (BreakRequestBuilder.() -> Unit)? = null
139-
) = BreakRequestBuilder(contexts, pendingInteractions, this).apply { builder?.invoke(this) }.build()
142+
) = BreakRequestBuilder(contexts, pendingInteractions, nowOrNothing, this).apply { builder?.invoke(this) }.build()
140143

141144
@BreakRequestDsl
142145
private fun BreakRequestBuilder.build(): BreakRequest = request

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

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ import com.lambda.interaction.request.ManagerUtils.newStage
2929
import com.lambda.interaction.request.ManagerUtils.newTick
3030
import com.lambda.interaction.request.RequestHandler
3131
import com.lambda.interaction.request.hotbar.HotbarManager.activeRequest
32+
import com.lambda.interaction.request.hotbar.HotbarManager.activeSlot
3233
import com.lambda.interaction.request.hotbar.HotbarManager.checkResetSwap
33-
import com.lambda.interaction.request.hotbar.HotbarManager.maxSwapsThisTick
3434
import com.lambda.interaction.request.hotbar.HotbarManager.setActiveRequest
35+
import com.lambda.interaction.request.hotbar.HotbarManager.setActiveSlot
3536
import com.lambda.module.hud.ManagerDebugLoggers.hotbarManagerLogger
3637
import com.lambda.threading.runSafe
3738
import net.minecraft.item.ItemStack
@@ -50,12 +51,20 @@ object HotbarManager : RequestHandler<HotbarRequest>(
5051
TickEvent.Input.Pre,
5152
TickEvent.Input.Post,
5253
TickEvent.Player.Post,
53-
onOpen = { if (HotbarManager.activeRequest != null) HotbarManager.logger.newStage(HotbarManager.tickStage) },
54+
onOpen = {
55+
if (activeRequest != null) {
56+
setActiveSlot()
57+
HotbarManager.logger.newStage(HotbarManager.tickStage)
58+
}
59+
},
5460
onClose = { checkResetSwap() }
5561
), Logger {
62+
var activeRequest: HotbarRequest? = null
63+
@JvmStatic var activeSlot: Int = -1
64+
5665
val serverSlot get() = runSafe {
5766
interaction.lastSelectedSlot
58-
} ?: 0
67+
} ?: -1
5968
//ToDo: something to manage stacks so the hotbar manager is strictly index based
6069
private var previousStack: ItemStack? = null
6170
private var swappedTicks = 0
@@ -64,8 +73,6 @@ object HotbarManager : RequestHandler<HotbarRequest>(
6473
private var maxSwapsThisTick = 0
6574
private var swapDelay = 0
6675

67-
var activeRequest: HotbarRequest? = null
68-
6976
override val logger = hotbarManagerLogger
7077

7178
override fun load(): String {
@@ -86,6 +93,7 @@ object HotbarManager : RequestHandler<HotbarRequest>(
8693
previousStack = currentStack
8794

8895
val activeInfo = activeRequest ?: return@listen
96+
if (activeInfo.slot != activeSlot) return@listen
8997
activeInfo.swapPauseAge = swappedTicks
9098
activeInfo.activeRequestAge++
9199
activeInfo.keepTicks--
@@ -95,52 +103,60 @@ object HotbarManager : RequestHandler<HotbarRequest>(
95103
}
96104

97105
/**
98-
* Attempts to accept the request and process it. If the [activeRequest] is not null, the new [request] matches hotbar index,
99-
* and the new request has an equal or longer [HotbarRequest.keepTicks] than the current request, the new request is accepted.
100-
* Otherwise, if the [activeRequest] is null, or is from an old request, assuming the swap doesn't exceed [maxSwapsThisTick],
101-
* the request is accepted.
106+
* If the [activeRequest] is not null, being kept, and the [request]'s slot matches the [activeSlot], the
107+
* [request]'s swapPauseAge is set to the swapped ticks and the request is denied. Otherwise, the active
108+
* request is set and it attempts to set the active slot.
102109
*
103110
* @see setActiveRequest
111+
* @see setActiveSlot
104112
*/
105113
override fun AutomatedSafeContext.handleRequest(request: HotbarRequest) {
106114
logger.debug("Handling request:", request)
107115

108-
if (tickStage !in hotbarConfig.sequenceStageMask) return
116+
if (request.nowOrNothing && tickStage !in hotbarConfig.sequenceStageMask) return
109117

110118
activeRequest?.let { active ->
111-
if (request.slot == serverSlot && request.keepTicks >= active.keepTicks) {
112-
logger.debug("Request is the same as current, but longer or the same keep time", request)
113-
setActiveRequest(request)
119+
if (active.activeRequestAge <= 0 && active.keepTicks > 0) {
120+
if (activeSlot == request.slot) request.swapPauseAge = swappedTicks
114121
return
115122
}
116-
117-
if (active.activeRequestAge <= 0 && active.keepTicks > 0) return
118-
} ?: run { maxSwapsThisTick = hotbarConfig.swapsPerTick }
119-
120-
if (request.slot != serverSlot)
121-
if (swapsThisTick + 1 > maxSwapsThisTick || swapDelay > 0) return
123+
}
122124

123125
setActiveRequest(request)
126+
if (!setActiveSlot() && request.nowOrNothing) {
127+
activeRequest = null
128+
activeSlot = -1
129+
}
124130
}
125131

126-
/**
127-
* Sets the [activeRequest]. This also calls syncSelectedSlot to
128-
* update the server now to keep predictability.
129-
*
130-
* @see net.minecraft.client.network.ClientPlayerInteractionManager.syncSelectedSlot
131-
*/
132132
private fun AutomatedSafeContext.setActiveRequest(request: HotbarRequest) {
133133
maxSwapsThisTick = hotbarConfig.swapsPerTick
134-
if (request.slot != serverSlot) {
135-
swapsThisTick++
136-
swappedTicks = 0
137-
swapDelay = hotbarConfig.swapDelay
138-
} else request.swapPauseAge = swappedTicks
139134
activeRequest = request
140-
interaction.syncSelectedSlot()
141135
logger.success("Set active request", request)
142136
}
143137

138+
/**
139+
* Sets the [activeSlot]. This also calls syncSelectedSlot to
140+
* update the server to keep predictability.
141+
*
142+
* @see net.minecraft.client.network.ClientPlayerInteractionManager.syncSelectedSlot
143+
*/
144+
private fun SafeContext.setActiveSlot(): Boolean {
145+
activeRequest?.let { activeRequest ->
146+
if (serverSlot != activeRequest.slot) {
147+
if (tickStage !in activeRequest.hotbarConfig.sequenceStageMask) return false
148+
if (swapsThisTick + 1 > maxSwapsThisTick || swapDelay > 0) return false
149+
swapsThisTick++
150+
swappedTicks = 0
151+
swapDelay = activeRequest.hotbarConfig.swapDelay
152+
} else activeRequest.swapPauseAge = swappedTicks
153+
if (activeRequest.slot == activeSlot) return true
154+
activeSlot = activeRequest.slot
155+
interaction.syncSelectedSlot()
156+
}
157+
return true
158+
}
159+
144160
/**
145161
* Called after every [tickStage] closes. This method checks if the current [activeRequest] should be stopped.
146162
* This action is counted as another swap, so the conditions for a regular swap must be met. If the requests
@@ -153,8 +169,11 @@ object HotbarManager : RequestHandler<HotbarRequest>(
153169
val canStopSwap = swapsThisTick < maxSwapsThisTick
154170
if (active.keepTicks <= 0 && tickStage in active.hotbarConfig.sequenceStageMask && canStopSwap) {
155171
logger.debug("Clearing request and syncing slot", activeRequest)
172+
val prevSlot = activeSlot
156173
activeRequest = null
174+
activeSlot = -1
157175
interaction.syncSelectedSlot()
176+
if (serverSlot != prevSlot) swapsThisTick++
158177
}
159178
}
160179
}

src/main/kotlin/com/lambda/interaction/request/hotbar/HotbarRequest.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,16 @@ class HotbarRequest(
2626
val slot: Int,
2727
automated: Automated,
2828
var keepTicks: Int = automated.hotbarConfig.keepTicks,
29-
var swapPause: Int = automated.hotbarConfig.swapPause
29+
var swapPause: Int = automated.hotbarConfig.swapPause,
30+
override val nowOrNothing: Boolean = true
3031
) : Request(), LogContext, Automated by automated {
3132
override val requestId = ++requestCount
3233

3334
var activeRequestAge = 0
3435
var swapPauseAge = 0
3536

3637
override val done: Boolean
37-
get() = slot == HotbarManager.serverSlot && swapPauseAge >= swapPause
38+
get() = slot == HotbarManager.activeSlot && swapPauseAge >= swapPause
3839

3940
override fun submit(queueIfClosed: Boolean) =
4041
HotbarManager.request(this, queueIfClosed)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ data class InteractRequest(
3131
val contexts: Collection<InteractionContext>,
3232
val pendingInteractionsList: MutableCollection<BuildContext>,
3333
private val automated: Automated,
34+
override val nowOrNothing: Boolean = false,
3435
val onInteract: ((BlockPos) -> Unit)?
3536
) : Request(), LogContext, Automated by automated {
3637
override val requestId = ++requestCount

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,20 @@ object InteractionManager : RequestHandler<InteractRequest>(
100100

101101
/**
102102
* Attempts to accept and process the request, if there is not already an [activeRequest] and the request's [InteractRequest.contexts]
103-
* collection is not empty.
103+
* collection is not empty. If nowOrNothing is true, the request is cleared after the first process.
104104
*
105105
* @see processRequest
106106
*/
107107
override fun AutomatedSafeContext.handleRequest(request: InteractRequest) {
108108
if (activeRequest != null || request.contexts.isEmpty()) return
109+
if (BreakManager.activeThisTick || PlaceManager.activeThisTick) return
109110

110111
activeRequest = request
111112
processRequest(request)
113+
if (request.nowOrNothing) {
114+
activeRequest = null
115+
potentialInteractions = mutableListOf()
116+
}
112117
if (interactionsThisTick > 0) activeThisTick = true
113118
}
114119

@@ -118,8 +123,6 @@ object InteractionManager : RequestHandler<InteractRequest>(
118123
* @see populateFrom
119124
*/
120125
fun AutomatedSafeContext.processRequest(request: InteractRequest) {
121-
if (BreakManager.activeThisTick || PlaceManager.activeThisTick) return
122-
123126
logger.debug("Processing request", request)
124127

125128
if (request.fresh) populateFrom(request)

0 commit comments

Comments
 (0)