Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement: Use EnumMap and fields for config #2033

Merged
merged 2 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
.idea/*
# Use **/*.* since if a directory is ignored, children of that directory *cannot* be unignored using ! again.
.idea/**/*.*
!.idea/icon.svg
!.idea/dictionaries/default_user.xml
!.idea/scopes/Mixins.xml
.vscode/
run/
build/
Expand Down
3 changes: 3 additions & 0 deletions .idea/scopes/Mixins.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions src/main/java/at/hannibal2/skyhanni/SkyHanniMod.kt
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,13 @@ class SkyHanniMod {
val version: String
get() = Loader.instance().indexedModList[MODID]!!.version

@JvmStatic
val feature: Features get() = configManager.features
val sackData: SackData get() = configManager.sackData
val friendsData: FriendsJson get() = configManager.friendsData
val knownFeaturesData: KnownFeaturesJson get() = configManager.knownFeaturesData
val jacobContestsData: JacobContestsJson get() = configManager.jacobContestData
val visualWordsData: VisualWordsJson get() = configManager.visualWordsData
@JvmField
var feature: Features = Features()
lateinit var sackData: SackData
lateinit var friendsData: FriendsJson
lateinit var knownFeaturesData: KnownFeaturesJson
lateinit var jacobContestsData: JacobContestsJson
lateinit var visualWordsData: VisualWordsJson

lateinit var repo: RepoManager
lateinit var configManager: ConfigManager
Expand Down
35 changes: 19 additions & 16 deletions src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,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()) {
Expand All @@ -53,20 +56,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?")
Expand All @@ -75,7 +78,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
Expand Down Expand Up @@ -260,13 +263,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") }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public void set(Position other) {
}

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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class MixinBlockFire {

@Redirect(method = "randomDisplayTick", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/World;spawnParticle(Lnet/minecraft/util/EnumParticleTypes;DDDDDD[I)V"))
private void onLivingUpdate(World world, EnumParticleTypes particleType, double x, double y, double z, double xOffset, double yOffset, double zOffset, int[] parameters) {
if (!SkyHanniMod.getFeature().misc.particleHiders.hideFireBlockParticles) {
if (!SkyHanniMod.feature.misc.particleHiders.hideFireBlockParticles) {
world.spawnParticle(particleType, x, y, z, xOffset, yOffset, zOffset, parameters);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class MixinEntityBlaze {

@Redirect(method = "onLivingUpdate", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/World;spawnParticle(Lnet/minecraft/util/EnumParticleTypes;DDDDDD[I)V"))
private void onLivingUpdate(World world, EnumParticleTypes particleType, double x, double y, double z, double xOffset, double yOffset, double zOffset, int[] parameters) {
if (!SkyHanniMod.getFeature().misc.particleHiders.hideBlazeParticles) {
if (!SkyHanniMod.feature.misc.particleHiders.hideBlazeParticles) {
world.spawnParticle(particleType, x, y, z, xOffset, yOffset, zOffset, parameters);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class MixinEntityEnderman {

@Redirect(method = "onLivingUpdate", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/World;spawnParticle(Lnet/minecraft/util/EnumParticleTypes;DDDDDD[I)V"))
private void onLivingUpdate(World world, EnumParticleTypes particleType, double x, double y, double z, double xOffset, double yOffset, double zOffset, int[] parameters) {
if (!SkyHanniMod.getFeature().misc.particleHiders.hideEndermanParticles) {
if (!SkyHanniMod.feature.misc.particleHiders.hideEndermanParticles) {
world.spawnParticle(particleType, x, y, z, xOffset, yOffset, zOffset, parameters);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class MixinEntityFireball {

@Redirect(method = "onUpdate", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/World;spawnParticle(Lnet/minecraft/util/EnumParticleTypes;DDDDDD[I)V"))
private void onLivingUpdate(World world, EnumParticleTypes particleType, double x, double y, double z, double xOffset, double yOffset, double zOffset, int[] parameters) {
if (!SkyHanniMod.getFeature().misc.particleHiders.hideFireballParticles) {
if (!SkyHanniMod.feature.misc.particleHiders.hideFireballParticles) {
world.spawnParticle(particleType, x, y, z, xOffset, yOffset, zOffset, parameters);
}
}
Expand Down
Loading