aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/config
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2024-06-12 18:01:37 +0200
committerGitHub <noreply@github.com>2024-06-12 18:01:37 +0200
commit488c0e3de136943604e21bee4f49969bd3121895 (patch)
treeba4e3d11a78dabc7169e033120ec697f1fc3c72d /src/main/java/at/hannibal2/skyhanni/config
parent584f7cd9597aeda0eb44c7d65387d255c592f94c (diff)
downloadskyhanni-488c0e3de136943604e21bee4f49969bd3121895.tar.gz
skyhanni-488c0e3de136943604e21bee4f49969bd3121895.tar.bz2
skyhanni-488c0e3de136943604e21bee4f49969bd3121895.zip
Use EnumMap and fields for config (#2033)
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/config')
-rw-r--r--src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt35
-rw-r--r--src/main/java/at/hannibal2/skyhanni/config/core/config/Position.java2
2 files changed, 20 insertions, 17 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt b/src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt
index 6be199cb7..db70f7e89 100644
--- a/src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt
+++ b/src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt
@@ -36,7 +36,10 @@ import java.io.OutputStreamWriter
import java.nio.charset.StandardCharsets
import java.nio.file.Files
import java.nio.file.StandardCopyOption
+import java.util.EnumMap
import kotlin.concurrent.fixedRateTimer
+import kotlin.reflect.KMutableProperty0
+import kotlin.reflect.KMutableProperty1
private fun GsonBuilder.registerIfBeta(create: TypeAdapterFactory): GsonBuilder {
return if (LorenzUtils.isBetaVersion()) {
@@ -54,20 +57,20 @@ class ConfigManager {
var configDirectory = File("config/skyhanni")
}
- val features get() = jsonHolder[ConfigFileType.FEATURES] as Features
- val sackData get() = jsonHolder[ConfigFileType.SACKS] as SackData
- val friendsData get() = jsonHolder[ConfigFileType.FRIENDS] as FriendsJson
- val knownFeaturesData get() = jsonHolder[ConfigFileType.KNOWN_FEATURES] as KnownFeaturesJson
- val jacobContestData get() = jsonHolder[ConfigFileType.JACOB_CONTESTS] as JacobContestsJson
- val visualWordsData get() = jsonHolder[ConfigFileType.VISUAL_WORDS] as VisualWordsJson
-
private val logger = LorenzLogger("config_manager")
- private val jsonHolder = mutableMapOf<ConfigFileType, Any>()
+ private val jsonHolder: Map<ConfigFileType, Any> = EnumMap(ConfigFileType::class.java)
lateinit var processor: MoulConfigProcessor<Features>
private var disableSaving = false
+ private fun setConfigHolder(type: ConfigFileType, value: Any) {
+ require(value.javaClass == type.clazz)
+ @Suppress("UNCHECKED_CAST")
+ (type.property as KMutableProperty0<Any>).set(value)
+ (jsonHolder as MutableMap<ConfigFileType, Any>)[type] = value
+ }
+
fun firstLoad() {
if (jsonHolder.isNotEmpty()) {
logger.log("Loading config despite config being already loaded?")
@@ -76,7 +79,7 @@ class ConfigManager {
for (fileType in ConfigFileType.entries) {
- jsonHolder[fileType] = firstLoadFile(fileType.file, fileType, fileType.clazz.newInstance())
+ setConfigHolder(fileType, firstLoadFile(fileType.file, fileType, fileType.clazz.newInstance()))
}
// TODO use SecondPassedEvent
@@ -265,13 +268,13 @@ class ConfigManager {
}
}
-enum class ConfigFileType(val fileName: String, val clazz: Class<*>) {
- FEATURES("config", Features::class.java),
- SACKS("sacks", SackData::class.java),
- FRIENDS("friends", FriendsJson::class.java),
- KNOWN_FEATURES("known_features", KnownFeaturesJson::class.java),
- JACOB_CONTESTS("jacob_contests", JacobContestsJson::class.java),
- VISUAL_WORDS("visual_words", VisualWordsJson::class.java),
+enum class ConfigFileType(val fileName: String, val clazz: Class<*>, val property: KMutableProperty0<*>) {
+ FEATURES("config", Features::class.java, SkyHanniMod::feature),
+ SACKS("sacks", SackData::class.java, SkyHanniMod::sackData),
+ FRIENDS("friends", FriendsJson::class.java, SkyHanniMod::friendsData),
+ KNOWN_FEATURES("known_features", KnownFeaturesJson::class.java, SkyHanniMod::knownFeaturesData),
+ JACOB_CONTESTS("jacob_contests", JacobContestsJson::class.java, SkyHanniMod::jacobContestsData),
+ VISUAL_WORDS("visual_words", VisualWordsJson::class.java, SkyHanniMod::visualWordsData),
;
val file by lazy { File(ConfigManager.configDirectory, "$fileName.json") }
diff --git a/src/main/java/at/hannibal2/skyhanni/config/core/config/Position.java b/src/main/java/at/hannibal2/skyhanni/config/core/config/Position.java
index 26b0a1e7d..140fbf007 100644
--- a/src/main/java/at/hannibal2/skyhanni/config/core/config/Position.java
+++ b/src/main/java/at/hannibal2/skyhanni/config/core/config/Position.java
@@ -91,7 +91,7 @@ public class Position {
}
public float getEffectiveScale() {
- return Math.max(Math.min(getScale() * SkyHanniMod.getFeature().gui.globalScale, 10F), 0.1F);
+ return Math.max(Math.min(getScale() * SkyHanniMod.feature.gui.globalScale, 10F), 0.1F);
}
public float getScale() {