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.BlockConfig
import mods.betterfoliage.config.MainConfig import mods.betterfoliage.config.MainConfig
import mods.betterfoliage.util.tryDefault import mods.betterfoliage.util.tryDefault
import mods.octarinecore.common.config.clothGuiRoot import mods.betterfoliage.config.clothGuiRoot
import mods.octarinecore.common.config.forgeSpecRoot import mods.betterfoliage.config.forgeSpecRoot
import net.minecraft.client.Minecraft import net.minecraft.client.Minecraft
import net.minecraft.client.gui.screen.Screen import net.minecraft.client.gui.screen.Screen
import net.minecraft.util.ResourceLocation import net.minecraft.util.ResourceLocation
@@ -50,7 +50,7 @@ object BetterFoliageMod {
ctx.registerConfig(ModConfig.Type.CLIENT, configSpec) ctx.registerConfig(ModConfig.Type.CLIENT, configSpec)
// Add config GUI extension if Cloth Config is available // 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) { if (clothLoaded) {
logger(this).log(Level.INFO, "Cloth Config found, registering GUI") logger(this).log(Level.INFO, "Cloth Config found, registering GUI")
ctx.registerExtensionPoint(CONFIGGUIFACTORY) { BiFunction<Minecraft, Screen, Screen> { client, parent -> 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.clothconfig2.forge.api.AbstractConfigListEntry
import me.shedaniel.forge.clothconfig2.api.ConfigBuilder import me.shedaniel.clothconfig2.forge.api.ConfigBuilder
import me.shedaniel.forge.clothconfig2.api.ConfigEntryBuilder import me.shedaniel.clothconfig2.forge.api.ConfigEntryBuilder
import me.shedaniel.forge.clothconfig2.gui.entries.SubCategoryListEntry import me.shedaniel.clothconfig2.forge.gui.entries.SubCategoryListEntry
import mods.betterfoliage.util.asText
import net.minecraft.client.gui.screen.Screen import net.minecraft.client.gui.screen.Screen
import net.minecraft.client.resources.I18n import net.minecraft.client.resources.I18n
import net.minecraft.util.ResourceLocation import net.minecraft.util.ResourceLocation
import net.minecraftforge.common.ForgeConfigSpec import net.minecraftforge.common.ForgeConfigSpec
import java.util.* import java.util.Optional
import kotlin.properties.ReadOnlyProperty import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty import kotlin.reflect.KProperty
@@ -26,12 +27,12 @@ fun DelegatingConfigGroup.clothGuiRoot(
saveAction: ()->Unit saveAction: ()->Unit
) = ConfigBuilder.create() ) = ConfigBuilder.create()
.setParentScreen(parentScreen) .setParentScreen(parentScreen)
.setTitle(I18n.format((prefix + "title").joinToString("."))) .setTitle(I18n.get((prefix + "title").joinToString(".")).asText())
.setDefaultBackgroundTexture(background) .setDefaultBackgroundTexture(background)
.setSavingRunnable(saveAction) .setSavingRunnable(saveAction)
.also { builder -> .also { builder ->
createClothNode(prefix).value.forEach { rootCategory -> createClothNode(prefix).value.forEach { rootCategory ->
builder.getOrCreateCategory("main").addEntry(rootCategory) builder.getOrCreateCategory("main".asText()).addEntry(rootCategory)
} }
} }
.build() .build()
@@ -79,9 +80,7 @@ fun <T: DelegatingConfigGroup> subNode(factory: ()->T) = object : DelegatingConf
override operator fun provideDelegate(parent: DelegatingConfigGroup, property: KProperty<*>): ReadOnlyProperty<DelegatingConfigGroup, T> { override operator fun provideDelegate(parent: DelegatingConfigGroup, property: KProperty<*>): ReadOnlyProperty<DelegatingConfigGroup, T> {
val child = factory() val child = factory()
parent.children[property.name] = child parent.children[property.name] = child
return object : ReadOnlyProperty<DelegatingConfigGroup, T> { return ReadOnlyProperty { _, _ -> child }
override fun getValue(thisRef: DelegatingConfigGroup, property: KProperty<*>) = child
}
} }
} }
@@ -107,17 +106,16 @@ abstract class CachingConfigProperty<T>(parent: DelegatingConfigGroup, property:
value ?: forgeValue.get().apply { value = this } value ?: forgeValue.get().apply { value = this }
} }
fun String.translate() = I18n.format(this) fun String.translate() = I18n.get(this).asText()
fun String.translateTooltip(lineLength: Int = MAX_LINE_LEN) = ("$this.tooltip").translate().let { tooltip -> fun String.translateTooltip(lineLength: Int = MAX_LINE_LEN) =
tooltip.splitToSequence(" ").fold(mutableListOf("")) { tooltips, word -> I18n.get("$this.tooltip").splitToSequence(" ").fold(mutableListOf("")) { tooltips, word ->
if (tooltips.last().length + word.length < lineLength) { if (tooltips.last().length + word.length < lineLength) {
tooltips[tooltips.lastIndex] += "$word " tooltips[tooltips.lastIndex] += "$word "
} else { } else {
tooltips.add("$word ") tooltips.add("$word ")
} }
tooltips tooltips
}.map { it.trim() }.toTypedArray() }.map { it.trim().asText() }.toTypedArray()
}
fun boolean( fun boolean(
default: Boolean, default: Boolean,
@@ -129,7 +127,7 @@ fun boolean(
override fun createClothNode(prop: CachingConfigProperty<Boolean>, path: List<String>) = ConfigEntryBuilder.create() override fun createClothNode(prop: CachingConfigProperty<Boolean>, path: List<String>) = ConfigEntryBuilder.create()
.startBooleanToggle(langKey(path).translate(), prop.forgeValue.get()) .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 } .setSaveConsumer { prop.forgeValue.set(valueOverride(it)); prop.value = null }
.build() .build()
} }
@@ -144,7 +142,7 @@ fun integer(
override fun createClothNode(prop: CachingConfigProperty<Int>, path: List<String>) = ConfigEntryBuilder.create() override fun createClothNode(prop: CachingConfigProperty<Int>, path: List<String>) = ConfigEntryBuilder.create()
.startIntField(langKey(path).translate(), prop.forgeValue.get()) .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) .setMin(min).setMax(max)
.setSaveConsumer { prop.forgeValue.set(valueOverride(it)); prop.value = null } .setSaveConsumer { prop.forgeValue.set(valueOverride(it)); prop.value = null }
.build() .build()
@@ -160,7 +158,7 @@ fun double(
override fun createClothNode(prop: CachingConfigProperty<Double>, path: List<String>) = ConfigEntryBuilder.create() override fun createClothNode(prop: CachingConfigProperty<Double>, path: List<String>) = ConfigEntryBuilder.create()
.startDoubleField(langKey(path).translate(), prop.forgeValue.get()) .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) .setMin(min).setMax(max)
.setSaveConsumer { prop.forgeValue.set(valueOverride(it)); prop.value = null } .setSaveConsumer { prop.forgeValue.set(valueOverride(it)); prop.value = null }
.build() .build()

View File

@@ -1,12 +1,6 @@
package mods.betterfoliage.config package mods.betterfoliage.config
import mods.betterfoliage.BetterFoliageMod 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 import java.util.Random
fun featureEnable(default: Boolean = true) = boolean(default, langKey = recurring) fun featureEnable(default: Boolean = true) = boolean(default, langKey = recurring)
@@ -70,6 +64,7 @@ class ConnectedGrassConfig() : DelegatingConfigGroup() {
class RoundLogConfig() : DelegatingConfigGroup() { class RoundLogConfig() : DelegatingConfigGroup() {
val enabled by featureEnable() val enabled by featureEnable()
val plantsOnly by boolean(true)
val radiusSmall by double(max=0.5, default=0.25) val radiusSmall by double(max=0.5, default=0.25)
val radiusLarge by double(max=0.5, default=0.44) val radiusLarge by double(max=0.5, default=0.44)
val dimming by double(default = 0.7) 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 { //fun textComponent(msg: String, color: TextFormatting = GRAY): StringTextComponent {
// val style = Style().apply { this.color = color } // val style = Style().apply { this.color = color }
// return StringTextComponent(msg).apply { this.style = style } // 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": "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.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": "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."
} }