Merge branch '1.20.2' into 1.20.1
This commit is contained in:
commit
4c5c2362ce
@ -8,11 +8,13 @@ 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
|
||||
@ -195,11 +197,11 @@ class EnumValueCodec<V : Enum<V>>(clazz: Class<out V>) : IStreamCodec<V>, Codec<
|
||||
override fun <T : Any> decode(ops: DynamicOps<T>, input: T): DataResult<Pair<V, T>> {
|
||||
if (ops.compressMaps()) {
|
||||
return ops.getNumberValue(input)
|
||||
.flatMap { values.getOrNull(it.toInt())?.let { DataResult.success(Pair(it, ops.empty())) } ?: DataResult.error { "No such enum with ordinal index $it" } }
|
||||
.flatMap { values.getOrNull(it.toInt())?.let { success(Pair(it, ops.empty())) } ?: failure { "No such enum with ordinal index $it" } }
|
||||
}
|
||||
|
||||
return ops.getStringValue(input)
|
||||
.flatMap { valuesMap[it]?.let { DataResult.success(Pair(it, ops.empty())) } ?: DataResult.error { "No such enum value $it" } }
|
||||
.flatMap { valuesMap[it]?.let { success(Pair(it, ops.empty())) } ?: failure { "No such enum value $it" } }
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
@ -5,6 +5,7 @@ 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
|
||||
@ -32,7 +33,7 @@ class CodecList<S : Any>(codecs: Stream<Codec<S>>) : Codec<S> {
|
||||
}
|
||||
}
|
||||
|
||||
return DataResult.error {
|
||||
return failure {
|
||||
"None of codecs encoded the input:\n " + results.joinToString(";\n ") { it.error().get().message() }
|
||||
}
|
||||
}
|
||||
@ -50,7 +51,7 @@ class CodecList<S : Any>(codecs: Stream<Codec<S>>) : Codec<S> {
|
||||
}
|
||||
}
|
||||
|
||||
return DataResult.error {
|
||||
return failure {
|
||||
"None of codecs decoded the input:\n " + results.joinToString(";\n ") { it.error().get().message() }
|
||||
}
|
||||
}
|
||||
@ -83,11 +84,11 @@ class PredicatedCodecList<S : Any>(codecs: Stream<kotlin.Pair<Codec<S>, Predicat
|
||||
}
|
||||
} else {
|
||||
val i2 = i
|
||||
results.add(DataResult.error { "Codec #$i2 predicate tested false" })
|
||||
results.add(failure { "Codec #$i2 predicate tested false" })
|
||||
}
|
||||
}
|
||||
|
||||
return DataResult.error {
|
||||
return failure {
|
||||
"None of codecs encoded the input:\n " + results.joinToString(";\n ") { it.error().get().message() }
|
||||
}
|
||||
}
|
||||
@ -105,7 +106,7 @@ class PredicatedCodecList<S : Any>(codecs: Stream<kotlin.Pair<Codec<S>, Predicat
|
||||
}
|
||||
}
|
||||
|
||||
return DataResult.error {
|
||||
return failure {
|
||||
"None of codecs decoded the input:\n " + results.joinToString(";\n ") { it.error().get().message() }
|
||||
}
|
||||
}
|
||||
|
@ -4,17 +4,18 @@ 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<S : Comparable<S>>(val parent: Codec<S>, val min: S? = null, val max: S? = null, val minExclusive: Boolean = false, val maxExclusive: Boolean = false) : Codec<S> {
|
||||
private fun <T : Any> check(input: S): DataResult<T>? {
|
||||
if (min != null) {
|
||||
if (minExclusive) {
|
||||
if (input <= min) {
|
||||
return DataResult.error { "Value $input is smaller or equal to minimal $min" }
|
||||
return failure { "Value $input is smaller or equal to minimal $min" }
|
||||
}
|
||||
} else {
|
||||
if (input < min) {
|
||||
return DataResult.error { "Value $input is smaller than minimal $min" }
|
||||
return failure { "Value $input is smaller than minimal $min" }
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -22,11 +23,11 @@ class ComparableCodec<S : Comparable<S>>(val parent: Codec<S>, val min: S? = nul
|
||||
if (max != null) {
|
||||
if (maxExclusive) {
|
||||
if (input >= max) {
|
||||
return DataResult.error { "Value $input is bigger or equal to maximal $max" }
|
||||
return failure { "Value $input is bigger or equal to maximal $max" }
|
||||
}
|
||||
} else {
|
||||
if (input > max) {
|
||||
return DataResult.error { "Value $input is bigger than maximal $max" }
|
||||
return failure { "Value $input is bigger than maximal $max" }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,8 @@ 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<Component> {
|
||||
override fun <T : Any> encode(input: Component, ops: DynamicOps<T>, prefix: T): DataResult<T> {
|
||||
@ -17,9 +19,9 @@ object ComponentCodec : Codec<Component> {
|
||||
val value = ops.convertTo(JsonOps.INSTANCE, input)
|
||||
|
||||
try {
|
||||
return DataResult.success(Pair(Component.Serializer.fromJson(value), ops.empty()))
|
||||
return success(Pair(Component.Serializer.fromJson(value), ops.empty()))
|
||||
} catch (err: JsonSyntaxException) {
|
||||
return DataResult.error { "Error decoding component: ${err.message}" }
|
||||
return failure { "Error decoding component: ${err.message}" }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,9 @@ 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
|
||||
@ -14,47 +16,47 @@ import java.util.stream.Stream
|
||||
object DecimalCodec : Codec<Decimal> {
|
||||
override fun <T : Any> encode(input: Decimal, ops: DynamicOps<T>, prefix: T): DataResult<T> {
|
||||
if (ops === NbtOps.INSTANCE) {
|
||||
return DataResult.success((ops as DynamicOps<T>).createByteList(ByteBuffer.wrap(input.toByteArray())))
|
||||
return success((ops as DynamicOps<T>).createByteList(ByteBuffer.wrap(input.toByteArray())))
|
||||
}
|
||||
|
||||
return DataResult.success(ops.createString(input.toString()))
|
||||
return success(ops.createString(input.toString()))
|
||||
}
|
||||
|
||||
override fun <T : Any> decode(ops: DynamicOps<T>, input: T): DataResult<Pair<Decimal, T>> {
|
||||
return ops.getStringValue(input).flatMap {
|
||||
try {
|
||||
DataResult.success(Pair(Decimal(it), ops.empty()))
|
||||
success(Pair(Decimal(it), ops.empty()))
|
||||
} catch (err: NumberFormatException) {
|
||||
DataResult.error { "Not a valid number for converting into Decimal: $it" }
|
||||
failure { "Not a valid number for converting into Decimal: $it" }
|
||||
}
|
||||
}.get().map(
|
||||
{
|
||||
DataResult.success(it)
|
||||
success(it)
|
||||
},
|
||||
{ e0 ->
|
||||
ops.getIntStream(input).flatMap {
|
||||
try {
|
||||
DataResult.success(Pair(Decimal.fromByteArray(
|
||||
success(Pair(Decimal.fromByteArray(
|
||||
it
|
||||
.collect(::ByteArrayList, { v, a -> v.add(a.toByte()) }, ByteArrayList::addAll)
|
||||
.toByteArray()
|
||||
), ops.empty()))
|
||||
} catch (err: NumberFormatException) {
|
||||
DataResult.error { "Failed to convert array of bytes into Decimal: $it" }
|
||||
failure { "Failed to convert array of bytes into Decimal: $it" }
|
||||
}
|
||||
}.get().map(
|
||||
{
|
||||
DataResult.success(it)
|
||||
success(it)
|
||||
},
|
||||
{ e1 ->
|
||||
ops.getNumberValue(input).flatMap {
|
||||
DataResult.success(Pair(Decimal(it.toString()), ops.empty()))
|
||||
success(Pair(Decimal(it.toString()), ops.empty()))
|
||||
}.get().map(
|
||||
{
|
||||
DataResult.success(it)
|
||||
success(it)
|
||||
},
|
||||
{ e2 ->
|
||||
DataResult.error { "None of attempts at decoding Decimal were successful: ${e0.message()}; ${e1.message()}; ${e2.message()}" }
|
||||
failure { "None of attempts at decoding Decimal were successful: ${e0.message()}; ${e1.message()}; ${e2.message()}" }
|
||||
}
|
||||
)
|
||||
}
|
||||
|
@ -11,7 +11,9 @@ 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
|
||||
@ -38,15 +40,15 @@ class IngredientMatrixCodec(ingredientCodec: Codec<Ingredient>) : Codec<IIngredi
|
||||
it.group(
|
||||
Codec.list(Codec.STRING)
|
||||
.flatXmap(
|
||||
{ DataResult.success(it) },
|
||||
{ if (it.iterator().map { it.length }.allEqual()) DataResult.success(it) else DataResult.error { "One or more of patten strings differ in length" } }
|
||||
{ success(it) },
|
||||
{ if (it.iterator().map { it.length }.allEqual()) success(it) else failure { "One or more of patten strings differ in length" } }
|
||||
)
|
||||
.fieldOf("pattern").forGetter(Handwritten::pattern),
|
||||
Codec.unboundedMap(
|
||||
Codec.STRING
|
||||
.flatXmap(
|
||||
{ if (it.length == 1) DataResult.success(it[0]) else DataResult.error { "Ingredient key must be exactly 1 symbol in length, '$it' is invalid" } },
|
||||
{ DataResult.success(it.toString()) }
|
||||
{ if (it.length == 1) success(it[0]) else failure { "Ingredient key must be exactly 1 symbol in length, '$it' is invalid" } },
|
||||
{ success(it.toString()) }
|
||||
), ingredientCodec).fieldOf("key").forGetter(Handwritten::key)
|
||||
).apply(it, ::Handwritten)
|
||||
}
|
||||
@ -69,11 +71,11 @@ class IngredientMatrixCodec(ingredientCodec: Codec<Ingredient>) : Codec<IIngredi
|
||||
}
|
||||
|
||||
if (errors.isNotEmpty()) {
|
||||
DataResult.error { "Failed to decode ingredient matrix: ${errors.joinToString { it.get() }}" }
|
||||
failure { "Failed to decode ingredient matrix: ${errors.joinToString { it.get() }}" }
|
||||
} else if (ingredients.isEmpty()) {
|
||||
DataResult.error { "Ingredient list is empty" }
|
||||
failure { "Ingredient list is empty" }
|
||||
} else if (!ingredients.iterator().map { it.size }.allEqual()) {
|
||||
DataResult.error { "Ingredient list is not a matrix (one or multiple of rows are mismatched size)" }
|
||||
failure { "Ingredient list is not a matrix (one or multiple of rows are mismatched size)" }
|
||||
} else {
|
||||
val result = IngredientMatrix(ingredients.first().size, ingredients.size)
|
||||
|
||||
@ -83,16 +85,16 @@ class IngredientMatrixCodec(ingredientCodec: Codec<Ingredient>) : Codec<IIngredi
|
||||
}
|
||||
}
|
||||
|
||||
DataResult.success(Pair(result, ops.empty()))
|
||||
success(Pair(result, ops.empty()))
|
||||
}
|
||||
},
|
||||
{ err1 ->
|
||||
handwrittenCodec.decode(ops, input).get().map(
|
||||
{
|
||||
DataResult.success(it)
|
||||
success(it)
|
||||
},
|
||||
{
|
||||
DataResult.error { "Failed to decode ingredients as list: ${err1.message()} and as pattern/dictionary: ${it.message()}" }
|
||||
failure { "Failed to decode ingredients as list: ${err1.message()} and as pattern/dictionary: ${it.message()}" }
|
||||
}
|
||||
).flatMap {
|
||||
val handwritten = it.first
|
||||
@ -100,12 +102,12 @@ class IngredientMatrixCodec(ingredientCodec: Codec<Ingredient>) : Codec<IIngredi
|
||||
|
||||
for ((row, pattern) in handwritten.pattern.withIndex()) {
|
||||
for ((column, symbol) in pattern.withIndex()) {
|
||||
val ingredient = if (symbol == ' ') handwritten.key[symbol] ?: Ingredient.EMPTY else handwritten.key[symbol] ?: return@flatMap DataResult.error { "Unknown ingredient with index '$symbol'" }
|
||||
val ingredient = if (symbol == ' ') handwritten.key[symbol] ?: Ingredient.EMPTY else handwritten.key[symbol] ?: return@flatMap failure { "Unknown ingredient with index '$symbol'" }
|
||||
result[column, row] = ingredient
|
||||
}
|
||||
}
|
||||
|
||||
DataResult.success(Pair(result, ops.empty()))
|
||||
success(Pair(result, ops.empty()))
|
||||
}
|
||||
}
|
||||
)
|
||||
|
@ -5,6 +5,8 @@ import com.mojang.serialization.Codec
|
||||
import com.mojang.serialization.DataResult
|
||||
import com.mojang.serialization.DynamicOps
|
||||
import net.minecraft.nbt.NbtOps
|
||||
import ru.dbotthepony.mc.otm.core.failure
|
||||
import ru.dbotthepony.mc.otm.core.success
|
||||
import java.util.UUID
|
||||
import java.util.stream.LongStream
|
||||
|
||||
@ -13,10 +15,10 @@ import java.util.stream.LongStream
|
||||
object UUIDCodec : Codec<UUID> {
|
||||
override fun <T : Any> encode(input: UUID, ops: DynamicOps<T>, prefix: T): DataResult<T> {
|
||||
if (ops === NbtOps.INSTANCE) {
|
||||
return DataResult.success((ops as DynamicOps<T>).createLongList(LongStream.of(input.mostSignificantBits, input.leastSignificantBits)))
|
||||
return success((ops as DynamicOps<T>).createLongList(LongStream.of(input.mostSignificantBits, input.leastSignificantBits)))
|
||||
}
|
||||
|
||||
return DataResult.success(ops.createString(input.toString()))
|
||||
return success(ops.createString(input.toString()))
|
||||
}
|
||||
|
||||
override fun <T : Any> decode(ops: DynamicOps<T>, input: T): DataResult<Pair<UUID, T>> {
|
||||
@ -25,25 +27,25 @@ object UUIDCodec : Codec<UUID> {
|
||||
|
||||
if (l.size == 4) {
|
||||
// 4 int
|
||||
DataResult.success(Pair(UUID((l[0] shl 32) or l[1], (l[2] shl 32) or l[3]), ops.empty()))
|
||||
success(Pair(UUID((l[0] shl 32) or l[1], (l[2] shl 32) or l[3]), ops.empty()))
|
||||
} else if (l.size == 2) {
|
||||
DataResult.success(Pair(UUID(l[0], l[1]), ops.empty()))
|
||||
success(Pair(UUID(l[0], l[1]), ops.empty()))
|
||||
} else {
|
||||
DataResult.error { "Can't construct UUID from ${l.size} elements" }
|
||||
failure { "Can't construct UUID from ${l.size} elements" }
|
||||
}
|
||||
}.get().map(
|
||||
{
|
||||
DataResult.success(it)
|
||||
success(it)
|
||||
},
|
||||
{ e0 ->
|
||||
ops.getStringValue(input).map {
|
||||
Pair(UUID.fromString(it), ops.empty())
|
||||
}.get().map(
|
||||
{
|
||||
DataResult.success(it)
|
||||
success(it)
|
||||
},
|
||||
{
|
||||
DataResult.error { "Unable to deserialize UUID: ${e0.message()}; ${it.message()}" }
|
||||
failure { "Unable to deserialize UUID: ${e0.message()}; ${it.message()}" }
|
||||
}
|
||||
)
|
||||
}
|
||||
|
@ -11,7 +11,9 @@ 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
|
||||
@ -103,19 +105,19 @@ class ComputeAction(
|
||||
val pair = this to this
|
||||
|
||||
override fun <T : Any> encode(input: Constant, ops: DynamicOps<T>, prefix: T): DataResult<T> {
|
||||
return DataResult.success(ops.createString(symbol + input.matter.toString()))
|
||||
return success(ops.createString(symbol + input.matter.toString()))
|
||||
}
|
||||
|
||||
override fun <T : Any> decode(ops: DynamicOps<T>, input: T): DataResult<Pair<Constant, T>> {
|
||||
return ops.getStringValue(input).flatMap {
|
||||
if (it[0] == symbol) {
|
||||
try {
|
||||
DataResult.success(Pair(Constant(Decimal(it.substring(1).trim()), it.substring(1).trim().toDouble(), fn, fn), ops.empty()))
|
||||
success(Pair(Constant(Decimal(it.substring(1).trim()), it.substring(1).trim().toDouble(), fn, fn), ops.empty()))
|
||||
} catch (err: NumberFormatException) {
|
||||
DataResult.error { "Not a number: ${it.substring(1).trim()} (input string: $it)" }
|
||||
failure { "Not a number: ${it.substring(1).trim()} (input string: $it)" }
|
||||
}
|
||||
} else {
|
||||
DataResult.error { "Input string does not match expected operand: expected $symbol, got ${it[0]} (for input $it)" }
|
||||
failure { "Input string does not match expected operand: expected $symbol, got ${it[0]} (for input $it)" }
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -127,15 +129,15 @@ class ComputeAction(
|
||||
|
||||
private object PlainStringConstantCodec : Codec<Constant>, Predicate<Constant> {
|
||||
override fun <T : Any> encode(input: Constant, ops: DynamicOps<T>, prefix: T): DataResult<T> {
|
||||
return DataResult.success(ops.createString(input.matter.toString()))
|
||||
return success(ops.createString(input.matter.toString()))
|
||||
}
|
||||
|
||||
override fun <T : Any> decode(ops: DynamicOps<T>, input: T): DataResult<Pair<Constant, T>> {
|
||||
val result = ops.getNumberValue(input).flatMap {
|
||||
try {
|
||||
DataResult.success(Pair(Constant(Decimal(it.toString()), it.toDouble(), IMatterFunction.PLUS, IMatterFunction.PLUS), ops.empty()))
|
||||
success(Pair(Constant(Decimal(it.toString()), it.toDouble(), IMatterFunction.PLUS, IMatterFunction.PLUS), ops.empty()))
|
||||
} catch (err: NumberFormatException) {
|
||||
DataResult.error { "Not a number: $it" }
|
||||
failure { "Not a number: $it" }
|
||||
}
|
||||
}
|
||||
|
||||
@ -144,9 +146,9 @@ class ComputeAction(
|
||||
|
||||
return ops.getStringValue(input).flatMap {
|
||||
try {
|
||||
DataResult.success(Pair(Constant(Decimal(it), it.toDouble(), IMatterFunction.PLUS, IMatterFunction.PLUS), ops.empty()))
|
||||
success(Pair(Constant(Decimal(it), it.toDouble(), IMatterFunction.PLUS, IMatterFunction.PLUS), ops.empty()))
|
||||
} catch (err: NumberFormatException) {
|
||||
DataResult.error { "Not a number: $it" }
|
||||
failure { "Not a number: $it" }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user