Skip to content

Commit 5cfdbac

Browse files
committed
Refactor block interaction logic to use expected positions
Replaced `resultingPos` with `expectedPos` across the codebase for improved clarity and consistency in block placement and interaction handling. Introduced enhanced logic for detecting unexpected positions and added a corresponding result type `UnexpectedPosition`. Updated related event systems and modules to align with the new `BlockChange` event structure.
1 parent a5ea17c commit 5cfdbac

File tree

13 files changed

+58
-63
lines changed

13 files changed

+58
-63
lines changed

common/src/main/java/com/lambda/mixin/world/WorldMixin.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,12 @@
2525
import org.spongepowered.asm.mixin.Mixin;
2626
import org.spongepowered.asm.mixin.injection.At;
2727
import org.spongepowered.asm.mixin.injection.Inject;
28-
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
28+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
2929

3030
@Mixin(World.class)
3131
public abstract class WorldMixin {
32-
@Inject(method = "setBlockState(Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/BlockState;II)Z", at = @At("HEAD"), cancellable = true)
33-
void setBlockStatePre(BlockPos pos, BlockState state, int flags, int maxUpdateDepth, CallbackInfoReturnable<Boolean> cir) {
34-
if (EventFlow.post(new WorldEvent.BlockUpdate.Pre(pos, state, flags, maxUpdateDepth)).isCanceled()) {
35-
cir.setReturnValue(false);
36-
}
37-
}
38-
39-
@Inject(method = "setBlockState(Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/BlockState;II)Z", at = @At("TAIL"))
40-
void setBlockStatePost(BlockPos pos, BlockState state, int flags, int maxUpdateDepth, CallbackInfoReturnable<Boolean> cir) {
41-
EventFlow.post(new WorldEvent.BlockUpdate.Post(pos, state, flags, maxUpdateDepth));
32+
@Inject(method = "onBlockChanged", at = @At("TAIL"))
33+
void onBlockChanged(BlockPos pos, BlockState oldBlock, BlockState newBlock, CallbackInfo ci) {
34+
EventFlow.post(new WorldEvent.BlockChange(pos, oldBlock, newBlock));
4235
}
4336
}

common/src/main/kotlin/com/lambda/event/events/WorldEvent.kt

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,11 @@ sealed class WorldEvent {
4545
) : Event
4646
}
4747

48-
/**
49-
* Represents a block update in the world
50-
*/
51-
sealed class BlockUpdate(
48+
class BlockChange(
5249
val pos: BlockPos,
53-
val state: BlockState,
54-
val flags: Int,
55-
val maxUpdateDepth: Int,
56-
) {
57-
class Pre(pos: BlockPos, state: BlockState, flags: Int, depth: Int) : BlockUpdate(pos, state, flags, depth), ICancellable by Cancellable()
58-
class Post(pos: BlockPos, state: BlockState, flags: Int, depth: Int) : BlockUpdate(pos, state, flags, depth), Event
59-
}
50+
val oldState: BlockState,
51+
val newState: BlockState,
52+
) : Event
6053

6154
/**
6255
* Represents an entity being added to the world

common/src/main/kotlin/com/lambda/graphics/renderer/esp/ChunkedESP.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class ChunkedESP private constructor(
5353
}
5454

5555
init {
56-
listenConcurrently<WorldEvent.BlockUpdate.Post> { event ->
56+
listenConcurrently<WorldEvent.BlockChange> { event ->
5757
world.getWorldChunk(event.pos).renderer.notifyChunks()
5858
}
5959

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ data class BreakContext(
3535
override var hand: Hand,
3636
val instantBreak: Boolean,
3737
) : BuildContext {
38-
override val resultingPos: BlockPos
38+
override val expectedPos: BlockPos
3939
get() = result.blockPos
4040

4141
override val distance: Double by lazy {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ interface BuildContext : Comparable<BuildContext> {
2929
val result: BlockHitResult
3030
val distance: Double
3131
val expectedState: BlockState
32+
val expectedPos: BlockPos
3233
val checkedState: BlockState
3334
val hand: Hand
34-
val resultingPos: BlockPos
3535
val rotation: RotationContext
3636

3737
override fun compareTo(other: BuildContext): Int {

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,12 @@ data class PlaceContext(
3535
override val expectedState: BlockState,
3636
override val checkedState: BlockState,
3737
override val hand: Hand,
38+
override val expectedPos: BlockPos,
3839
val targetState: TargetState,
3940
val sneak: Boolean,
4041
val insideBlock: Boolean,
4142
val primeDirection: Direction?
4243
) : BuildContext {
43-
override val resultingPos: BlockPos
44-
get() = result.blockPos.offset(result.side)
45-
4644
override fun compareTo(other: BuildContext): Int {
4745
return when (other) {
4846
is PlaceContext -> compareBy<PlaceContext> {

common/src/main/kotlin/com/lambda/interaction/construction/result/BreakResult.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ sealed class BreakResult : BuildResult() {
5656
}
5757

5858
override fun SafeContext.buildRenderer() {
59-
withPos(context.resultingPos, color, context.result.side)
59+
withPos(context.expectedPos, color, context.result.side)
6060
}
6161

6262
override fun compareTo(other: ComparableResult<Rank>): Int {

common/src/main/kotlin/com/lambda/interaction/construction/result/PlaceResult.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ sealed class PlaceResult : BuildResult() {
5959
withPos(hitPos, color, context.result.side)
6060

6161
val light = Color(35, 188, 254, 20)
62-
withState(context.expectedState, context.resultingPos, light)
62+
withState(context.expectedState, context.expectedPos, light)
6363
}
6464

6565
override fun compareTo(other: ComparableResult<Rank>): Int {
@@ -136,6 +136,13 @@ sealed class PlaceResult : BuildResult() {
136136
override val rank = Rank.PLACE_BLOCK_FEATURE_DISABLED
137137
}
138138

139+
data class UnexpectedPosition(
140+
override val blockPos: BlockPos,
141+
val actualPos: BlockPos
142+
) : PlaceResult() {
143+
override val rank = Rank.UNEXPECTED_POSITION
144+
}
145+
139146
/**
140147
* The player has no permission to interact with the block. Or the stack cannot be used on the block.
141148
*/

common/src/main/kotlin/com/lambda/interaction/construction/result/Rank.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ enum class Rank {
4242
BREAK_NO_PERMISSION,
4343
PLACE_SCAFFOLD_EXCEEDED,
4444
PLACE_BLOCK_FEATURE_DISABLED,
45+
UNEXPECTED_POSITION,
4546
PLACE_ILLEGAL_USAGE,
4647

4748
// not an issue

common/src/main/kotlin/com/lambda/interaction/construction/simulation/BuildSimulator.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,12 @@ object BuildSimulator {
257257

258258
var context = ItemPlacementContext(usageContext)
259259

260+
// ToDo: Actually find these result positions as well and use them smartly
261+
if (context.blockPos != pos) {
262+
acc.add(PlaceResult.UnexpectedPosition(pos, context.blockPos))
263+
return@forEach
264+
}
265+
260266
if (!optimalStack.item.isEnabled(world.enabledFeatures)) {
261267
acc.add(PlaceResult.BlockFeatureDisabled(pos, optimalStack))
262268
return@forEach
@@ -309,6 +315,7 @@ object BuildSimulator {
309315
resultState,
310316
blockHit.blockPos.blockState(world),
311317
Hand.MAIN_HAND,
318+
context.blockPos,
312319
target,
313320
shouldSneak,
314321
false,

0 commit comments

Comments
 (0)