Improve Configuration.kt by adding operator functions

This commit is contained in:
Username404 2021-06-04 23:09:14 +02:00
parent 79a75f7681
commit fa7ecb9d5d
Signed by: Username404-59
GPG Key ID: 7AB361FBB257A5D1
7 changed files with 24 additions and 21 deletions

View File

@ -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"

View File

@ -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(

View File

@ -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<String, Supplier<ConfigValue>> = mapOf<String, () -> ConfigValue>(
private val ModifiableValues: MutableMap<String, () -> 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<String, Int>(*ClickGui.clickboxes.filter { it.name != null }.map { it.name!!.key to it.color }.toTypedArray())) },
"sortAlphabetically" to { ConfigValueFactory.fromAnyRef(sortAlphabetically) }
).mapValues { Supplier<ConfigValue>(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 <T> 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()) {

View File

@ -67,9 +67,11 @@ class ClickBox(
companion object {
private val savedColors = "box_colors".let {
if (!Configuration.obtained.hasPath(it)) null else Configuration.obtained.extract<Map<String, Int>>(it)
with(Configuration()) {
if (!hasPath(it)) null else extract<Map<String, Int>>(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

View File

@ -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
)

View File

@ -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)
)
}

View File

@ -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<Boolean>("riskyCheats")
internal var riskyCheats: Boolean by Configuration
@ButtonInfo(Category.RISKY)
object NoHurtCamera: ButtonImpl()