Skip to content

Commit 771469d

Browse files
committed
consistent confirmation checks for block updates in the managers
1 parent 1b70a1a commit 771469d

File tree

8 files changed

+30
-35
lines changed

8 files changed

+30
-35
lines changed

common/src/main/kotlin/com/lambda/interaction/construction/context/InteractionContext.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class InteractionContext(
3636
override val result: BlockHitResult,
3737
override val rotation: RotationRequest,
3838
override var hotbarIndex: Int,
39-
override val cachedState: BlockState,
39+
override var cachedState: BlockState,
4040
override val expectedState: BlockState,
4141
) : BuildContext() {
4242
private val baseColor = Color(35, 254, 79, 25)

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ import com.lambda.interaction.request.rotating.RotationRequest
6767
import com.lambda.threading.runSafe
6868
import com.lambda.util.BlockUtils.blockState
6969
import com.lambda.util.BlockUtils.calcItemBlockBreakingDelta
70-
import com.lambda.util.BlockUtils.emptyState
7170
import com.lambda.util.BlockUtils.isEmpty
7271
import com.lambda.util.BlockUtils.isNotBroken
7372
import com.lambda.util.BlockUtils.isNotEmpty
@@ -192,8 +191,8 @@ object BreakManager : RequestHandler<BreakRequest>(
192191
if (isNotBroken(currentState, event.newState)) {
193192
// check to see if its just some small property changes, e.g. redstone ore changing the LIT property
194193
if (!currentState.matches(event.newState, ProcessorRegistry.postProcessedProperties))
195-
this@BreakManager.warn("Break at ${event.pos.toShortString()} was rejected with ${event.newState} instead of ${info.context.cachedState.emptyState}")
196-
// update the checked state
194+
this@BreakManager.warn("Server updated breaking block at ${event.pos.toShortString()} with a new state: ${event.newState}")
195+
// update the cached state
197196
info.context.cachedState = event.newState
198197
return@listen
199198
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ object BrokenBlockHandler : PostActionHandler<BreakInfo>() {
5959
if (!info.broken) warn("${info::class.simpleName} at ${info.context.blockPos.toShortString()} timed out")
6060
else if (!TaskFlowModule.ignoreItemDropWarnings) warn("${info::class.simpleName}'s item drop at ${info.context.blockPos.toShortString()} timed out")
6161

62-
if (!info.broken && info.breakConfig.breakConfirmation != BreakConfirmationMode.AwaitThenBreak) {
62+
if (info.broken && info.breakConfig.breakConfirmation != BreakConfirmationMode.AwaitThenBreak) {
6363
world.setBlockState(info.context.blockPos, info.context.cachedState)
6464
}
6565
}
@@ -78,8 +78,10 @@ object BrokenBlockHandler : PostActionHandler<BreakInfo>() {
7878
// return if the block's not broken
7979
if (isNotBroken(currentState, event.newState)) {
8080
// return if the state hasn't changed
81-
if (event.newState.matches(currentState, ProcessorRegistry.postProcessedProperties))
81+
if (event.newState.matches(currentState, ProcessorRegistry.postProcessedProperties)) {
82+
pending.context.cachedState = event.newState
8283
return@listen
84+
}
8385

8486
if (!pending.isReBreaking) {
8587
this@BrokenBlockHandler.warn("Broken block at ${event.pos.toShortString()} was rejected with ${event.newState} instead of ${pending.context.cachedState.emptyState}")

common/src/main/kotlin/com/lambda/interaction/request/interacting/InteractedBlockHandler.kt

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ import com.lambda.util.BlockUtils.matches
2727
import com.lambda.util.Communication.info
2828
import com.lambda.util.Communication.warn
2929
import com.lambda.util.collections.LimitedDecayQueue
30-
import net.minecraft.block.BlockState
31-
import net.minecraft.util.Hand
32-
import net.minecraft.util.math.BlockPos
3330

3431
object InteractedBlockHandler : PostActionHandler<InteractionInfo>() {
3532
override val pendingActions = LimitedDecayQueue<InteractionInfo>(
@@ -46,24 +43,20 @@ object InteractedBlockHandler : PostActionHandler<InteractionInfo>() {
4643
listen<WorldEvent.BlockUpdate.Server>(priority = Int.MIN_VALUE) { event ->
4744
pendingActions
4845
.firstOrNull { it.context.blockPos == event.pos }
49-
?.let { info ->
50-
info.stopPending()
46+
?.let { pending ->
47+
pending.stopPending()
5148

52-
if (!matchesTargetState(event.pos, info.context.expectedState, event.newState))
49+
if (!pending.context.expectedState.matches(event.newState)) {
50+
this@InteractedBlockHandler.warn("Interacted block at ${event.pos.toShortString()} was rejected with ${event.newState} instead of ${pending.context.expectedState}")
5351
return@listen
52+
}
5453

55-
if (info.interactConfirmationMode == InteractionConfig.InteractConfirmationMode.AwaitThenInteract)
56-
with (info.context) {
57-
cachedState.onUse(world, player, Hand.MAIN_HAND, result)
58-
}
54+
//ToDo: reliable way to recreate the sounds played when interacting with any given block
55+
// if (pending.interactConfirmationMode == InteractionConfig.InteractConfirmationMode.AwaitThenInteract)
56+
// with (pending.context) {
57+
// cachedState.onUse(world, player, Hand.MAIN_HAND, result)
58+
// }
5959
}
6060
}
6161
}
62-
63-
private fun matchesTargetState(pos: BlockPos, targetState: BlockState, newState: BlockState) =
64-
if (targetState.matches(newState)) true
65-
else {
66-
this@InteractedBlockHandler.warn("Interaction at ${pos.toShortString()} was rejected with $newState instead of $targetState")
67-
false
68-
}
6962
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ package com.lambda.interaction.request.placing
2020
import com.lambda.interaction.construction.context.BuildContext
2121
import com.lambda.interaction.construction.context.PlaceContext
2222
import com.lambda.interaction.request.ActionInfo
23+
import net.minecraft.util.math.BlockPos
2324

2425
data class PlaceInfo(
2526
override val context: PlaceContext,
2627
override val pendingInteractionsList: MutableCollection<BuildContext>,
27-
val onPlace: () -> Unit,
28+
val onPlace: ((BlockPos) -> Unit)?,
2829
val placeConfig: PlaceConfig
2930
) : ActionInfo

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ object PlaceManager : RequestHandler<PlaceRequest>(
316316
if (!player.abilities.creativeMode) itemStack.decrement(1)
317317

318318
if (placeConfig.placeConfirmationMode == PlaceConfig.PlaceConfirmationMode.None) {
319-
request.onPlace()
319+
request.onPlace?.invoke(placeContext.blockPos)
320320
}
321321

322322
return ActionResult.success(world.isClient)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@ import com.lambda.interaction.request.rotating.RotationConfig
2626
import com.lambda.threading.runSafe
2727
import com.lambda.util.BlockUtils.blockState
2828
import com.lambda.util.BlockUtils.matches
29+
import net.minecraft.util.math.BlockPos
2930

3031
data class PlaceRequest(
3132
val contexts: Collection<PlaceContext>,
3233
val build: BuildConfig,
3334
val rotation: RotationConfig,
3435
val hotbar: HotbarConfig,
3536
val pendingInteractions: MutableCollection<BuildContext>,
36-
val onPlace: () -> Unit
37+
val onPlace: ((BlockPos) -> Unit)? = null
3738
) : Request(), PlaceConfig by build.placing {
3839
override val config = build.placing
3940
override val done: Boolean

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,20 @@ object PlacedBlockHandler : PostActionHandler<PlaceInfo>() {
4545
listen<WorldEvent.BlockUpdate.Server>(priority = Int.MIN_VALUE) { event ->
4646
pendingActions
4747
.firstOrNull { it.context.blockPos == event.pos }
48-
?.let { info ->
49-
info.stopPending()
48+
?.let { pending ->
49+
pending.stopPending()
5050

5151
// return if the block wasn't placed properly
52-
if (!event.newState.matches(info.context.expectedState)) {
53-
this@PlacedBlockHandler.warn(
54-
"Place at ${event.pos.toShortString()} was rejected with ${event.newState} instead of ${info.context.expectedState}"
55-
)
52+
if (!pending.context.expectedState.matches(event.newState)) {
53+
this@PlacedBlockHandler.warn("Place at ${event.pos.toShortString()} was rejected with ${event.newState} instead of ${pending.context.expectedState}")
54+
return@listen
5655
}
5756

58-
if (info.placeConfig.placeConfirmationMode == PlaceConfig.PlaceConfirmationMode.AwaitThenPlace)
59-
with (info.context) {
57+
if (pending.placeConfig.placeConfirmationMode == PlaceConfig.PlaceConfirmationMode.AwaitThenPlace)
58+
with (pending.context) {
6059
placeSound(expectedState.block.item as BlockItem, expectedState, blockPos)
6160
}
62-
info.onPlace()
61+
pending.onPlace?.invoke(pending.context.blockPos)
6362
}
6463
}
6564
}

0 commit comments

Comments
 (0)