Skip to content

Commit 9e0ba5a

Browse files
committed
Refactor placement handling in BuildTask implementation
Lifted placement-related logic into the BuildTask to simplify task nesting and enhance data flow. This change improves maintainability and ensures more accessible context sharing across the build process.
1 parent d0a1902 commit 9e0ba5a

File tree

23 files changed

+297
-201
lines changed

23 files changed

+297
-201
lines changed

common/src/main/kotlin/com/lambda/command/commands/BuildCommand.kt

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -46,38 +46,34 @@ object BuildCommand : LambdaCommand(
4646
required(greedyString("structure")) { structure ->
4747
suggests { _, builder ->
4848
StructureRegistry.forEach { key, _ -> builder.suggest(key) }
49-
5049
builder.buildFuture()
5150
}
52-
optional(boolean("pathing")) { pathing ->
53-
executeWithResult {
54-
val pathString = structure().value()
55-
val doPathing = if (pathing != null) pathing().value() else false
56-
runSafe<Unit> {
57-
try {
58-
StructureRegistry
59-
.loadStructureByRelativePath(Path.of(pathString))
60-
?.let { template ->
61-
info("Building structure $pathString with dimensions ${template.size.toShortString()} created by ${template.author}")
62-
template.toStructure()
63-
.move(player.blockPos)
64-
.toBlueprint()
65-
.build(pathing = doPathing)
66-
.run()
51+
executeWithResult {
52+
val pathString = structure().value()
53+
runSafe<Unit> {
54+
try {
55+
StructureRegistry
56+
.loadStructureByRelativePath(Path.of(pathString))
57+
?.let { template ->
58+
info("Building structure $pathString with dimensions ${template.size.toShortString()} created by ${template.author}")
59+
template.toStructure()
60+
.move(player.blockPos)
61+
.toBlueprint()
62+
.build()
63+
.run()
6764

68-
return@executeWithResult CommandResult.success()
69-
}
70-
} catch (e: InvalidPathException) {
71-
return@executeWithResult CommandResult.failure("Invalid path $pathString")
72-
} catch (e: NoSuchFileException) {
73-
return@executeWithResult CommandResult.failure("Structure $pathString not found")
74-
} catch (e: Exception) {
75-
return@executeWithResult CommandResult.failure(e.message ?: "Failed to load structure $pathString")
76-
}
65+
return@executeWithResult CommandResult.success()
66+
}
67+
} catch (e: InvalidPathException) {
68+
return@executeWithResult CommandResult.failure("Invalid path $pathString")
69+
} catch (e: NoSuchFileException) {
70+
return@executeWithResult CommandResult.failure("Structure $pathString not found")
71+
} catch (e: Exception) {
72+
return@executeWithResult CommandResult.failure(e.message ?: "Failed to load structure $pathString")
7773
}
78-
79-
CommandResult.failure("Structure $pathString not found")
8074
}
75+
76+
CommandResult.failure("Structure $pathString not found")
8177
}
8278
}
8379
}

common/src/main/kotlin/com/lambda/config/groups/BuildConfig.kt

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,23 @@
1818
package com.lambda.config.groups
1919

2020
interface BuildConfig {
21-
val breakConfirmation: Boolean
22-
val placeConfirmation: Boolean
23-
val collectDrops: Boolean
24-
val breakWeakBlocks: Boolean
21+
// General
2522
val pathing: Boolean
26-
val breaksPerTick: Int
23+
val stayInRange: Boolean
24+
val collectDrops: Boolean
25+
val forceSilkTouch: Boolean
26+
27+
// Breaking
2728
val rotateForBreak: Boolean
29+
val breakConfirmation: Boolean
30+
val maxPendingBreaks: Int
31+
val breaksPerTick: Int
32+
val breakWeakBlocks: Boolean
33+
34+
// Placing
2835
val rotateForPlace: Boolean
36+
val placeConfirmation: Boolean
37+
val placeTimeout: Int
38+
val maxPendingPlacements: Int
39+
val placementsPerTick: Int
2940
}

common/src/main/kotlin/com/lambda/config/groups/BuildSettings.kt

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,23 @@ class BuildSettings(
2929

3030
private val page by c.setting("Build Page", Page.GENERAL, "Current page", vis)
3131

32+
// General
3233
override val pathing by c.setting("Pathing", true, "Path to blocks") { vis() && page == Page.GENERAL }
34+
override val stayInRange by c.setting("Stay In Range", true, "Stay in range of blocks") { vis() && page == Page.GENERAL && pathing }
35+
override val collectDrops by c.setting("Collect All Drops", false, "Collect all drops when breaking blocks") { vis() && page == Page.GENERAL }
3336

37+
// Breaking
38+
override val rotateForBreak by c.setting("Rotate For Break", true, "Rotate towards block while breaking") { vis() && page == Page.BREAK }
3439
override val breakConfirmation by c.setting("Break Confirmation", false, "Wait for block break confirmation") { vis() && page == Page.BREAK }
40+
override val maxPendingBreaks by c.setting("Max Pending Breaks", 1, 1..10, 1, "Maximum pending block breaks") { vis() && page == Page.BREAK }
41+
override val breaksPerTick by c.setting("Instant Breaks Per Tick", 5, 1..30, 1, "Maximum instant block breaks per tick") { vis() && page == Page.BREAK }
3542
override val breakWeakBlocks by c.setting("Break Weak Blocks", false, "Break blocks that dont have structural integrity (e.g: grass)") { vis() && page == Page.BREAK }
36-
override val breaksPerTick by c.setting("Instant Breaks Per Tick", 10, 1..30, 1, "Maximum instant block breaks per tick") { vis() && page == Page.BREAK }
37-
override val rotateForBreak by c.setting("Rotate For Break", true, "Rotate towards block while breaking") { vis() && page == Page.BREAK }
38-
39-
override val collectDrops by c.setting("Collect All Drops", false, "Collect all drops when breaking blocks") { vis() && page == Page.BREAK }
40-
41-
override val placeConfirmation by c.setting("Place Confirmation", true, "Wait for block placement confirmation") { vis() && page == Page.PLACE }
43+
override val forceSilkTouch by c.setting("Force Silk Touch", false, "Force silk touch when breaking blocks") { vis() && page == Page.BREAK }
4244

45+
// Placing
4346
override val rotateForPlace by c.setting("Rotate For Place", true, "Rotate towards block while placing") { vis() && page == Page.PLACE }
47+
override val placeConfirmation by c.setting("Place Confirmation", true, "Wait for block placement confirmation") { vis() && page == Page.PLACE }
48+
override val placeTimeout by c.setting("Place Timeout", 10, 1..30, 1, "Timeout for block placement in ticks", unit = " ticks") { vis() && page == Page.PLACE && placeConfirmation }
49+
override val maxPendingPlacements by c.setting("Max Pending Places", 1, 1..10, 1, "Maximum pending block places") { vis() && page == Page.PLACE }
50+
override val placementsPerTick by c.setting("Instant Places Per Tick", 1, 1..30, 1, "Maximum instant block places per tick") { vis() && page == Page.PLACE }
4451
}

common/src/main/kotlin/com/lambda/config/groups/IRotationConfig.kt renamed to common/src/main/kotlin/com/lambda/config/groups/RotationConfig.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package com.lambda.config.groups
1919

2020
import com.lambda.interaction.rotation.RotationMode
2121

22-
interface IRotationConfig {
22+
interface RotationConfig {
2323
/**
2424
* - [RotationMode.SILENT] Spoofing server-side rotation.
2525
* - [RotationMode.SYNC] Spoofing server-side rotation and adjusting client-side movement based on reported rotation (for Grim).
@@ -42,7 +42,7 @@ interface IRotationConfig {
4242
*/
4343
val resetTicks: Int
4444

45-
interface Instant : IRotationConfig {
45+
interface Instant : RotationConfig {
4646
override val turnSpeed get() = 360.0
4747
override val keepTicks get() = 1
4848
override val resetTicks get() = 1

common/src/main/kotlin/com/lambda/config/groups/RotationSettings.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import kotlin.random.Random
2525
class RotationSettings(
2626
c: Configurable,
2727
vis: () -> Boolean = { true },
28-
) : IRotationConfig {
28+
) : RotationConfig {
2929
/**
3030
* The rotation mode
3131
*/

common/src/main/kotlin/com/lambda/interaction/RotationManager.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ object RotationManager : Loadable {
6060
) {
6161
var lastCtx: RotationContext? = null
6262

63-
this.listen<RotationEvent.Update>(priority, alwaysListen) { event ->
63+
listen<RotationEvent.Update>(priority, alwaysListen) { event ->
6464
val rotationContext = onUpdate(event.context)
6565

6666
rotationContext?.let {
@@ -70,7 +70,7 @@ object RotationManager : Loadable {
7070
lastCtx = rotationContext
7171
}
7272

73-
this.listen<RotationEvent.Post> { event ->
73+
listen<RotationEvent.Post> { event ->
7474
if (event.context == lastCtx && event.context.isValid) {
7575
onReceive()
7676
}

common/src/main/kotlin/com/lambda/interaction/construction/blueprint/DynamicBlueprint.kt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,24 @@
1818
package com.lambda.interaction.construction.blueprint
1919

2020
import com.lambda.context.SafeContext
21+
import com.lambda.threading.runSafe
2122
import com.lambda.util.extension.Structure
2223
import net.minecraft.util.math.Vec3i
2324

2425
data class DynamicBlueprint(
2526
val init: SafeContext.(Structure) -> Structure = { emptyMap() },
2627
val update: SafeContext.(Structure) -> Structure = { it },
2728
) : Blueprint() {
28-
fun update(ctx: SafeContext) {
29-
structure = ctx.update(structure)
29+
fun update() {
30+
runSafe {
31+
structure = update(structure)
32+
}
3033
}
3134

32-
fun create(ctx: SafeContext) {
33-
structure = ctx.init(structure)
35+
fun create() {
36+
runSafe {
37+
structure = init(structure)
38+
}
3439
}
3540

3641
override var structure: Structure = emptyMap()

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,8 @@ data class BreakContext(
5858
else -> 1
5959
}
6060
}
61+
62+
override fun SafeContext.buildRenderer() {
63+
64+
}
6165
}

common/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.interaction.construction.result.Drawable
2021
import com.lambda.interaction.rotation.RotationContext
2122
import net.minecraft.block.BlockState
2223
import net.minecraft.util.Hand
2324
import net.minecraft.util.hit.BlockHitResult
2425
import net.minecraft.util.math.BlockPos
2526
import net.minecraft.util.math.Vec3d
2627

27-
interface BuildContext : Comparable<BuildContext> {
28+
interface BuildContext : Comparable<BuildContext>, Drawable {
2829
val pov: Vec3d
2930
val result: BlockHitResult
3031
val distance: Double

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

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,21 @@
1717

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

20+
import com.lambda.context.SafeContext
21+
import com.lambda.graphics.renderer.esp.DirectionMask
22+
import com.lambda.graphics.renderer.esp.DirectionMask.exclude
2023
import com.lambda.interaction.construction.verify.TargetState
2124
import com.lambda.interaction.rotation.RotationContext
25+
import com.lambda.threading.runSafe
2226
import com.lambda.util.BlockUtils
27+
import com.lambda.util.Communication.warn
2328
import net.minecraft.block.BlockState
2429
import net.minecraft.util.Hand
2530
import net.minecraft.util.hit.BlockHitResult
2631
import net.minecraft.util.math.BlockPos
2732
import net.minecraft.util.math.Direction
2833
import net.minecraft.util.math.Vec3d
34+
import java.awt.Color
2935

3036
data class PlaceContext(
3137
override val pov: Vec3d,
@@ -39,10 +45,35 @@ data class PlaceContext(
3945
val targetState: TargetState,
4046
val sneak: Boolean,
4147
val insideBlock: Boolean,
42-
val primeDirection: Direction?
48+
val primeDirection: Direction?,
4349
) : BuildContext {
44-
override fun compareTo(other: BuildContext): Int {
45-
return when (other) {
50+
var placeTick = 0L
51+
private val baseColor = Color(35, 188, 254, 25)
52+
private val sideColor = Color(35, 188, 254, 100)
53+
54+
fun place(swingHand: Boolean) {
55+
runSafe {
56+
val actionResult = interaction.interactBlock(
57+
player, hand, result
58+
)
59+
60+
if (actionResult.isAccepted) {
61+
if (actionResult.shouldSwingHand() && swingHand) {
62+
player.swingHand(hand)
63+
}
64+
65+
if (!player.getStackInHand(hand).isEmpty && interaction.hasCreativeInventory()) {
66+
mc.gameRenderer.firstPersonRenderer.resetEquipProgress(hand)
67+
}
68+
placeTick = mc.uptimeInTicks
69+
} else {
70+
warn("Internal interaction failed with $actionResult")
71+
}
72+
}
73+
}
74+
75+
override fun compareTo(other: BuildContext) =
76+
when (other) {
4677
is PlaceContext -> compareBy<PlaceContext> {
4778
BlockUtils.fluids.indexOf(it.checkedState.fluidState.fluid)
4879
}.thenByDescending {
@@ -59,5 +90,9 @@ data class PlaceContext(
5990

6091
else -> 1
6192
}
93+
94+
override fun SafeContext.buildRenderer() {
95+
withState(expectedState, expectedPos, baseColor, DirectionMask.ALL.exclude(result.side.opposite))
96+
withState(expectedState, expectedPos, sideColor, result.side.opposite)
6297
}
6398
}

0 commit comments

Comments
 (0)