Skip to content

Commit 7591b93

Browse files
committed
Added function button
1 parent 195086d commit 7591b93

File tree

7 files changed

+105
-8
lines changed

7 files changed

+105
-8
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ abstract class AbstractSetting<T : Any>(
126126
listeners.add(ValueListener(false, block))
127127
}
128128

129-
private fun reset() {
129+
fun reset() {
130130
value = defaultValue
131131
}
132132

common/src/main/kotlin/com/lambda/config/Configurable.kt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,14 @@ import com.google.gson.reflect.TypeToken
2323
import com.lambda.Lambda
2424
import com.lambda.Lambda.LOG
2525
import com.lambda.config.settings.CharSetting
26+
import com.lambda.config.settings.FunctionSetting
2627
import com.lambda.config.settings.StringSetting
2728
import com.lambda.config.settings.collections.ListSetting
2829
import com.lambda.config.settings.collections.MapSetting
2930
import com.lambda.config.settings.collections.SetSetting
3031
import com.lambda.config.settings.comparable.BooleanSetting
3132
import com.lambda.config.settings.comparable.EnumSetting
32-
import com.lambda.config.settings.complex.BlockPosSetting
33-
import com.lambda.config.settings.complex.BlockSetting
34-
import com.lambda.config.settings.complex.ColorSetting
35-
import com.lambda.config.settings.complex.KeyBindSetting
33+
import com.lambda.config.settings.complex.*
3634
import com.lambda.config.settings.numeric.*
3735
import com.lambda.util.Communication.logError
3836
import com.lambda.util.KeyCode
@@ -173,7 +171,6 @@ abstract class Configurable(
173171
* @param name The unique identifier for the setting.
174172
* @param defaultValue The default [List] value of type [T] for the setting.
175173
* @param description A brief explanation of the setting's purpose and behavior.
176-
* @param hackDelegates A flag that determines whether the setting should be serialized with the default value.
177174
* @param visibility A lambda expression that determines the visibility status of the setting.
178175
*
179176
* ```kotlin
@@ -204,7 +201,6 @@ abstract class Configurable(
204201
* @param name The unique identifier for the setting.
205202
* @param defaultValue The default [Map] value of type [K] and [V] for the setting.
206203
* @param description A brief explanation of the setting's purpose and behavior.
207-
* @param hackDelegates A flag that determines whether the setting should be serialized with the default value.
208204
* @param visibility A lambda expression that determines the visibility status of the setting.
209205
*
210206
* ```kotlin
@@ -424,4 +420,11 @@ abstract class Configurable(
424420
description: String = "",
425421
visibility: () -> Boolean = { true },
426422
) = BlockSetting(name, defaultValue, description, visibility).register()
423+
424+
fun setting(
425+
name: String,
426+
defaultValue: () -> Unit,
427+
description: String = "",
428+
visibility: () -> Boolean = { true }
429+
) = FunctionSetting(name, defaultValue, description, visibility).register()
427430
}
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.config.settings
19+
20+
import com.google.gson.JsonElement
21+
import com.google.gson.JsonNull
22+
import com.google.gson.reflect.TypeToken
23+
import com.lambda.config.AbstractSetting
24+
25+
open class FunctionSetting<T : Any>(
26+
override val name: String,
27+
private val defaultValue: () -> T,
28+
description: String,
29+
visibility: () -> Boolean,
30+
) : AbstractSetting<() -> T>(
31+
defaultValue,
32+
TypeToken.get(defaultValue::class.java).type,
33+
description,
34+
visibility
35+
) {
36+
override fun toJson(): JsonElement = JsonNull.INSTANCE
37+
override fun loadFromJson(serialized: JsonElement) { value = defaultValue }
38+
}

common/src/main/kotlin/com/lambda/gui/GuiManager.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ package com.lambda.gui
1919

2020
import com.lambda.config.settings.comparable.BooleanSetting
2121
import com.lambda.config.settings.comparable.EnumSetting
22+
import com.lambda.config.settings.FunctionSetting
2223
import com.lambda.core.Loadable
2324
import com.lambda.gui.component.core.UIBuilder
2425
import com.lambda.gui.component.layout.Layout
2526
import com.lambda.gui.impl.clickgui.settings.BooleanButton.Companion.booleanSetting
2627
import com.lambda.gui.impl.clickgui.settings.EnumSelector.Companion.enumSetting
28+
import com.lambda.gui.impl.clickgui.settings.UnitButton.Companion.unitSetting
2729
import kotlin.reflect.KClass
2830

2931
object GuiManager : Loadable {
@@ -42,6 +44,10 @@ object GuiManager : Loadable {
4244
owner.enumSetting(ref)
4345
}
4446

47+
typeAdapter<FunctionSetting<Unit>> { owner, ref ->
48+
owner.unitSetting(ref)
49+
}
50+
4551
return "Loaded ${typeMap.size} gui type adapters."
4652
}
4753

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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.gui.impl.clickgui.settings
19+
20+
import com.lambda.config.settings.FunctionSetting
21+
import com.lambda.gui.component.core.UIBuilder
22+
import com.lambda.gui.component.layout.Layout
23+
import com.lambda.gui.impl.clickgui.SettingLayout
24+
import com.lambda.util.Mouse
25+
26+
class UnitButton(
27+
owner: Layout,
28+
setting: FunctionSetting<Unit>,
29+
) : SettingLayout<() -> Unit, FunctionSetting<Unit>>(owner, setting) {
30+
init {
31+
onMouseClick(Mouse.Button.Left, Mouse.Action.Click) {
32+
setting.value()
33+
}
34+
}
35+
36+
companion object {
37+
/**
38+
* Creates a [UnitButton] - visual representation of the [FunctionSetting]
39+
*/
40+
@UIBuilder
41+
fun Layout.unitSetting(setting: FunctionSetting<Unit>) =
42+
UnitButton(this, setting).apply(children::add)
43+
}
44+
}

common/src/main/kotlin/com/lambda/module/Module.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import com.lambda.event.listener.UnsafeListener
3434
import com.lambda.module.tag.ModuleTag
3535
import com.lambda.sound.LambdaSound
3636
import com.lambda.sound.SoundManager.playSoundRandomly
37+
import com.lambda.util.Communication.info
3738
import com.lambda.util.KeyCode
3839
import com.lambda.util.Nameable
3940

@@ -116,6 +117,7 @@ abstract class Module(
116117
private val isEnabledSetting = setting("Enabled", enabledByDefault, visibility = { false })
117118
private val keybindSetting = setting("Keybind", defaultKeybind)
118119
private val isVisible = setting("Visible", true)
120+
val reset by setting("Reset", { settings.forEach { it.reset() }; this@Module.info("Settings set to default") })
119121
val customTags = setting("Tags", setOf<ModuleTag>(), visibility = { false })
120122

121123
var isEnabled by isEnabledSetting

common/src/main/kotlin/com/lambda/module/modules/debug/SettingTest.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package com.lambda.module.modules.debug
1919

2020
import com.lambda.module.Module
2121
import com.lambda.module.tag.ModuleTag
22+
import com.lambda.util.Communication.info
2223
import com.lambda.util.KeyCode
2324
import net.minecraft.block.Blocks
2425
import net.minecraft.util.math.BlockPos
@@ -61,9 +62,12 @@ object SettingTest : Module(
6162
private val colorMap by setting("Color Map", mapOf("Primary" to Color.GREEN))
6263
private val keyBindSet by setting("Key Bind Set", setOf(KeyCode.T))
6364

65+
// Other
66+
private val unitSetting by setting("Unit Test", { this@SettingTest.info("Unit setting") })
67+
6468
enum class ExampleEnum {
6569
VALUE_ONE,
6670
VALUE_TWO,
6771
VALUE_THREE
6872
}
69-
}
73+
}

0 commit comments

Comments
 (0)