@@ -31,10 +31,8 @@ import com.lambda.event.events.onStaticRender
3131import com.lambda.interaction.construction.result.Drawable
3232import com.lambda.module.Module
3333import com.lambda.util.NamedEnum
34- import kotlin.reflect.KProperty0
35- import kotlin.reflect.jvm.isAccessible
3634
37- @Suppress( " unchecked_cast " , " unused " )
35+
3836open class AutomationConfig (
3937 override val name : String ,
4038 configuration : Configuration = AutomationConfigs
@@ -45,7 +43,6 @@ open class AutomationConfig(
4543 Place (" Place" ),
4644 Interact (" Interact" ),
4745 Rotation (" Rotation" ),
48- Interaction (" Interaction" ),
4946 Inventory (" Inventory" ),
5047 Hotbar (" Hotbar" ),
5148 Eat (" Eat" ),
@@ -66,11 +63,19 @@ open class AutomationConfig(
6663
6764 companion object {
6865 context(module: Module )
69- fun automationConfig (name : String = module.name, edits : (AutomationConfig .() -> Unit )? = null): AutomationConfig =
70- AutomationConfig (" Default $name Automation Config" ).apply { edits?.invoke(this ) }
71-
72- fun automationConfig (name : String , edits : (AutomationConfig .() -> Unit )? = null): AutomationConfig =
73- AutomationConfig (" Default $name Automation Config" ).apply { edits?.invoke(this ) }
66+ fun MutableAutomationConfig.setDefaultAutomationConfig (
67+ name : String = module.name,
68+ edits : (AutomationConfig .() -> Unit )? = null
69+ ) {
70+ defaultAutomationConfig = AutomationConfig (" Default $name Automation Config" ).apply { edits?.invoke(this ) }
71+ }
72+
73+ fun MutableAutomationConfig.setDefaultAutomationConfig (
74+ name : String ,
75+ edits : (AutomationConfig .() -> Unit )? = null
76+ ) {
77+ defaultAutomationConfig = AutomationConfig (" Default $name Automation Config" ).apply { edits?.invoke(this ) }
78+ }
7479
7580 object DEFAULT : AutomationConfig(" Default" ) {
7681 val renders by setting(" Render" , false ).group(Group .Render )
@@ -91,118 +96,4 @@ open class AutomationConfig(
9196 }
9297 }
9398 }
94-
95- @DslMarker
96- annotation class SettingEditorDsl
97-
98- private val KProperty0 <* >.delegate
99- get() = try {
100- apply { isAccessible = true }.getDelegate()
101- } catch (e: Exception ) {
102- throw IllegalStateException (" Could not access delegate for property $name " , e)
103- }
104-
105- @SettingEditorDsl
106- internal inline fun <T : Any > KProperty0<T>.edit (edits : TypedEditBuilder <T >.(AbstractSetting <T >) -> Unit ) {
107- val setting = delegate as ? AbstractSetting <T > ? : throw IllegalStateException (" Setting delegate did not match current value's type" )
108- TypedEditBuilder (this @AutomationConfig, listOf (setting)).edits(setting)
109- }
110-
111- @SettingEditorDsl
112- internal inline fun <T : Any , R : Any > KProperty0<T>.editWith (
113- other : KProperty0 <R >,
114- edits : TypedEditBuilder <T >.(AbstractSetting <R >) -> Unit
115- ) {
116- val setting = delegate as ? AbstractSetting <T > ? : throw IllegalStateException (" Setting delegate did not match current value's type" )
117- TypedEditBuilder (this @AutomationConfig, listOf (setting)).edits(other.delegate as AbstractSetting <R >)
118- }
119-
120- @SettingEditorDsl
121- fun edit (
122- vararg settings : KProperty0 <* >,
123- edits : BasicEditBuilder .() -> Unit
124- ) { BasicEditBuilder (this @AutomationConfig, settings.map { it.delegate } as List <AbstractSetting <* >>).apply (edits) }
125-
126- @SettingEditorDsl
127- internal inline fun <T : Any > editWith (
128- vararg settings : KProperty0 <* >,
129- other : KProperty0 <T >,
130- edits : BasicEditBuilder .(AbstractSetting <T >) -> Unit
131- ) { BasicEditBuilder (this @AutomationConfig, settings.map { it.delegate } as List <AbstractSetting <* >>).edits(other.delegate as AbstractSetting <T >) }
132-
133- @SettingEditorDsl
134- internal inline fun <T : Any > editTyped (
135- vararg settings : KProperty0 <T >,
136- edits : TypedEditBuilder <T >.() -> Unit
137- ) { TypedEditBuilder (this @AutomationConfig, settings.map { it.delegate } as List <AbstractSetting <T >>).apply (edits) }
138-
139- @SettingEditorDsl
140- internal inline fun <T : Any , R : Any > editTypedWith (
141- vararg settings : KProperty0 <T >,
142- other : KProperty0 <R >,
143- edits : TypedEditBuilder <T >.(AbstractSetting <R >) -> Unit
144- ) = TypedEditBuilder (this @AutomationConfig, settings.map { it.delegate } as List <AbstractSetting <T >>).edits(other.delegate as AbstractSetting <R >)
145-
146- @SettingEditorDsl
147- fun hide (vararg settings : KProperty0 <* >) {
148- hideAll((settings.map { it.delegate } as List <AbstractSetting <* >>))
149- }
150-
151- @SettingEditorDsl
152- fun hideAll (settingGroup : SettingGroup ) = hideAll(settingGroup.settings)
153-
154- @SettingEditorDsl
155- fun hideAll (settings : Collection <AbstractSetting <* >>) {
156- this @AutomationConfig.settings.removeAll(settings)
157- hiddenSettings.addAll(settings)
158- }
159-
160- @SettingEditorDsl
161- fun hideAll (vararg settingGroups : SettingGroup ) {
162- settingGroups.forEach { hideAll(it.settings) }
163- }
164-
165- @SettingEditorDsl
166- fun hideAllExcept (settingGroup : SettingGroup , vararg settings : KProperty0 <* >) {
167- this @AutomationConfig.settings.removeIf {
168- return @removeIf if (it in settingGroup.settings && it !in (settings.toList() as List <AbstractSetting <* >>)) {
169- hiddenSettings.add(it)
170- true
171- } else false
172- }
173- }
174-
175- open class BasicEditBuilder (val c : AutomationConfig , open val settings : Collection <AbstractSetting <* >>) {
176- @SettingEditorDsl
177- fun visibility (vis : () -> Boolean ) =
178- settings.forEach { it.visibility = vis }
179-
180- @SettingEditorDsl
181- fun hide () = c.hideAll(settings)
182-
183- @SettingEditorDsl
184- fun groups (vararg groups : NamedEnum ) =
185- settings.forEach { it.groups = mutableListOf (groups.toList()) }
186-
187- @SettingEditorDsl
188- fun groups (groups : MutableList <List <NamedEnum >>) =
189- settings.forEach { it.groups = groups }
190- }
191-
192- open class TypedEditBuilder <T : Any >(
193- c : AutomationConfig ,
194- override val settings : Collection <AbstractSetting <T >>
195- ) : BasicEditBuilder(c, settings) {
196- @SettingEditorDsl
197- fun defaultValue (value : T ) =
198- settings.forEach {
199- it.defaultValue = value
200- it.value = value
201- }
202- }
203-
204- enum class InsertMode {
205- Above ,
206- Below
207- }
20899}
0 commit comments