1717
1818package com.lambda.interaction.request.placing
1919
20- import com.lambda.config.groups.BuildConfig
20+ import com.lambda.Lambda.mc
2121import com.lambda.context.SafeContext
2222import com.lambda.event.EventFlow.post
2323import com.lambda.event.events.MovementEvent
@@ -36,6 +36,8 @@ import com.lambda.interaction.request.rotation.RotationManager.onRotate
3636import com.lambda.interaction.request.rotation.RotationManager.onRotatePost
3737import com.lambda.interaction.request.rotation.RotationRequest
3838import com.lambda.module.modules.client.TaskFlowModule
39+ import com.lambda.util.BlockUtils.item
40+ import com.lambda.util.BlockUtils.matches
3941import com.lambda.util.Communication.info
4042import com.lambda.util.Communication.warn
4143import com.lambda.util.collections.LimitedDecayQueue
@@ -56,15 +58,17 @@ import net.minecraft.util.Hand
5658import net.minecraft.util.hit.BlockHitResult
5759import net.minecraft.util.math.BlockPos
5860import net.minecraft.world.GameMode
59- import org.apache.commons.lang3.mutable.MutableObject
6061
6162object PlaceManager : RequestHandler<PlaceRequest>(), PositionBlocking {
62- private val pendingInteractions = LimitedDecayQueue <PlaceInfo >(
63+ private val pendingInteractions = LimitedDecayQueue <PlaceRequest >(
6364 TaskFlowModule .build.maxPendingInteractions, TaskFlowModule .build.interactionTimeout * 50L
64- ) { info(" ${it::class .simpleName} at ${it.context.expectedPos.toShortString()} timed out" ) }
65+ ) {
66+ info(" ${it::class .simpleName} at ${it.placeContext.expectedPos.toShortString()} timed out" )
67+ mc.world?.setBlockState(it.placeContext.expectedPos, it.placeContext.checkedState)
68+ }
6569
6670 override val blockedPositions
67- get() = pendingInteractions.map { it.context .expectedPos }
71+ get() = pendingInteractions.map { it.placeContext .expectedPos }
6872
6973 private var rotation: RotationRequest ? = null
7074 private var validRotation = false
@@ -95,6 +99,7 @@ object PlaceManager : RequestHandler<PlaceRequest>(), PositionBlocking {
9599 return @listen
96100
97101 placeBlock(request, Hand .MAIN_HAND )
102+ activeThisTick = true
98103 }
99104 }
100105
@@ -117,8 +122,6 @@ object PlaceManager : RequestHandler<PlaceRequest>(), PositionBlocking {
117122 return @onRotate
118123 }
119124
120- activeThisTick = true
121-
122125 rotation = if (request.buildConfig.placeSettings.rotateForPlace)
123126 request.rotationConfig.request(request.placeContext.rotation)
124127 else null
@@ -139,20 +142,27 @@ object PlaceManager : RequestHandler<PlaceRequest>(), PositionBlocking {
139142
140143 listen<WorldEvent .BlockUpdate .Server > { event ->
141144 pendingInteractions
142- .firstOrNull { it.context.expectedPos == event.pos }
143- ?.let { pending ->
144- pendingInteractions.remove(pending)
145- if (! matchesTargetState(event.pos, pending.context.targetState, event.newState)) return @listen
146- if (pending.buildConfig.placeSettings.placeConfirmationMode == PlaceConfig .PlaceConfirmationMode .AwaitThenPlace )
147- placeSound(pending.item, pending.context.expectedState, pending.context.expectedPos)
148- pending.onPlace()
145+ .firstOrNull { it.placeContext.expectedPos == event.pos }
146+ ?.let { request ->
147+ pendingInteractions.remove(request)
148+
149+ // return if the block wasn't placed
150+ if (! matchesTargetState(event.pos, request.placeContext.targetState, event.newState))
151+ return @listen
152+
153+ if (request.buildConfig.placeSettings.placeConfirmationMode == PlaceConfig .PlaceConfirmationMode .AwaitThenPlace )
154+ with (request.placeContext) {
155+ placeSound(expectedState.block.item as BlockItem , expectedState, expectedPos)
156+ }
157+ request.onPlace()
158+ return @listen
149159 }
150160 }
151161 }
152162
153163 private fun canPlace (placeContext : PlaceContext ) =
154164 pendingInteractions.none { pending ->
155- pending.context .expectedPos == placeContext.expectedPos
165+ pending.placeContext .expectedPos == placeContext.expectedPos
156166 }
157167
158168 private fun SafeContext.matchesTargetState (pos : BlockPos , targetState : TargetState , newState : BlockState ) =
@@ -162,48 +172,35 @@ object PlaceManager : RequestHandler<PlaceRequest>(), PositionBlocking {
162172 false
163173 }
164174
165- private fun SafeContext.placeBlock (request : PlaceRequest , hand : Hand ) {
166- val stackInHand = player.getStackInHand(hand)
167- val item = stackInHand.item as ? BlockItem ? : return
168- val stackCountPre = stackInHand.count
169- val actionResult = interactBlock(request.buildConfig.placeSettings, hand, request.placeContext.result)
170-
171- if (actionResult.isAccepted) {
172- if (request.buildConfig.placeSettings.placeConfirmationMode == PlaceConfig .PlaceConfirmationMode .None )
173- request.onPlace()
174- else {
175- pendingInteractions.add(
176- PlaceInfo (
177- request.placeContext,
178- item,
179- request.buildConfig,
180- request.onPlace
181- )
182- )
183- }
184-
185- if (actionResult.shouldSwingHand() && request.buildConfig.placeSettings.swing) {
186- swingHand(request.buildConfig.placeSettings.swingType)
187- }
175+ private fun SafeContext.placeBlock (request : PlaceRequest , hand : Hand ) =
176+ interactBlock(request, request.buildConfig.placeSettings, hand, request.placeContext.result)
188177
189- if (! stackInHand.isEmpty && (stackInHand.count != stackCountPre || interaction.hasCreativeInventory())) {
190- mc.gameRenderer.firstPersonRenderer.resetEquipProgress(hand)
191- }
192- } else {
193- warn(" Placement interaction failed with $actionResult " )
194- }
195- }
196-
197- private fun SafeContext.interactBlock (placeConfig : PlaceConfig , hand : Hand , hitResult : BlockHitResult ): ActionResult {
178+ private fun SafeContext.interactBlock (request : PlaceRequest , placeConfig : PlaceConfig , hand : Hand , hitResult : BlockHitResult ) {
198179 interaction.syncSelectedSlot()
199- if (! world.worldBorder.contains(hitResult.blockPos)) return ActionResult . FAIL
180+ if (! world.worldBorder.contains(hitResult.blockPos)) return
200181
201- val mutableActionResult = MutableObject <ActionResult >()
202182 interaction.sendSequencedPacket(world) { sequence: Int ->
203- mutableActionResult.value = interactBlockInternal(placeConfig, hand, hitResult)
183+ val stackInHand = player.getStackInHand(hand)
184+ val stackCountPre = stackInHand.count
185+ val actionResult = interactBlockInternal(placeConfig, hand, hitResult)
186+ if (actionResult.isAccepted) {
187+ if (request.buildConfig.placeSettings.placeConfirmationMode == PlaceConfig .PlaceConfirmationMode .None )
188+ request.onPlace()
189+ else
190+ pendingInteractions.add(request)
191+
192+ if (actionResult.shouldSwingHand() && request.buildConfig.placeSettings.swing) {
193+ swingHand(request.buildConfig.placeSettings.swingType)
194+ }
195+
196+ if (! stackInHand.isEmpty && (stackInHand.count != stackCountPre || interaction.hasCreativeInventory())) {
197+ mc.gameRenderer.firstPersonRenderer.resetEquipProgress(hand)
198+ }
199+ } else {
200+ warn(" Placement interaction failed with $actionResult " )
201+ }
204202 PlayerInteractBlockC2SPacket (hand, hitResult, sequence)
205203 }
206- return mutableActionResult.value
207204 }
208205
209206 private fun SafeContext.interactBlockInternal (
@@ -212,7 +209,7 @@ object PlaceManager : RequestHandler<PlaceRequest>(), PositionBlocking {
212209 hitResult : BlockHitResult
213210 ): ActionResult {
214211 val itemStack = player.getStackInHand(hand)
215- if (gamemode == GameMode .SPECTATOR ) return ActionResult .SUCCESS
212+ if (gamemode == GameMode .SPECTATOR ) return ActionResult .PASS
216213
217214 // checks if the player should be able to interact with the block for if its something
218215 // like a furnace or chest where an action would happen
@@ -297,13 +294,6 @@ object PlaceManager : RequestHandler<PlaceRequest>(), PositionBlocking {
297294 )
298295 }
299296
300- data class PlaceInfo (
301- val context : PlaceContext ,
302- val item : BlockItem ,
303- val buildConfig : BuildConfig ,
304- val onPlace : () -> Unit
305- )
306-
307297 override fun preEvent () = UpdateManagerEvent .Place .Pre ().post()
308298 override fun postEvent () = UpdateManagerEvent .Place .Post ().post()
309299}
0 commit comments