[WIP] initial Fabric port

major package refactoring
This commit is contained in:
octarine-noise
2020-02-05 13:42:48 +01:00
parent 2252fb3b42
commit df50f61b0d
151 changed files with 5181 additions and 5663 deletions

View File

@@ -0,0 +1,36 @@
package mods.betterfoliage.config
import mods.betterfoliage.BetterFoliage
import mods.betterfoliage.util.Invalidator
import mods.betterfoliage.resource.discovery.ConfigurableBlockMatcher
import mods.betterfoliage.resource.discovery.ModelTextureListConfiguration
import net.minecraft.resource.ResourceManager
import net.minecraft.util.Identifier
class BlockConfig {
private val list = mutableListOf<Any>()
val leafBlocks = blocks("leaves_blocks_default.cfg")
val leafModels = models("leaves_models_default.cfg")
val grassBlocks = blocks("grass_blocks_default.cfg")
val grassModels = models("grass_models_default.cfg")
val mycelium = blocks("mycelium_blocks_default.cfg")
// val dirt = blocks("dirt_default.cfg")
val crops = blocks("crop_default.cfg")
val logBlocks = blocks("log_blocks_default.cfg")
val logModels = models("log_models_default.cfg")
val sand = blocks("sand_default.cfg")
val lilypad = blocks("lilypad_default.cfg")
val cactus = blocks("cactus_default.cfg")
val netherrack = blocks("netherrack_blocks_default.cfg")
private fun blocks(cfgName: String) = ConfigurableBlockMatcher(BetterFoliage.logDetail, Identifier(BetterFoliage.MOD_ID, cfgName)).apply { list.add(this) }
private fun models(cfgName: String) = ModelTextureListConfiguration(BetterFoliage.logDetail, Identifier(BetterFoliage.MOD_ID, cfgName)).apply { list.add(this) }
fun reloadConfig(manager: ResourceManager) {
list.forEach { when(it) {
is ConfigurableBlockMatcher -> it.readDefaults(manager)
is ModelTextureListConfiguration -> it.readDefaults(manager)
} }
}
}

View File

@@ -0,0 +1,137 @@
package mods.betterfoliage.config
import me.shedaniel.clothconfig2.api.AbstractConfigListEntry
import me.shedaniel.clothconfig2.api.ConfigEntryBuilder
import me.shedaniel.clothconfig2.gui.entries.SubCategoryListEntry
import me.zeroeightsix.fiber.builder.ConfigValueBuilder
import me.zeroeightsix.fiber.tree.ConfigLeaf
import me.zeroeightsix.fiber.tree.ConfigNode
import me.zeroeightsix.fiber.tree.ConfigValue
import net.minecraft.client.resource.language.I18n
import java.util.*
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty
const val MAX_LINE_LEN = 30
sealed class DelegatingConfigNode<N: ConfigLeaf>(val fiberNode: N) {
abstract fun createClothNode(names: List<String>): AbstractConfigListEntry<*>
}
abstract class DelegatingConfigValue<T>(fiberNode: ConfigValue<T>) : DelegatingConfigNode<ConfigValue<T>>(fiberNode), ReadOnlyProperty<DelegatingConfigGroup, T>
open class DelegatingConfigGroup(fiberNode: ConfigNode) : DelegatingConfigNode<ConfigNode>(fiberNode) {
val children = mutableListOf<DelegatingConfigNode<*>>()
override fun createClothNode(names: List<String>): SubCategoryListEntry {
val builder = ConfigEntryBuilder.create()
.startSubCategory(names.joinToString(".").translate())
.setTooltip(*names.joinToString(".").translateTooltip())
.setExpended(false)
children.forEach { builder.add(it.createClothNode(names + it.fiberNode.name!!)) }
return builder.build()
}
operator fun get(name: String) = children.find { it.fiberNode.name == name }
}
interface DelegatingConfigGroupFactory<T> {
operator fun provideDelegate(parent: DelegatingConfigGroup, property: KProperty<*>): ReadOnlyProperty<DelegatingConfigGroup, T>
}
fun <T: DelegatingConfigGroup> subNode(factory: (ConfigNode)->T) = object : DelegatingConfigGroupFactory<T> {
override operator fun provideDelegate(parent: DelegatingConfigGroup, property: KProperty<*>): ReadOnlyProperty<DelegatingConfigGroup, T> {
val childNode = ConfigNode(property.name, null)
val configGroup = factory(childNode)
parent.fiberNode.items.add(childNode)
parent.children.add(configGroup)
return object : ReadOnlyProperty<DelegatingConfigGroup, T> {
override fun getValue(thisRef: DelegatingConfigGroup, property: KProperty<*>) = configGroup
}
}
}
interface DelegatingConfigValueFactory<T> {
fun createFiberNode(parent: ConfigNode, name: String): ConfigValue<T>
fun createClothNode(node: ConfigValue<T>, names: List<String>): AbstractConfigListEntry<T>
operator fun provideDelegate(parent: DelegatingConfigGroup, property: KProperty<*>): ReadOnlyProperty<DelegatingConfigGroup, T> {
return object : DelegatingConfigValue<T>(createFiberNode(parent.fiberNode, property.name)) {
override fun createClothNode(names: List<String>) = createClothNode(fiberNode, names)
override fun getValue(thisRef: DelegatingConfigGroup, property: KProperty<*>) = fiberNode.value!!
}.apply { parent.children.add(this) }
}
}
fun String.translate() = I18n.translate(this)
fun String.translateTooltip(lineLength: Int = MAX_LINE_LEN) = ("$this.tooltip").translate().let { tooltip ->
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()
}
fun boolean(
default: Boolean,
langKey: (List<String>)->String = { it.joinToString(".") },
valueOverride: (Boolean)->Boolean = { it }
) = object : DelegatingConfigValueFactory<Boolean> {
override fun createFiberNode(parent: ConfigNode, name: String) = ConfigValueBuilder(Boolean::class.java)
.withName(name)
.withParent(parent)
.withDefaultValue(default)
.build()
override fun createClothNode(node: ConfigValue<Boolean>, names: List<String>) = ConfigEntryBuilder.create()
.startBooleanToggle(langKey(names).translate(), node.value!!)
.setTooltip(langKey(names).let { if (I18n.hasTranslation("$it.tooltip")) Optional.of(it.translateTooltip()) else Optional.empty() })
.setSaveConsumer { node.value = valueOverride(it) }
.build()
}
fun integer(
default: Int, min: Int, max: Int,
langKey: (List<String>)->String = { it.joinToString(".") },
valueOverride: (Int)->Int = { it }
) = object : DelegatingConfigValueFactory<Int> {
override fun createFiberNode(parent: ConfigNode, name: String) = ConfigValueBuilder(Int::class.java)
.withName(name)
.withParent(parent)
.withDefaultValue(default)
.constraints().minNumerical(min).maxNumerical(max).finish()
.build()
override fun createClothNode(node: ConfigValue<Int>, names: List<String>) = ConfigEntryBuilder.create()
.startIntField(langKey(names).translate(), node.value!!)
.setTooltip(langKey(names).let { if (I18n.hasTranslation("$it.tooltip")) Optional.of(it.translateTooltip()) else Optional.empty() })
.setMin(min).setMax(max)
.setSaveConsumer { node.value = valueOverride(it) }
.build()
}
fun double(
default: Double, min: Double, max: Double,
langKey: (List<String>)->String = { it.joinToString(".") },
valueOverride: (Double)->Double = { it }
) = object : DelegatingConfigValueFactory<Double> {
override fun createFiberNode(parent: ConfigNode, name: String) = ConfigValueBuilder(Double::class.java)
.withName(name)
.withParent(parent)
.withDefaultValue(default)
.constraints().minNumerical(min).maxNumerical(max).finish()
.build()
override fun createClothNode(node: ConfigValue<Double>, names: List<String>) = ConfigEntryBuilder.create()
.startDoubleField(langKey(names).translate(), node.value!!)
.setTooltip(langKey(names).let { if (I18n.hasTranslation("$it.tooltip")) Optional.of(it.translateTooltip()) else Optional.empty() })
.setMin(min).setMax(max)
.setSaveConsumer { node.value = valueOverride(it) }
.build()
}
val recurring = { names: List<String> -> "${names.first()}.${names.last()}" }
fun fakeCategory(name: String) = { names: List<String> ->
(listOf(names.first(), name) + names.drop(1)).joinToString(".")
}

View File

@@ -0,0 +1,154 @@
package mods.betterfoliage.config
import me.zeroeightsix.fiber.tree.ConfigNode
import java.util.*
interface PopulationConfigData {
val enabled: Boolean
val population: Int
fun enabled(random: Random) = random.nextInt(64) < population && enabled
}
fun population(default: Int) = integer(default, min = 0, max = 64, langKey = recurring)
class MainConfig : DelegatingConfigGroup(ConfigNode("root", null)) {
val enabled by boolean(true, langKey = fakeCategory("global"))
val nVidia by boolean(true, langKey = fakeCategory("global"))
val leaves by subNode { LeavesConfig(it) }
val shortGrass by subNode { ShortGrassConfig(it) }
val connectedGrass by subNode { ConnectedGrassConfig(it) }
val roundLogs by subNode { RoundLogConfig(it) }
val cactus by subNode { CactusConfig(it) }
val lilypad by subNode { LilypadConfig(it) }
val reed by subNode { ReedConfig(it) }
val algae by subNode { AlgaeConfig(it) }
val coral by subNode { CoralConfig(it) }
val netherrack by subNode { NetherrackConfig(it) }
val fallingLeaves by subNode { FallingLeavesConfig(it) }
val risingSoul by subNode { RisingSoulConfig(it) }
}
class LeavesConfig(node: ConfigNode) : DelegatingConfigGroup(node) {
val enabled by boolean(true, langKey = recurring)
val snowEnabled by boolean(true)
val dense by boolean(false)
val hideInternal by boolean(true)
val hOffset by double(0.2, min = 0.0, max = 0.4, langKey = recurring)
val vOffset by double(0.1, min = 0.0, max = 0.4, langKey = recurring)
val size by double(1.4, min = 0.75, max = 2.5, langKey = recurring)
}
class ShortGrassConfig(node: ConfigNode) : DelegatingConfigGroup(node), PopulationConfigData {
override val enabled by boolean(true, langKey = recurring)
val myceliumEnabled by boolean(true)
val snowEnabled by boolean(true)
val hOffset by double(0.2, min = 0.0, max = 0.4, langKey = recurring)
val heightMin by double(0.6, min = 0.1, max = 2.5, langKey = recurring)
val heightMax by double(0.6, min = 0.1, max = 2.5, langKey = recurring) { it.coerceAtLeast(heightMin) }
val size by double(1.0, min = 0.5, max = 1.5, langKey = recurring)
override val population by population(64)
val useGenerated by boolean(false)
val shaderWind by boolean(true, langKey = recurring)
val saturationThreshold by double(0.1, min = 0.0, max = 1.0)
}
class ConnectedGrassConfig(node: ConfigNode) : DelegatingConfigGroup(node) {
val enabled by boolean(true, langKey = recurring)
val snowEnabled by boolean(true)
}
class RoundLogConfig(node: ConfigNode) : DelegatingConfigGroup(node) {
val enabled by boolean(true, langKey = recurring)
val defaultY by boolean(false)
val connectSolids by boolean(false)
val lenientConnect by boolean(true)
val connectPerpendicular by boolean(true)
val connectGrass by boolean(true)
val radiusSmall by double(0.25, min = 0.0, max = 0.5)
val radiusLarge by double(0.25, min = 0.0, max = 0.5) { it.coerceAtLeast(radiusSmall) }
val dimming by double(0.7, min = 0.0, max = 1.0)
val zProtection by double(0.99, min = 0.9, max = 1.0)
}
class CactusConfig(node: ConfigNode) : DelegatingConfigGroup(node) {
val enabled by boolean(true, langKey = recurring)
val size by double(1.3, min = 0.5, max = 1.5, langKey = recurring)
val sizeVariation by double(0.1, min = 0.0, max = 0.5)
val hOffset by double(0.1, min = 0.0, max = 0.5, langKey = recurring)
}
class LilypadConfig(node: ConfigNode) : DelegatingConfigGroup(node), PopulationConfigData {
override val enabled by boolean(true, langKey = recurring)
val hOffset by double(0.1, min = 0.0, max = 0.25, langKey = recurring)
override val population by population(16)
}
class ReedConfig(node: ConfigNode) : DelegatingConfigGroup(node), PopulationConfigData {
override val enabled by boolean(true, langKey = recurring)
val hOffset by double(0.2, min = 0.0, max = 0.4, langKey = recurring)
val heightMin by double(1.7, min = 1.5, max = 3.0, langKey = recurring)
val heightMax by double(2.2, min = 1.5, max = 3.0, langKey = recurring) { it.coerceAtLeast(heightMin) }
override val population by population(32)
val minBiomeTemp by double(0.4, min = 0.0, max = 2.0)
val minBiomeRainfall by double(0.4, min = 0.0, max = 1.0)
val shaderWind by boolean(true, langKey = recurring)
}
class AlgaeConfig(node: ConfigNode) : DelegatingConfigGroup(node), PopulationConfigData {
override val enabled by boolean(true, langKey = recurring)
val hOffset by double(0.1, min = 0.0, max = 0.4, langKey = recurring)
val size by double(1.0, min = 0.5, max = 1.5, langKey = recurring)
val heightMin by double(0.5, min = 0.1, max = 1.0, langKey = recurring)
val heightMax by double(0.5, min = 0.1, max = 1.0, langKey = recurring) { it.coerceAtLeast(heightMin) }
override val population by population(48)
val shaderWind by boolean(true, langKey = recurring)
}
class CoralConfig(node: ConfigNode) : DelegatingConfigGroup(node), PopulationConfigData {
override val enabled by boolean(true, langKey = recurring)
val shallowWater by boolean(false)
val hOffset by double(0.2, min = 0.0, max = 0.4, langKey = recurring)
val vOffset by double(0.1, min = 0.0, max = 0.4, langKey = recurring)
val size by double(0.7, min = 0.5, max = 1.5, langKey = recurring)
val crustSize by double(1.4, min = 0.5, max = 1.5)
val chance by integer(32, min = 0, max = 64)
override val population by population(48)
}
class NetherrackConfig(node: ConfigNode) : DelegatingConfigGroup(node) {
val enabled by boolean(true, langKey = recurring)
val hOffset by double(0.2, min = 0.0, max = 0.4, langKey = recurring)
val size by double(1.0, min = 0.5, max = 1.5, langKey = recurring)
val heightMin by double(0.6, min = 0.5, max = 1.5, langKey = recurring)
val heightMax by double(0.8, min = 0.5, max = 1.5, langKey = recurring) { it.coerceAtLeast(heightMin) }
}
class FallingLeavesConfig(node: ConfigNode) : DelegatingConfigGroup(node) {
val enabled by boolean(true, langKey = recurring)
val speed by double(0.05, min = 0.01, max = 0.15)
val windStrength by double(0.5, min = 0.1, max = 2.0)
val stormStrength by double(0.8, min = 0.1, max = 2.0) { it.coerceAtLeast(windStrength) }
val size by double(0.75, min = 0.25, max = 1.5)
val chance by double(0.05, min = 0.001, max = 1.0)
val perturb by double(0.25, min = 0.01, max = 1.0)
val lifetime by double(7.5, min = 1.0, max = 15.0)
}
class RisingSoulConfig(node: ConfigNode) : DelegatingConfigGroup(node) {
val enabled by boolean(true, langKey = recurring)
val chance by double(0.02, min = 0.001, max = 1.0)
val perturb by double(0.05, min = 0.01, max = 0.25)
val headSize by double(1.0, min = 0.25, max = 1.5)
val trailSize by double(0.75, min = 0.25, max = 1.5)
val opacity by double(0.5, min = 0.05, max = 1.0)
val sizeDecay by double(0.97, min = 0.5, max = 1.0)
val opacityDecay by double(0.97, min = 0.5, max = 1.0)
val lifetime by double(4.0, min = 1.0, max = 15.0)
val trailLength by integer(48, min = 2, max = 128)
val trailDensity by integer(3, min = 1, max = 16)
}