Skip to content

Commit 1a112fe

Browse files
committed
inlined post placement interactions
1 parent ad4e8c2 commit 1a112fe

File tree

1 file changed

+57
-35
lines changed
  • common/src/main/kotlin/com/lambda/interaction/request/placing

1 file changed

+57
-35
lines changed

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

Lines changed: 57 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import com.lambda.interaction.request.rotation.RotationManager.onRotate
3636
import com.lambda.interaction.request.rotation.RotationManager.onRotatePost
3737
import com.lambda.interaction.request.rotation.RotationRequest
3838
import com.lambda.module.modules.client.TaskFlowModule
39+
import com.lambda.util.BlockUtils.blockState
3940
import com.lambda.util.BlockUtils.item
4041
import com.lambda.util.Communication.info
4142
import com.lambda.util.Communication.warn
@@ -97,7 +98,10 @@ object PlaceManager : RequestHandler<PlaceRequest>(), PositionBlocking {
9798
if ((request.placeContext.sneak && notSneaking) || !hotbarRequest.done || invalidRotation)
9899
return@listen
99100

100-
placeBlock(request, Hand.MAIN_HAND)
101+
val actionResult = placeBlock(request, Hand.MAIN_HAND)
102+
if (!actionResult.isAccepted) {
103+
warn("Placement interaction failed with $actionResult")
104+
}
101105
activeThisTick = true
102106
}
103107
}
@@ -174,63 +178,60 @@ object PlaceManager : RequestHandler<PlaceRequest>(), PositionBlocking {
174178
private fun SafeContext.placeBlock(request: PlaceRequest, hand: Hand) =
175179
interactBlock(request, request.buildConfig.placeSettings, hand, request.placeContext.result)
176180

177-
private fun SafeContext.interactBlock(request: PlaceRequest, placeConfig: PlaceConfig, hand: Hand, hitResult: BlockHitResult) {
181+
private fun SafeContext.interactBlock(
182+
request: PlaceRequest,
183+
placeConfig: PlaceConfig,
184+
hand: Hand,
185+
hitResult: BlockHitResult
186+
): ActionResult {
178187
interaction.syncSelectedSlot()
179-
if (!world.worldBorder.contains(hitResult.blockPos)) return
180-
181-
interaction.sendSequencedPacket(world) { sequence: Int ->
182-
val stackInHand = player.getStackInHand(hand)
183-
val stackCountPre = stackInHand.count
184-
val actionResult = interactBlockInternal(placeConfig, hand, hitResult)
185-
if (actionResult.isAccepted) {
186-
if (request.buildConfig.placeSettings.placeConfirmationMode == PlaceConfig.PlaceConfirmationMode.None)
187-
request.onPlace()
188-
else
189-
pendingPlacements.add(request)
190-
191-
if (actionResult.shouldSwingHand() && request.buildConfig.placeSettings.swing) {
192-
swingHand(request.buildConfig.placeSettings.swingType)
193-
}
194-
195-
if (!stackInHand.isEmpty && (stackInHand.count != stackCountPre || interaction.hasCreativeInventory())) {
196-
mc.gameRenderer.firstPersonRenderer.resetEquipProgress(hand)
197-
}
198-
} else {
199-
warn("Placement interaction failed with $actionResult")
200-
}
201-
PlayerInteractBlockC2SPacket(hand, hitResult, sequence)
202-
}
188+
if (!world.worldBorder.contains(hitResult.blockPos)) return ActionResult.FAIL
189+
return interactBlockInternal(request, placeConfig, hand, hitResult)
203190
}
204191

205192
private fun SafeContext.interactBlockInternal(
193+
request: PlaceRequest,
206194
placeConfig: PlaceConfig,
207195
hand: Hand,
208196
hitResult: BlockHitResult
209197
): ActionResult {
210198
val itemStack = player.getStackInHand(hand)
211199
if (gamemode == GameMode.SPECTATOR) return ActionResult.PASS
212200

213-
// checks if the player should be able to interact with the block for if its something
214-
// like a furnace or chest where an action would happen
215-
// val handNotEmpty = player.getStackInHand(hand).isEmpty.not()
216-
// val cantInteract = player.shouldCancelInteraction() && handNotEmpty
217-
// if (!cantInteract) return ActionResult.PASS
201+
val handNotEmpty = player.getStackInHand(hand).isEmpty.not()
202+
val cantInteract = player.shouldCancelInteraction() && handNotEmpty
203+
if (!cantInteract) {
204+
val blockState = blockState(hitResult.blockPos)
205+
if (!connection.hasFeature(blockState.block.requiredFeatures)) {
206+
return ActionResult.FAIL
207+
}
208+
209+
// checks if the player should be able to interact with the block for if its something
210+
// like a furnace or chest where an action would happen
211+
// val actionResult = blockState.onUse(world, player, hand, hitResult)
212+
// if (actionResult.isAccepted) {
213+
// return actionResult
214+
// }
215+
}
218216

219217
if (!itemStack.isEmpty && !isItemOnCooldown(itemStack.item)) {
220218
val itemUsageContext = ItemUsageContext(player, hand, hitResult)
221219
return if (gamemode.isCreative) {
222220
val i = itemStack.count
223-
useOnBlock(placeConfig, itemStack, itemUsageContext)
221+
useOnBlock(request, hand, hitResult, placeConfig, itemStack, itemUsageContext)
224222
.also {
225223
itemStack.count = i
226224
}
227225
} else
228-
useOnBlock(placeConfig, itemStack, itemUsageContext)
226+
useOnBlock(request, hand, hitResult, placeConfig, itemStack, itemUsageContext)
229227
}
230228
return ActionResult.PASS
231229
}
232230

233231
private fun SafeContext.useOnBlock(
232+
request: PlaceRequest,
233+
hand: Hand,
234+
hitResult: BlockHitResult,
234235
placeConfig: PlaceConfig,
235236
itemStack: ItemStack,
236237
context: ItemUsageContext
@@ -242,12 +243,14 @@ object PlaceManager : RequestHandler<PlaceRequest>(), PositionBlocking {
242243
if (cantModifyWorld && cantPlaceOn) return ActionResult.PASS
243244

244245
val item = (itemStack.item as? BlockItem) ?: return ActionResult.PASS
245-
val actionResult = place(placeConfig, item, ItemPlacementContext(context))
246246

247-
return actionResult
247+
return place(request, hand, hitResult, placeConfig, item, ItemPlacementContext(context))
248248
}
249249

250250
private fun SafeContext.place(
251+
request: PlaceRequest,
252+
hand: Hand,
253+
hitResult: BlockHitResult,
251254
placeConfig: PlaceConfig,
252255
item: BlockItem,
253256
context: ItemPlacementContext
@@ -258,6 +261,25 @@ object PlaceManager : RequestHandler<PlaceRequest>(), PositionBlocking {
258261
val itemPlacementContext = item.getPlacementContext(context) ?: return ActionResult.FAIL
259262
val blockState = item.getPlacementState(itemPlacementContext) ?: return ActionResult.FAIL
260263

264+
val stackInHand = player.getStackInHand(hand)
265+
val stackCountPre = stackInHand.count
266+
if (request.buildConfig.placeSettings.placeConfirmationMode == PlaceConfig.PlaceConfirmationMode.None)
267+
request.onPlace()
268+
else
269+
pendingPlacements.add(request)
270+
271+
interaction.sendSequencedPacket(world) { sequence: Int ->
272+
PlayerInteractBlockC2SPacket(hand, hitResult, sequence)
273+
}
274+
275+
if (request.buildConfig.placeSettings.swing) {
276+
swingHand(request.buildConfig.placeSettings.swingType)
277+
}
278+
279+
if (!stackInHand.isEmpty && (stackInHand.count != stackCountPre || interaction.hasCreativeInventory())) {
280+
mc.gameRenderer.firstPersonRenderer.resetEquipProgress(hand)
281+
}
282+
261283
if (placeConfig.placeConfirmationMode == PlaceConfig.PlaceConfirmationMode.AwaitThenPlace)
262284
return ActionResult.success(world.isClient)
263285

0 commit comments

Comments
 (0)