Add configured compat

[no changelog]
This commit is contained in:
Linnea Gräf
2024-08-31 11:59:47 +02:00
parent d2f240ff0c
commit 816f80f862
19 changed files with 366 additions and 101 deletions

View File

@@ -0,0 +1,39 @@
package moe.nea.firmament.compat.configured
import com.mrcrayfish.configured.api.IConfigEntry
import com.mrcrayfish.configured.api.IConfigValue
import net.minecraft.text.Text
import moe.nea.firmament.gui.config.AllConfigsGui
object BaseConfigNode : IConfigEntry {
override fun getChildren(): List<IConfigEntry> {
return AllConfigsGui.allConfigs.map {
ConfigNode(it)
}
}
override fun isRoot(): Boolean {
return true
}
override fun isLeaf(): Boolean {
return false
}
override fun getValue(): IConfigValue<*>? {
return null
}
override fun getEntryName(): String {
return "Firmament"
}
override fun getTooltip(): Text? {
return null
}
override fun getTranslationKey(): String? {
return null
}
}

View File

@@ -0,0 +1,48 @@
package moe.nea.firmament.compat.configured
import com.mrcrayfish.configured.api.ConfigType
import com.mrcrayfish.configured.api.IConfigEntry
import com.mrcrayfish.configured.api.IModConfig
import com.mrcrayfish.configured.util.ConfigHelper
import java.nio.file.Path
import java.util.function.Consumer
import moe.nea.firmament.Firmament
import moe.nea.firmament.gui.config.ManagedConfig
class ConfigCategory(
val category: ManagedConfig
) : BaseConfig() {
override fun getRoot(): IConfigEntry {
return ConfigNode(category)
}
override fun getTranslationKey(): String? {
return category.translationKey
}
}
abstract class BaseConfig : IModConfig {
override fun update(p0: IConfigEntry) {
ConfigHelper.getChangedValues(p0).forEach {
it as ConfigValue
it.saveValue()
}
}
override fun getType(): ConfigType {
return ConfigType.CLIENT
}
override fun getFileName(): String {
return ""
}
override fun getModId(): String {
return Firmament.MOD_ID
}
override fun loadWorldConfig(p0: Path?, p1: Consumer<IModConfig>?) {
}
}

View File

@@ -0,0 +1,39 @@
package moe.nea.firmament.compat.configured
import com.mrcrayfish.configured.api.IConfigEntry
import com.mrcrayfish.configured.api.IConfigValue
import net.minecraft.text.Text
import moe.nea.firmament.gui.config.ManagedConfig
class ConfigNode(val config: ManagedConfig) : IConfigEntry {
override fun getChildren(): List<IConfigEntry> {
return config.allOptions.map {
ConfigValueNode(it.value)
}
}
override fun isRoot(): Boolean {
return false
}
override fun isLeaf(): Boolean {
return false
}
override fun getValue(): IConfigValue<*>? {
return null
}
override fun getEntryName(): String {
return config.translationKey
}
override fun getTooltip(): Text? {
return null
}
override fun getTranslationKey(): String {
return config.translationKey
}
}

View File

@@ -0,0 +1,72 @@
package moe.nea.firmament.compat.configured
import com.mrcrayfish.configured.api.IConfigValue
import net.minecraft.text.Text
import moe.nea.firmament.gui.config.ManagedOption
class ConfigValue<T: Any>(val option: ManagedOption<T>) : IConfigValue<T> {
var value = option.get()
var initialValue = option.get()
override fun get(): T {
return value
}
override fun set(p0: T) {
this.value = p0
}
override fun getDefault(): T {
return option.default()
}
override fun isDefault(): Boolean {
// TODO: should this be an option in handlers?
return option == option.default()
}
override fun isChanged(): Boolean {
return value != initialValue
}
override fun restore() {
this.value = option.default()
}
override fun getComment(): Text? {
return null
}
override fun getTranslationKey(): String? {
return option.rawLabelText
}
override fun getValidationHint(): Text? {
return null
}
override fun getName(): String {
return ""
}
override fun cleanCache() {
}
override fun requiresWorldRestart(): Boolean {
return false
}
override fun requiresGameRestart(): Boolean {
return false
}
override fun isValid(p0: T): Boolean {
// TODO: should this be validated?
return true
}
fun saveValue() {
option.set(value)
}
}

View File

@@ -0,0 +1,37 @@
package moe.nea.firmament.compat.configured
import com.mrcrayfish.configured.api.IConfigEntry
import com.mrcrayfish.configured.api.IConfigValue
import net.minecraft.text.Text
import moe.nea.firmament.gui.config.ManagedOption
class ConfigValueNode(val option: ManagedOption<*>) : IConfigEntry {
override fun getChildren(): List<IConfigEntry> {
return listOf()
}
override fun isRoot(): Boolean {
return false
}
override fun isLeaf(): Boolean {
return true
}
val value = ConfigValue(option)
override fun getValue(): IConfigValue<*>? {
return value
}
override fun getEntryName(): String {
return option.propertyName
}
override fun getTooltip(): Text? {
return null
}
override fun getTranslationKey(): String? {
return option.rawLabelText
}
}

View File

@@ -0,0 +1,29 @@
package moe.nea.firmament.compat.configured
import com.mrcrayfish.configured.api.IConfigEntry
import com.mrcrayfish.configured.api.IModConfig
import com.mrcrayfish.configured.api.IModConfigProvider
import com.mrcrayfish.configured.api.ModContext
import moe.nea.firmament.Firmament
import moe.nea.firmament.gui.config.AllConfigsGui
/**
* Registered in `fabric.mod.json` at `custom.configured.providers`
*/
class ConfiguredCompat : IModConfigProvider {
override fun getConfigurationsForMod(modContext: ModContext): Set<IModConfig> {
if (modContext.modId != Firmament.MOD_ID) return emptySet()
return buildSet {
add(object : BaseConfig() {
override fun getRoot(): IConfigEntry {
return BaseConfigNode
}
override fun getTranslationKey(): String? {
return "firmament.config.all-configs"
}
})
AllConfigsGui.allConfigs.mapTo(this) { ConfigCategory(it) }
}
}
}

View File

@@ -0,0 +1,22 @@
package moe.nea.firmament.compat.configured
import com.google.auto.service.AutoService
import com.mrcrayfish.configured.integration.CatalogueConfigFactory
import net.fabricmc.loader.api.FabricLoader
import net.minecraft.client.gui.screen.Screen
import moe.nea.firmament.Firmament
import moe.nea.firmament.gui.config.FirmamentConfigScreenProvider
@AutoService(FirmamentConfigScreenProvider::class)
class ConfiguredConfigScreenProvider : FirmamentConfigScreenProvider {
override val key: String
get() = "configured"
override val isEnabled: Boolean
get() = FabricLoader.getInstance().isModLoaded("configured")
override fun open(parent: Screen?): Screen {
return CatalogueConfigFactory.createConfigScreen(
parent,
FabricLoader.getInstance().getModContainer(Firmament.MOD_ID).get())
}
}

View File

@@ -1,3 +1,5 @@
package moe.nea.firmament.compat.sodium
import me.jellysquid.mods.sodium.client.render.SodiumWorldRenderer
import moe.nea.firmament.mixins.accessor.sodium.AccessorSodiumWorldRenderer

View File

@@ -146,7 +146,9 @@ public class AutoDiscoveryPlugin {
*/
private void walkDir(Path classRoot) {
System.out.println("Trying to find mixins from directory");
try (Stream<Path> classes = Files.walk(classRoot.resolve(getMixinBaseDir()))) {
var path = classRoot.resolve(getMixinBaseDir());
if (!Files.exists(path)) return;
try (Stream<Path> classes = Files.walk(path)) {
classes.map(it -> classRoot.relativize(it).toString())
.forEach(this::tryAddMixinClass);
} catch (IOException e) {

View File

@@ -1,5 +1,3 @@
package moe.nea.firmament.gui.config
import io.github.notenoughupdates.moulconfig.observer.ObservableList
@@ -36,10 +34,16 @@ object AllConfigsGui {
}
}
fun makeScreen(parent: Screen? = null): Screen {
fun makeBuiltInScreen(parent: Screen? = null): Screen {
return MoulConfigUtils.loadScreen("config/main", MainMapping(allConfigs), parent)
}
fun makeScreen(parent: Screen? = null): Screen {
val provider = FirmamentConfigScreenProvider.providers.find { it.key == "builtin" }
?: FirmamentConfigScreenProvider.providers.first()
return provider.open(parent)
}
fun showAllGuis() {
setScreenLater(makeScreen())
}

View File

@@ -0,0 +1,14 @@
package moe.nea.firmament.gui.config
import com.google.auto.service.AutoService
import net.minecraft.client.gui.screen.Screen
@AutoService(FirmamentConfigScreenProvider::class)
class BuiltInConfigScreenProvider : FirmamentConfigScreenProvider {
override val key: String
get() = "builtin"
override fun open(parent: Screen?): Screen {
return AllConfigsGui.makeBuiltInScreen(parent)
}
}

View File

@@ -0,0 +1,27 @@
package moe.nea.firmament.gui.config
import java.util.ServiceLoader
import kotlin.streams.asSequence
import net.minecraft.client.gui.screen.Screen
import moe.nea.firmament.Firmament
interface FirmamentConfigScreenProvider {
val key: String
val isEnabled: Boolean get() = true
fun open(parent: Screen?): Screen
companion object {
private val loader = ServiceLoader.load(FirmamentConfigScreenProvider::class.java)
val providers by lazy {
loader.stream().asSequence().mapNotNull { service ->
kotlin.runCatching { service.get() }
.getOrElse {
Firmament.logger.warn("Could not load config provider ${service.type()}", it)
null
}
}.filter { it.isEnabled }.toList()
}
}
}

View File

@@ -145,7 +145,8 @@ abstract class ManagedConfig(override val name: String) : ManagedConfigElement()
latestGuiAppender?.reloadables?.forEach { it() }
}
val labelText = Text.translatable("firmament.config.${name}")
val translationKey get() = "firmament.config.${name}"
val labelText = Text.translatable(translationKey)
fun getConfigEditor(parent: Screen? = null): Screen {
var screen: Screen? = null

View File

@@ -20,6 +20,7 @@
"firmament.poweruser.entity.armor": "Entity Armor:",
"firmament.poweruser.entity.armor.item": " - %s",
"firmament.poweruser.entity.passengers": "%s Passengers",
"firmament.config.all-configs": "- All Configs -",
"firmament.config.anniversary": "Anniversary Features",
"firmament.config.anniversary.shiny-pigs": "Shiny Pigs Tracker",
"firmament.config.anniversary.pig-hud": "Pig Tracker Hud",

View File

@@ -50,6 +50,11 @@
"roughlyenoughitems": ">=${rei_version}"
},
"custom": {
"configured": {
"providers": [
"moe.nea.firmament.compat.configured.ConfiguredCompat"
]
},
"modmenu": {
"links": {
"modmenu.discord": "https://discord.gg/64pFP94AWA"

View File

@@ -0,0 +1,7 @@
{
"required": true,
"plugin": "moe.nea.firmament.init.MixinPlugin",
"package": "moe.nea.firmament.mixins",
"compatibilityLevel": "JAVA_21",
"refmap": "Firmament-refmap.json"
}

View File

@@ -0,0 +1,3 @@
SPDX-FileCopyrightText: 2024 Linnea Gräf <nea@nea.moe>
SPDX-License-Identifier: CC0-1.0