Skip to content

Commit b7b922e

Browse files
committed
only handle request when the manager is open and the tick stage is whitelisted in the config
1 parent 9554ee9 commit b7b922e

File tree

23 files changed

+80
-42
lines changed

23 files changed

+80
-42
lines changed

src/main/kotlin/com/lambda/config/groups/ActionConfig.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717

1818
package com.lambda.config.groups
1919

20+
import com.lambda.event.events.TickEvent
2021
import com.lambda.util.Describable
2122
import com.lambda.util.NamedEnum
2223

2324
interface ActionConfig {
2425
val sorter: SortMode
26+
val tickStageMask: Set<TickEvent>
2527

2628
enum class SortMode(
2729
override val displayName: String,

src/main/kotlin/com/lambda/config/groups/HotbarSettings.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ class HotbarSettings(
3131
override val swapDelay by c.setting("Swap Delay", 0, 0..3, 1, "The number of ticks delay before allowing another hotbar selection swap", " ticks").group(baseGroup).index()
3232
override val swapsPerTick by c.setting("Swaps Per Tick", 3, 1..10, 1, "The number of hotbar selection swaps that can take place each tick") { swapDelay <= 0 }.group(baseGroup).index()
3333
override val swapPause by c.setting("Swap Pause", 0, 0..20, 1, "The delay in ticks to pause actions after switching to the slot", " ticks").group(baseGroup).index()
34-
override val sequenceStageMask by c.setting("Hotbar Stage Mask", setOf(TickEvent.Input.Post), description = "The sub-tick timing at which hotbar actions are performed").group(baseGroup).index()
34+
override val tickStageMask by c.setting("Hotbar Stage Mask", setOf(TickEvent.Input.Post), description = "The sub-tick timing at which hotbar actions are performed").group(baseGroup).index()
3535
}

src/main/kotlin/com/lambda/config/groups/RotationSettings.kt

Lines changed: 3 additions & 0 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.config.SettingGroup
22+
import com.lambda.event.events.TickEvent.Companion.ALL_STAGES
2223
import com.lambda.interaction.request.rotating.RotationConfig
2324
import com.lambda.interaction.request.rotating.RotationMode
2425
import com.lambda.util.NamedEnum
@@ -41,6 +42,8 @@ class RotationSettings(
4142
/** How many ticks to wait before resetting the rotation */
4243
override val decayTicks by c.setting("Reset Rotation", 1, 1..10, 1, "Ticks before rotation is reset", " ticks") { rotate }.group(baseGroup).index()
4344

45+
override val tickStageMask = ALL_STAGES.toSet()
46+
4447
/** Whether the rotation is instant */
4548
var instant by c.setting("Instant Rotation", true, "Instantly rotate") { rotate }.group(baseGroup).index()
4649

src/main/kotlin/com/lambda/event/events/TickEvent.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,25 @@ sealed class TickEvent : Event {
159159
*/
160160
data object Post : TickEvent()
161161
}
162+
163+
companion object {
164+
val ALL_STAGES by lazy {
165+
listOf(
166+
Pre,
167+
Post,
168+
Network.Pre,
169+
Network.Post,
170+
Input.Pre,
171+
Input.Post,
172+
WorldRender.Pre,
173+
WorldRender.Post,
174+
Sound.Pre,
175+
Sound.Post,
176+
Render.Pre,
177+
Render.Post,
178+
Player.Pre,
179+
Player.Post,
180+
)
181+
}
182+
}
162183
}

src/main/kotlin/com/lambda/interaction/material/container/containers/CreativeContainer.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ data object CreativeContainer : MaterialContainer(Rank.Creative) {
5959
}
6060
}
6161
onComplete { success() }
62-
}.submit(queueIfClosed = false)
62+
}.submit(queueIfMismatchedStage = false)
6363
}
6464
}
6565
}
@@ -87,7 +87,7 @@ data object CreativeContainer : MaterialContainer(Rank.Creative) {
8787
clickCreativeStack(optimalStack, 36 + player.inventory.selectedSlot)
8888
action { player.inventory.selectedStack = optimalStack }
8989
onComplete { success() }
90-
}.submit(queueIfClosed = false)
90+
}.submit(queueIfMismatchedStage = false)
9191
return@listen
9292
}
9393

src/main/kotlin/com/lambda/interaction/material/container/containers/MainHandContainer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ object MainHandContainer : MaterialContainer(Rank.MainHand) {
7676
if (hand == Hand.OFF_HAND) swapHands()
7777

7878
onComplete { success() }
79-
}.submit(queueIfClosed = false)
79+
}.submit(queueIfMismatchedStage = false)
8080
}
8181
}
8282
}

src/main/kotlin/com/lambda/interaction/material/transfer/SlotTransfer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class SlotTransfer @Ta5kBuilder constructor(
7474
swap(nextTo.id, 1)
7575
swap(nextFrom.id, 1)
7676
onComplete { success() }
77-
}.submit(queueIfClosed = false)
77+
}.submit(queueIfMismatchedStage = false)
7878
}
7979
}
8080

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package com.lambda.interaction.request
1919

2020
import com.lambda.context.Automated
21+
import com.lambda.event.events.TickEvent
2122

2223
/**
2324
* A simple format to ensure basic requirements and information when requesting a manager.
@@ -30,11 +31,12 @@ abstract class Request : Automated {
3031
abstract val requestId: Int
3132
var fresh = true
3233

34+
abstract val tickStageMask: Set<TickEvent>
3335
abstract val nowOrNothing: Boolean
3436

3537
abstract val done: Boolean
3638

37-
abstract fun submit(queueIfClosed: Boolean = true): Request
39+
abstract fun submit(queueIfMismatchedStage: Boolean = true): Request
3840

3941
companion object {
4042
fun submit(request: Request, queueIfClosed: Boolean = true) =

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import com.lambda.context.SafeContext
2323
import com.lambda.core.Loadable
2424
import com.lambda.event.Event
2525
import com.lambda.event.events.TickEvent
26+
import com.lambda.event.events.TickEvent.Companion.ALL_STAGES
2627
import com.lambda.event.listener.SafeListener.Companion.listen
2728
import com.lambda.interaction.request.ManagerUtils.accumulatedManagerPriority
2829
import com.lambda.threading.runSafeAutomated
@@ -77,6 +78,7 @@ abstract class RequestHandler<R : Request>(
7778
listen(instance, priority = (Int.MAX_VALUE - 1) - (accumulatedManagerPriority - stagePriority)) {
7879
tickStage = stage
7980
queuedRequest?.let { request ->
81+
if (tickStage !in request.tickStageMask) return@let
8082
request.runSafeAutomated { handleRequest(request) }
8183
request.fresh = false
8284
queuedRequest = null
@@ -96,15 +98,18 @@ abstract class RequestHandler<R : Request>(
9698
* Registers a new request
9799
*
98100
* @param request The request to register.
99-
* @param queueIfClosed queues the request for the next time the handlers accepting requests
101+
* @param queueIfMismatchedStage queues the request for the next time the handlers accepting requests
100102
* @return The registered request.
101103
*/
102-
fun request(request: R, queueIfClosed: Boolean = true): R {
103-
if (!acceptingRequests) {
104-
val canOverrideQueued = queuedRequest?.let { it as Automated === request as Automated } != false
105-
if (queueIfClosed && canOverrideQueued) {
106-
queuedRequest = request
107-
}
104+
fun request(request: R, queueIfMismatchedStage: Boolean = true): R {
105+
val canOverrideQueued = queuedRequest?.let { it as Automated === request as Automated } != false
106+
if (!canOverrideQueued) return request
107+
if ((!acceptingRequests || tickStage !in request.tickStageMask)) {
108+
if (!queueIfMismatchedStage || request.nowOrNothing) return request
109+
val currentStageIndex = ALL_STAGES.indexOf(tickStage)
110+
if (openStages.none { ALL_STAGES.indexOf(it) > currentStageIndex && it in request.tickStageMask })
111+
return request
112+
queuedRequest = request
108113
return request
109114
}
110115

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package com.lambda.interaction.request.breaking
1919

2020
import com.lambda.config.groups.ActionConfig
2121
import com.lambda.config.groups.BuildConfig
22-
import com.lambda.event.Event
2322
import com.lambda.util.Describable
2423
import com.lambda.util.NamedEnum
2524
import net.minecraft.block.Block
@@ -39,8 +38,6 @@ interface BreakConfig : ActionConfig {
3938
// abstract val desyncFix: Boolean
4039
val breakDelay: Int
4140

42-
val tickStageMask: Set<Event>
43-
4441
val swapMode: SwapMode
4542

4643
val swing: SwingMode

0 commit comments

Comments
 (0)