upgrade to Cloth Config v4

This commit is contained in:
octarine-noise
2021-05-22 19:00:54 +02:00
parent 65c9596a14
commit b7bdd438e4
5 changed files with 28 additions and 30 deletions

View File

@@ -3,8 +3,8 @@ package mods.betterfoliage
import mods.betterfoliage.config.BlockConfig
import mods.betterfoliage.config.MainConfig
import mods.betterfoliage.util.tryDefault
import mods.octarinecore.common.config.clothGuiRoot
import mods.octarinecore.common.config.forgeSpecRoot
import mods.betterfoliage.config.clothGuiRoot
import mods.betterfoliage.config.forgeSpecRoot
import net.minecraft.client.Minecraft
import net.minecraft.client.gui.screen.Screen
import net.minecraft.util.ResourceLocation
@@ -50,7 +50,7 @@ object BetterFoliageMod {
ctx.registerConfig(ModConfig.Type.CLIENT, configSpec)
// Add config GUI extension if Cloth Config is available
val clothLoaded = tryDefault(false) { Class.forName("me.shedaniel.forge.clothconfig2.api.ConfigBuilder"); true }
val clothLoaded = tryDefault(false) { Class.forName("me.shedaniel.clothconfig2.forge.api.ConfigBuilder"); true }
if (clothLoaded) {
logger(this).log(Level.INFO, "Cloth Config found, registering GUI")
ctx.registerExtensionPoint(CONFIGGUIFACTORY) { BiFunction<Minecraft, Screen, Screen> { client, parent ->

View File

@@ -1,14 +1,15 @@
package mods.octarinecore.common.config
package mods.betterfoliage.config
import me.shedaniel.forge.clothconfig2.api.AbstractConfigListEntry
import me.shedaniel.forge.clothconfig2.api.ConfigBuilder
import me.shedaniel.forge.clothconfig2.api.ConfigEntryBuilder
import me.shedaniel.forge.clothconfig2.gui.entries.SubCategoryListEntry
import me.shedaniel.clothconfig2.forge.api.AbstractConfigListEntry
import me.shedaniel.clothconfig2.forge.api.ConfigBuilder
import me.shedaniel.clothconfig2.forge.api.ConfigEntryBuilder
import me.shedaniel.clothconfig2.forge.gui.entries.SubCategoryListEntry
import mods.betterfoliage.util.asText
import net.minecraft.client.gui.screen.Screen
import net.minecraft.client.resources.I18n
import net.minecraft.util.ResourceLocation
import net.minecraftforge.common.ForgeConfigSpec
import java.util.*
import java.util.Optional
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty
@@ -26,12 +27,12 @@ fun DelegatingConfigGroup.clothGuiRoot(
saveAction: ()->Unit
) = ConfigBuilder.create()
.setParentScreen(parentScreen)
.setTitle(I18n.format((prefix + "title").joinToString(".")))
.setTitle(I18n.get((prefix + "title").joinToString(".")).asText())
.setDefaultBackgroundTexture(background)
.setSavingRunnable(saveAction)
.also { builder ->
createClothNode(prefix).value.forEach { rootCategory ->
builder.getOrCreateCategory("main").addEntry(rootCategory)
builder.getOrCreateCategory("main".asText()).addEntry(rootCategory)
}
}
.build()
@@ -79,9 +80,7 @@ fun <T: DelegatingConfigGroup> subNode(factory: ()->T) = object : DelegatingConf
override operator fun provideDelegate(parent: DelegatingConfigGroup, property: KProperty<*>): ReadOnlyProperty<DelegatingConfigGroup, T> {
val child = factory()
parent.children[property.name] = child
return object : ReadOnlyProperty<DelegatingConfigGroup, T> {
override fun getValue(thisRef: DelegatingConfigGroup, property: KProperty<*>) = child
}
return ReadOnlyProperty { _, _ -> child }
}
}
@@ -107,17 +106,16 @@ abstract class CachingConfigProperty<T>(parent: DelegatingConfigGroup, property:
value ?: forgeValue.get().apply { value = this }
}
fun String.translate() = I18n.format(this)
fun String.translateTooltip(lineLength: Int = MAX_LINE_LEN) = ("$this.tooltip").translate().let { tooltip ->
tooltip.splitToSequence(" ").fold(mutableListOf("")) { tooltips, word ->
fun String.translate() = I18n.get(this).asText()
fun String.translateTooltip(lineLength: Int = MAX_LINE_LEN) =
I18n.get("$this.tooltip").splitToSequence(" ").fold(mutableListOf("")) { tooltips, word ->
if (tooltips.last().length + word.length < lineLength) {
tooltips[tooltips.lastIndex] += "$word "
} else {
tooltips.add("$word ")
}
tooltips
}.map { it.trim() }.toTypedArray()
}
}.map { it.trim().asText() }.toTypedArray()
fun boolean(
default: Boolean,
@@ -129,7 +127,7 @@ fun boolean(
override fun createClothNode(prop: CachingConfigProperty<Boolean>, path: List<String>) = ConfigEntryBuilder.create()
.startBooleanToggle(langKey(path).translate(), prop.forgeValue.get())
.setTooltip(langKey(path).let { if (I18n.hasKey("$it.tooltip")) Optional.of(it.translateTooltip()) else Optional.empty() })
.setTooltip(langKey(path).let { if (I18n.exists("$it.tooltip")) Optional.of(it.translateTooltip()) else Optional.empty() })
.setSaveConsumer { prop.forgeValue.set(valueOverride(it)); prop.value = null }
.build()
}
@@ -144,7 +142,7 @@ fun integer(
override fun createClothNode(prop: CachingConfigProperty<Int>, path: List<String>) = ConfigEntryBuilder.create()
.startIntField(langKey(path).translate(), prop.forgeValue.get())
.setTooltip(langKey(path).let { if (I18n.hasKey("$it.tooltip")) Optional.of(it.translateTooltip()) else Optional.empty() })
.setTooltip(langKey(path).let { if (I18n.exists("$it.tooltip")) Optional.of(it.translateTooltip()) else Optional.empty() })
.setMin(min).setMax(max)
.setSaveConsumer { prop.forgeValue.set(valueOverride(it)); prop.value = null }
.build()
@@ -160,7 +158,7 @@ fun double(
override fun createClothNode(prop: CachingConfigProperty<Double>, path: List<String>) = ConfigEntryBuilder.create()
.startDoubleField(langKey(path).translate(), prop.forgeValue.get())
.setTooltip(langKey(path).let { if (I18n.hasKey("$it.tooltip")) Optional.of(it.translateTooltip()) else Optional.empty() })
.setTooltip(langKey(path).let { if (I18n.exists("$it.tooltip")) Optional.of(it.translateTooltip()) else Optional.empty() })
.setMin(min).setMax(max)
.setSaveConsumer { prop.forgeValue.set(valueOverride(it)); prop.value = null }
.build()

View File

@@ -1,12 +1,6 @@
package mods.betterfoliage.config
import mods.betterfoliage.BetterFoliageMod
import mods.octarinecore.common.config.DelegatingConfigGroup
import mods.octarinecore.common.config.boolean
import mods.octarinecore.common.config.double
import mods.octarinecore.common.config.integer
import mods.octarinecore.common.config.recurring
import mods.octarinecore.common.config.subNode
import java.util.Random
fun featureEnable(default: Boolean = true) = boolean(default, langKey = recurring)
@@ -70,6 +64,7 @@ class ConnectedGrassConfig() : DelegatingConfigGroup() {
class RoundLogConfig() : DelegatingConfigGroup() {
val enabled by featureEnable()
val plantsOnly by boolean(true)
val radiusSmall by double(max=0.5, default=0.25)
val radiusLarge by double(max=0.5, default=0.44)
val dimming by double(default = 0.7)

View File

@@ -19,4 +19,7 @@ import net.minecraft.util.text.TextFormatting.GRAY
//fun textComponent(msg: String, color: TextFormatting = GRAY): StringTextComponent {
// val style = Style().apply { this.color = color }
// return StringTextComponent(msg).apply { this.style = style }
//}
//}
val styleGray = Style.EMPTY.applyFormats(GRAY)
fun String.asText() = StringTextComponent(this).setStyle(styleGray)

View File

@@ -171,5 +171,7 @@
"betterfoliage.roundLogs.zProtection": "Z-Protection",
"betterfoliage.roundLogs.zProtection.tooltip": "Amount to scale parallel log connection bits to stop Z-fighting (flickering). Try to set it as high as possible without having glitches.",
"betterfoliage.roundLogs.defaultY": "Default to vertical",
"betterfoliage.roundLogs.defaultY.tooltip": "If true, log blocks where the orientation cannot be determined will be rendered as vertical. Otherwise, they will be rendered as cube blocks."
"betterfoliage.roundLogs.defaultY.tooltip": "If true, log blocks where the orientation cannot be determined will be rendered as vertical. Otherwise, they will be rendered as cube blocks.",
"betterfoliage.roundLogs.plantsOnly": "Plants only",
"betterfoliage.roundLogs.plantsOnly.tooltip": "If true, only blocks with plant materials (wood, grass) will be rounded. If false, all column blocks will be rounded, including stone columns."
}