Actually deserializeFunctionTree should be private
This commit is contained in:
parent
1330fe6925
commit
f7a1d8aeed
@ -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 {
|
||||
if (funcs.isEmpty()) {
|
||||
return value
|
||||
|
@ -28,21 +28,13 @@ enum class MatterFunction {
|
||||
},
|
||||
DIVIDE_UP {
|
||||
override fun updateValue(self: Int, other: Int): Int = integerDivisionUp(self, other)
|
||||
override fun updateValue(self: ImpreciseFraction, other: ImpreciseFraction): ImpreciseFraction = 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"
|
||||
)
|
||||
override fun updateValue(self: ImpreciseFraction, other: ImpreciseFraction): ImpreciseFraction = 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 {
|
||||
override fun updateValue(self: Int, other: Int): Int = integerDivisionDown(self, other)
|
||||
override fun updateValue(self: ImpreciseFraction, other: ImpreciseFraction): ImpreciseFraction = 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"
|
||||
)
|
||||
override fun updateValue(self: ImpreciseFraction, other: ImpreciseFraction): ImpreciseFraction = 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 {
|
||||
override fun updateValue(self: Int, other: Int): Int = self % other
|
||||
|
@ -11,9 +11,40 @@ import net.minecraft.world.item.Item
|
||||
import ru.dbotthepony.mc.otm.core.ImpreciseFraction
|
||||
import ru.dbotthepony.mc.otm.core.set
|
||||
import ru.dbotthepony.mc.otm.data.stream
|
||||
import ru.dbotthepony.mc.otm.matter.BoundMatterFunction.Companion.deserializeFunctionTree
|
||||
|
||||
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 complexityFunctions: List<BoundMatterFunction<Double>>
|
||||
val priorityFunctions: List<BoundMatterFunction<Int>>
|
||||
|
Loading…
Reference in New Issue
Block a user