Skip to content

Commit

Permalink
add new entry types for EntryBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
Treetrain1 authored Nov 9, 2023
1 parent d62b3eb commit b0f9716
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 25 deletions.
12 changes: 10 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,13 @@ Make sure to clear this after each release
Put changelog here:

-----------------
- Added support for an optional `tag` entry on Recipe results
- In the form of a `CompoundTag`
- Added support for Cloth Config's `Dependency` in `TypedEntryUtils`
- Added support for lists in `EntryBuilder`
- Via the new `StringList`, `IntList`, `LongList`, `FloatList`, and `DoubleList` data classes
- Kotlin only
- Added support for enums in `EntryBuilder`
- Via the new `EnumEntry` data class
- Kotlin only
- Added support for array selectors in `EntryBuilder`
- Via the new `SelectorEntry` data class
- Kotlin only
1 change: 0 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ plugins {
`java-library`
java
kotlin("jvm") version("1.9.20")
kotlin("plugin.serialization") version("1.9.20")
}

val minecraft_version: String by project
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ public final class LightningOverrideMixin {
return false;
}
Holder<Biome> biome = level.getBiome(position);
return ((biome.value().hasPrecipitation() && biome.value().warmEnoughToRain(position)) || biome.is(FrozenBiomeTags.CAN_LIGHTNING_OVERRIDE)) && !biome.is(FrozenBiomeTags.CANNOT_LIGHTNING_OVERRIDE);
return (
(biome.value().hasPrecipitation() && biome.value().warmEnoughToRain(position))
|| biome.is(FrozenBiomeTags.CAN_LIGHTNING_OVERRIDE)
) && !biome.is(FrozenBiomeTags.CANNOT_LIGHTNING_OVERRIDE);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,51 @@ data class EntryBuilder<T>(
else -> throw IllegalArgumentException("Unsupported slider type: ${usedValue.type}")
}
}
is StringList -> {
val consumer = saveConsumer as? Consumer<StringList> ?: consumerError()
entryBuilder.startStrList(title, usedValue.list)
.setDefaultValue((defaultValue as StringList).list)
.setSaveConsumer { strList -> consumer.accept(StringList(strList)) }
.apply {
tooltip?.let { tooltip -> this.setTooltip(tooltip) }
requiresRestart?.let { requiresRestart -> this.requireRestart(requiresRestart) }
requirement?.let { requirement -> this.setRequirement(requirement) }
}
}
is IntList -> {
val consumer = saveConsumer as? Consumer<IntList> ?: consumerError()
entryBuilder.startIntList(title, usedValue.list)
.setDefaultValue((defaultValue as IntList).list)
.setSaveConsumer { intList -> consumer.accept(IntList(intList)) }
.apply {
tooltip?.let { tooltip -> this.setTooltip(tooltip) }
requiresRestart?.let { requiresRestart -> this.requireRestart(requiresRestart) }
requirement?.let { requirement -> this.setRequirement(requirement) }
}
}
is LongList -> {
val consumer = saveConsumer as? Consumer<LongList> ?: consumerError()
entryBuilder.startLongList(title, usedValue.list)
.setDefaultValue((defaultValue as LongList).list)
.setSaveConsumer { longList -> consumer.accept(LongList(longList)) }
.apply {
tooltip?.let { tooltip -> this.setTooltip(tooltip) }
requiresRestart?.let { requiresRestart -> this.requireRestart(requiresRestart) }
requirement?.let { requirement -> this.setRequirement(requirement) }
}
}
is FloatList -> {
val consumer = saveConsumer as? Consumer<FloatList> ?: consumerError()
entryBuilder.startFloatList(title, usedValue.list)
.setDefaultValue((defaultValue as FloatList).list)
.setSaveConsumer { floatList -> consumer.accept(FloatList(floatList)) }
.apply {
tooltip?.let { tooltip -> this.setTooltip(tooltip) }
requiresRestart?.let { requiresRestart -> this.requireRestart(requiresRestart) }
requirement?.let { requirement -> this.setRequirement(requirement) }
}
}
TODO("add the other new entries")
else -> throw IllegalArgumentException("Unsupported type: ${usedValue!!::class.java}")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,60 @@

package net.frozenblock.lib.config.api.client.gui

import kotlinx.serialization.Serializable

/**
* An [Int] wrapper that represents a color.
* [<P>]
* Should only be used if Fabric Language Kotlin is installed.
* @param color The [Int] representation of the color
* @since 1.3.8
*/
@Serializable
data class Color(@JvmField val color: Int) {

override fun toString(): String = "Color[$color]"

}

// lists

data class StringList(@JvmField val list: List<String>) {

override fun toString(): String = "StringList[$list]"
}

data class IntList(@JvmField val list: List<Int>) {

override fun toString(): String = "IntList[$list]"
}

data class LongList(@JvmField val list: List<Long>) {

override fun toString(): String = "LongList[$list]"
}

data class FloatList(@JvmField val list: List<Float>) {

override fun toString(): String = "FloatList[$list]"
}

data class DoubleList(@JvmField val list: List<Double>) {

override fun toString(): String = "DoubleList[$list]"
}

// not lists anymore

data class EnumEntry<T : Enum<?>>(
@JvmField val `class`: KClass<T>,
@JvmField val value: T
) {

override fun toString(): String = "EnumEntry[class=$`class`, value=$value]"
}

data class SelectorEntry<T>(
@JvmField val valuesArray: Array<T>,
@JvmField val value: T
) {

override fun toString(): String = "SelectorEntry[valuesArray=$valuesArray, value=$value]"
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@

package net.frozenblock.lib.config.api.client.gui

import kotlinx.serialization.Contextual
import kotlinx.serialization.Serializable

/**
* A wrapper of a value, minimum, and maximum that represents a slider.
* [<P>]
Expand All @@ -30,21 +27,16 @@ import kotlinx.serialization.Serializable
* @param max The maximum [Int] of the slider
* @since 1.4
*/
@Serializable
data class Slider<T>(
@Contextual
val value: Number,
@Contextual
val min: Number,
@Contextual
val max: Number,
val type: SliderType<T>
) {

override fun toString(): String = "Slider[value=$value, min=$min, max=$max]"
}

@Serializable
sealed class SliderType<T> {
data object INT : SliderType<Int>()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,18 @@ import java.util.function.Supplier
* Should only be used if Fabric Language Kotlin is installed.
* @since 1.3.8
*/
fun <T> makeTypedEntryList(entryBuilder: ConfigEntryBuilder, title: Component, entrySupplier: Supplier<TypedEntry<List<T>>?>?, defaultValue: Supplier<TypedEntry<List<T>>>, expandedByDefault: Boolean = false, tooltip: Component, setterConsumer: Consumer<TypedEntry<List<T>>>, cellCreator: BiFunction<T, NestedListListEntry<T, AbstractConfigListEntry<T>>, AbstractConfigListEntry<T>>, requiresRestart: Boolean = false): NestedListListEntry<T, AbstractConfigListEntry<T>> {
fun <T> makeTypedEntryList(
entryBuilder: ConfigEntryBuilder,
title: Component,
entrySupplier: Supplier<TypedEntry<List<T>>?>?,
defaultValue: Supplier<TypedEntry<List<T>>>,
expandedByDefault: Boolean = false,
tooltip: Component,
setterConsumer: Consumer<TypedEntry<List<T>>>,
cellCreator: BiFunction<T, NestedListListEntry<T, AbstractConfigListEntry<T>>, AbstractConfigListEntry<T>>,
requiresRestart: Boolean = false,
requirement: Requirement? = null
): NestedListListEntry<T, AbstractConfigListEntry<T>> {
val typedEntry: TypedEntry<List<T>> = entrySupplier?.get() ?: defaultValue.get()

return NestedListListEntry(
Expand Down Expand Up @@ -63,14 +74,28 @@ fun <T> makeTypedEntryList(entryBuilder: ConfigEntryBuilder, title: Component, e
true,
// New Cell Creation
cellCreator
).let {it.isRequiresRestart = requiresRestart; it}
).apply {
this.isRequiresRestart = requiresRestart
this.requirement = requirement
}
}

/**
* Should only be used if Fabric Language Kotlin is installed.
* @since 1.3.8
*/
fun <T> makeNestedList(entryBuilder: ConfigEntryBuilder, title: Component, entrySupplier: Supplier<List<T>?>?, defaultValue: Supplier<List<T>>, expandedByDefault: Boolean = false, tooltip: Component, setterConsumer: Consumer<List<T>>, cellCreator: BiFunction<T, NestedListListEntry<T, AbstractConfigListEntry<T>>, AbstractConfigListEntry<T>>, requiresRestart: Boolean = false): NestedListListEntry<T, out AbstractConfigListEntry<T>> {
fun <T> makeNestedList(
entryBuilder: ConfigEntryBuilder,
title: Component,
entrySupplier: Supplier<List<T>?>?,
defaultValue: Supplier<List<T>>,
expandedByDefault: Boolean = false,
tooltip: Component,
setterConsumer: Consumer<List<T>>,
cellCreator: BiFunction<T, NestedListListEntry<T, AbstractConfigListEntry<T>>, AbstractConfigListEntry<T>>,
requiresRestart: Boolean = false,
requirement: Requirement? = null
): NestedListListEntry<T, out AbstractConfigListEntry<T>> {
val value: List<T> = entrySupplier?.get() ?: defaultValue.get()

return NestedListListEntry(
Expand Down Expand Up @@ -98,17 +123,30 @@ fun <T> makeNestedList(entryBuilder: ConfigEntryBuilder, title: Component, entry
true,
// New Cell Creation
cellCreator
).let {it.isRequiresRestart = requiresRestart; it}
).apply {
this.isRequiresRestart = requiresRestart
this.requirement = requirement
}
}

/**
* Should only be used if Fabric Language Kotlin is installed.
* @since 1.3.8
*/
fun <T> makeMultiElementEntry(title: Component, value: T, defaultExpanded: Boolean = true, vararg entries: AbstractConfigListEntry<out Any>, requiresRestart: Boolean = false): MultiElementListEntry<T> =
fun <T> makeMultiElementEntry(
title: Component,
value: T,
defaultExpanded: Boolean = true,
vararg entries: AbstractConfigListEntry<out Any>,
requiresRestart: Boolean = false,
requirement: Requirement? = null
): MultiElementListEntry<T> =
MultiElementListEntry(
title,
value, // Default Value
entries.asList(),
defaultExpanded
).let { it.isRequiresRestart = requiresRestart; it }
).apply {
this.isRequiresRestart = requiresRestart
this.requirement = requirement
}
3 changes: 0 additions & 3 deletions src/main/kotlin/net/frozenblock/lib/util/ParameterUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ package net.frozenblock.lib.util
import com.mojang.serialization.Codec
import com.mojang.serialization.DataResult
import com.mojang.serialization.codecs.RecordCodecBuilder
import kotlinx.serialization.Serializable
import net.minecraft.util.ExtraCodecs
import net.minecraft.world.level.biome.Climate

@Serializable
data class MutableParameter(
@JvmField var min: Long?,
@JvmField var max: Long?
Expand All @@ -52,7 +50,6 @@ data class MutableParameter(

fun Climate.Parameter.mutable(): MutableParameter = MutableParameter(min, max)

@Serializable
data class MutableParameterPoint(
@JvmField var temperature: MutableParameter?,
@JvmField var humidity: MutableParameter?,
Expand Down
2 changes: 1 addition & 1 deletion src/testmod/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"depends": {
"fabricloader": ">=0.14.22",
"fabric-api": ">=0.90.0",
"minecraft": "~1.20-",
"minecraft": "~1.20.2-",
"java": ">=17"
},
"breaks": {
Expand Down

0 comments on commit b0f9716

Please sign in to comment.