Skip to content

Commit b3d4cd6

Browse files
committed
fixed place and break delays with tick timing issues
1 parent c39f2c2 commit b3d4cd6

File tree

4 files changed

+90
-71
lines changed

4 files changed

+90
-71
lines changed

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

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ package com.lambda.interaction.request.breaking
2020
import com.lambda.config.groups.BuildConfig
2121
import com.lambda.context.SafeContext
2222
import com.lambda.event.EventFlow.post
23-
import com.lambda.event.events.TickEvent
2423
import com.lambda.event.events.UpdateManagerEvent
2524
import com.lambda.event.events.WorldEvent
2625
import com.lambda.event.listener.SafeListener.Companion.listen
@@ -34,6 +33,8 @@ import com.lambda.interaction.request.hotbar.HotbarRequest
3433
import com.lambda.interaction.request.placing.PlaceManager
3534
import com.lambda.interaction.request.rotation.RotationConfig
3635
import com.lambda.interaction.request.rotation.RotationManager.onRotate
36+
import com.lambda.interaction.request.rotation.RotationManager.onRotatePost
37+
import com.lambda.interaction.request.rotation.RotationRequest
3738
import com.lambda.module.modules.client.TaskFlowModule
3839
import com.lambda.util.BlockUtils.blockState
3940
import com.lambda.util.BlockUtils.calcItemBlockBreakingDelta
@@ -70,21 +71,23 @@ object BreakManager : RequestHandler<BreakRequest>() {
7071

7172
private var blockBreakingCooldown = 0
7273

74+
private var rotation: RotationRequest? = null
75+
7376
init {
74-
listen<TickEvent.Pre>(Int.MIN_VALUE) {
77+
onRotate(priority = Int.MIN_VALUE) {
7578
preEvent()
7679

7780
if (isOnBreakCooldown()) {
7881
blockBreakingCooldown--
7982
updateRequest(true) { true }
8083
postEvent()
81-
return@listen
84+
return@onRotate
8285
}
8386

8487
if (PlaceManager.activeThisTick()) {
8588
updateRequest(true) { true }
8689
postEvent()
87-
return@listen
90+
return@onRotate
8891
}
8992

9093
//ToDo: improve instamine / non instamine integration
@@ -121,16 +124,25 @@ object BreakManager : RequestHandler<BreakRequest>() {
121124

122125
breakingInfos
123126
.filterNotNull()
127+
.firstOrNull { it.breakConfig.rotateForBreak }
128+
?.let { info ->
129+
rotation = info.rotationConfig.request(info.context.rotation)
130+
}
131+
}
132+
133+
onRotatePost {
134+
val notNullInfos = breakingInfos.filterNotNull()
135+
136+
notNullInfos
124137
.firstOrNull()?.let { info ->
125138
activeThisTick = true
126-
if (info.breakConfig.rotateForBreak && !info.context.rotation.done) {
139+
if (info.breakConfig.rotateForBreak && rotation?.done != true) {
127140
postEvent()
128-
return@listen
141+
return@onRotatePost
129142
}
130143
}
131144

132-
breakingInfos
133-
.filterNotNull()
145+
notNullInfos
134146
.reversed()
135147
.forEach { info ->
136148
if (info.hotbarConfig.request(HotbarRequest(info.context.hotbarIndex)).done.not()) return@forEach
@@ -140,14 +152,6 @@ object BreakManager : RequestHandler<BreakRequest>() {
140152
postEvent()
141153
}
142154

143-
onRotate {
144-
breakingInfos
145-
.filterNotNull()
146-
.firstOrNull { it.breakConfig.rotateForBreak }?.let { info ->
147-
info.rotationConfig.request(info.context.rotation)
148-
}
149-
}
150-
151155
//ToDo: Clean this up
152156
listen<WorldEvent.BlockUpdate.Server> { event ->
153157
pendingInteractions

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ object HotbarManager : RequestHandler<HotbarRequest>(), Loadable {
4646
it.slot = currentRequest?.slot ?: return@listen
4747
}
4848

49-
listen<TickEvent.Pre> {
49+
listen<TickEvent.Pre>(priority = Int.MIN_VALUE) {
5050
preEvent()
5151
if (updateRequest()) interaction.syncSelectedSlot()
5252
if (currentRequest != null) activeThisTick = true

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

Lines changed: 55 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import com.lambda.config.groups.BuildConfig
2121
import com.lambda.context.SafeContext
2222
import com.lambda.event.EventFlow.post
2323
import com.lambda.event.events.MovementEvent
24-
import com.lambda.event.events.TickEvent
2524
import com.lambda.event.events.UpdateManagerEvent
2625
import com.lambda.event.events.WorldEvent
2726
import com.lambda.event.listener.SafeListener.Companion.listen
@@ -31,6 +30,8 @@ import com.lambda.interaction.request.RequestHandler
3130
import com.lambda.interaction.request.breaking.BreakManager
3231
import com.lambda.interaction.request.hotbar.HotbarRequest
3332
import com.lambda.interaction.request.rotation.RotationManager.onRotate
33+
import com.lambda.interaction.request.rotation.RotationManager.onRotatePost
34+
import com.lambda.interaction.request.rotation.RotationRequest
3435
import com.lambda.module.modules.client.TaskFlowModule
3536
import com.lambda.util.Communication.info
3637
import com.lambda.util.Communication.warn
@@ -57,41 +58,56 @@ object PlaceManager : RequestHandler<PlaceRequest>() {
5758
TaskFlowModule.build.maxPendingInteractions, TaskFlowModule.build.interactionTimeout * 50L
5859
) { info("${it::class.simpleName} at ${it.context.expectedPos.toShortString()} timed out") }
5960

61+
private var rotation: RotationRequest? = null
62+
6063
init {
61-
listen<TickEvent.Pre>(Int.MIN_VALUE) {
64+
onRotate(priority = Int.MIN_VALUE) {
6265
preEvent()
6366

64-
if (!updateRequest { request ->
65-
pendingInteractions.none { pending -> pending.context.expectedPos == request.value.placeContext.expectedPos }
66-
}) {
67+
if (!updateRequest { request -> canPlace(request.value.placeContext) }) {
6768
postEvent()
68-
return@listen
69+
return@onRotate
6970
}
7071

7172
if (BreakManager.activeThisTick()) {
7273
postEvent()
73-
return@listen
74+
return@onRotate
7475
}
7576

7677
currentRequest?.let request@ { request ->
77-
if (pendingInteractions.size >= request.buildConfig.placeSettings.maxPendingPlacements)
78-
return@request
78+
if (pendingInteractions.size >= request.buildConfig.placeSettings.maxPendingPlacements) {
79+
postEvent()
80+
return@onRotate
81+
}
7982

8083
activeThisTick = true
8184

82-
if (request.placeContext.sneak && !player.isSneaking
83-
|| (request.buildConfig.placeSettings.rotateForPlace && !request.placeContext.rotation.done)
84-
|| (!request.hotbarConfig.request(HotbarRequest(request.placeContext.hotbarIndex)).done)
85-
) {
86-
postEvent()
87-
return@listen
85+
if (request.buildConfig.placeSettings.rotateForPlace) {
86+
rotation = request.rotationConfig.request(request.placeContext.rotation)
8887
}
88+
8989
pendingInteractions.setMaxSize(request.buildConfig.maxPendingInteractions)
9090
pendingInteractions.setDecayTime(request.buildConfig.interactionTimeout * 50L)
91+
}
92+
}
93+
94+
onRotatePost {
95+
currentRequest?.let { request ->
96+
val notSneaking = !player.isSneaking
97+
val hotbarRequest = request.hotbarConfig.request(HotbarRequest(request.placeContext.hotbarIndex))
98+
val invalidRotation = request.buildConfig.placeSettings.rotateForPlace && rotation?.done != true
99+
if ((request.placeContext.sneak && notSneaking) || !hotbarRequest.done || invalidRotation) {
100+
postEvent()
101+
return@onRotatePost
102+
}
103+
91104
placeBlock(request, Hand.MAIN_HAND)
105+
postEvent()
92106
}
107+
}
93108

94-
postEvent()
109+
listen<MovementEvent.InputUpdate> {
110+
if (currentRequest?.placeContext?.sneak == true) it.input.sneaking = true
95111
}
96112

97113
listen<WorldEvent.BlockUpdate.Server> { event ->
@@ -105,20 +121,13 @@ object PlaceManager : RequestHandler<PlaceRequest>() {
105121
pending.onPlace()
106122
}
107123
}
124+
}
108125

109-
onRotate {
110-
currentRequest?.let { request ->
111-
if (request.buildConfig.placeSettings.rotateForPlace) {
112-
request.rotationConfig.request(request.placeContext.rotation)
113-
}
114-
}
126+
private fun canPlace(placeContext: PlaceContext) =
127+
pendingInteractions.none { pending ->
128+
pending.context.expectedPos == placeContext.expectedPos
115129
}
116130

117-
listen<MovementEvent.InputUpdate> {
118-
if (currentRequest?.placeContext?.sneak == true) it.input.sneaking = true
119-
}
120-
}
121-
122131
private fun SafeContext.matchesTargetState(pos: BlockPos, targetState: TargetState, newState: BlockState) =
123132
if (targetState.matches(newState, pos, world)) true
124133
else {
@@ -160,16 +169,14 @@ object PlaceManager : RequestHandler<PlaceRequest>() {
160169

161170
private fun SafeContext.interactBlock(placeConfig: PlaceConfig, hand: Hand, hitResult: BlockHitResult): ActionResult {
162171
interaction.syncSelectedSlot()
163-
if (!world.worldBorder.contains(hitResult.blockPos)) {
164-
return ActionResult.FAIL
165-
} else {
166-
val mutableActionResult = MutableObject<ActionResult>()
167-
interaction.sendSequencedPacket(world) { sequence: Int ->
168-
mutableActionResult.value = interactBlockInternal(placeConfig, hand, hitResult)
169-
PlayerInteractBlockC2SPacket(hand, hitResult, sequence)
170-
}
171-
return mutableActionResult.value
172+
if (!world.worldBorder.contains(hitResult.blockPos)) return ActionResult.FAIL
173+
174+
val mutableActionResult = MutableObject<ActionResult>()
175+
interaction.sendSequencedPacket(world) { sequence: Int ->
176+
mutableActionResult.value = interactBlockInternal(placeConfig, hand, hitResult)
177+
PlayerInteractBlockC2SPacket(hand, hitResult, sequence)
172178
}
179+
return mutableActionResult.value
173180
}
174181

175182
private fun SafeContext.interactBlockInternal(
@@ -188,15 +195,14 @@ object PlaceManager : RequestHandler<PlaceRequest>() {
188195

189196
if (!itemStack.isEmpty && !player.itemCooldownManager.isCoolingDown(itemStack.item)) {
190197
val itemUsageContext = ItemUsageContext(player, hand, hitResult)
191-
val itemUseResult: ActionResult
192-
if (interaction.currentGameMode.isCreative) {
198+
return if (interaction.currentGameMode.isCreative) {
193199
val i = itemStack.count
194-
itemUseResult = useOnBlock(placeConfig, itemStack, itemUsageContext)
195-
itemStack.count = i
200+
useOnBlock(placeConfig, itemStack, itemUsageContext)
201+
.also {
202+
itemStack.count = i
203+
}
196204
} else
197-
itemUseResult = useOnBlock(placeConfig, itemStack, itemUsageContext)
198-
199-
return itemUseResult
205+
useOnBlock(placeConfig, itemStack, itemUsageContext)
200206
}
201207
return ActionResult.PASS
202208
}
@@ -206,13 +212,12 @@ object PlaceManager : RequestHandler<PlaceRequest>() {
206212
itemStack: ItemStack,
207213
context: ItemUsageContext
208214
): ActionResult {
209-
val blockPos = context.blockPos
210-
val cachedBlockPosition = CachedBlockPosition(context.world, blockPos, false)
211-
if (!player.abilities.allowModifyWorld
212-
&& !itemStack.canPlaceOn(context.world.registryManager.get(RegistryKeys.BLOCK), cachedBlockPosition)
213-
) {
214-
return ActionResult.PASS
215-
}
215+
val cachedBlockPosition = CachedBlockPosition(context.world, context.blockPos, false)
216+
217+
val cantModifyWorld = !player.abilities.allowModifyWorld
218+
val cantPlaceOn = !itemStack.canPlaceOn(context.world.registryManager.get(RegistryKeys.BLOCK), cachedBlockPosition)
219+
if (cantModifyWorld && cantPlaceOn) return ActionResult.PASS
220+
216221
val item = (itemStack.item as? BlockItem) ?: return ActionResult.PASS
217222
val actionResult = place(placeConfig, item, ItemPlacementContext(context))
218223

common/src/main/kotlin/com/lambda/interaction/request/rotation/RotationManager.kt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import com.lambda.event.EventFlow.post
2424
import com.lambda.event.events.*
2525
import com.lambda.event.listener.SafeListener.Companion.listen
2626
import com.lambda.event.listener.UnsafeListener.Companion.listenUnsafe
27+
import com.lambda.interaction.request.Priority
2728
import com.lambda.interaction.request.RequestHandler
2829
import com.lambda.interaction.request.rotation.Rotation.Companion.slerp
2930
import com.lambda.interaction.request.rotation.visibilty.lookAt
@@ -59,8 +60,17 @@ object RotationManager : RequestHandler<RotationRequest>(), Loadable {
5960
*/
6061
fun Any.onRotate(
6162
alwaysListen: Boolean = false,
62-
block: SafeContext.() -> Unit,
63-
) = this.listen<PlayerPacketEvent.Post>(0, alwaysListen) {
63+
priority: Priority = 0,
64+
block: SafeContext.() -> Unit
65+
) = this.listen<UpdateManagerEvent.Rotation.Pre>(priority, alwaysListen) {
66+
block()
67+
}
68+
69+
fun Any.onRotatePost(
70+
alwaysListen: Boolean = false,
71+
priority: Priority = 0,
72+
block: SafeContext.() -> Unit
73+
) = this.listen<UpdateManagerEvent.Rotation.Post>(priority, alwaysListen) {
6474
block()
6575
}
6676

@@ -265,6 +275,6 @@ object RotationManager : RequestHandler<RotationRequest>(), Loadable {
265275
}
266276
}
267277

268-
override fun preEvent() = UpdateManagerEvent.Rotation.Pre()
269-
override fun postEvent() = UpdateManagerEvent.Rotation.Post()
278+
override fun preEvent() = UpdateManagerEvent.Rotation.Pre().post()
279+
override fun postEvent() = UpdateManagerEvent.Rotation.Post().post()
270280
}

0 commit comments

Comments
 (0)