@@ -29,9 +29,10 @@ import com.lambda.interaction.request.ManagerUtils.newStage
2929import com.lambda.interaction.request.ManagerUtils.newTick
3030import com.lambda.interaction.request.RequestHandler
3131import com.lambda.interaction.request.hotbar.HotbarManager.activeRequest
32+ import com.lambda.interaction.request.hotbar.HotbarManager.activeSlot
3233import com.lambda.interaction.request.hotbar.HotbarManager.checkResetSwap
33- import com.lambda.interaction.request.hotbar.HotbarManager.maxSwapsThisTick
3434import com.lambda.interaction.request.hotbar.HotbarManager.setActiveRequest
35+ import com.lambda.interaction.request.hotbar.HotbarManager.setActiveSlot
3536import com.lambda.module.hud.ManagerDebugLoggers.hotbarManagerLogger
3637import com.lambda.threading.runSafe
3738import 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 }
0 commit comments