Skip to content

Commit 0d7c191

Browse files
committed
refactor / improvement
1 parent 04cf8ca commit 0d7c191

File tree

11 files changed

+111
-86
lines changed

11 files changed

+111
-86
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ abstract class AbstractSetting<T : Any>(
101101
override var description: String,
102102
var visibility: () -> Boolean,
103103
) : Jsonable, Nameable, Describable, Layout {
104+
var hidden = false
104105
private val listeners = mutableListOf<ValueListener<T>>()
105106
var groups: MutableList<List<NamedEnum>> = mutableListOf()
106107

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

Lines changed: 66 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -36,64 +36,104 @@ abstract class SettingGroup(val c: Configurable, val startIndex: Int) {
3636
annotation class SettingEditorDsl
3737

3838
@SettingEditorDsl
39-
internal inline fun <T : Any> editSetting(setting: KProperty0<T>, edits: FullEditBuilder<T>.() -> Unit) {
40-
val setting = setting.delegate as? AbstractSetting<T> ?: throw IllegalStateException("Setting delegate did not match current value's type")
41-
FullEditBuilder(setting, c).apply(edits)
39+
internal inline fun <T : Any> KProperty0<T>.edit(edits: FullEditBuilder<T>.(AbstractSetting<T>) -> Unit) {
40+
val setting = delegate as? AbstractSetting<T> ?: throw IllegalStateException("Setting delegate did not match current value's type")
41+
FullEditBuilder(setting, c).edits(setting)
4242
}
4343

4444
@SettingEditorDsl
45-
fun editSettings(vararg settings: KProperty0<*>, edits: BasicEditBuilder.() -> Unit) {
46-
BasicEditBuilder(settings.toSet() as Set<AbstractSetting<*>>)
47-
.apply(edits)
45+
internal inline fun <T : Any> KProperty0<T>.editWith(
46+
other: KProperty0<*>,
47+
edits: FullEditBuilder<T>.(AbstractSetting<*>) -> Unit
48+
) {
49+
val setting = delegate as? AbstractSetting<T> ?: throw IllegalStateException("Setting delegate did not match current value's type")
50+
FullEditBuilder(setting, c).edits(other.delegate as AbstractSetting<*>)
4851
}
4952

5053
@SettingEditorDsl
51-
internal inline fun <T : Any> editTypedSettings(vararg settings: KProperty0<T>, edits: TypedEditBuilder<T>.() -> Unit) {
52-
TypedEditBuilder((settings.map { it.delegate } as List<AbstractSetting<T>>), c)
53-
.apply(edits)
54-
}
54+
fun edit(
55+
vararg settings: KProperty0<*>,
56+
edits: BasicEditBuilder.() -> Unit
57+
) { BasicEditBuilder(settings.toSet() as Set<AbstractSetting<*>>).apply(edits) }
5558

5659
@SettingEditorDsl
57-
fun hide(vararg settings: KProperty0<*>) {
58-
(settings.map { it.delegate } as List<AbstractSetting<*>>).forEach { setting ->
59-
setting.visibility = { false }
60-
}
60+
fun editWith(
61+
vararg settings: KProperty0<*>,
62+
other: KProperty0<*>,
63+
edits: BasicEditBuilder.(AbstractSetting<*>) -> Unit
64+
) { BasicEditBuilder(settings.toSet() as Set<AbstractSetting<*>>).edits(other.delegate as AbstractSetting<*>) }
65+
66+
@SettingEditorDsl
67+
internal inline fun <T : Any> editTyped(
68+
vararg settings: KProperty0<T>,
69+
edits: TypedEditBuilder<T>.() -> Unit
70+
) { TypedEditBuilder(settings.map { it.delegate } as List<AbstractSetting<T>>, c).apply(edits) }
71+
72+
@SettingEditorDsl
73+
internal inline fun <T : Any, R : Any> editTypedWith(
74+
other: KProperty0<R>,
75+
vararg settings: KProperty0<T>,
76+
edits: TypedEditBuilder<T>.(AbstractSetting<R>) -> Unit
77+
) = TypedEditBuilder(settings.map { it.delegate } as List<AbstractSetting<T>>, c).edits(other.delegate as AbstractSetting<R>)
78+
79+
@SettingEditorDsl
80+
fun hide(vararg settings: KProperty0<*>) =
81+
(settings.map { it.delegate } as List<AbstractSetting<*>>).forEach { it.hidden = true }
82+
83+
@SettingEditorDsl
84+
fun hideAll() =
85+
c.settings.listIterator(startIndex).forEach { it.hidden = true }
86+
87+
@SettingEditorDsl
88+
fun KProperty0<*>.insert(insert: KProperty0<*>, insertMode: InsertMode) {
89+
val index = c.settings.indexOf(delegate as AbstractSetting<*>)
90+
val delegate = insert.delegate as AbstractSetting<*>
91+
c.settings.remove(delegate)
92+
c.settings.add(if (insertMode == InsertMode.Above) index - 1 else index, delegate)
6193
}
6294

6395
@SettingEditorDsl
64-
fun hideAll() {
65-
c.settings.listIterator(startIndex).forEach {
66-
it.visibility = { false }
67-
}
96+
fun KProperty0<*>.insert(vararg inserts: KProperty0<*>, insertMode: InsertMode) {
97+
val index = c.settings.indexOf(delegate as AbstractSetting<*>)
98+
inserts.forEach { c.settings.remove(it.delegate as AbstractSetting<*>) }
99+
c.settings.addAll(
100+
if (insertMode == InsertMode.Above) index - 1 else index,
101+
inserts.map { it.delegate } as List<AbstractSetting<*>>
102+
)
68103
}
69104

70105
open class BasicEditBuilder(open val settings: Collection<AbstractSetting<*>>) {
71106
@SettingEditorDsl
72-
fun visibility(vis: () -> Boolean) {
107+
fun visibility(vis: () -> Boolean) =
73108
settings.forEach { it.visibility = vis }
74-
}
75109

76110
@SettingEditorDsl
77-
fun groups(groups: List<NamedEnum>) {
78-
settings.forEach { it.groups = mutableListOf(groups) }
79-
}
111+
fun hide() =
112+
settings.forEach { it.hidden = true }
113+
114+
@SettingEditorDsl
115+
fun groups(vararg groups: NamedEnum) =
116+
settings.forEach { it.groups = mutableListOf(groups.toList()) }
117+
118+
@SettingEditorDsl
119+
fun groups(groups: MutableList<List<NamedEnum>>) =
120+
settings.forEach { it.groups = groups }
80121
}
81122

82123
open class TypedEditBuilder<T : Any>(
83124
override val settings: Collection<AbstractSetting<T>>,
84125
val c: Configurable
85126
) : BasicEditBuilder(settings) {
86127
@SettingEditorDsl
87-
fun defaultValue(value: T) {
128+
fun defaultValue(value: T) =
88129
settings.forEach {
89130
it.defaultValue = value
90131
it.value = value
91132
}
92-
}
93133
}
94134

95135
class FullEditBuilder<T : Any>(
96-
val setting: AbstractSetting<T>,
136+
private val setting: AbstractSetting<T>,
97137
c: Configurable
98138
) : TypedEditBuilder<T>(setOf(setting), c) {
99139
@SettingEditorDsl
@@ -105,24 +145,6 @@ abstract class SettingGroup(val c: Configurable, val startIndex: Int) {
105145
fun description(description: String) {
106146
setting.description = description
107147
}
108-
109-
@SettingEditorDsl
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)
115-
}
116-
117-
@SettingEditorDsl
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-
)
125-
}
126148
}
127149

128150
enum class InsertMode {

src/main/kotlin/com/lambda/config/settings/NumericSetting.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,18 +99,18 @@ abstract class NumericSetting<T>(
9999
@SettingGroup.SettingEditorDsl
100100
@Suppress("unchecked_cast")
101101
fun SettingGroup.TypedEditBuilder<T>.range(range: ClosedRange<T>) {
102-
(settings as Set<NumericSetting<T>>).forEach { it.range = range }
102+
(settings as Collection<NumericSetting<T>>).forEach { it.range = range }
103103
}
104104

105105
@SettingGroup.SettingEditorDsl
106106
@Suppress("unchecked_cast")
107107
fun SettingGroup.TypedEditBuilder<T>.step(step: T) {
108-
(settings as Set<NumericSetting<T>>).forEach { it.step = step }
108+
(settings as Collection<NumericSetting<T>>).forEach { it.step = step }
109109
}
110110

111111
@SettingGroup.SettingEditorDsl
112112
@Suppress("unchecked_cast")
113113
fun SettingGroup.TypedEditBuilder<*>.unit(unit: String) {
114-
(settings as Set<NumericSetting<T>>).forEach { it.unit = unit}
114+
(settings as Collection<NumericSetting<T>>).forEach { it.unit = unit}
115115
}
116116
}

src/main/kotlin/com/lambda/config/settings/StringSetting.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ class StringSetting(
6666
@SettingGroup.SettingEditorDsl
6767
@Suppress("unchecked_cast")
6868
fun SettingGroup.TypedEditBuilder<String>.multiline(multiline: Boolean) {
69-
(settings as Set<StringSetting>).forEach { it.multiline = multiline }
69+
(settings as Collection<StringSetting>).forEach { it.multiline = multiline }
7070
}
7171

7272
@SettingGroup.SettingEditorDsl
7373
@Suppress("unchecked_cast")
7474
fun SettingGroup.TypedEditBuilder<String>.flags(flags: Int) {
75-
(settings as Set<StringSetting>).forEach { it.flags = flags }
75+
(settings as Collection<StringSetting>).forEach { it.flags = flags }
7676
}
7777
}

src/main/kotlin/com/lambda/config/settings/collections/ListSetting.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,6 @@ class ListSetting<T : Any>(
7777
@SettingGroup.SettingEditorDsl
7878
@Suppress("unchecked_cast")
7979
fun <T> SettingGroup.TypedEditBuilder<MutableList<T>>.immutableList(immutableList: List<T>) {
80-
(settings as Set<ListSetting<T>>).forEach { it.immutableList = immutableList }
80+
(settings as Collection<ListSetting<T>>).forEach { it.immutableList = immutableList }
8181
}
8282
}

src/main/kotlin/com/lambda/config/settings/collections/SetSetting.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,6 @@ class SetSetting<T : Any>(
7878
@SettingGroup.SettingEditorDsl
7979
@Suppress("unchecked_cast")
8080
fun <T> SettingGroup.TypedEditBuilder<MutableSet<T>>.immutableSet(immutableSet: Set<T>) {
81-
(settings as Set<SetSetting<T>>).forEach { it.immutableSet = immutableSet }
81+
(settings as Collection<SetSetting<T>>).forEach { it.immutableSet = immutableSet }
8282
}
8383
}

src/main/kotlin/com/lambda/gui/components/SettingsWidget.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ object SettingsWidget {
4141
}
4242
separator()
4343
val toIgnoreSettings = if (config is Module) setOf(config.keybindSetting, config.disableOnReleaseSetting) else emptySet()
44-
val visibleSettings = config.settings.filter { it.visibility() } - toIgnoreSettings
44+
val visibleSettings = config.settings.filter { it.visibility() && !it.hidden } - toIgnoreSettings
4545
val (grouped, ungrouped) = visibleSettings.partition { it.groups.isNotEmpty() }
4646
ungrouped.forEach { with(it) { buildLayout() } }
4747
renderGroup(grouped, emptyList(), config)

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,15 @@ object FastBreak : Module(
6363
}
6464

6565
override val breakConfig = BreakSettings(this, Group.Break).apply {
66-
editTypedSettings(
66+
editTyped(
6767
::avoidLiquids,
6868
::avoidSupporting,
6969
::suitableToolsOnly,
7070
::rotateForBreak,
7171
::doubleBreak
7272
) { defaultValue(false) }
73-
editSetting(::breaksPerTick) { defaultValue(1) }
74-
editSetting(::breakWeakBlocks) { defaultValue(true) }
73+
::breaksPerTick.edit { defaultValue(1) }
74+
::breakWeakBlocks.edit { defaultValue(true) }
7575
hide(
7676
::sorter,
7777
::doubleBreak,
@@ -82,14 +82,14 @@ object FastBreak : Module(
8282
)
8383
}
8484
override val inventoryConfig = InventorySettings(this, Group.Inventory).apply {
85-
editTypedSettings(
85+
editTyped(
8686
::accessShulkerBoxes,
8787
::accessEnderChest,
8888
::accessChests,
8989
::accessStashes
9090
) {
9191
defaultValue(false)
92-
visibility { false }
92+
hide()
9393
}
9494
}
9595
override val hotbarConfig = HotbarSettings(this, Group.Hotbar)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ object HighwayTools : Module(
8686

8787
override val buildConfig = BuildSettings(this, Group.Build)
8888
override val breakConfig = BreakSettings(this, Group.Break).apply {
89-
editSetting(::swapMode) { defaultValue(BreakConfig.SwapMode.Constant) }
89+
::swapMode.edit { defaultValue(BreakConfig.SwapMode.Constant) }
9090
}
9191
override val placeConfig = PlaceSettings(this, Group.Place)
9292
override val interactConfig = InteractSettings(this, Group.Interact)

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

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ object PacketMine : Module(
6363
Hotbar("Hotbar"),
6464
}
6565

66-
private val rebreakMode by setting("Rebreak Mode", RebreakMode.Manual, "The method used to re-break blocks after they've been broken once").group(Group.Break, BreakSettings.Group.General)
66+
private val rebreakMode by setting("Rebreak Mode", RebreakMode.Manual, "The method used to re-break blocks after they've been broken once")
6767
private val breakRadius by setting("Break Radius", 0, 0..5, 1, "Selects and breaks all blocks within the break radius of the selected block").group(Group.Break, BreakSettings.Group.General)
6868
private val flatten by setting("Flatten", true, "Wont allow breaking extra blocks under your players position") { breakRadius > 0 }.group(Group.Break, BreakSettings.Group.General)
6969
private val queue by setting("Queue", false, "Queues blocks to break so you can select multiple at once").group(Group.Break, BreakSettings.Group.General)
@@ -79,45 +79,47 @@ object PacketMine : Module(
7979
private val endColor by setting("End Color", Color(255, 0, 0, 60).brighter(), "The color of the end (farthest from breaking) of the queue") { renderQueue && dynamicColor }.group(Group.Break, BreakSettings.Group.Cosmetic)
8080

8181
override val breakConfig = BreakSettings(this, Group.Break).apply {
82-
editTypedSettings(::avoidLiquids, ::avoidSupporting, ::suitableToolsOnly) { defaultValue(false) }
83-
editSetting(::breakWeakBlocks) { defaultValue(true) }
84-
editSetting(::swing) { defaultValue(BreakConfig.SwingMode.Start) }
85-
editSetting(::rebreak) { insert(::rebreakMode, SettingGroup.InsertMode.Below) }
86-
editSetting(::rebreakMode) { visibility { rebreak } }
82+
editTyped(::avoidLiquids, ::avoidSupporting, ::suitableToolsOnly) { defaultValue(false) }
83+
::breakWeakBlocks.edit { defaultValue(true) }
84+
::swing.edit { defaultValue(BreakConfig.SwingMode.Start) }
8785

88-
editSetting(::sounds) {
89-
insert(
90-
::renderQueue,
91-
::renderSize,
92-
::renderMode,
93-
::dynamicColor,
94-
::staticColor,
95-
::startColor,
96-
::endColor,
97-
insertMode = SettingGroup.InsertMode.Above
98-
)
86+
::rebreak.insert(::rebreakMode, SettingGroup.InsertMode.Below)
87+
::rebreakMode.editWith(::rebreak) { rebreakSetting ->
88+
visibility { rebreak }
89+
groups(rebreakSetting.groups)
9990
}
91+
92+
::sounds.insert(
93+
::renderQueue,
94+
::renderSize,
95+
::renderMode,
96+
::dynamicColor,
97+
::staticColor,
98+
::startColor,
99+
::endColor,
100+
insertMode = SettingGroup.InsertMode.Above
101+
)
100102
}
101103
override val buildConfig = BuildSettings(this, Group.Build).apply {
102-
editTypedSettings(::pathing, ::stayInRange, ::collectDrops) {
104+
editTyped(::pathing, ::stayInRange, ::collectDrops) {
103105
defaultValue(false)
104-
visibility { false }
106+
hide()
105107
}
106108
}
107109
override val rotationConfig = RotationSettings(this, Group.Rotation)
108110
override val inventoryConfig = InventorySettings(this, Group.Inventory).apply {
109-
editTypedSettings(
111+
editTyped(
110112
::accessShulkerBoxes,
111113
::accessEnderChest,
112114
::accessChests,
113115
::accessStashes
114116
) {
115117
defaultValue(false)
116-
visibility { false }
118+
hide()
117119
}
118120
}
119121
override val hotbarConfig = HotbarSettings(this, Group.Hotbar).apply {
120-
editSetting(::keepTicks) { defaultValue(0) }
122+
::keepTicks.edit { defaultValue(0) }
121123
}
122124

123125
private val pendingInteractions = ConcurrentLinkedQueue<BuildContext>()

0 commit comments

Comments
 (0)