aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin
diff options
context:
space:
mode:
authorRoman / Linnea Gräf <roman.graef@gmail.com>2023-01-31 10:19:39 +0100
committerGitHub <noreply@github.com>2023-01-31 10:19:39 +0100
commit291860435d5487562b122aaddbe57c178a8734de (patch)
tree3f3974a581390c22ebadc00c7774605629557440 /src/main/kotlin
parent4465c640cb5fa9a69c0b8c38f63aba6f571a4e8d (diff)
downloadNotEnoughUpdates-291860435d5487562b122aaddbe57c178a8734de.tar.gz
NotEnoughUpdates-291860435d5487562b122aaddbe57c178a8734de.tar.bz2
NotEnoughUpdates-291860435d5487562b122aaddbe57c178a8734de.zip
Add EnforcedConfigValues: Allow disabling options via the repo (#535)
* Add EnforcedConfigValues: Allow disabling options via the repo * EnforcedConfigValues: Allow filtering for mod version * nitpicking
Diffstat (limited to 'src/main/kotlin')
-rw-r--r--src/main/kotlin/io/github/moulberry/notenoughupdates/miscfeatures/EnforcedConfigValues.kt123
-rw-r--r--src/main/kotlin/io/github/moulberry/notenoughupdates/util/Shimmy.kt85
-rw-r--r--src/main/kotlin/io/github/moulberry/notenoughupdates/util/kotlin/gson.kt33
3 files changed, 241 insertions, 0 deletions
diff --git a/src/main/kotlin/io/github/moulberry/notenoughupdates/miscfeatures/EnforcedConfigValues.kt b/src/main/kotlin/io/github/moulberry/notenoughupdates/miscfeatures/EnforcedConfigValues.kt
new file mode 100644
index 00000000..7351a4a0
--- /dev/null
+++ b/src/main/kotlin/io/github/moulberry/notenoughupdates/miscfeatures/EnforcedConfigValues.kt
@@ -0,0 +1,123 @@
+/*
+ * Copyright (C) 2022 Linnea Gräf
+ *
+ * This file is part of NotEnoughUpdates.
+ *
+ * NotEnoughUpdates is free software: you can redistribute it
+ * and/or modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * NotEnoughUpdates is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with NotEnoughUpdates. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package io.github.moulberry.notenoughupdates.miscfeatures
+
+import com.google.gson.JsonElement
+import io.github.moulberry.notenoughupdates.NotEnoughUpdates
+import io.github.moulberry.notenoughupdates.autosubscribe.NEUAutoSubscribe
+import io.github.moulberry.notenoughupdates.events.RepositoryReloadEvent
+import io.github.moulberry.notenoughupdates.util.NotificationHandler
+import io.github.moulberry.notenoughupdates.util.Shimmy
+import io.github.moulberry.notenoughupdates.util.Utils
+import io.github.moulberry.notenoughupdates.util.kotlin.fromJson
+import net.minecraft.client.Minecraft
+import net.minecraftforge.client.event.GuiOpenEvent
+import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
+import net.minecraftforge.fml.common.gameevent.TickEvent
+
+@NEUAutoSubscribe
+object EnforcedConfigValues {
+
+ class EnforcedValue {
+ // lateinit var because we use gson (instead of kotlinx.serialization which can handle data classes)
+ lateinit var path: String
+ lateinit var value: JsonElement
+ }
+
+ class EnforcedValueData {
+ var enforcedValues: List<EnforcedValue> = listOf()
+ var notificationPSA: List<String>? = null
+ var chatPSA: List<String>? = null
+ lateinit var affectedVersions: List<Int>
+ }
+
+
+ var enforcedValues: List<EnforcedValueData> = listOf()
+
+ @SubscribeEvent
+ fun onRepoReload(event: RepositoryReloadEvent) {
+ val fixedValues = event.repositoryRoot.resolve("enforced_values")
+ enforcedValues = if (fixedValues.exists()) {
+ fixedValues.listFiles()
+ .filter {
+ it != null && it.isFile && it.canRead()
+ }
+ .map {
+ NotEnoughUpdates.INSTANCE.manager.gson.fromJson<EnforcedValueData>(it.readText())
+ }.filter {
+ NotEnoughUpdates.VERSION_ID in it.affectedVersions
+ }
+ } else {
+ listOf()
+ }
+ if (!event.isFirstLoad)
+ sendPSAs()
+ }
+
+ @SubscribeEvent
+ fun onGuiClose(event: GuiOpenEvent) {
+ enforceOntoConfig(NotEnoughUpdates.INSTANCE.config ?: return)
+ }
+
+ var hasSentPSAsOnce = false
+
+ @SubscribeEvent
+ fun onTick(tickEvent: TickEvent.ClientTickEvent) {
+ if (hasSentPSAsOnce || Minecraft.getMinecraft().thePlayer == null || !NotEnoughUpdates.INSTANCE.isOnSkyblock) return
+ hasSentPSAsOnce = true
+ sendPSAs()
+ enforceOntoConfig(NotEnoughUpdates.INSTANCE.config ?: return)
+ }
+
+ fun sendPSAs() {
+ val notification = enforcedValues.flatMap { it.notificationPSA ?: emptyList() }
+ if (notification.isNotEmpty()) {
+ NotificationHandler.displayNotification(notification, true)
+ }
+ 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.flatMap { it.enforcedValues }) {
+ val shimmy = Shimmy.makeShimmy(config, enforcedValue.path.split("."))
+ if (shimmy == null) {
+ println("Could not create shimmy for path ${enforcedValue.path}")
+ continue
+ }
+ val currentValue = shimmy.getJson()
+ if (currentValue != enforcedValue.value) {
+ println("Resetting ${enforcedValue.path} to ${enforcedValue.value} from $currentValue")
+ shimmy.setJson(enforcedValue.value)
+ }
+ }
+ }
+
+ fun isBlockedFromEditing(optionPath: String): Boolean {
+ return enforcedValues.flatMap { it.enforcedValues }.any { it.path == optionPath }
+ }
+
+
+}
diff --git a/src/main/kotlin/io/github/moulberry/notenoughupdates/util/Shimmy.kt b/src/main/kotlin/io/github/moulberry/notenoughupdates/util/Shimmy.kt
new file mode 100644
index 00000000..37cdd3ac
--- /dev/null
+++ b/src/main/kotlin/io/github/moulberry/notenoughupdates/util/Shimmy.kt
@@ -0,0 +1,85 @@
+/*
+ * Copyright (C) 2022 Linnea Gräf
+ *
+ * This file is part of NotEnoughUpdates.
+ *
+ * NotEnoughUpdates is free software: you can redistribute it
+ * and/or modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * NotEnoughUpdates is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with NotEnoughUpdates. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package io.github.moulberry.notenoughupdates.util
+
+import com.google.gson.Gson
+import com.google.gson.JsonElement
+import java.lang.reflect.Field
+
+class Shimmy private constructor(
+ val source: Any,
+ val field: Field,
+) {
+ companion object {
+ val gson = Gson()
+ private fun shimmy(source: Any?, fieldName: String): Any? {
+ if (source == null) return null
+ return try {
+ val declaredField = source.javaClass.getDeclaredField(fieldName)
+ declaredField.isAccessible = true
+ declaredField.get(source)
+ } catch (e: NoSuchFieldException) {
+ null
+ }
+ }
+
+ @JvmStatic
+ fun makeShimmy(source: Any?, path: List<String>): Shimmy? {
+ if (path.isEmpty())
+ return null
+ var source = source
+ for (part in path.dropLast(1)) {
+ source = shimmy(source, part)
+ }
+ if (source == null) return null
+ val lastName = path.last()
+ return try {
+ val field = source.javaClass.getDeclaredField(lastName)
+ field.isAccessible = true
+ Shimmy(
+ source,
+ field,
+ )
+ } catch (e: NoSuchFieldException) {
+ null
+ }
+ }
+
+ }
+
+ val clazz: Class<*> = field.type
+ fun get(): Any? {
+ return field.get(source)
+ }
+
+ fun set(value: Any?) {
+ field.set(source, value)
+ }
+
+ fun getJson(): JsonElement {
+ return gson.toJsonTree(get())
+ }
+
+ fun setJson(element: JsonElement) {
+ set(gson.fromJson(element, clazz))
+ }
+
+
+}
diff --git a/src/main/kotlin/io/github/moulberry/notenoughupdates/util/kotlin/gson.kt b/src/main/kotlin/io/github/moulberry/notenoughupdates/util/kotlin/gson.kt
new file mode 100644
index 00000000..00c3d729
--- /dev/null
+++ b/src/main/kotlin/io/github/moulberry/notenoughupdates/util/kotlin/gson.kt
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 2022 NotEnoughUpdates contributors
+ *
+ * This file is part of NotEnoughUpdates.
+ *
+ * NotEnoughUpdates is free software: you can redistribute it
+ * and/or modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * NotEnoughUpdates is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with NotEnoughUpdates. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package io.github.moulberry.notenoughupdates.util.kotlin
+
+import com.google.gson.Gson
+import com.google.gson.reflect.TypeToken
+import java.lang.reflect.Type
+
+
+inline fun <reified T : Any> typeToken(): Type {
+ return (object : TypeToken<T>() {}).type
+}
+
+inline fun <reified T : Any> Gson.fromJson(string: String): T {
+ return fromJson(string, typeToken<T>())
+}