From 2cc8215cb0026f484a88d48168cba6d24758aacc Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Mon, 1 Jan 2024 17:30:48 +0700 Subject: [PATCH] Revert "Polyfill dataresult" This reverts commit e3df0d82e0d2f4416c05efa0a61680105f216417. --- .../kotlin/ru/dbotthepony/mc/otm/core/Ext.kt | 18 +++---------- .../mc/otm/core/util/StreamCodecs.kt | 6 ++--- .../mc/otm/data/Codec2RecipeSerializer.kt | 6 ++--- .../ru/dbotthepony/mc/otm/data/CodecList.kt | 11 ++++---- .../mc/otm/data/ComparableCodec.kt | 9 +++---- .../dbotthepony/mc/otm/data/ComponentCodec.kt | 6 ++--- .../dbotthepony/mc/otm/data/DecimalCodec.kt | 24 ++++++++--------- .../mc/otm/data/IngredientMatrixCodec.kt | 26 +++++++++---------- .../ru/dbotthepony/mc/otm/data/UUIDCodec.kt | 18 ++++++------- .../mc/otm/matter/ComputeAction.kt | 20 +++++++------- .../mc/otm/triggers/MCriterionTrigger.kt | 10 +++---- 11 files changed, 63 insertions(+), 91 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/Ext.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/Ext.kt index 60b55c578..bc23bfbce 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/Ext.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/Ext.kt @@ -10,10 +10,10 @@ import com.google.common.collect.ImmutableSet import com.google.gson.JsonElement import com.google.gson.JsonObject import com.google.gson.JsonPrimitive -import com.mojang.serialization.DataResult import it.unimi.dsi.fastutil.objects.ObjectComparators import net.minecraft.core.BlockPos import net.minecraft.nbt.CompoundTag +import net.minecraft.nbt.NbtAccounter import net.minecraft.network.FriendlyByteBuf import net.minecraft.network.chat.ComponentContents import net.minecraft.network.chat.contents.TranslatableContents @@ -26,6 +26,7 @@ import net.minecraft.world.item.crafting.Ingredient import net.minecraft.world.level.BlockGetter import net.minecraft.world.level.Level import net.minecraft.world.level.block.Block +import net.minecraft.world.level.block.Blocks import net.minecraft.world.level.block.state.BlockState import net.minecraft.world.level.block.state.StateHolder import net.minecraft.world.level.block.state.properties.Property @@ -40,7 +41,9 @@ import net.minecraftforge.registries.IForgeRegistry import ru.dbotthepony.mc.otm.core.math.BlockRotation import ru.dbotthepony.mc.otm.core.math.BlockRotationFreedom import ru.dbotthepony.mc.otm.core.math.Vector +import ru.dbotthepony.mc.otm.core.util.readInt import ru.dbotthepony.mc.otm.core.util.readVarIntLE +import ru.dbotthepony.mc.otm.core.util.writeInt import ru.dbotthepony.mc.otm.core.util.writeVarIntLE import java.io.InputStream import java.io.OutputStream @@ -464,16 +467,3 @@ fun lazy2(a: () -> A, b: A.() -> B): Supplier { val first = lazy(a) return Supplier { b.invoke(first.value) } } - -// 1.19.3 and earlier polyfill -fun failure(message: String): DataResult { - return DataResult.error { message } -} - -fun failure(message: Supplier): DataResult { - return DataResult.error(message) -} - -fun success(result: R): DataResult { - return DataResult.success(result) -} diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/StreamCodecs.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/StreamCodecs.kt index 191ff8fb4..961847d1e 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/StreamCodecs.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/StreamCodecs.kt @@ -8,13 +8,11 @@ import com.mojang.serialization.DynamicOps import net.minecraft.nbt.NbtAccounter import net.minecraft.world.item.ItemStack import net.minecraftforge.fluids.FluidStack -import ru.dbotthepony.mc.otm.core.failure import ru.dbotthepony.mc.otm.core.immutableMap import ru.dbotthepony.mc.otm.core.math.RGBAColor import ru.dbotthepony.mc.otm.core.math.readDecimal import ru.dbotthepony.mc.otm.core.math.writeDecimal import ru.dbotthepony.mc.otm.core.readItemType -import ru.dbotthepony.mc.otm.core.success import ru.dbotthepony.mc.otm.core.writeItemType import java.io.DataInput import java.io.DataInputStream @@ -197,11 +195,11 @@ class EnumValueCodec>(clazz: Class) : IStreamCodec, Codec< override fun decode(ops: DynamicOps, input: T): DataResult> { if (ops.compressMaps()) { return ops.getNumberValue(input) - .flatMap { values.getOrNull(it.toInt())?.let { success(Pair(it, ops.empty())) } ?: failure { "No such enum with ordinal index $it" } } + .flatMap { values.getOrNull(it.toInt())?.let { DataResult.success(Pair(it, ops.empty())) } ?: DataResult.error { "No such enum with ordinal index $it" } } } return ops.getStringValue(input) - .flatMap { valuesMap[it]?.let { success(Pair(it, ops.empty())) } ?: failure { "No such enum value $it" } } + .flatMap { valuesMap[it]?.let { DataResult.success(Pair(it, ops.empty())) } ?: DataResult.error { "No such enum value $it" } } } companion object { diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/data/Codec2RecipeSerializer.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/data/Codec2RecipeSerializer.kt index f09e62ffe..b32702611 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/data/Codec2RecipeSerializer.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/data/Codec2RecipeSerializer.kt @@ -19,9 +19,7 @@ import net.minecraft.world.item.crafting.Ingredient import net.minecraft.world.item.crafting.Recipe import net.minecraft.world.item.crafting.RecipeSerializer import org.apache.logging.log4j.LogManager -import ru.dbotthepony.mc.otm.core.failure import ru.dbotthepony.mc.otm.core.set -import ru.dbotthepony.mc.otm.core.success import ru.dbotthepony.mc.otm.core.util.readBinaryJsonWithCodecIndirect import ru.dbotthepony.mc.otm.core.util.writeBinaryJsonWithCodec import kotlin.collections.ArrayDeque @@ -71,9 +69,9 @@ class Codec2RecipeSerializer>( val read = serializer.fromNetwork(buff) if (read == null) - failure { "Unable to read parent recipe from network" } + DataResult.error { "Unable to read parent recipe from network" } else - success(Pair(read, ops.empty())) + DataResult.success(Pair(read, ops.empty())) } } else { return serializer.codec().decode(ops, input) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/data/CodecList.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/data/CodecList.kt index 00b73e8ec..572fa629c 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/data/CodecList.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/data/CodecList.kt @@ -5,7 +5,6 @@ import com.mojang.datafixers.util.Pair import com.mojang.serialization.Codec import com.mojang.serialization.DataResult import com.mojang.serialization.DynamicOps -import ru.dbotthepony.mc.otm.core.failure import ru.dbotthepony.mc.otm.core.stream import java.util.function.Predicate import java.util.stream.Stream @@ -33,7 +32,7 @@ class CodecList(codecs: Stream>) : Codec { } } - return failure { + return DataResult.error { "None of codecs encoded the input:\n " + results.joinToString(";\n ") { it.error().get().message() } } } @@ -51,7 +50,7 @@ class CodecList(codecs: Stream>) : Codec { } } - return failure { + return DataResult.error { "None of codecs decoded the input:\n " + results.joinToString(";\n ") { it.error().get().message() } } } @@ -84,11 +83,11 @@ class PredicatedCodecList(codecs: Stream, Predicat } } else { val i2 = i - results.add(failure { "Codec #$i2 predicate tested false" }) + results.add(DataResult.error { "Codec #$i2 predicate tested false" }) } } - return failure { + return DataResult.error { "None of codecs encoded the input:\n " + results.joinToString(";\n ") { it.error().get().message() } } } @@ -106,7 +105,7 @@ class PredicatedCodecList(codecs: Stream, Predicat } } - return failure { + return DataResult.error { "None of codecs decoded the input:\n " + results.joinToString(";\n ") { it.error().get().message() } } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/data/ComparableCodec.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/data/ComparableCodec.kt index 40e4e8545..3a49fcf06 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/data/ComparableCodec.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/data/ComparableCodec.kt @@ -4,18 +4,17 @@ import com.mojang.datafixers.util.Pair import com.mojang.serialization.Codec import com.mojang.serialization.DataResult import com.mojang.serialization.DynamicOps -import ru.dbotthepony.mc.otm.core.failure class ComparableCodec>(val parent: Codec, val min: S? = null, val max: S? = null, val minExclusive: Boolean = false, val maxExclusive: Boolean = false) : Codec { private fun check(input: S): DataResult? { if (min != null) { if (minExclusive) { if (input <= min) { - return failure { "Value $input is smaller or equal to minimal $min" } + return DataResult.error { "Value $input is smaller or equal to minimal $min" } } } else { if (input < min) { - return failure { "Value $input is smaller than minimal $min" } + return DataResult.error { "Value $input is smaller than minimal $min" } } } } @@ -23,11 +22,11 @@ class ComparableCodec>(val parent: Codec, val min: S? = nul if (max != null) { if (maxExclusive) { if (input >= max) { - return failure { "Value $input is bigger or equal to maximal $max" } + return DataResult.error { "Value $input is bigger or equal to maximal $max" } } } else { if (input > max) { - return failure { "Value $input is bigger than maximal $max" } + return DataResult.error { "Value $input is bigger than maximal $max" } } } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/data/ComponentCodec.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/data/ComponentCodec.kt index 2a007ea95..ec65bb31a 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/data/ComponentCodec.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/data/ComponentCodec.kt @@ -7,8 +7,6 @@ import com.mojang.serialization.DataResult import com.mojang.serialization.DynamicOps import com.mojang.serialization.JsonOps import net.minecraft.network.chat.Component -import ru.dbotthepony.mc.otm.core.failure -import ru.dbotthepony.mc.otm.core.success object ComponentCodec : Codec { override fun encode(input: Component, ops: DynamicOps, prefix: T): DataResult { @@ -19,9 +17,9 @@ object ComponentCodec : Codec { val value = ops.convertTo(JsonOps.INSTANCE, input) try { - return success(Pair(Component.Serializer.fromJson(value), ops.empty())) + return DataResult.success(Pair(Component.Serializer.fromJson(value), ops.empty())) } catch (err: JsonSyntaxException) { - return failure { "Error decoding component: ${err.message}" } + return DataResult.error { "Error decoding component: ${err.message}" } } } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/data/DecimalCodec.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/data/DecimalCodec.kt index 7ec7322cd..062564586 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/data/DecimalCodec.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/data/DecimalCodec.kt @@ -6,9 +6,7 @@ import com.mojang.serialization.DataResult import com.mojang.serialization.DynamicOps import it.unimi.dsi.fastutil.bytes.ByteArrayList import net.minecraft.nbt.NbtOps -import ru.dbotthepony.mc.otm.core.failure import ru.dbotthepony.mc.otm.core.math.Decimal -import ru.dbotthepony.mc.otm.core.success import java.nio.ByteBuffer import java.util.stream.Collector import java.util.stream.Stream @@ -16,47 +14,47 @@ import java.util.stream.Stream object DecimalCodec : Codec { override fun encode(input: Decimal, ops: DynamicOps, prefix: T): DataResult { if (ops === NbtOps.INSTANCE) { - return success((ops as DynamicOps).createByteList(ByteBuffer.wrap(input.toByteArray()))) + return DataResult.success((ops as DynamicOps).createByteList(ByteBuffer.wrap(input.toByteArray()))) } - return success(ops.createString(input.toString())) + return DataResult.success(ops.createString(input.toString())) } override fun decode(ops: DynamicOps, input: T): DataResult> { return ops.getStringValue(input).flatMap { try { - success(Pair(Decimal(it), ops.empty())) + DataResult.success(Pair(Decimal(it), ops.empty())) } catch (err: NumberFormatException) { - failure { "Not a valid number for converting into Decimal: $it" } + DataResult.error { "Not a valid number for converting into Decimal: $it" } } }.get().map( { - success(it) + DataResult.success(it) }, { e0 -> ops.getIntStream(input).flatMap { try { - success(Pair(Decimal.fromByteArray( + DataResult.success(Pair(Decimal.fromByteArray( it .collect(::ByteArrayList, { v, a -> v.add(a.toByte()) }, ByteArrayList::addAll) .toByteArray() ), ops.empty())) } catch (err: NumberFormatException) { - failure { "Failed to convert array of bytes into Decimal: $it" } + DataResult.error { "Failed to convert array of bytes into Decimal: $it" } } }.get().map( { - success(it) + DataResult.success(it) }, { e1 -> ops.getNumberValue(input).flatMap { - success(Pair(Decimal(it.toString()), ops.empty())) + DataResult.success(Pair(Decimal(it.toString()), ops.empty())) }.get().map( { - success(it) + DataResult.success(it) }, { e2 -> - failure { "None of attempts at decoding Decimal were successful: ${e0.message()}; ${e1.message()}; ${e2.message()}" } + DataResult.error { "None of attempts at decoding Decimal were successful: ${e0.message()}; ${e1.message()}; ${e2.message()}" } } ) } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/data/IngredientMatrixCodec.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/data/IngredientMatrixCodec.kt index 731ae577b..12d7117cd 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/data/IngredientMatrixCodec.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/data/IngredientMatrixCodec.kt @@ -11,9 +11,7 @@ import ru.dbotthepony.mc.otm.core.collect.allEqual import ru.dbotthepony.mc.otm.core.collect.map import ru.dbotthepony.mc.otm.core.collect.toList import ru.dbotthepony.mc.otm.core.collect.toStream -import ru.dbotthepony.mc.otm.core.failure import ru.dbotthepony.mc.otm.core.stream -import ru.dbotthepony.mc.otm.core.success import ru.dbotthepony.mc.otm.recipe.IIngredientMatrix import ru.dbotthepony.mc.otm.recipe.IngredientMatrix import java.util.function.Supplier @@ -40,15 +38,15 @@ class IngredientMatrixCodec(ingredientCodec: Codec) : Codec) : Codec) : Codec handwrittenCodec.decode(ops, input).get().map( { - success(it) + DataResult.success(it) }, { - failure { "Failed to decode ingredients as list: ${err1.message()} and as pattern/dictionary: ${it.message()}" } + DataResult.error { "Failed to decode ingredients as list: ${err1.message()} and as pattern/dictionary: ${it.message()}" } } ).flatMap { val handwritten = it.first @@ -102,12 +100,12 @@ class IngredientMatrixCodec(ingredientCodec: Codec) : Codec { override fun encode(input: UUID, ops: DynamicOps, prefix: T): DataResult { if (ops === NbtOps.INSTANCE) { - return success((ops as DynamicOps).createLongList(LongStream.of(input.mostSignificantBits, input.leastSignificantBits))) + return DataResult.success((ops as DynamicOps).createLongList(LongStream.of(input.mostSignificantBits, input.leastSignificantBits))) } - return success(ops.createString(input.toString())) + return DataResult.success(ops.createString(input.toString())) } override fun decode(ops: DynamicOps, input: T): DataResult> { @@ -27,25 +25,25 @@ object UUIDCodec : Codec { if (l.size == 4) { // 4 int - success(Pair(UUID((l[0] shl 32) or l[1], (l[2] shl 32) or l[3]), ops.empty())) + DataResult.success(Pair(UUID((l[0] shl 32) or l[1], (l[2] shl 32) or l[3]), ops.empty())) } else if (l.size == 2) { - success(Pair(UUID(l[0], l[1]), ops.empty())) + DataResult.success(Pair(UUID(l[0], l[1]), ops.empty())) } else { - failure { "Can't construct UUID from ${l.size} elements" } + DataResult.error { "Can't construct UUID from ${l.size} elements" } } }.get().map( { - success(it) + DataResult.success(it) }, { e0 -> ops.getStringValue(input).map { Pair(UUID.fromString(it), ops.empty()) }.get().map( { - success(it) + DataResult.success(it) }, { - failure { "Unable to deserialize UUID: ${e0.message()}; ${it.message()}" } + DataResult.error { "Unable to deserialize UUID: ${e0.message()}; ${it.message()}" } } ) } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/matter/ComputeAction.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/matter/ComputeAction.kt index 886a961a1..50449c5dd 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/matter/ComputeAction.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/matter/ComputeAction.kt @@ -11,9 +11,7 @@ import net.minecraft.resources.ResourceLocation import net.minecraft.tags.TagKey import net.minecraft.world.item.Item import net.minecraftforge.registries.ForgeRegistries -import ru.dbotthepony.mc.otm.core.failure import ru.dbotthepony.mc.otm.core.math.Decimal -import ru.dbotthepony.mc.otm.core.success import ru.dbotthepony.mc.otm.data.DecimalCodec import ru.dbotthepony.mc.otm.data.PredicatedCodecList import java.util.Optional @@ -105,19 +103,19 @@ class ComputeAction( val pair = this to this override fun encode(input: Constant, ops: DynamicOps, prefix: T): DataResult { - return success(ops.createString(symbol + input.matter.toString())) + return DataResult.success(ops.createString(symbol + input.matter.toString())) } override fun decode(ops: DynamicOps, input: T): DataResult> { return ops.getStringValue(input).flatMap { if (it[0] == symbol) { try { - success(Pair(Constant(Decimal(it.substring(1).trim()), it.substring(1).trim().toDouble(), fn, fn), ops.empty())) + DataResult.success(Pair(Constant(Decimal(it.substring(1).trim()), it.substring(1).trim().toDouble(), fn, fn), ops.empty())) } catch (err: NumberFormatException) { - failure { "Not a number: ${it.substring(1).trim()} (input string: $it)" } + DataResult.error { "Not a number: ${it.substring(1).trim()} (input string: $it)" } } } else { - failure { "Input string does not match expected operand: expected $symbol, got ${it[0]} (for input $it)" } + DataResult.error { "Input string does not match expected operand: expected $symbol, got ${it[0]} (for input $it)" } } } } @@ -129,15 +127,15 @@ class ComputeAction( private object PlainStringConstantCodec : Codec, Predicate { override fun encode(input: Constant, ops: DynamicOps, prefix: T): DataResult { - return success(ops.createString(input.matter.toString())) + return DataResult.success(ops.createString(input.matter.toString())) } override fun decode(ops: DynamicOps, input: T): DataResult> { val result = ops.getNumberValue(input).flatMap { try { - success(Pair(Constant(Decimal(it.toString()), it.toDouble(), IMatterFunction.PLUS, IMatterFunction.PLUS), ops.empty())) + DataResult.success(Pair(Constant(Decimal(it.toString()), it.toDouble(), IMatterFunction.PLUS, IMatterFunction.PLUS), ops.empty())) } catch (err: NumberFormatException) { - failure { "Not a number: $it" } + DataResult.error { "Not a number: $it" } } } @@ -146,9 +144,9 @@ class ComputeAction( return ops.getStringValue(input).flatMap { try { - success(Pair(Constant(Decimal(it), it.toDouble(), IMatterFunction.PLUS, IMatterFunction.PLUS), ops.empty())) + DataResult.success(Pair(Constant(Decimal(it), it.toDouble(), IMatterFunction.PLUS, IMatterFunction.PLUS), ops.empty())) } catch (err: NumberFormatException) { - failure { "Not a number: $it" } + DataResult.error { "Not a number: $it" } } } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/triggers/MCriterionTrigger.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/triggers/MCriterionTrigger.kt index 971cfe407..d1c9b2370 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/triggers/MCriterionTrigger.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/triggers/MCriterionTrigger.kt @@ -22,9 +22,7 @@ import net.minecraft.server.PlayerAdvancements import net.minecraft.server.level.ServerPlayer import ru.dbotthepony.mc.otm.core.collect.filter import ru.dbotthepony.mc.otm.core.collect.toImmutableList -import ru.dbotthepony.mc.otm.core.failure import ru.dbotthepony.mc.otm.core.set -import ru.dbotthepony.mc.otm.core.success import ru.dbotthepony.mc.otm.core.toJsonStrict import java.util.Optional import java.util.function.Predicate @@ -87,16 +85,16 @@ abstract class MCriterionTrigger.AbstractInstance>(val @JvmStatic protected val predicateCodec: Codec = object : Codec { override fun encode(input: ContextAwarePredicate, ops: DynamicOps, prefix: T): DataResult { - return success(JsonOps.INSTANCE.convertTo(ops, input.toJson())) + return DataResult.success(JsonOps.INSTANCE.convertTo(ops, input.toJson())) } override fun decode(ops: DynamicOps, input: T): DataResult> { - val context = deserializationContext.get().lastOrNull() ?: return failure { "Not current deserializing trigger instance" } + val context = deserializationContext.get().lastOrNull() ?: return DataResult.error { "Not current deserializing trigger instance" } return try { - success(Pair.of(EntityPredicate.fromJson(JsonObject().also { it["a"] = ops.convertTo(JsonOps.INSTANCE, input) }, "a", context).get(), ops.empty())) + DataResult.success(Pair.of(EntityPredicate.fromJson(JsonObject().also { it["a"] = ops.convertTo(JsonOps.INSTANCE, input) }, "a", context).get(), ops.empty())) } catch (err: Exception) { - failure { "Failed to deserialize ContextAwarePredicate: " + err.message } + DataResult.error { "Failed to deserialize ContextAwarePredicate: " + err.message } } } }