diff options
author | nea <romangraef@gmail.com> | 2022-12-31 18:34:42 +0100 |
---|---|---|
committer | nea <romangraef@gmail.com> | 2023-01-11 22:09:16 +0100 |
commit | 0134c83fbc02147794e3b7134cff6f9b6dc446f6 (patch) | |
tree | d62c943c572d44be3909546745a6fa42f7cfb9a1 /src/main/kotlin/io | |
parent | 49c9345807ed4d13d035e27a0665e2f7c63b0c9e (diff) | |
download | NotEnoughUpdates-0134c83fbc02147794e3b7134cff6f9b6dc446f6.tar.gz NotEnoughUpdates-0134c83fbc02147794e3b7134cff6f9b6dc446f6.tar.bz2 NotEnoughUpdates-0134c83fbc02147794e3b7134cff6f9b6dc446f6.zip |
Add EnforcedConfigValues: Allow disabling options via the repo
Diffstat (limited to 'src/main/kotlin/io')
3 files changed, 229 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..50c45832 --- /dev/null +++ b/src/main/kotlin/io/github/moulberry/notenoughupdates/miscfeatures/EnforcedConfigValues.kt @@ -0,0 +1,111 @@ +/* + * 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.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 + +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 + } + + + var enforcedValues = EnforcedValueData() + + @SubscribeEvent + fun onRepoReload(event: RepositoryReloadEvent) { + val fixedValues = event.repositoryRoot.resolve("constants/enforced_values.json") + enforcedValues = if (fixedValues.exists()) { + NotEnoughUpdates.INSTANCE.manager.gson.fromJson(fixedValues.readText()) + } else { + EnforcedValueData() + } + 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) return + hasSentPSAsOnce = true + sendPSAs() + enforceOntoConfig(NotEnoughUpdates.INSTANCE.config ?: return) + } + + fun sendPSAs() { + val notification = enforcedValues.notificationPSA + if (notification != null) { + NotificationHandler.displayNotification(notification, true) + } + val chat = enforcedValues.chatPSA + if (chat != null) { + for (line in chat) + Utils.addChatMessage(line) + } + } + + + fun enforceOntoConfig(config: Any) { + for (enforcedValue in enforcedValues.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.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>()) +} |