Skip to content

Commit ce8744d

Browse files
committed
BreakContext compareTo rework
1 parent 9f91242 commit ce8744d

File tree

6 files changed

+43
-21
lines changed

6 files changed

+43
-21
lines changed

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

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

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

20+
import com.lambda.context.Automated
2021
import com.lambda.graphics.renderer.esp.DirectionMask
2122
import com.lambda.graphics.renderer.esp.DirectionMask.exclude
2223
import com.lambda.graphics.renderer.esp.ShapeBuilder
@@ -27,12 +28,16 @@ import com.lambda.interaction.request.LogContext.Companion.getLogContextBuilder
2728
import com.lambda.interaction.request.breaking.BreakConfig
2829
import com.lambda.interaction.request.hotbar.HotbarManager
2930
import com.lambda.interaction.request.rotating.RotationRequest
31+
import com.lambda.threading.runSafe
3032
import com.lambda.util.BlockUtils.emptyState
33+
import net.minecraft.block.Block
3134
import net.minecraft.block.BlockState
3235
import net.minecraft.block.FallingBlock
3336
import net.minecraft.util.hit.BlockHitResult
3437
import net.minecraft.util.math.BlockPos
38+
import net.minecraft.util.math.Vec3d
3539
import java.awt.Color
40+
import kotlin.math.sqrt
3641
import kotlin.random.Random
3742

3843
data class BreakContext(
@@ -42,8 +47,9 @@ data class BreakContext(
4247
var itemSelection: StackSelection,
4348
var instantBreak: Boolean,
4449
override var cachedState: BlockState,
45-
val sortMode: BreakConfig.SortMode
46-
) : BuildContext(), LogContext {
50+
val sortMode: BreakConfig.SortMode,
51+
private val automated: Automated
52+
) : BuildContext(), LogContext, Automated by automated {
4753
private val baseColor = Color(222, 0, 0, 25)
4854
private val sideColor = Color(222, 0, 0, 100)
4955

@@ -52,28 +58,37 @@ data class BreakContext(
5258

5359
val random = Random.nextDouble()
5460

55-
override fun compareTo(other: BuildContext): Int {
61+
override fun compareTo(other: BuildContext): Int = runSafe {
5662
return when (other) {
57-
is BreakContext -> compareBy<BreakContext> {
63+
is BreakContext -> compareByDescending<BreakContext> {
64+
if (sortMode == BreakConfig.SortMode.Tool) it.hotbarIndex == HotbarManager.serverSlot
65+
else 0
66+
}.thenBy {
5867
when (sortMode) {
59-
BreakConfig.SortMode.Closest -> it.distance
60-
BreakConfig.SortMode.Farthest -> -it.distance
61-
BreakConfig.SortMode.Tool -> it.hotbarIndex != HotbarManager.serverSlot
68+
BreakConfig.SortMode.Tool,
69+
BreakConfig.SortMode.Closest -> player.eyePos.distance(it.result.pos, it.cachedState.block)
70+
BreakConfig.SortMode.Farthest -> -player.eyePos.distance(it.result.pos, it.cachedState.block)
6271
BreakConfig.SortMode.Rotation -> it.rotation.target.angleDistance
6372
BreakConfig.SortMode.Random -> it.random
6473
}
65-
}.thenBy {
66-
it.distance
67-
}.thenBy {
68-
it.instantBreak
6974
}.thenByDescending {
70-
if (it.cachedState.block is FallingBlock) it.blockPos.y else 0
71-
}.thenBy {
7275
it.hotbarIndex == HotbarManager.serverSlot
73-
}.compare(this, other)
76+
}.thenBy {
77+
it.instantBreak
78+
}.compare(this@BreakContext, other)
7479

7580
else -> 1
7681
}
82+
} ?: 0
83+
84+
private fun Vec3d.distance(vec: Vec3d, block: Block): Double {
85+
val d = vec.x - x
86+
val e = (vec.y - y).let {
87+
if (block is FallingBlock) it - (interactionConfig.attackReach / 2)
88+
else it
89+
}
90+
val f = vec.z - z
91+
return sqrt(d * d + e * e + f * f)
7792
}
7893

7994
override fun ShapeBuilder.buildRenderer() {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@
1717

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

20+
import com.lambda.context.Automated
2021
import com.lambda.interaction.construction.result.Drawable
2122
import com.lambda.interaction.request.rotating.RotationRequest
2223
import com.lambda.threading.runSafe
2324
import net.minecraft.block.BlockState
2425
import net.minecraft.util.hit.BlockHitResult
2526
import net.minecraft.util.math.BlockPos
2627

27-
abstract class BuildContext : Comparable<BuildContext>, Drawable {
28+
abstract class BuildContext : Comparable<BuildContext>, Drawable, Automated {
2829
abstract val result: BlockHitResult
2930
abstract val rotation: RotationRequest
3031
abstract val hotbarIndex: Int

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

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

20+
import com.lambda.context.Automated
2021
import com.lambda.context.interactConfig
2122
import com.lambda.graphics.renderer.esp.DirectionMask.mask
2223
import com.lambda.graphics.renderer.esp.ShapeBuilder
@@ -40,7 +41,8 @@ class InteractionContext(
4041
override var hotbarIndex: Int,
4142
override var cachedState: BlockState,
4243
override val expectedState: BlockState,
43-
) : BuildContext(), LogContext {
44+
val automated: Automated
45+
) : BuildContext(), LogContext, Automated by automated {
4446
private val baseColor = Color(35, 254, 79, 25)
4547
private val sideColor = Color(35, 254, 79, 100)
4648

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,8 @@ object BuildSimulator {
278278
RotationRequest(rotationTarget, this),
279279
player.inventory.selectedSlot,
280280
state,
281-
expectedState
281+
expectedState,
282+
this
282283
)
283284

284285
val stackSelection = (item ?: player.mainHandStack.item).select()
@@ -748,7 +749,8 @@ object BuildSimulator {
748749
StackSelection.EVERYTHING.select(),
749750
instantBreakable(state, pos, breakConfig.breakThreshold),
750751
state,
751-
breakConfig.sorter
752+
breakConfig.sorter,
753+
this
752754
)
753755
acc.add(BreakResult.Break(pos, breakContext))
754756
return acc
@@ -805,7 +807,8 @@ object BuildSimulator {
805807
StackSelection.EVERYTHING.select(),
806808
instant,
807809
state,
808-
breakConfig.sorter
810+
breakConfig.sorter,
811+
this
809812
)
810813

811814
if (gamemode.isCreative) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ object BreakManager : RequestHandler<BreakRequest>(
146146
private var activeRequest: BreakRequest? = null
147147

148148
private var hotbarRequest: HotbarRequest? = null
149-
val swapped get() = hotbarRequest?.done != false
149+
private val swapped get() = hotbarRequest?.done != false
150150

151151
private var rotationRequest: RotationRequest? = null
152152
private val rotated get() = rotationRequest?.done != false

src/main/kotlin/com/lambda/module/modules/player/FastBreak.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ object FastBreak : Module(
6565
player.mainHandStack.select(),
6666
state.calcBlockBreakingDelta(player, world, pos) >= buildConfig.breakConfig.breakThreshold,
6767
state,
68-
buildConfig.breakConfig.sorter
68+
buildConfig.breakConfig.sorter,
69+
this@FastBreak
6970
)
7071

7172
BreakRequest(setOf(breakContext), pendingInteractions, this@FastBreak).submit()

0 commit comments

Comments
 (0)