Skip to content

Commit 5de8935

Browse files
committed
cleaner configs idea
1 parent 439e414 commit 5de8935

File tree

23 files changed

+310
-288
lines changed

23 files changed

+310
-288
lines changed

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import com.lambda.brigadier.argument.value
2525
import com.lambda.brigadier.executeWithResult
2626
import com.lambda.brigadier.required
2727
import com.lambda.command.LambdaCommand
28+
import com.lambda.context.DefaultConfigs
2829
import com.lambda.interaction.construction.StructureRegistry
2930
import com.lambda.interaction.construction.blueprint.Blueprint.Companion.toStructure
3031
import com.lambda.interaction.construction.blueprint.StaticBlueprint.Companion.toBlueprint
@@ -61,11 +62,14 @@ object BuildCommand : LambdaCommand(
6162
.loadStructureByRelativePath(Path.of(pathString))
6263
.let { template ->
6364
info("Building structure $pathString with dimensions ${template.size.toShortString()} created by ${template.author}")
64-
lastBuildTask = template.toStructure()
65-
.move(player.blockPos)
66-
.toBlueprint()
67-
.build()
68-
.run()
65+
DefaultConfigs.run {
66+
lastBuildTask = build(
67+
template
68+
.toStructure()
69+
.move(player.blockPos)
70+
.toBlueprint()
71+
).run()
72+
}
6973

7074
return@executeWithResult success()
7175
}

common/src/main/kotlin/com/lambda/context/AbstractContext.kt

Lines changed: 0 additions & 41 deletions
This file was deleted.

common/src/main/kotlin/com/lambda/context/ClientContext.kt

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,29 @@ import net.minecraft.client.world.ClientWorld
3535
*
3636
* @function toSafe Converts the `ClientContext` to a `SafeContext` if all properties are not `null`, or returns `null` otherwise.
3737
*/
38-
open class ClientContext : AbstractContext() {
39-
final override val world: ClientWorld? = mc.world
40-
final override val player: ClientPlayerEntity? = mc.player
41-
final override val interaction: ClientPlayerInteractionManager? = mc.interactionManager
42-
final override val connection: ClientPlayNetworkHandler? = mc.networkHandler
38+
open class ClientContext {
39+
val mc: MinecraftClient = MinecraftClient.getInstance()
40+
val clientWorld: ClientWorld? = mc.world
41+
val clientPlayer: ClientPlayerEntity? = mc.player
42+
val clientInteraction: ClientPlayerInteractionManager? = mc.interactionManager
43+
val clientConnection: ClientPlayNetworkHandler? = mc.networkHandler
4344

4445
fun toSafe(): SafeContext? {
45-
if (world == null || player == null || interaction == null || connection == null) {
46+
if (clientWorld == null || clientPlayer == null || clientInteraction == null || clientConnection == null) {
4647
return null
4748
}
48-
return SafeContext(world, player, interaction, connection)
49+
return object : SafeContext {
50+
override val mc = this@ClientContext.mc
51+
override val world: ClientWorld = clientWorld
52+
override val player: ClientPlayerEntity = clientPlayer
53+
override val interaction: ClientPlayerInteractionManager = clientInteraction
54+
override val connection: ClientPlayNetworkHandler = clientConnection
55+
}
56+
}
57+
58+
fun toSafeConfigured(configured: Configured): ConfiguredSafeContext? {
59+
return toSafe()?.let { safeContext ->
60+
ConfiguredSafeContext(safeContext, configured)
61+
}
4962
}
5063
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2025 Lambda
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package com.lambda.context
19+
20+
import com.lambda.config.groups.BuildConfig
21+
import com.lambda.config.groups.InteractionConfig
22+
import com.lambda.config.groups.InventoryConfig
23+
import com.lambda.interaction.request.hotbar.HotbarConfig
24+
import com.lambda.interaction.request.rotation.RotationConfig
25+
26+
interface Configured {
27+
val build: BuildConfig
28+
val interact: InteractionConfig
29+
val inventory: InventoryConfig
30+
val hotbar: HotbarConfig
31+
val rotation: RotationConfig
32+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2025 Lambda
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package com.lambda.context
19+
20+
class ConfiguredSafeContext(
21+
safeContext: SafeContext,
22+
configured: Configured
23+
) : SafeContext by safeContext, Configured by configured
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2025 Lambda
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package com.lambda.context
19+
20+
import com.lambda.module.modules.client.TaskFlowModule
21+
22+
object DefaultConfigs : Configured {
23+
override val build = TaskFlowModule.build
24+
override val interact = TaskFlowModule.interact
25+
override val inventory = TaskFlowModule.inventory
26+
override val hotbar = TaskFlowModule.hotbar
27+
override val rotation = TaskFlowModule.rotation
28+
}
29+
30+
val Configured.breaking get() = build.breaking
31+
val Configured.placing get() = build.placing

common/src/main/kotlin/com/lambda/context/SafeContext.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,10 @@ import net.minecraft.client.world.ClientWorld
4646
* @property interaction The interaction manager for the player.
4747
* @property connection The network handler for the player.
4848
**/
49-
open class SafeContext internal constructor(
50-
override val world: ClientWorld,
51-
override val player: ClientPlayerEntity,
52-
override val interaction: ClientPlayerInteractionManager,
53-
override val connection: ClientPlayNetworkHandler,
54-
) : AbstractContext()
49+
interface SafeContext {
50+
val mc: MinecraftClient
51+
val world: ClientWorld
52+
val player: ClientPlayerEntity
53+
val interaction: ClientPlayerInteractionManager
54+
val connection: ClientPlayNetworkHandler
55+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package com.lambda.interaction.construction.result
2020
import baritone.api.pathing.goals.GoalBlock
2121
import baritone.api.pathing.goals.GoalInverted
2222
import com.lambda.config.groups.InventoryConfig
23+
import com.lambda.context.Configured
2324
import com.lambda.context.SafeContext
2425
import com.lambda.interaction.construction.context.BreakContext
2526
import com.lambda.interaction.material.StackSelection.Companion.selectStack
@@ -96,7 +97,7 @@ sealed class BreakResult : BuildResult() {
9697

9798
override val pausesParent get() = true
9899

99-
override fun resolve() =
100+
override fun Configured.resolve() =
100101
selectStack {
101102
isItem(badItem).not()
102103
}.transfer(MainHandContainer, inventory)

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package com.lambda.interaction.construction.result
2020
import baritone.api.pathing.goals.GoalBlock
2121
import baritone.api.pathing.goals.GoalNear
2222
import com.lambda.config.groups.InventoryConfig
23+
import com.lambda.context.Configured
2324
import com.lambda.context.SafeContext
2425
import com.lambda.interaction.construction.context.BuildContext
2526
import com.lambda.interaction.material.StackSelection
@@ -205,7 +206,7 @@ abstract class BuildResult : ComparableResult<Rank>, Nameable {
205206

206207
override val pausesParent get() = true
207208

208-
override fun resolve() = neededSelection
209+
override fun Configured.resolve() = neededSelection
209210
.transfer(MainHandContainer, inventory) ?: MaterialContainer.FailureTask("Couldn't find $neededSelection anywhere.")
210211

211212
override fun SafeContext.buildRenderer() {
@@ -241,7 +242,7 @@ abstract class BuildResult : ComparableResult<Rank>, Nameable {
241242

242243
override val pausesParent get() = true
243244

244-
override fun resolve() =
245+
override fun Configured.resolve() =
245246
neededStack.select()
246247
.transfer(MainHandContainer, inventory) ?: MaterialContainer.FailureTask("Couldn't find ${neededStack.item.name.string} anywhere.")
247248

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package com.lambda.interaction.construction.result
1919

2020
import baritone.api.pathing.goals.GoalBlock
2121
import baritone.api.pathing.goals.GoalInverted
22+
import com.lambda.context.Configured
2223
import com.lambda.context.SafeContext
2324
import com.lambda.interaction.construction.context.PlaceContext
2425
import com.lambda.task.tasks.BuildTask.Companion.breakBlock
@@ -109,7 +110,7 @@ sealed class PlaceResult : BuildResult() {
109110
) : Resolvable, PlaceResult() {
110111
override val rank = Rank.PLACE_CANT_REPLACE
111112

112-
override fun resolve() = breakBlock(blockPos)
113+
override fun Configured.resolve() = breakBlock(blockPos)
113114
}
114115

115116
/**

0 commit comments

Comments
 (0)