Skip to content

Commit 04cf8ca

Browse files
committed
clean up setting groups in modules
1 parent f0c668a commit 04cf8ca

File tree

9 files changed

+256
-72
lines changed

9 files changed

+256
-72
lines changed

src/main/kotlin/com/lambda/config/AbstractSetting.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ import kotlin.reflect.KProperty
9696
*/
9797
abstract class AbstractSetting<T : Any>(
9898
override var name: String,
99-
internal val defaultValue: T,
99+
internal var defaultValue: T,
100100
val type: Type,
101101
override var description: String,
102102
var visibility: () -> Boolean,

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class BreakSettings(
4141

4242
// General
4343
override val breakMode by c.setting("Break Mode", BreakMode.Packet, visibility = vis).group(baseGroup, Group.General)
44-
override val sorter by c.setting("Sorter", SortMode.Closest, "The order in which breaks are performed", visibility = vis).group(baseGroup, Group.General)
44+
override val sorter by c.setting("Sorter", SortMode.Tool, "The order in which breaks are performed", visibility = vis).group(baseGroup, Group.General)
4545
override val rebreak by c.setting("Rebreak", true, "Re-breaks blocks after they've been broken once", visibility = vis).group(baseGroup, Group.General)
4646

4747
// Double break
@@ -80,16 +80,16 @@ class BreakSettings(
8080
override val ignoredBlocks by c.setting("Ignored Blocks", allSigns, description = "Blocks that wont be broken", visibility = vis).group(baseGroup, Group.General)
8181

8282
// Tool
83-
override val suitableToolsOnly by c.setting("Suitable Tools Only", true, "Only use tools suitable for the given block (will get the item drop)", visibility = vis).group(baseGroup, Group.General)
84-
override val forceSilkTouch by c.setting("Force Silk Touch", false, "Force silk touch when breaking blocks", visibility = vis).group(baseGroup, Group.General)
85-
override val forceFortunePickaxe by c.setting("Force Fortune Pickaxe", false, "Force fortune pickaxe when breaking blocks", visibility = vis).group(baseGroup, Group.General)
86-
override val minFortuneLevel by c.setting("Min Fortune Level", 1, 1..3, 1, "The minimum fortune level to use") { vis() && forceFortunePickaxe }.group(baseGroup, Group.General)
87-
override val useWoodenTools by c.setting("Use Wooden Tools", true, "Use wooden tools when breaking blocks", visibility = vis).group(baseGroup, Group.General)
88-
override val useStoneTools by c.setting("Use Stone Tools", true, "Use stone tools when breaking blocks", visibility = vis).group(baseGroup, Group.General)
89-
override val useIronTools by c.setting("Use Iron Tools", true, "Use iron tools when breaking blocks", visibility = vis).group(baseGroup, Group.General)
90-
override val useDiamondTools by c.setting("Use Diamond Tools", true, "Use diamond tools when breaking blocks", visibility = vis).group(baseGroup, Group.General)
91-
override val useGoldTools by c.setting("Use Gold Tools", true, "Use gold tools when breaking blocks", visibility = vis).group(baseGroup, Group.General)
92-
override val useNetheriteTools by c.setting("Use Netherite Tools", true, "Use netherite tools when breaking blocks", visibility = vis).group(baseGroup, Group.General)
83+
override val suitableToolsOnly by c.setting("Suitable Tools Only", true, "Only use tools suitable for the given block (will get the item drop)") { vis() && swapMode.isEnabled() }.group(baseGroup, Group.General)
84+
override val forceSilkTouch by c.setting("Force Silk Touch", false, "Force silk touch when breaking blocks") { vis() && swapMode.isEnabled() }.group(baseGroup, Group.General)
85+
override val forceFortunePickaxe by c.setting("Force Fortune Pickaxe", false, "Force fortune pickaxe when breaking blocks") { vis() && swapMode.isEnabled() }.group(baseGroup, Group.General)
86+
override val minFortuneLevel by c.setting("Min Fortune Level", 1, 1..3, 1, "The minimum fortune level to use") { vis() && swapMode.isEnabled() && forceFortunePickaxe }.group(baseGroup, Group.General)
87+
override val useWoodenTools by c.setting("Use Wooden Tools", true, "Use wooden tools when breaking blocks") { vis() && swapMode.isEnabled() }.group(baseGroup, Group.General)
88+
override val useStoneTools by c.setting("Use Stone Tools", true, "Use stone tools when breaking blocks") { vis() && swapMode.isEnabled() }.group(baseGroup, Group.General)
89+
override val useIronTools by c.setting("Use Iron Tools", true, "Use iron tools when breaking blocks") { vis() && swapMode.isEnabled() }.group(baseGroup, Group.General)
90+
override val useDiamondTools by c.setting("Use Diamond Tools", true, "Use diamond tools when breaking blocks") { vis() && swapMode.isEnabled() }.group(baseGroup, Group.General)
91+
override val useGoldTools by c.setting("Use Gold Tools", true, "Use gold tools when breaking blocks") { vis() && swapMode.isEnabled() }.group(baseGroup, Group.General)
92+
override val useNetheriteTools by c.setting("Use Netherite Tools", true, "Use netherite tools when breaking blocks") { vis() && swapMode.isEnabled() }.group(baseGroup, Group.General)
9393

9494
// Cosmetics
9595
override val sounds by c.setting("Break Sounds", true, "Plays the breaking sounds", visibility = vis).group(baseGroup, Group.Cosmetic)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class BuildSettings(
3939
override val collectDrops by c.setting("Collect All Drops", false, "Collect all drops when breaking blocks", vis).group(*baseGroup, Group.General)
4040
override val interactionsPerTick by c.setting("Interactions Per Tick", 5, 1..30, 1, "The amount of interactions that can happen per tick", visibility = vis).group(*baseGroup, Group.General)
4141
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(*baseGroup, Group.General)
42-
override val interactionTimeout by c.setting("Interaction Timeout", 10, 1..30, 1, "Timeout for block breaks in ticks", unit = " ticks", visibility = vis).group(Group.General)
42+
override val interactionTimeout by c.setting("Interaction Timeout", 10, 1..30, 1, "Timeout for block breaks in ticks", unit = " ticks", visibility = vis).group(*baseGroup, Group.General)
4343

4444
override val useDefaultReach by c.setting("Default Reach", true, "Whether to use vanilla interaction ranges", vis).group(*baseGroup, Group.Reach)
4545
override val attackReach by c.setting("Attack Reach", DEFAULT_ATTACK_REACH, 1.0..10.0, 0.01, "Maximum entity interaction distance") { vis() && !useDefaultReach }.group(*baseGroup, Group.Reach)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class HotbarSettings(
2727
baseGroup: NamedEnum,
2828
vis: () -> Boolean = { true }
2929
) : HotbarConfig, SettingGroup(c, c.settings.size) {
30-
override val keepTicks by c.setting("Keep Ticks", 3, 0..20, 1, "The number of ticks to keep the current hotbar selection active", " ticks", visibility = vis).group(baseGroup)
30+
override val keepTicks by c.setting("Keep Ticks", 1, 0..20, 1, "The number of ticks to keep the current hotbar selection active", " ticks", visibility = vis).group(baseGroup)
3131
override val swapDelay by c.setting("Swap Delay", 0, 0..3, 1, "The number of ticks delay before allowing another hotbar selection swap", " ticks", visibility = vis).group(baseGroup)
3232
override val swapsPerTick by c.setting("Swaps Per Tick", 3, 1..10, 1, "The number of hotbar selection swaps that can take place each tick") { swapDelay <= 0 && vis() }.group(baseGroup)
3333
override val swapPause by c.setting("Swap Pause", 0, 0..20, 1, "The delay in ticks to pause actions after switching to the slot", " ticks", visibility = vis).group(baseGroup)

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

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ import com.lambda.util.NamedEnum
2323
import kotlin.reflect.KProperty0
2424
import kotlin.reflect.jvm.isAccessible
2525

26+
private val KProperty0<*>.delegate
27+
get() = try {
28+
apply { isAccessible = true }.getDelegate()
29+
} catch (e: Exception) {
30+
throw IllegalStateException("Could not access delegate for property $name", e)
31+
}
32+
2633
@Suppress("unchecked_cast")
2734
abstract class SettingGroup(val c: Configurable, val startIndex: Int) {
28-
private val KProperty0<*>.delegate
29-
get() = try {
30-
apply { isAccessible = true }.getDelegate()
31-
} catch (e: Exception) {
32-
throw IllegalStateException("Could not access delegate for property $name", e)
33-
}
34-
3535
@DslMarker
3636
annotation class SettingEditorDsl
3737

@@ -48,7 +48,7 @@ abstract class SettingGroup(val c: Configurable, val startIndex: Int) {
4848
}
4949

5050
@SettingEditorDsl
51-
internal inline fun <T : Any> editSettings(vararg settings: KProperty0<T>, edits: TypedEditBuilder<T>.() -> Unit) {
51+
internal inline fun <T : Any> editTypedSettings(vararg settings: KProperty0<T>, edits: TypedEditBuilder<T>.() -> Unit) {
5252
TypedEditBuilder((settings.map { it.delegate } as List<AbstractSetting<T>>), c)
5353
.apply(edits)
5454
}
@@ -85,7 +85,10 @@ abstract class SettingGroup(val c: Configurable, val startIndex: Int) {
8585
) : BasicEditBuilder(settings) {
8686
@SettingEditorDsl
8787
fun defaultValue(value: T) {
88-
settings.forEach { it.value = value }
88+
settings.forEach {
89+
it.defaultValue = value
90+
it.value = value
91+
}
8992
}
9093
}
9194

@@ -104,20 +107,26 @@ abstract class SettingGroup(val c: Configurable, val startIndex: Int) {
104107
}
105108

106109
@SettingEditorDsl
107-
fun insertSetting(insert: AbstractSetting<*>, at: AbstractSetting<*>, insertMode: InsertMode) {
108-
val index = c.settings.indexOf(at)
109-
c.settings.add(if (insertMode == InsertMode.Below) index + 1 else index, insert)
110+
fun insert(insert: KProperty0<*>, insertMode: InsertMode) {
111+
val index = c.settings.indexOf(setting)
112+
val delegate = insert.delegate as AbstractSetting<*>
113+
c.settings.remove(delegate)
114+
c.settings.add(if (insertMode == InsertMode.Above) index - 1 else index, delegate)
110115
}
111116

112117
@SettingEditorDsl
113-
fun insertSettings(vararg inserts: AbstractSetting<*>, at: AbstractSetting<*>, insertMode: InsertMode) {
114-
val index = c.settings.indexOf(at)
115-
c.settings.addAll(if (insertMode == InsertMode.Below) index + 1 else index, inserts.toList())
118+
fun insert(vararg inserts: KProperty0<*>, insertMode: InsertMode) {
119+
val index = c.settings.indexOf(setting)
120+
inserts.forEach { c.settings.remove(it.delegate as AbstractSetting<*>) }
121+
c.settings.addAll(
122+
if (insertMode == InsertMode.Above) index - 1 else index,
123+
inserts.map { it.delegate } as List<AbstractSetting<*>>
124+
)
116125
}
126+
}
117127

118-
enum class InsertMode {
119-
Above,
120-
Below
121-
}
128+
enum class InsertMode {
129+
Above,
130+
Below
122131
}
123132
}

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

Lines changed: 118 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,37 @@
1717

1818
package com.lambda.module.modules.player
1919

20-
import com.lambda.config.groups.BuildSettings
20+
import com.lambda.config.groups.BreakSettings
21+
import com.lambda.config.groups.HotbarSettings
22+
import com.lambda.config.groups.InventorySettings
2123
import com.lambda.event.events.PlayerEvent
2224
import com.lambda.event.listener.SafeListener.Companion.listen
2325
import com.lambda.interaction.construction.context.BreakContext
2426
import com.lambda.interaction.construction.context.BuildContext
25-
import com.lambda.interaction.material.StackSelection.Companion.select
27+
import com.lambda.interaction.material.ContainerSelection.Companion.selectContainer
28+
import com.lambda.interaction.material.StackSelection.Companion.selectStack
29+
import com.lambda.interaction.material.container.ContainerManager.containerWithMaterial
30+
import com.lambda.interaction.material.container.MaterialContainer
2631
import com.lambda.interaction.request.breaking.BreakRequest
2732
import com.lambda.interaction.request.rotating.Rotation.Companion.rotation
2833
import com.lambda.interaction.request.rotating.RotationRequest
2934
import com.lambda.interaction.request.rotating.visibilty.lookAt
3035
import com.lambda.module.Module
3136
import com.lambda.module.tag.ModuleTag
3237
import com.lambda.util.BlockUtils.blockState
38+
import com.lambda.util.BlockUtils.calcItemBlockBreakingDelta
39+
import com.lambda.util.BlockUtils.instantBreakable
3340
import com.lambda.util.NamedEnum
34-
import net.minecraft.util.Hand
41+
import com.lambda.util.player.SlotUtils.hotbar
42+
import net.minecraft.block.pattern.CachedBlockPosition
43+
import net.minecraft.enchantment.Enchantments
44+
import net.minecraft.item.ItemStack
45+
import net.minecraft.registry.tag.ItemTags.DIAMOND_TOOL_MATERIALS
46+
import net.minecraft.registry.tag.ItemTags.GOLD_TOOL_MATERIALS
47+
import net.minecraft.registry.tag.ItemTags.IRON_TOOL_MATERIALS
48+
import net.minecraft.registry.tag.ItemTags.NETHERITE_TOOL_MATERIALS
49+
import net.minecraft.registry.tag.ItemTags.STONE_TOOL_MATERIALS
50+
import net.minecraft.registry.tag.ItemTags.WOODEN_TOOL_MATERIALS
3551
import net.minecraft.util.hit.BlockHitResult
3652
import java.util.concurrent.ConcurrentLinkedQueue
3753

@@ -41,29 +57,122 @@ object FastBreak : Module(
4157
tag = ModuleTag.PLAYER,
4258
) {
4359
private enum class Group(override val displayName: String) : NamedEnum {
44-
Build("Build")
60+
Break("Break"),
61+
Inventory("Inventory"),
62+
Hotbar("Hotbar")
4563
}
4664

47-
override val buildConfig = BuildSettings(this, Group.Build)
65+
override val breakConfig = BreakSettings(this, Group.Break).apply {
66+
editTypedSettings(
67+
::avoidLiquids,
68+
::avoidSupporting,
69+
::suitableToolsOnly,
70+
::rotateForBreak,
71+
::doubleBreak
72+
) { defaultValue(false) }
73+
editSetting(::breaksPerTick) { defaultValue(1) }
74+
editSetting(::breakWeakBlocks) { defaultValue(true) }
75+
hide(
76+
::sorter,
77+
::doubleBreak,
78+
::unsafeCancels,
79+
::rotateForBreak,
80+
::breaksPerTick,
81+
::breakWeakBlocks
82+
)
83+
}
84+
override val inventoryConfig = InventorySettings(this, Group.Inventory).apply {
85+
editTypedSettings(
86+
::accessShulkerBoxes,
87+
::accessEnderChest,
88+
::accessChests,
89+
::accessStashes
90+
) {
91+
defaultValue(false)
92+
visibility { false }
93+
}
94+
}
95+
override val hotbarConfig = HotbarSettings(this, Group.Hotbar)
4896

4997
private val pendingInteractions = ConcurrentLinkedQueue<BuildContext>()
5098

5199
init {
52100
listen<PlayerEvent.Attack.Block> { it.cancel() }
53101
listen<PlayerEvent.Breaking.Update> { event ->
54102
event.cancel()
55-
player.swingHand(Hand.MAIN_HAND)
56103

57104
val hitResult = mc.crosshairTarget as? BlockHitResult ?: return@listen
58105
val pos = event.pos
59106
val state = blockState(pos)
60107

108+
//ToDo: Copied this swap logic from the build sim. Needs reworking when we rework the build sim. Probably need to
109+
// adjust the build sim to accept partial simulations. For example, ignoring hit scanning in this situation
110+
val silentSwapSelection = selectContainer {
111+
ofAnyType(MaterialContainer.Rank.HOTBAR)
112+
}
113+
114+
val stackSelection = selectStack(
115+
sorter = compareByDescending<ItemStack> {
116+
it.canBreak(CachedBlockPosition(world, pos, false))
117+
}.thenByDescending {
118+
state.calcItemBlockBreakingDelta(pos, it)
119+
}
120+
) {
121+
isTool() and if (breakConfig.suitableToolsOnly) {
122+
isSuitableForBreaking(state)
123+
} else any() and if (breakConfig.forceSilkTouch) {
124+
hasEnchantment(Enchantments.SILK_TOUCH)
125+
} else any() and if (breakConfig.forceFortunePickaxe) {
126+
hasEnchantment(Enchantments.FORTUNE)
127+
} else any() and if (!breakConfig.useWoodenTools) {
128+
hasTag(WOODEN_TOOL_MATERIALS).not()
129+
} else any() and if (!breakConfig.useStoneTools) {
130+
hasTag(STONE_TOOL_MATERIALS).not()
131+
} else any() and if (!breakConfig.useIronTools) {
132+
hasTag(IRON_TOOL_MATERIALS).not()
133+
} else any() and if (!breakConfig.useDiamondTools) {
134+
hasTag(DIAMOND_TOOL_MATERIALS).not()
135+
} else any() and if (!breakConfig.useGoldTools) {
136+
hasTag(GOLD_TOOL_MATERIALS).not()
137+
} else any() and if (!breakConfig.useNetheriteTools) {
138+
hasTag(NETHERITE_TOOL_MATERIALS).not()
139+
} else any()
140+
}
141+
142+
val swapCandidates = stackSelection.containerWithMaterial(silentSwapSelection)
143+
if (swapCandidates.isEmpty()) return@listen
144+
145+
val swapStack = swapCandidates
146+
.map { it.matchingStacks(stackSelection) }
147+
.asSequence()
148+
.flatten()
149+
.let { containerStacks ->
150+
var bestStack = ItemStack.EMPTY
151+
var bestBreakDelta = -1f
152+
containerStacks.forEach { stack ->
153+
val breakDelta = state.calcItemBlockBreakingDelta(pos, stack)
154+
if (breakDelta > bestBreakDelta ||
155+
(stack == player.mainHandStack && breakDelta >= bestBreakDelta)
156+
) {
157+
bestBreakDelta = breakDelta
158+
bestStack = stack
159+
}
160+
}
161+
bestStack
162+
}
163+
61164
val breakContext = BreakContext(
62165
hitResult,
63166
RotationRequest(lookAt(player.rotation), this@FastBreak),
64-
player.inventory.selectedSlot,
65-
player.mainHandStack.select(),
66-
state.calcBlockBreakingDelta(player, world, pos) >= breakConfig.breakThreshold,
167+
player.hotbar.indexOf(swapStack),
168+
stackSelection,
169+
instantBreakable(
170+
state,
171+
pos,
172+
if (breakConfig.swapMode.isEnabled()) swapStack
173+
else player.mainHandStack,
174+
breakConfig.breakThreshold
175+
),
67176
state,
68177
breakConfig.sorter
69178
)

0 commit comments

Comments
 (0)