From 5190017bc8f7bb1177da239842e4b3a18a888d8c Mon Sep 17 00:00:00 2001 From: nea Date: Sat, 31 Dec 2022 19:40:50 +0100 Subject: EnforcedConfigValues: Allow filtering for mod version --- .../miscfeatures/EnforcedConfigValues.kt | 32 ++++++++++++++-------- 1 file changed, 21 insertions(+), 11 deletions(-) (limited to 'src/main/kotlin/io') diff --git a/src/main/kotlin/io/github/moulberry/notenoughupdates/miscfeatures/EnforcedConfigValues.kt b/src/main/kotlin/io/github/moulberry/notenoughupdates/miscfeatures/EnforcedConfigValues.kt index 50c45832..d5a1260e 100644 --- a/src/main/kotlin/io/github/moulberry/notenoughupdates/miscfeatures/EnforcedConfigValues.kt +++ b/src/main/kotlin/io/github/moulberry/notenoughupdates/miscfeatures/EnforcedConfigValues.kt @@ -43,18 +43,27 @@ object EnforcedConfigValues { var enforcedValues: List = listOf() var notificationPSA: List? = null var chatPSA: List? = null + lateinit var affectedVersions: List } - var enforcedValues = EnforcedValueData() + var enforcedValues: List = listOf() @SubscribeEvent fun onRepoReload(event: RepositoryReloadEvent) { - val fixedValues = event.repositoryRoot.resolve("constants/enforced_values.json") + val fixedValues = event.repositoryRoot.resolve("enforced_values") enforcedValues = if (fixedValues.exists()) { - NotEnoughUpdates.INSTANCE.manager.gson.fromJson(fixedValues.readText()) + fixedValues.listFiles() + .filter { + it != null && it.isFile && it.canRead() + } + .map { + NotEnoughUpdates.INSTANCE.manager.gson.fromJson(it.readText()) + }.filter { + NotEnoughUpdates.VERSION_ID in it.affectedVersions + } } else { - EnforcedValueData() + listOf() } if (!event.isFirstLoad) sendPSAs() @@ -76,20 +85,21 @@ object EnforcedConfigValues { } fun sendPSAs() { - val notification = enforcedValues.notificationPSA - if (notification != null) { + val notification = enforcedValues.flatMap { it.notificationPSA ?: emptyList() } + if (notification.isNotEmpty()) { NotificationHandler.displayNotification(notification, true) } - val chat = enforcedValues.chatPSA - if (chat != null) { - for (line in chat) + val chat = enforcedValues.flatMap { it.chatPSA ?: emptyList() } + if (chat.isNotEmpty()) { + for (line in chat) { Utils.addChatMessage(line) + } } } fun enforceOntoConfig(config: Any) { - for (enforcedValue in enforcedValues.enforcedValues) { + for (enforcedValue in enforcedValues.flatMap { it.enforcedValues }) { val shimmy = Shimmy.makeShimmy(config, enforcedValue.path.split(".")) if (shimmy == null) { println("Could not create shimmy for path ${enforcedValue.path}") @@ -104,7 +114,7 @@ object EnforcedConfigValues { } fun isBlockedFromEditing(optionPath: String): Boolean { - return enforcedValues.enforcedValues.any { it.path == optionPath } + return enforcedValues.flatMap { it.enforcedValues }.any { it.path == optionPath } } -- cgit