Only save buttons which can be toggled

Signed-off-by: Username404 <w.iron.zombie@gmail.com>
This commit is contained in:
Username404 2022-03-26 11:00:38 +01:00
parent bd02e2c1e7
commit 904c0ac831
Signed by: Username404-59
GPG Key ID: 7AB361FBB257A5D1
3 changed files with 11 additions and 3 deletions

View File

@ -11,6 +11,7 @@ import com.typesafe.config.ConfigValueFactory
import fr.username404.snowygui.ClickGui import fr.username404.snowygui.ClickGui
import fr.username404.snowygui.Snowy import fr.username404.snowygui.Snowy
import fr.username404.snowygui.Snowy.Companion.MissingComponent import fr.username404.snowygui.Snowy.Companion.MissingComponent
import fr.username404.snowygui.gui.feature.shouldSave
import fr.username404.snowygui.gui.feature.Category import fr.username404.snowygui.gui.feature.Category
import fr.username404.snowygui.gui.feature.Macro import fr.username404.snowygui.gui.feature.Macro
import io.github.config4k.extract import io.github.config4k.extract
@ -84,7 +85,11 @@ object Configuration {
val enabledFeatures = mutableMapOf<String, Boolean>().apply { val enabledFeatures = mutableMapOf<String, Boolean>().apply {
"enabledFeatures".let { obtained.run { "enabledFeatures".let { obtained.run {
if (hasPath(it)) { if (hasPath(it)) {
putAll(extract(it)) putAll(extract<Map<out String, Boolean>>(it).filterKeys { keyName -> ClickGui.clickBoxes.any {
it.buttons.any { button ->
button.info.shouldSave() && button.title == keyName
}
}})
} }
} } } }
} }
@ -98,7 +103,7 @@ object Configuration {
Thread { Thread {
runBlocking { runBlocking {
ClickGui.boxContext { ClickGui.boxContext {
enabledFeatures.putAll(buttons.filter { it.info.shouldSave }.map { enabledFeatures.putAll(buttons.filter { it.info.shouldSave() }.map {
it.title to it.toggled it.title to it.toggled
}) })
} }

View File

@ -17,7 +17,7 @@ sealed class ButtonImpl: ColoredElement(0.0, 0.0, 73, 8, opacity = 0.60F) {
buttons.groupBy { impl -> buttons.groupBy { impl ->
ClickGui.clickBoxes.find { box -> ClickGui.clickBoxes.find { box ->
with(impl) { with(impl) {
if (info.shouldSave && info.kind == Type.TOGGLE && !info.parent.shouldHide) { if (info.shouldSave() && !info.parent.shouldHide) {
Configuration.enabledFeatures[title]?.let { bool -> Configuration.enabledFeatures[title]?.let { bool ->
toggled = bool toggled = bool
} }

View File

@ -12,6 +12,7 @@ import net.minecraft.network.chat.TranslatableComponent
annotation class ButtonInfo( annotation class ButtonInfo(
val parent: Category, val parent: Category,
val kind: Type = Type.TOGGLE, val kind: Type = Type.TOGGLE,
@Deprecated("Use shouldSave() instead", level = DeprecationLevel.ERROR)
val shouldSave: Boolean = true, /** Whether or not the state of the button should be saved */ val shouldSave: Boolean = true, /** Whether or not the state of the button should be saved */
val ignored: Boolean = false /** Excludes a class from button collection */ val ignored: Boolean = false /** Excludes a class from button collection */
) { ) {
@ -53,4 +54,6 @@ annotation class ButtonInfo(
} }
} }
fun ButtonInfo.shouldSave(): Boolean = @Suppress("DEPRECATION_ERROR") shouldSave && kind == ButtonInfo.Companion.Type.TOGGLE
typealias Category = ButtonInfo.Companion.Category typealias Category = ButtonInfo.Companion.Category