Skip to content

Commit 3aebf1e

Browse files
committed
move all place logic to the update method
1 parent e08684f commit 3aebf1e

File tree

2 files changed

+43
-45
lines changed

2 files changed

+43
-45
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ object BreakManager : RequestHandler<BreakRequest>(), PositionBlocking {
132132
?: breakInfos.firstOrNull { it != null }?.request?.hotbarConfig
133133
?: return@listen
134134

135-
hotbarRequest = HotbarRequest(hotbarConfig) { if (update(::swapTo, tickPre = true)) done() }
135+
hotbarRequest = HotbarRequest(hotbarConfig) {
136+
if (update(::swapTo, tickPre = true)) done()
137+
}
136138
hotbarRequest?.let { hotbarRequest ->
137139
hotbarConfig.request(hotbarRequest)
138140
}

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

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -118,37 +118,10 @@ object PlaceManager : RequestHandler<PlaceRequest>(), PositionBlocking {
118118
}
119119

120120
val request = currentRequest ?: return@listen
121-
122121
if (BreakManager.activeThisTick) return@listen
123-
val placeConfig = request.buildConfig.placeSettings
124-
125-
pendingPlacements.setMaxSize(placeConfig.maxPendingPlacements)
126-
pendingPlacements.setDecayTime(request.buildConfig.interactionTimeout * 50L)
127-
128-
val isSneaking = player.isSneaking
129-
val currentHotbarIndex = HotbarManager.serverSlot
130-
val placeContexts = request.placeContexts
131-
.filter { canPlace(it) }
132-
.sortedWith(
133-
compareByDescending<PlaceContext> { it.hotbarIndex == currentHotbarIndex }
134-
.thenByDescending { it.sneak == isSneaking }
135-
)
136-
137-
val maxPlacementsThisTick = (placeConfig.maxPendingPlacements - pendingPlacements.size).coerceAtLeast(0)
138-
val takeCount = (placeConfig.placementsPerTick.coerceAtMost(maxPlacementsThisTick))
139-
nextPredictedRotation = if (request.rotationConfig.rotate) placeContexts.getOrNull(takeCount)?.rotation else null
140122

141123
hotbarRequest = HotbarRequest(request.hotbarConfig) {
142-
potentialPlacements = placeContexts.take(takeCount)
143-
potentialPlacements.forEach { ctx ->
144-
swapTo(ctx.hotbarIndex)
145-
if (request.buildConfig.placeSettings.rotate) request.rotationConfig.request(ctx.rotation)
146-
if (ctx.sneak) shouldSneak = true
147-
if (placeConfig.sequenceMode != BuildConfig.InteractSequenceMode.TickStart) return@HotbarRequest
148-
if (!attemptContextPlace(ctx, request)) return@HotbarRequest
149-
}
150-
requestNextPredictedRotation(request)
151-
done()
124+
if (update(::swapTo, tickPre = true)) done()
152125
}
153126
hotbarRequest?.let { hotbarRequest ->
154127
request.hotbarConfig.request(hotbarRequest)
@@ -161,7 +134,7 @@ object PlaceManager : RequestHandler<PlaceRequest>(), PositionBlocking {
161134
listen<MovementEvent.Player.Post> {
162135
currentRequest?.let { request ->
163136
if (request.buildConfig.placeSettings.sequenceMode == BuildConfig.InteractSequenceMode.PostMovement) {
164-
handlePlacementContexts(request)
137+
update(null)
165138
postEvent()
166139
}
167140
}
@@ -198,24 +171,47 @@ object PlaceManager : RequestHandler<PlaceRequest>(), PositionBlocking {
198171
}
199172
}
200173

201-
private fun SafeContext.handlePlacementContexts(request: PlaceRequest): Boolean {
202-
potentialPlacements.forEach { ctx ->
203-
if (request.buildConfig.placeSettings.rotate) request.rotationConfig.request(ctx.rotation)
204-
if (ctx.sneak) shouldSneak = true
205-
if (!attemptContextPlace(ctx, request)) return false
206-
}
207-
requestNextPredictedRotation(request)
208-
return true
209-
}
174+
private fun SafeContext.update(swapTo: ((slot: Int) -> Boolean)?, tickPre: Boolean = false): Boolean {
175+
currentRequest?.let { request ->
176+
val placeConfig = request.buildConfig.placeSettings
210177

211-
private fun SafeContext.attemptContextPlace(ctx: PlaceContext, request: PlaceRequest): Boolean {
212-
if (!swappedTo(ctx.hotbarIndex) || !ctx.rotation.done || !validSneak(player)) return false
178+
if (tickPre) {
179+
pendingPlacements.setMaxSize(placeConfig.maxPendingPlacements)
180+
pendingPlacements.setDecayTime(request.buildConfig.interactionTimeout * 50L)
181+
182+
val isSneaking = player.isSneaking
183+
val currentHotbarIndex = HotbarManager.serverSlot
184+
val placeContexts = request.placeContexts
185+
.filter { canPlace(it) }
186+
.sortedWith(
187+
compareByDescending<PlaceContext> { it.hotbarIndex == currentHotbarIndex }
188+
.thenByDescending { it.sneak == isSneaking }
189+
)
190+
191+
val maxPlacementsThisTick = (placeConfig.maxPendingPlacements - pendingPlacements.size).coerceAtLeast(0)
192+
val takeCount = (placeConfig.placementsPerTick.coerceAtMost(maxPlacementsThisTick))
193+
nextPredictedRotation = if (request.rotationConfig.rotate) placeContexts.getOrNull(takeCount)?.rotation else null
194+
potentialPlacements = placeContexts.take(takeCount)
195+
}
213196

214-
val actionResult = placeBlock(ctx, request, Hand.MAIN_HAND)
215-
if (!actionResult.isAccepted) {
216-
warn("Placement interaction failed with $actionResult")
197+
potentialPlacements.forEach { ctx ->
198+
swapTo?.invoke(ctx.hotbarIndex)
199+
if (request.buildConfig.placeSettings.rotate) request.rotationConfig.request(ctx.rotation)
200+
if (ctx.sneak) shouldSneak = true
201+
val mismatchedTiming = tickPre && placeConfig.sequenceMode != BuildConfig.InteractSequenceMode.TickStart
202+
if (mismatchedTiming) return false
203+
if (!swappedTo(ctx.hotbarIndex) || !ctx.rotation.done || !validSneak(player)) return false
204+
205+
val actionResult = placeBlock(ctx, request, Hand.MAIN_HAND)
206+
if (!actionResult.isAccepted) {
207+
warn("Placement interaction failed with $actionResult")
208+
}
209+
}
210+
requestNextPredictedRotation(request)
211+
return true
217212
}
218-
return true
213+
214+
return false
219215
}
220216

221217
private fun requestNextPredictedRotation(request: PlaceRequest) {

0 commit comments

Comments
 (0)