From fa7ecb9d5db09932a91858f6b30aae91d08d0b46 Mon Sep 17 00:00:00 2001 From: Username404 Date: Fri, 4 Jun 2021 23:09:14 +0200 Subject: [PATCH] Improve Configuration.kt by adding operator functions --- .../kotlin/fr/username404/snowygui/Snowy.kt | 5 ++--- .../snowygui/config/ConfigScreen.kt | 6 +++--- .../snowygui/config/Configuration.kt | 21 +++++++++++-------- .../snowygui/gui/elements/ClickBox.kt | 6 ++++-- .../snowygui/gui/feature/ButtonAnnotations.kt | 2 +- .../snowygui/gui/feature/ButtonImpl.kt | 2 +- .../snowygui/gui/feature/RiskyCheats.kt | 3 +-- 7 files changed, 24 insertions(+), 21 deletions(-) diff --git a/common/src/main/kotlin/fr/username404/snowygui/Snowy.kt b/common/src/main/kotlin/fr/username404/snowygui/Snowy.kt index 50bb00e..bc8f26d 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/Snowy.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/Snowy.kt @@ -1,18 +1,17 @@ package fr.username404.snowygui import fr.username404.snowygui.EventSnowy.Companion.useKey -import fr.username404.snowygui.config.Configuration.obtained +import fr.username404.snowygui.config.Configuration import fr.username404.snowygui.gui.feature.ButtonImpl import fr.username404.snowygui.gui.feature.Ignored import fr.username404.snowygui.misc.AddKeyMaps -import io.github.config4k.getValue import org.apache.logging.log4j.LogManager import org.apache.logging.log4j.Logger import java.lang.reflect.Modifier abstract class Snowy { protected fun Class<*>.isValidForButtonCollection(): Boolean = (!((Modifier.isAbstract(javaClass.modifiers)) || javaClass.isAnnotationPresent(Ignored::class.java))) - private val displayInitMessage: Boolean by obtained + private val displayInitMessage: Boolean by Configuration companion object { @JvmStatic protected val FeaturePackage: String = "fr.username404.snowygui.gui.feature" diff --git a/common/src/main/kotlin/fr/username404/snowygui/config/ConfigScreen.kt b/common/src/main/kotlin/fr/username404/snowygui/config/ConfigScreen.kt index 5895bb6..38581d2 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/config/ConfigScreen.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/config/ConfigScreen.kt @@ -9,7 +9,7 @@ import fr.username404.snowygui.gui.elements.ClickBox.Companion.sortAlphabeticall import fr.username404.snowygui.gui.feature.Category import fr.username404.snowygui.gui.feature.Colors import fr.username404.snowygui.gui.feature.Macro -import fr.username404.snowygui.gui.feature.riskyCheatsEnabled +import fr.username404.snowygui.gui.feature.riskyCheats import net.minecraft.client.gui.screens.Screen import net.minecraft.network.chat.Component import net.minecraft.network.chat.TranslatableComponent @@ -44,11 +44,11 @@ val SnowyConfigScreen: Screen get() { ) }.build()) .addEntry( - startBooleanToggle(Component.nullToEmpty("Risky Cheats"), riskyCheatsEnabled) + startBooleanToggle(Component.nullToEmpty("Risky Cheats"), riskyCheats) .setDefaultValue(false) .requireRestart() .setSaveConsumer { - riskyCheatsEnabled = it + riskyCheats = it }.setTooltip(Component.nullToEmpty("WARNING: Do not use this on servers or you might get banned.")).build() ).addEntry(startSubCategory(TranslatableComponent("$confPrefix.colors")).also { builder -> builder.addAll( diff --git a/common/src/main/kotlin/fr/username404/snowygui/config/Configuration.kt b/common/src/main/kotlin/fr/username404/snowygui/config/Configuration.kt index b36b6eb..74e516b 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/config/Configuration.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/config/Configuration.kt @@ -5,11 +5,10 @@ import com.typesafe.config.ConfigFactory.* import fr.username404.snowygui.ClickGui import fr.username404.snowygui.Snowy import fr.username404.snowygui.gui.elements.ClickBox -import fr.username404.snowygui.gui.elements.ClickBox.Companion.sortAlphabetically import fr.username404.snowygui.gui.feature.Category import fr.username404.snowygui.gui.feature.Macro -import fr.username404.snowygui.gui.feature.riskyCheatsEnabled import io.github.config4k.extract +import io.github.config4k.getValue import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking @@ -18,20 +17,18 @@ import kotlinx.serialization.encodeToString import kotlinx.serialization.json.Json import net.minecraft.client.Minecraft import java.io.File -import java.util.function.Supplier +import kotlin.reflect.KProperty object Configuration { - val ModifiableValues: MutableMap> = mapOf ConfigValue>( + private val ModifiableValues: MutableMap ConfigValue> = mapOf( "enabledFeatures" to { ConfigValueFactory.fromMap(enabledFeatures) }, - "riskyCheats" to { ConfigValueFactory.fromAnyRef(riskyCheatsEnabled)!! }, "macros" to { ConfigValueFactory.fromIterable((ClickGui.components.find { it is ClickBox && it.isCategory(Category.MACROS) } as ClickBox).buttons.map { Json.encodeToString(it as Macro) }) }, "box_colors" to { ConfigValueFactory.fromAnyRef(mapOf(*ClickGui.clickboxes.filter { it.name != null }.map { it.name!!.key to it.color }.toTypedArray())) }, - "sortAlphabetically" to { ConfigValueFactory.fromAnyRef(sortAlphabetically) } - ).mapValues { Supplier(it.value) }.toMutableMap(); inline fun setConfigValue(s: String, crossinline value: () -> Any) = ModifiableValues.put(s, Supplier { ConfigValueFactory.fromAnyRef(value.invoke()) }) + ).toMutableMap() private fun Config.withFullModifiableValues() = ModifiableValues.entries.fold(this) { previous, entry -> - previous.withValue(entry.key, entry.value.get()) + previous.withValue(entry.key, entry.value()) } private val configDirectory: String = (Minecraft.getInstance().gameDirectory.absolutePath + File.separator + "config").also { File(it).mkdir() } private val file: File = File(configDirectory + File.separator + "snowy.conf") @@ -53,7 +50,13 @@ object Configuration { ) } } - val obtained: Config by lazy { + + operator fun getValue(ref: Any?, property: KProperty<*>): Boolean = invoke().getValue(ref, property) + operator fun setValue(ref: Any?, property: KProperty<*>, value: T) { ModifiableValues[property.name] = { ConfigValueFactory.fromAnyRef(value) } } + + operator fun invoke() = obtained + + private val obtained: Config by lazy { var result: Config = empty() with(file) { if (!exists()) { diff --git a/common/src/main/kotlin/fr/username404/snowygui/gui/elements/ClickBox.kt b/common/src/main/kotlin/fr/username404/snowygui/gui/elements/ClickBox.kt index 767c65e..58298b4 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/gui/elements/ClickBox.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/gui/elements/ClickBox.kt @@ -67,9 +67,11 @@ class ClickBox( companion object { private val savedColors = "box_colors".let { - if (!Configuration.obtained.hasPath(it)) null else Configuration.obtained.extract>(it) + with(Configuration()) { + if (!hasPath(it)) null else extract>(it) + } } - var sortAlphabetically: Boolean = Configuration.obtained.extract("sortAlphabetically") + var sortAlphabetically: Boolean by Configuration const val buttonsMax: Short = 16 // TODO Remove the buttons limit const val clickboxHeightOffset: Int = 80 private const val inclination: Double = 2.5 diff --git a/common/src/main/kotlin/fr/username404/snowygui/gui/feature/ButtonAnnotations.kt b/common/src/main/kotlin/fr/username404/snowygui/gui/feature/ButtonAnnotations.kt index a40348d..1a0d898 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/gui/feature/ButtonAnnotations.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/gui/feature/ButtonAnnotations.kt @@ -25,7 +25,7 @@ annotation class ButtonInfo( RISKY("snowy.clickbox.risky", Colors.RED), MACROS("snowy.clickbox.macros", Colors.GREEN); val box = ClickBox( - x = 4.0 + (if (ordinal >= 1 && !riskyCheatsEnabled) ordinal - 1 else ordinal) * 86, y = 4.0, + x = 4.0 + (if (ordinal >= 1 && !riskyCheats) ordinal - 1 else ordinal) * 86, y = 4.0, name = TranslatableComponent(translationKey), color = categoryColor ) diff --git a/common/src/main/kotlin/fr/username404/snowygui/gui/feature/ButtonImpl.kt b/common/src/main/kotlin/fr/username404/snowygui/gui/feature/ButtonImpl.kt index 97c1ca6..891bf28 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/gui/feature/ButtonImpl.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/gui/feature/ButtonImpl.kt @@ -51,7 +51,7 @@ sealed class ButtonImpl: ColoredElement(0.0, 0.0, 73, 8, opacity = 0.60F) { } catch (e: NoSuchFieldException) {} }) as? ButtonImpl) }.filterNot { - (it.info.parent == Category.RISKY) && !riskyCheatsEnabled + (it.info.parent == Category.RISKY) && !riskyCheats }.plus(Configuration.foundMacros) ) } diff --git a/common/src/main/kotlin/fr/username404/snowygui/gui/feature/RiskyCheats.kt b/common/src/main/kotlin/fr/username404/snowygui/gui/feature/RiskyCheats.kt index 1ade49b..b14ac4d 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/gui/feature/RiskyCheats.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/gui/feature/RiskyCheats.kt @@ -1,10 +1,9 @@ package fr.username404.snowygui.gui.feature import fr.username404.snowygui.config.Configuration -import io.github.config4k.extract import net.minecraft.client.Minecraft -internal var riskyCheatsEnabled = Configuration.obtained.extract("riskyCheats") +internal var riskyCheats: Boolean by Configuration @ButtonInfo(Category.RISKY) object NoHurtCamera: ButtonImpl()