From f7a1d8aeed415f550b278c7ae5acf2f9c2657f0a Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Fri, 11 Nov 2022 00:54:39 +0700 Subject: [PATCH] Actually deserializeFunctionTree should be private --- .../mc/otm/matter/BoundMatterFunction.kt | 30 ----------------- .../mc/otm/matter/MatterFunction.kt | 16 +++------ .../dbotthepony/mc/otm/matter/UpdateAction.kt | 33 ++++++++++++++++++- 3 files changed, 36 insertions(+), 43 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/matter/BoundMatterFunction.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/matter/BoundMatterFunction.kt index fd38c23f0..c63bac90a 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/matter/BoundMatterFunction.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/matter/BoundMatterFunction.kt @@ -35,36 +35,6 @@ class BoundMatterFunction(val function: MatterFunction, val value: T } } - inline fun deserializeFunctionTree(input: JsonElement?, name: String): List> { - 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 apply(value: T, funcs: Collection>): T { if (funcs.isEmpty()) { return value diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/matter/MatterFunction.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/matter/MatterFunction.kt index a889c0311..d467a0e5a 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/matter/MatterFunction.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/matter/MatterFunction.kt @@ -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 diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/matter/UpdateAction.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/matter/UpdateAction.kt index 4dc292bde..63b24259d 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/matter/UpdateAction.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/matter/UpdateAction.kt @@ -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 deserializeFunctionTree(input: JsonElement?, name: String): List> { + 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> val complexityFunctions: List> val priorityFunctions: List>