Skip to content

Commit 2ba5f58

Browse files
committed
sort contextual build results by current tool, if not build context
1 parent 25f1c9b commit 2ba5f58

File tree

7 files changed

+37
-33
lines changed

7 files changed

+37
-33
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ data class BreakContext(
6060
override fun compareTo(other: BuildContext): Int = runSafe {
6161
return when (other) {
6262
is BreakContext -> compareByDescending<BreakContext> {
63-
if (breakConfig.sorter == BreakConfig.SortMode.Tool) it.hotbarIndex == HotbarManager.serverSlot
63+
if (breakConfig.sorter == BreakConfig.SortMode.Tool)
64+
it.hotbarIndex == HotbarManager.serverSlot
6465
else 0
6566
}.thenBy {
6667
when (breakConfig.sorter) {

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,9 @@ package com.lambda.interaction.construction.result
2020
import com.lambda.util.Nameable
2121
import net.minecraft.util.math.BlockPos
2222

23-
abstract class BuildResult : Nameable, ComparableResult<Rank>() {
23+
abstract class BuildResult : Nameable, ComparableResult<Rank> {
2424
abstract val pos: BlockPos
2525
override val compareBy = this
2626

27-
final override fun compareTo(other: ComparableResult<Rank>) =
28-
compareBy.compareResult(other.compareBy)
29-
30-
open fun compareResult(other: ComparableResult<Rank>) =
31-
compareBy.rank.compareTo(other.compareBy.rank)
27+
final override fun compareTo(other: ComparableResult<Rank>) = super.compareTo(other)
3228
}

src/main/kotlin/com/lambda/interaction/construction/result/ComparableResult.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@
1717

1818
package com.lambda.interaction.construction.result
1919

20-
abstract class ComparableResult<T : Enum<T>> : Comparable<ComparableResult<T>> {
21-
abstract val rank: T
22-
abstract val compareBy: ComparableResult<T>
20+
interface ComparableResult<T : Enum<T>> : Comparable<ComparableResult<T>> {
21+
val rank: T
22+
val compareBy: ComparableResult<T>
23+
24+
override fun compareTo(other: ComparableResult<T>) =
25+
compareBy.compareResult(other.compareBy)
26+
27+
fun compareResult(other: ComparableResult<T>): Int =
28+
compareBy.rank.compareTo(other.compareBy.rank)
2329
}

src/main/kotlin/com/lambda/interaction/construction/result/Contextual.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,20 @@
1818
package com.lambda.interaction.construction.result
1919

2020
import com.lambda.interaction.construction.context.BuildContext
21+
import com.lambda.interaction.request.hotbar.HotbarManager
2122

22-
interface Contextual {
23+
interface Contextual : ComparableResult<Rank> {
2324
val context: BuildContext
25+
26+
override fun compareResult(other: ComparableResult<Rank>) =
27+
when (other) {
28+
29+
is Contextual -> compareByDescending<Contextual> {
30+
it.context.hotbarIndex == HotbarManager.serverSlot
31+
}.thenBy {
32+
it.compareBy.rank
33+
}.compare(this, other)
34+
35+
else -> compareBy.rank.compareTo(other.compareBy.rank)
36+
}
2437
}

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,16 @@ import net.minecraft.util.math.Direction
4242
import java.awt.Color
4343

4444
sealed class BreakResult : BuildResult() {
45+
override val name: String get() = "${this::class.simpleName} at ${pos.toShortString()}"
46+
4547
/**
4648
* Represents a successful break. All checks have been passed.
4749
* @param context The context of the break.
4850
*/
4951
data class Break(
5052
override val pos: BlockPos,
5153
override val context: BreakContext,
52-
) : Drawable, Contextual, BreakResult() {
53-
override val name: String get() = "${this::class.simpleName} at ${pos.toShortString()}"
54+
) : Contextual, Drawable, BreakResult() {
5455
override val rank = Rank.BreakSuccess
5556

5657
override fun ShapeBuilder.buildRenderer() {
@@ -60,7 +61,7 @@ sealed class BreakResult : BuildResult() {
6061
override fun compareResult(other: ComparableResult<Rank>) =
6162
when (other) {
6263
is Break -> context.compareTo(other.context)
63-
else -> super.compareResult(other)
64+
else -> super<Contextual>.compareResult(other)
6465
}
6566
}
6667

@@ -73,7 +74,6 @@ sealed class BreakResult : BuildResult() {
7374
override val pos: BlockPos,
7475
val side: Direction,
7576
) : Drawable, BreakResult() {
76-
override val name: String get() = "${this::class.simpleName} at ${pos.toShortString()}"
7777
override val rank = Rank.BreakNotExposed
7878
private val color = Color(46, 0, 0, 30)
7979

@@ -98,7 +98,6 @@ sealed class BreakResult : BuildResult() {
9898
val blockState: BlockState,
9999
val badItem: Item
100100
) : Drawable, Resolvable, BreakResult() {
101-
override val name: String get() = "${this::class.simpleName} at ${pos.toShortString()}"
102101
override val rank = Rank.BreakItemCantMine
103102
private val color = Color(255, 0, 0, 100)
104103

@@ -134,7 +133,6 @@ sealed class BreakResult : BuildResult() {
134133
override val pos: BlockPos,
135134
val blockState: BlockState
136135
) : Drawable, BreakResult() {
137-
override val name: String get() = "${this::class.simpleName} at ${pos.toShortString()}"
138136
override val rank = Rank.BreakSubmerge
139137
private val color = Color(114, 27, 255, 100)
140138

@@ -150,7 +148,6 @@ sealed class BreakResult : BuildResult() {
150148
override val pos: BlockPos,
151149
val blockState: BlockState,
152150
) : Drawable, BreakResult() {
153-
override val name: String get() = "${this::class.simpleName} at ${pos.toShortString()}"
154151
override val rank = Rank.BreakIsBlockedByFluid
155152
private val color = Color(50, 12, 112, 100)
156153

@@ -166,7 +163,6 @@ sealed class BreakResult : BuildResult() {
166163
override val pos: BlockPos,
167164
val blockState: BlockState,
168165
) : Navigable, Drawable, BreakResult() {
169-
override val name: String get() = "${this::class.simpleName} at ${pos.toShortString()}"
170166
override val rank = Rank.BreakPlayerOnTop
171167
private val color = Color(252, 3, 207, 100)
172168

@@ -181,7 +177,6 @@ sealed class BreakResult : BuildResult() {
181177
override val pos: BlockPos,
182178
override val dependency: BuildResult
183179
) : BreakResult(), Dependent by Dependent.Nested(dependency) {
184-
override val name: String get() = "${this::class.simpleName} at ${pos.toShortString()}"
185180
override val rank = dependency.rank
186181
override val compareBy = lastDependency
187182
}

src/main/kotlin/com/lambda/interaction/construction/result/results/InteractResult.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ import com.lambda.interaction.construction.result.Rank
2828
import net.minecraft.util.math.BlockPos
2929

3030
sealed class InteractResult : BuildResult() {
31+
override val name: String get() = "${this::class.simpleName} at ${pos.toShortString()}"
32+
3133
data class Interact(
3234
override val pos: BlockPos,
3335
override val context: InteractionContext
3436
) : Contextual, Drawable, InteractResult() {
35-
override val name: String get() = "${this::class.simpleName} at ${pos.toShortString()}"
3637
override val rank = Rank.InteractSuccess
3738

3839
override fun ShapeBuilder.buildRenderer() {
@@ -42,15 +43,14 @@ sealed class InteractResult : BuildResult() {
4243
override fun compareResult(other: ComparableResult<Rank>) =
4344
when (other) {
4445
is Interact -> context.compareTo(other.context)
45-
else -> super.compareResult(other)
46+
else -> super<Contextual>.compareResult(other)
4647
}
4748
}
4849

4950
data class Dependency(
5051
override val pos: BlockPos,
5152
override val dependency: BuildResult
5253
) : InteractResult(), Dependent by Dependent.Nested(dependency) {
53-
override val name: String get() = "${this::class.simpleName} at ${pos.toShortString()}"
5454
override val rank = dependency.rank
5555
override val compareBy = lastDependency
5656
}

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ import java.awt.Color
4545
* First based on the context, then based on the [com.lambda.interaction.construction.result.Rank].
4646
*/
4747
sealed class PlaceResult : BuildResult() {
48+
override val name: String get() = "${this::class.simpleName} at ${pos.toShortString()}"
49+
4850
/**
4951
* Represents a successful placement. All checks have been passed.
5052
* @param context The context of the placement.
@@ -53,7 +55,6 @@ sealed class PlaceResult : BuildResult() {
5355
override val pos: BlockPos,
5456
override val context: PlaceContext,
5557
) : Contextual, Drawable, PlaceResult() {
56-
override val name: String get() = "${this::class.simpleName} at ${pos.toShortString()}"
5758
override val rank = Rank.PlaceSuccess
5859

5960
override fun ShapeBuilder.buildRenderer() {
@@ -63,7 +64,7 @@ sealed class PlaceResult : BuildResult() {
6364
override fun compareResult(other: ComparableResult<Rank>) =
6465
when (other) {
6566
is Place -> context.compareTo(other.context)
66-
else -> super.compareResult(other)
67+
else -> super<Contextual>.compareResult(other)
6768
}
6869
}
6970

@@ -84,7 +85,6 @@ sealed class PlaceResult : BuildResult() {
8485
val simulated: ItemPlacementContext,
8586
val actual: BlockState? = null,
8687
) : Drawable, PlaceResult() {
87-
override val name: String get() = "${this::class.simpleName} at ${pos.toShortString()}"
8888
override val rank = Rank.PlaceNoIntegrity
8989
private val color = Color(252, 3, 3, 100)
9090

@@ -101,7 +101,6 @@ sealed class PlaceResult : BuildResult() {
101101
data class BlockedByEntity(
102102
override val pos: BlockPos,
103103
) : Navigable, PlaceResult() {
104-
override val name: String get() = "${this::class.simpleName} at ${pos.toShortString()}"
105104
override val rank = Rank.PlaceBlockedByPlayer
106105

107106
// ToDo: check what type of entity. player -> leave box, other entity -> kill?
@@ -118,7 +117,6 @@ sealed class PlaceResult : BuildResult() {
118117
override val pos: BlockPos,
119118
val simulated: ItemPlacementContext,
120119
) : Resolvable, PlaceResult() {
121-
override val name: String get() = "${this::class.simpleName} at ${pos.toShortString()}"
122120
override val rank = Rank.PlaceCantReplace
123121

124122
context(automated: Automated)
@@ -135,7 +133,6 @@ sealed class PlaceResult : BuildResult() {
135133
override val pos: BlockPos,
136134
val simulated: ItemPlacementContext,
137135
) : PlaceResult() {
138-
override val name: String get() = "${this::class.simpleName} at ${pos.toShortString()}"
139136
override val rank = Rank.PlaceScaffoldExceeded
140137
}
141138

@@ -150,7 +147,6 @@ sealed class PlaceResult : BuildResult() {
150147
override val pos: BlockPos,
151148
val itemStack: ItemStack,
152149
) : PlaceResult() {
153-
override val name: String get() = "${this::class.simpleName} at ${pos.toShortString()}"
154150
override val rank = Rank.PlaceBlockFeatureDisabled
155151
}
156152

@@ -164,7 +160,6 @@ sealed class PlaceResult : BuildResult() {
164160
override val pos: BlockPos,
165161
val actualPos: BlockPos,
166162
) : PlaceResult() {
167-
override val name: String get() = "${this::class.simpleName} at ${pos.toShortString()}"
168163
override val rank = Rank.UnexpectedPosition
169164
}
170165

@@ -178,15 +173,13 @@ sealed class PlaceResult : BuildResult() {
178173
data class IllegalUsage(
179174
override val pos: BlockPos,
180175
) : PlaceResult() {
181-
override val name: String get() = "${this::class.simpleName} at ${pos.toShortString()}"
182176
override val rank = Rank.PlaceIllegalUsage
183177
}
184178

185179
data class Dependency(
186180
override val pos: BlockPos,
187181
override val dependency: BuildResult
188182
) : PlaceResult(), Dependent by Dependent.Nested(dependency) {
189-
override val name: String get() = "${this::class.simpleName} at ${pos.toShortString()}"
190183
override val rank = lastDependency.rank
191184
override val compareBy = lastDependency
192185
}

0 commit comments

Comments
 (0)