Actually deserializeFunctionTree should be private

This commit is contained in:
DBotThePony 2022-11-11 00:54:39 +07:00
parent 1330fe6925
commit f7a1d8aeed
Signed by: DBot
GPG Key ID: DCC23B5715498507
3 changed files with 36 additions and 43 deletions

View File

@ -35,36 +35,6 @@ class BoundMatterFunction<T : Number>(val function: MatterFunction, val value: T
} }
} }
inline fun <reified T : Number> deserializeFunctionTree(input: JsonElement?, name: String): List<BoundMatterFunction<T>> {
if (input == null) {
return listOf()
}
if (input !is JsonArray) {
throw JsonParseException("Expected $name to be JsonArray, ${input::class.qualifiedName} given")
}
return input.stream().map {
if (it !is JsonObject) {
throw JsonParseException("All elements in function tree must be JsonObjects, ${it::class.qualifiedName} given")
}
val type = it["type"]?.asString ?: throw JsonParseException("Invalid `type` value in function object")
val value = it["value"] ?: throw JsonParseException("Missing `value` value in function object")
val parsedValue: T = getValue(value)
val fn: MatterFunction
try {
fn = MatterFunction.valueOf(type.uppercase())
} catch (err: NoSuchElementException) {
throw JsonParseException("No such function: $type", err)
}
return@map BoundMatterFunction(fn, parsedValue)
}.collect(ImmutableList.toImmutableList())
}
fun <T : Number> apply(value: T, funcs: Collection<BoundMatterFunction<T>>): T { fun <T : Number> apply(value: T, funcs: Collection<BoundMatterFunction<T>>): T {
if (funcs.isEmpty()) { if (funcs.isEmpty()) {
return value return value

View File

@ -28,21 +28,13 @@ enum class MatterFunction {
}, },
DIVIDE_UP { DIVIDE_UP {
override fun updateValue(self: Int, other: Int): Int = integerDivisionUp(self, other) override fun updateValue(self: Int, other: Int): Int = integerDivisionUp(self, other)
override fun updateValue(self: ImpreciseFraction, other: ImpreciseFraction): ImpreciseFraction = throw JsonParseException( override fun updateValue(self: ImpreciseFraction, other: ImpreciseFraction): ImpreciseFraction = throw JsonParseException("Integer division up is available only for integers")
"Integer division up is available only for integers" override fun updateValue(self: Double, other: Double): Double = throw JsonParseException("Integer division up is available only for integers")
)
override fun updateValue(self: Double, other: Double): Double = throw JsonParseException(
"Integer division up is available only for integers"
)
}, },
DIVIDE_DOWN { DIVIDE_DOWN {
override fun updateValue(self: Int, other: Int): Int = integerDivisionDown(self, other) override fun updateValue(self: Int, other: Int): Int = integerDivisionDown(self, other)
override fun updateValue(self: ImpreciseFraction, other: ImpreciseFraction): ImpreciseFraction = throw JsonParseException( override fun updateValue(self: ImpreciseFraction, other: ImpreciseFraction): ImpreciseFraction = throw JsonParseException("Integer division down is available only for integers")
"Integer division down is available only for integers" override fun updateValue(self: Double, other: Double): Double = throw JsonParseException("Integer division down is available only for integers")
)
override fun updateValue(self: Double, other: Double): Double = throw JsonParseException(
"Integer division down is available only for integers"
)
}, },
MODULO { MODULO {
override fun updateValue(self: Int, other: Int): Int = self % other override fun updateValue(self: Int, other: Int): Int = self % other

View File

@ -11,9 +11,40 @@ import net.minecraft.world.item.Item
import ru.dbotthepony.mc.otm.core.ImpreciseFraction import ru.dbotthepony.mc.otm.core.ImpreciseFraction
import ru.dbotthepony.mc.otm.core.set import ru.dbotthepony.mc.otm.core.set
import ru.dbotthepony.mc.otm.data.stream import ru.dbotthepony.mc.otm.data.stream
import ru.dbotthepony.mc.otm.matter.BoundMatterFunction.Companion.deserializeFunctionTree
class UpdateAction : AbstractRegistryAction { class UpdateAction : AbstractRegistryAction {
companion object {
private inline fun <reified T : Number> deserializeFunctionTree(input: JsonElement?, name: String): List<BoundMatterFunction<T>> {
if (input == null) {
return listOf()
}
if (input !is JsonArray) {
throw JsonParseException("Expected $name to be JsonArray, ${input::class.qualifiedName} given")
}
return input.stream().map {
if (it !is JsonObject) {
throw JsonParseException("All elements in function tree must be JsonObjects, ${it::class.qualifiedName} given")
}
val type = it["type"]?.asString ?: throw JsonParseException("Invalid `type` value in function object")
val value = it["value"] ?: throw JsonParseException("Missing `value` value in function object")
val parsedValue: T = BoundMatterFunction.getValue(value)
val fn: MatterFunction
try {
fn = MatterFunction.valueOf(type.uppercase())
} catch (err: NoSuchElementException) {
throw JsonParseException("No such function: $type", err)
}
return@map BoundMatterFunction(fn, parsedValue)
}.collect(ImmutableList.toImmutableList())
}
}
val matterFunctions: List<BoundMatterFunction<ImpreciseFraction>> val matterFunctions: List<BoundMatterFunction<ImpreciseFraction>>
val complexityFunctions: List<BoundMatterFunction<Double>> val complexityFunctions: List<BoundMatterFunction<Double>>
val priorityFunctions: List<BoundMatterFunction<Int>> val priorityFunctions: List<BoundMatterFunction<Int>>