Skip to content

Commit 780148a

Browse files
authored
Config passing refactor (#157)
1 parent 87d13ab commit 780148a

File tree

89 files changed

+756
-821
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+756
-821
lines changed

build.gradle.kts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1616
*/
1717

18-
import org.apache.tools.ant.taskdefs.condition.Os
19-
import org.gradle.internal.jvm.Jvm
2018
import java.util.*
2119

2220
val modId: String by project
@@ -214,6 +212,10 @@ tasks {
214212
}
215213

216214
kotlin {
215+
compilerOptions {
216+
freeCompilerArgs.add("-Xcontext-parameters")
217+
}
218+
217219
jvmToolchain(21)
218220
}
219221

src/main/java/com/lambda/mixin/entity/EntityMixin.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,15 @@ private boolean modifyGetFlagGlowing(boolean original) {
150150
private boolean wrapSetYaw(Entity instance, float yaw) {
151151
return (instance != Lambda.getMc().player ||
152152
RotationLock.INSTANCE.isDisabled() ||
153-
RotationLock.getRotationSettings().getRotationMode() != RotationMode.Lock ||
153+
RotationLock.INSTANCE.getRotationConfig().getRotationMode() != RotationMode.Lock ||
154154
RotationLock.getYawMode() == RotationLock.RotationMode.None);
155155
}
156156

157157
@WrapWithCondition(method = "changeLookDirection", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/Entity;setPitch(F)V"))
158158
private boolean wrapSetPitch(Entity instance, float yaw) {
159159
return (instance != Lambda.getMc().player ||
160160
RotationLock.INSTANCE.isDisabled() ||
161-
RotationLock.getRotationSettings().getRotationMode() != RotationMode.Lock ||
161+
RotationLock.INSTANCE.getRotationConfig().getRotationMode() != RotationMode.Lock ||
162162
RotationLock.getPitchMode() == RotationLock.RotationMode.None);
163163
}
164164
}

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

Lines changed: 8 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.AutomationConfig
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,13 @@ 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+
lastBuildTask = with(AutomationConfig) {
66+
template.toStructure()
67+
.move(player.blockPos)
68+
.toBlueprint()
69+
.build()
70+
.run()
71+
}
6972

7073
return@executeWithResult success()
7174
}

src/main/kotlin/com/lambda/command/commands/TransferCommand.kt

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import com.lambda.brigadier.argument.value
2727
import com.lambda.brigadier.executeWithResult
2828
import com.lambda.brigadier.required
2929
import com.lambda.command.LambdaCommand
30+
import com.lambda.context.AutomationConfig
3031
import com.lambda.interaction.material.StackSelection.Companion.selectStack
3132
import com.lambda.interaction.material.container.ContainerManager
3233
import com.lambda.interaction.material.container.ContainerManager.containerWithMaterial
@@ -52,8 +53,10 @@ object TransferCommand : LambdaCommand(
5253
val selection = selectStack(count) {
5354
isItem(stack(ctx).value().item)
5455
}
55-
selection.containerWithMaterial().forEachIndexed { i, container ->
56-
builder.suggest("\"${i + 1}. ${container.name}\"", container.description(selection))
56+
with(AutomationConfig) {
57+
selection.containerWithMaterial().forEachIndexed { i, container ->
58+
builder.suggest("\"${i + 1}. ${container.name}\"", container.description(selection))
59+
}
5760
}
5861
builder.buildFuture()
5962
}
@@ -62,8 +65,10 @@ object TransferCommand : LambdaCommand(
6265
val selection = selectStack(amount(ctx).value()) {
6366
isItem(stack(ctx).value().item)
6467
}
65-
containerWithSpace(selection).forEachIndexed { i, container ->
66-
builder.suggest("\"${i + 1}. ${container.name}\"", container.description(selection))
68+
with(AutomationConfig) {
69+
containerWithSpace(selection).forEachIndexed { i, container ->
70+
builder.suggest("\"${i + 1}. ${container.name}\"", container.description(selection))
71+
}
6772
}
6873
builder.buildFuture()
6974
}
@@ -79,22 +84,24 @@ object TransferCommand : LambdaCommand(
7984
it.name == to().value().split(".").last().trim()
8085
} ?: return@executeWithResult failure("To container not found")
8186

82-
when (val transaction = fromContainer.transfer(selection, toContainer)) {
83-
is TransferResult.ContainerTransfer -> {
84-
info("${transaction.name} started.")
85-
lastContainerTransfer = transaction
86-
transaction.finally {
87-
info("${transaction.name} completed.")
88-
}.run()
89-
return@executeWithResult success()
90-
}
87+
with(AutomationConfig) {
88+
when (val transaction = fromContainer.transfer(selection, toContainer)) {
89+
is TransferResult.ContainerTransfer -> {
90+
info("${transaction.name} started.")
91+
lastContainerTransfer = transaction
92+
transaction.finally {
93+
info("${transaction.name} completed.")
94+
}.run()
95+
return@executeWithResult success()
96+
}
9197

92-
is TransferResult.MissingItems -> {
93-
return@executeWithResult failure("Missing items: ${transaction.missing}")
94-
}
98+
is TransferResult.MissingItems -> {
99+
return@executeWithResult failure("Missing items: ${transaction.missing}")
100+
}
95101

96-
is TransferResult.NoSpace -> {
97-
return@executeWithResult failure("No space in ${toContainer.name}")
102+
is TransferResult.NoSpace -> {
103+
return@executeWithResult failure("No space in ${toContainer.name}")
104+
}
98105
}
99106
}
100107

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ interface BuildConfig {
3030
val interactionTimeout: Int
3131

3232
// Breaking
33-
val breaking: BreakSettings
33+
val breakConfig: BreakSettings
3434

3535
// Placing
36-
val placing: PlaceSettings
36+
val placeConfig: PlaceSettings
3737

3838
// Interacting
39-
val interacting: InteractSettings
39+
val interactConfig: InteractSettings
4040

4141
enum class SwingType(
4242
override val displayName: String,

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,17 @@ class BuildSettings(
4242
override val maxPendingInteractions by c.setting("Max Pending Interactions", 15, 1..30, 1, "The maximum count of pending interactions to allow before pausing future interactions", visibility = vis).group(*groupPath, Group.General)
4343

4444
// Breaking
45-
override val breaking = BreakSettings(c, groupPath.toList() + Group.Break, vis)
45+
override val breakConfig = BreakSettings(c, groupPath.toList() + Group.Break, vis)
4646

4747
// Placing
48-
override val placing = PlaceSettings(c, groupPath.toList() + Group.Place, vis)
48+
override val placeConfig = PlaceSettings(c, groupPath.toList() + Group.Place, vis)
4949

5050
//Interacting
51-
override val interacting = InteractSettings(c, groupPath.toList() + Group.Interact, vis)
51+
override val interactConfig = InteractSettings(c, groupPath.toList() + Group.Interact, vis)
5252

5353
override val interactionTimeout by c.setting("Interaction Timeout", 10, 1..30, 1, "Timeout for block breaks in ticks", unit = " ticks") {
54-
vis() && (placing.placeConfirmationMode != PlaceConfig.PlaceConfirmationMode.None
55-
|| breaking.breakConfirmation != BreakConfirmationMode.None
56-
|| interacting.interactConfirmationMode != InteractionConfig.InteractConfirmationMode.None)
54+
vis() && (placeConfig.placeConfirmationMode != PlaceConfig.PlaceConfirmationMode.None
55+
|| breakConfig.breakConfirmation != BreakConfirmationMode.None
56+
|| interactConfig.interactConfirmationMode != InteractionConfig.InteractConfirmationMode.None)
5757
}.group(*groupPath, Group.Break, BreakSettings.Group.General).group(*groupPath, Group.Place).group(*groupPath, Group.Interact)
5858
}

src/main/kotlin/com/lambda/config/groups/EatConfig.kt

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717

1818
package com.lambda.config.groups
1919

20-
import com.lambda.context.SafeContext
20+
import com.lambda.context.Automated
21+
import com.lambda.context.AutomatedSafeContext
2122
import com.lambda.interaction.material.StackSelection.Companion.selectStack
2223
import com.lambda.threading.runSafe
2324
import com.lambda.util.Describable
@@ -77,10 +78,11 @@ interface EatConfig {
7778

7879
fun shouldEat() = this != None
7980

80-
fun shouldKeepEating(config: EatConfig, stack: ItemStack?) = runSafe {
81+
context(c: Automated)
82+
fun shouldKeepEating(stack: ItemStack?) = runSafe {
8183
if (stack == null || stack.isEmpty) return@runSafe false
8284
when(this@Reason) {
83-
Hunger -> when(config.saturated) {
85+
Hunger -> when(c.eatConfig.saturated) {
8486
Saturation.EatSmart -> stack.item.nutrition + player.hungerManager.foodLevel <= 20
8587
Saturation.EatUntilFull -> player.hungerManager.isNotFull
8688
}
@@ -90,21 +92,22 @@ interface EatConfig {
9092
}
9193
} ?: false
9294

93-
fun selector(config: EatConfig) = selectStack(sorter = config.selectionPriority.comparator) {
95+
context(c: Automated)
96+
fun selector() = selectStack(sorter = c.eatConfig.selectionPriority.comparator) {
9497
when(this@Reason) {
9598
None -> any()
96-
Hunger -> isOneOfItems(config.nutritiousFood)
97-
Damage -> isOneOfItems(config.regenerationFood)
98-
Fire -> isOneOfItems(config.resistanceFood)
99-
} and if (config.ignoreBadFood) isNoneOfItems(config.badFood) else any()
99+
Hunger -> isOneOfItems(c.eatConfig.nutritiousFood)
100+
Damage -> isOneOfItems(c.eatConfig.regenerationFood)
101+
Fire -> isOneOfItems(c.eatConfig.resistanceFood)
102+
} and if (c.eatConfig.ignoreBadFood) isNoneOfItems(c.eatConfig.badFood) else any()
100103
}
101104
}
102105

103106
companion object {
104-
fun SafeContext.reasonEating(config: EatConfig) = when {
105-
config.eatOnHunger && player.hungerManager.foodLevel <= config.minFoodLevel -> Reason.Hunger
106-
config.eatOnDamage && player.health <= config.minDamage && !player.hasStatusEffect(StatusEffects.REGENERATION) -> Reason.Damage
107-
config.eatOnFire && player.isOnFire && !player.hasStatusEffect(StatusEffects.FIRE_RESISTANCE) -> Reason.Fire
107+
fun AutomatedSafeContext.reasonEating() = when {
108+
eatConfig.eatOnHunger && player.hungerManager.foodLevel <= eatConfig.minFoodLevel -> Reason.Hunger
109+
eatConfig.eatOnDamage && player.health <= eatConfig.minDamage && !player.hasStatusEffect(StatusEffects.REGENERATION) -> Reason.Damage
110+
eatConfig.eatOnFire && player.isOnFire && !player.hasStatusEffect(StatusEffects.FIRE_RESISTANCE) -> Reason.Fire
108111
else -> Reason.None
109112
}
110113
}

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

Lines changed: 0 additions & 41 deletions
This file was deleted.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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.EatConfig
22+
import com.lambda.config.groups.InteractionConfig
23+
import com.lambda.interaction.request.hotbar.HotbarConfig
24+
import com.lambda.interaction.request.inventory.InventoryConfig
25+
import com.lambda.interaction.request.rotating.RotationConfig
26+
27+
interface Automated {
28+
val buildConfig: BuildConfig
29+
val rotationConfig: RotationConfig
30+
val interactionConfig: InteractionConfig
31+
val inventoryConfig: InventoryConfig
32+
val hotbarConfig: HotbarConfig
33+
val eatConfig: EatConfig
34+
}
35+
36+
val Automated.breakConfig get() = buildConfig.breakConfig
37+
val Automated.placeConfig get() = buildConfig.placeConfig
38+
val Automated.interactConfig get() = buildConfig.interactConfig
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 AutomatedSafeContext(
21+
safeContext: SafeContext,
22+
automated: Automated
23+
) : SafeContext by safeContext, Automated by automated

0 commit comments

Comments
 (0)