Skip to content

Commit f280938

Browse files
committed
Placement interaction simulation
1 parent 8bf3778 commit f280938

File tree

13 files changed

+504
-101
lines changed

13 files changed

+504
-101
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,21 @@ import com.lambda.util.world.raycast.RayCastUtils.distanceTo
66
import net.minecraft.block.BlockState
77
import net.minecraft.util.Hand
88
import net.minecraft.util.hit.BlockHitResult
9+
import net.minecraft.util.math.BlockPos
910
import net.minecraft.util.math.Direction
1011
import net.minecraft.util.math.Vec3d
1112

1213
data class BreakContext(
1314
val pov: Vec3d,
1415
val result: BlockHitResult,
1516
val rotation: RotationContext,
16-
val checkedState: BlockState,
17-
var hand: Hand,
17+
override val checkedState: BlockState,
18+
override var hand: Hand,
1819
val instantBreak: Boolean,
19-
) : BuildContext, ComparableContext {
20+
) : BuildContext {
21+
override val resultingPos: BlockPos
22+
get() = result.blockPos
23+
2024
override val distance: Double by lazy {
2125
result.distanceTo(pov)
2226
}
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package com.lambda.interaction.construction.context
22

33
import net.minecraft.block.BlockState
4+
import net.minecraft.util.Hand
5+
import net.minecraft.util.math.BlockPos
46

5-
interface BuildContext {
7+
interface BuildContext : ComparableContext {
68
val distance: Double
79
val expectedState: BlockState
8-
// val resultingPos: BlockPos
10+
val checkedState: BlockState
11+
val hand: Hand
12+
val resultingPos: BlockPos
913
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.lambda.interaction.construction.context
2+
3+
import com.lambda.interaction.construction.verify.TargetState
4+
import com.lambda.interaction.rotation.RotationContext
5+
import com.lambda.util.BlockUtils
6+
import net.minecraft.block.BlockState
7+
import net.minecraft.util.Hand
8+
import net.minecraft.util.hit.BlockHitResult
9+
import net.minecraft.util.math.BlockPos
10+
import net.minecraft.util.math.Vec3d
11+
12+
data class PlaceContext(
13+
val pov: Vec3d,
14+
val result: BlockHitResult,
15+
val rotation: RotationContext,
16+
override val distance: Double,
17+
override val expectedState: BlockState,
18+
override val checkedState: BlockState,
19+
val targetState: TargetState,
20+
override val hand: Hand,
21+
val sneak: Boolean,
22+
val insideBlock: Boolean,
23+
) : BuildContext {
24+
override val resultingPos: BlockPos
25+
get() = result.blockPos.offset(result.side)
26+
27+
override fun compareTo(other: ComparableContext): Int {
28+
return when (other) {
29+
is PlaceContext -> compareBy<PlaceContext> {
30+
BlockUtils.fluids.indexOf(it.checkedState.fluidState.fluid)
31+
}.thenByDescending {
32+
it.checkedState.fluidState.level
33+
}.thenBy {
34+
it.hand
35+
}.thenBy {
36+
it.sneak
37+
}.thenBy {
38+
it.distance
39+
}.thenBy {
40+
it.insideBlock
41+
}.compare(this, other)
42+
else -> 1
43+
}
44+
}
45+
}

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

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,6 @@ sealed class BreakResult : BuildResult() {
3737
}
3838
}
3939

40-
/**
41-
* Represents a break out of reach.
42-
* @param blockPos The position of the block that is out of reach.
43-
* @param distance The distance to the hit vector.
44-
*/
45-
data class OutOfReach(
46-
override val blockPos: BlockPos,
47-
val distance: Double
48-
) : Resolvable, BreakResult() {
49-
override val rank = Rank.BREAK_OUT_OF_REACH
50-
51-
override val resolve = moveToBlock(blockPos)
52-
53-
override fun compareTo(other: ComparableResult<Rank>): Int {
54-
return when (other) {
55-
is OutOfReach -> distance.compareTo(other.distance)
56-
else -> super.compareTo(other)
57-
}
58-
}
59-
}
60-
6140
/**
6241
* Represents a break configuration where the hit side is not exposed to air.
6342
* @param blockPos The position of the block that is not exposed.
@@ -79,28 +58,6 @@ sealed class BreakResult : BuildResult() {
7958
}
8059
}
8160

82-
/**
83-
* The checked break configuration hits on a side not in the player direction.
84-
* @param blockPos The position of the block that is not exposed.
85-
* @param side The side that is not exposed.
86-
*/
87-
data class NotVisible(
88-
override val blockPos: BlockPos,
89-
val side: Direction,
90-
val distance: Double
91-
) : Resolvable, BreakResult() {
92-
override val rank = Rank.BREAK_NOT_VISIBLE
93-
94-
override val resolve = emptyTask()
95-
96-
override fun compareTo(other: ComparableResult<Rank>): Int {
97-
return when (other) {
98-
is NotVisible -> distance.compareTo(other.distance)
99-
else -> super.compareTo(other)
100-
}
101-
}
102-
}
103-
10461
/**
10562
* The equipped item is not suitable for breaking blocks.
10663
* @param blockState The block state that is being broken.
@@ -129,28 +86,6 @@ sealed class BreakResult : BuildResult() {
12986
}
13087
}
13188

132-
/**
133-
* Player has an inefficient tool equipped.
134-
* @param bestTool The best tool for the block state.
135-
*/
136-
data class WrongTool(
137-
override val blockPos: BlockPos,
138-
val context: BreakContext,
139-
val bestTool: Item
140-
) : Resolvable, BreakResult() {
141-
override val rank = Rank.BREAK_WRONG_TOOL
142-
143-
override val resolve: Task<*> =
144-
bestTool.select().transfer(MainHandContainer).solve
145-
146-
override fun compareTo(other: ComparableResult<Rank>): Int {
147-
return when (other) {
148-
is WrongTool -> context.compareTo(other.context)
149-
else -> super.compareTo(other)
150-
}
151-
}
152-
}
153-
15489
/**
15590
* The block is a liquid and first has to be submerged.
15691
* @param blockPos The position of the block that is a liquid.

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

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
package com.lambda.interaction.construction.result
22

3+
import com.lambda.interaction.construction.context.BreakContext
4+
import com.lambda.interaction.construction.context.BuildContext
5+
import com.lambda.interaction.material.ContainerManager.transfer
6+
import com.lambda.interaction.material.StackSelection.Companion.select
7+
import com.lambda.interaction.material.container.MainHandContainer
8+
import com.lambda.task.Task
9+
import com.lambda.task.Task.Companion.emptyTask
10+
import com.lambda.task.tasks.GoalTask.Companion.moveToBlock
311
import com.lambda.task.tasks.GoalTask.Companion.moveUntilLoaded
4-
import net.minecraft.block.Block
512
import net.minecraft.block.BlockState
13+
import net.minecraft.item.Item
614
import net.minecraft.util.math.BlockPos
15+
import net.minecraft.util.math.Direction
716

817
abstract class BuildResult : ComparableResult<Rank> {
918
abstract val blockPos: BlockPos
@@ -51,7 +60,7 @@ abstract class BuildResult : ComparableResult<Rank> {
5160
*/
5261
data class Restricted(
5362
override val blockPos: BlockPos
54-
) : BreakResult() {
63+
) : BuildResult() {
5564
override val rank = Rank.BREAK_RESTRICTED
5665
}
5766

@@ -63,7 +72,7 @@ abstract class BuildResult : ComparableResult<Rank> {
6372
data class NoPermission(
6473
override val blockPos: BlockPos,
6574
val blockState: BlockState
66-
) : BreakResult() {
75+
) : BuildResult() {
6776
override val rank = Rank.BREAK_NO_PERMISSION
6877
}
6978

@@ -73,7 +82,7 @@ abstract class BuildResult : ComparableResult<Rank> {
7382
*/
7483
data class OutOfWorld(
7584
override val blockPos: BlockPos
76-
) : BreakResult() {
85+
) : BuildResult() {
7786
override val rank = Rank.BREAK_OUT_OF_WORLD
7887
}
7988

@@ -85,7 +94,72 @@ abstract class BuildResult : ComparableResult<Rank> {
8594
data class Unbreakable(
8695
override val blockPos: BlockPos,
8796
val blockState: BlockState
88-
) : BreakResult() {
89-
override val rank = Rank.BREAK_UNBREAKABLE
97+
) : BuildResult() {
98+
override val rank = Rank.UNBREAKABLE
99+
}
100+
101+
/**
102+
* The checked configuration hits on a side not in the player direction.
103+
* @param blockPos The position of the block that is not exposed.
104+
* @param side The side that is not exposed.
105+
*/
106+
data class NotVisible(
107+
override val blockPos: BlockPos,
108+
val side: Direction,
109+
val distance: Double
110+
) : Resolvable, BuildResult() {
111+
override val rank = Rank.NOT_VISIBLE
112+
113+
override val resolve = emptyTask()
114+
115+
override fun compareTo(other: ComparableResult<Rank>): Int {
116+
return when (other) {
117+
is NotVisible -> distance.compareTo(other.distance)
118+
else -> super.compareTo(other)
119+
}
120+
}
121+
}
122+
123+
/**
124+
* Player has an inefficient tool equipped.
125+
* @param neededItem The best tool for the block state.
126+
*/
127+
data class WrongItem(
128+
override val blockPos: BlockPos,
129+
val context: BuildContext,
130+
val neededItem: Item
131+
) : Resolvable, BuildResult() {
132+
override val rank = Rank.WRONG_ITEM
133+
134+
override val resolve: Task<*> =
135+
neededItem.select().transfer(MainHandContainer).solve
136+
137+
override fun compareTo(other: ComparableResult<Rank>): Int {
138+
return when (other) {
139+
is WrongItem -> context.compareTo(other.context)
140+
else -> super.compareTo(other)
141+
}
142+
}
143+
}
144+
145+
/**
146+
* Represents a break out of reach.
147+
* @param blockPos The position of the block that is out of reach.
148+
* @param distance The distance to the hit vector.
149+
*/
150+
data class OutOfReach(
151+
override val blockPos: BlockPos,
152+
val distance: Double
153+
) : Resolvable, BuildResult() {
154+
override val rank = Rank.OUT_OF_REACH
155+
156+
override val resolve = moveToBlock(blockPos)
157+
158+
override fun compareTo(other: ComparableResult<Rank>): Int {
159+
return when (other) {
160+
is OutOfReach -> distance.compareTo(other.distance)
161+
else -> super.compareTo(other)
162+
}
163+
}
90164
}
91165
}

0 commit comments

Comments
 (0)