@@ -21,7 +21,6 @@ import com.lambda.config.groups.BuildConfig
2121import com.lambda.context.SafeContext
2222import com.lambda.event.EventFlow.post
2323import com.lambda.event.events.MovementEvent
24- import com.lambda.event.events.TickEvent
2524import com.lambda.event.events.UpdateManagerEvent
2625import com.lambda.event.events.WorldEvent
2726import com.lambda.event.listener.SafeListener.Companion.listen
@@ -31,6 +30,8 @@ import com.lambda.interaction.request.RequestHandler
3130import com.lambda.interaction.request.breaking.BreakManager
3231import com.lambda.interaction.request.hotbar.HotbarRequest
3332import com.lambda.interaction.request.rotation.RotationManager.onRotate
33+ import com.lambda.interaction.request.rotation.RotationManager.onRotatePost
34+ import com.lambda.interaction.request.rotation.RotationRequest
3435import com.lambda.module.modules.client.TaskFlowModule
3536import com.lambda.util.Communication.info
3637import 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
0 commit comments