Fixes to data loading
This commit is contained in:
parent
e5a982ae13
commit
65cf8bf9df
@ -1,6 +1,7 @@
|
||||
package ru.dbotthepony.mc.otm.data
|
||||
|
||||
import com.google.gson.JsonDeserializationContext
|
||||
import com.google.gson.JsonNull
|
||||
import com.google.gson.JsonObject
|
||||
import com.google.gson.JsonSerializationContext
|
||||
import com.google.gson.JsonSyntaxException
|
||||
@ -31,7 +32,7 @@ class Codec2Serializer<T : Any>(val codec: Codec<T>, val embed: Boolean = false)
|
||||
if (embed) {
|
||||
return codec.fromJsonStrict(data)
|
||||
} else {
|
||||
return codec.fromJsonStrict(data["value"] ?: throw JsonSyntaxException("Missing 'value' element"))
|
||||
return codec.fromJsonStrict(data["value"] ?: JsonNull.INSTANCE)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -17,7 +17,16 @@ val DoublesCodec: Codec<MinMaxBounds.Doubles> = RecordCodecBuilder.create {
|
||||
it.group(
|
||||
Codec.DOUBLE.optionalFieldOf("min").forGetter { Optional.ofNullable(it.min) },
|
||||
Codec.DOUBLE.optionalFieldOf("max").forGetter { Optional.ofNullable(it.max) },
|
||||
).apply(it) { a, b -> MinMaxBounds.Doubles.between(a.orElse(null), b.orElse(null)) }
|
||||
).apply(it) { min, max ->
|
||||
if (min.isEmpty && max.isEmpty)
|
||||
MinMaxBounds.Doubles.ANY
|
||||
else if (min.isEmpty)
|
||||
MinMaxBounds.Doubles.atMost(max.get())
|
||||
else if (max.isEmpty)
|
||||
MinMaxBounds.Doubles.atLeast(min.get())
|
||||
else
|
||||
MinMaxBounds.Doubles.between(min.get(), max.get())
|
||||
}
|
||||
}
|
||||
|
||||
private val dealtReceived: Codec<Pair<MinMaxBounds.Doubles, MinMaxBounds.Doubles>> = RecordCodecBuilder.create {
|
||||
|
@ -1,6 +1,5 @@
|
||||
package ru.dbotthepony.mc.otm.data
|
||||
|
||||
import com.google.gson.JsonNull
|
||||
import com.google.gson.JsonObject
|
||||
import com.mojang.datafixers.util.Pair
|
||||
import com.mojang.serialization.Codec
|
||||
@ -9,10 +8,8 @@ import com.mojang.serialization.DynamicOps
|
||||
import com.mojang.serialization.JsonOps
|
||||
import io.netty.buffer.UnpooledByteBufAllocator
|
||||
import net.minecraft.network.FriendlyByteBuf
|
||||
import net.minecraft.resources.ResourceLocation
|
||||
import net.minecraft.world.item.crafting.Recipe
|
||||
import net.minecraft.world.item.crafting.RecipeSerializer
|
||||
import ru.dbotthepony.mc.otm.core.set
|
||||
import java.nio.ByteBuffer
|
||||
|
||||
data class RecipePair<R : Recipe<*>>(val recipe: R, val data: JsonObject)
|
||||
@ -31,7 +28,7 @@ fun <R : Recipe<*>> RecipeSerializer<R>.codec(context: Codec2RecipeSerializer<*>
|
||||
array.position(0)
|
||||
return DataResult.success(ops.createByteList(array))
|
||||
} else {
|
||||
return DataResult.success(JsonOps.INSTANCE.convertTo(ops, JsonObject().also { it["id"] = input.recipe.id.toString(); it["recipe"] = input.data }))
|
||||
return DataResult.success(JsonOps.INSTANCE.convertTo(ops, input.data))
|
||||
}
|
||||
}
|
||||
|
||||
@ -47,7 +44,7 @@ fun <R : Recipe<*>> RecipeSerializer<R>.codec(context: Codec2RecipeSerializer<*>
|
||||
}
|
||||
} else {
|
||||
val getJson = ops.convertTo(JsonOps.INSTANCE, input) as JsonObject
|
||||
return DataResult.success(Pair(RecipePair(fromJson(ResourceLocation(getJson["id"].asString), getJson["recipe"] as JsonObject), getJson["recipe"] as JsonObject), ops.empty()))
|
||||
return DataResult.success(Pair(RecipePair(fromJson(context.id, getJson), getJson), ops.empty()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user