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 d2e04238e..c577e9eb8 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/data/Codec2RecipeSerializer.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/data/Codec2RecipeSerializer.kt @@ -39,6 +39,9 @@ class Codec2RecipeSerializer>( get() = checkNotNull(context.idStack.lastOrNull()) { "Not currently deserializing recipe" } val ingredients: Codec get() = ActualIngredientCodec + + val isNetwork: Boolean + get() = context.isNetwork > 0 } private val codec = codec.invoke(Context()) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/data/RecipeWrapperCodec.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/data/RecipeWrapperCodec.kt new file mode 100644 index 000000000..7374cd200 --- /dev/null +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/data/RecipeWrapperCodec.kt @@ -0,0 +1,54 @@ +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 +import com.mojang.serialization.DataResult +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>(val recipe: R, val data: JsonObject) + +fun > RecipeSerializer.codec(context: Codec2RecipeSerializer<*>.Context): Codec> { + return object : Codec> { + override fun encode(input: RecipePair, ops: DynamicOps, prefix: T): DataResult { + if (context.isNetwork) { + val buffer = FriendlyByteBuf(UnpooledByteBufAllocator.DEFAULT.heapBuffer()) + buffer.writeResourceLocation(input.recipe.id) + toNetwork(buffer, input.recipe) + buffer.writerIndex(0) + buffer.readerIndex(0) + val array = ByteBuffer.allocate(buffer.readableBytes()) + buffer.readBytes(array) + 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 })) + } + } + + override fun decode(ops: DynamicOps, input: T): DataResult, T>> { + if (context.isNetwork) { + return ops.getByteBuffer(input).map { + it.position(0) + val buffer = FriendlyByteBuf(UnpooledByteBufAllocator.DEFAULT.heapBuffer()) + buffer.writeBytes(it) + buffer.writerIndex(0) + buffer.readerIndex(0) + Pair(RecipePair(fromNetwork(buffer.readResourceLocation(), buffer)!!, JsonObject()), ops.empty()) + } + } 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())) + } + } + } +} diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/item/FluidCapsuleItem.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/item/FluidCapsuleItem.kt index 782884378..d41260d93 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/item/FluidCapsuleItem.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/item/FluidCapsuleItem.kt @@ -106,7 +106,7 @@ class FluidCapsuleItem(val capacity: IntSupplier) : Item(Properties().stacksTo(6 actionResult.result } else { val state = level.getBlockState(hitPos) - val placePos = if (state.block is LiquidBlockContainer && (state.block as LiquidBlockContainer).canPlaceLiquid(player, level, hitPos, state, fluid.fluid)) hitPos else nextPos + val placePos = if (state.block is LiquidBlockContainer && (state.block as LiquidBlockContainer).canPlaceLiquid(level, hitPos, state, fluid.fluid)) hitPos else nextPos val actionResult = FluidUtil.tryPlaceFluid(player, level, hand, placePos, targetItem, fluid) if (!actionResult.isSuccess) return InteractionResultHolder.pass(item) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/EnergyContainerRecipe.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/EnergyContainerRecipe.kt index 9f3667807..6541d8ab9 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/EnergyContainerRecipe.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/EnergyContainerRecipe.kt @@ -1,9 +1,11 @@ package ru.dbotthepony.mc.otm.recipe +import com.google.gson.JsonObject import com.mojang.serialization.Codec import net.minecraft.core.NonNullList import net.minecraft.core.RegistryAccess import net.minecraft.network.FriendlyByteBuf +import net.minecraft.resources.ResourceLocation import net.minecraft.world.inventory.CraftingContainer import net.minecraft.world.item.ItemStack import net.minecraft.world.item.crafting.CraftingBookCategory @@ -99,16 +101,12 @@ class EnergyContainerRecipe(val parent: ShapedRecipe) : CraftingRecipe, IShapedR } companion object : RecipeSerializer { - private val codec by lazy { - RecipeSerializer.SHAPED_RECIPE.codec().xmap(::EnergyContainerRecipe, EnergyContainerRecipe::parent) + override fun fromNetwork(id: ResourceLocation, data: FriendlyByteBuf): EnergyContainerRecipe? { + return ShapedRecipe.Serializer.SHAPED_RECIPE.fromNetwork(id, data)?.let(::EnergyContainerRecipe) } - override fun codec(): Codec { - return codec - } - - override fun fromNetwork(data: FriendlyByteBuf): EnergyContainerRecipe? { - return ShapedRecipe.Serializer.SHAPED_RECIPE.fromNetwork(data)?.let(::EnergyContainerRecipe) + override fun fromJson(p_44103_: ResourceLocation, p_44104_: JsonObject): EnergyContainerRecipe { + return EnergyContainerRecipe(ShapedRecipe.Serializer.SHAPED_RECIPE.fromJson(p_44103_, p_44104_)) } override fun toNetwork(buff: FriendlyByteBuf, value: EnergyContainerRecipe) { diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/ExplosiveHammerPrimingRecipe.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/ExplosiveHammerPrimingRecipe.kt index 5349dc5f7..b713fb6b0 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/ExplosiveHammerPrimingRecipe.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/ExplosiveHammerPrimingRecipe.kt @@ -19,7 +19,7 @@ import ru.dbotthepony.mc.otm.data.Codec2RecipeSerializer import ru.dbotthepony.mc.otm.item.tool.ExplosiveHammerItem import ru.dbotthepony.mc.otm.registry.MItems -class ExplosiveHammerPrimingRecipe(val payload: Ingredient, val id: ResourceLocation) : CraftingRecipe { +class ExplosiveHammerPrimingRecipe(val payload: Ingredient, private val id: ResourceLocation) : CraftingRecipe { override fun getId(): ResourceLocation { return id } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/MatterEntanglerRecipe.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/MatterEntanglerRecipe.kt index 4f512b37a..aa0633bbd 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/MatterEntanglerRecipe.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/MatterEntanglerRecipe.kt @@ -42,7 +42,7 @@ interface IMatterEntanglerRecipe : IMatteryRecipe { } open class MatterEntanglerRecipe( - val id: ResourceLocation, + private val id: ResourceLocation, override val ingredients: IIngredientMatrix, override val matter: Decimal, override val ticks: Double, diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/MatteryCookingRecipe.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/MatteryCookingRecipe.kt index ca1a3cea3..22aeea670 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/MatteryCookingRecipe.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/MatteryCookingRecipe.kt @@ -24,7 +24,7 @@ import ru.dbotthepony.mc.otm.registry.MItems import ru.dbotthepony.mc.otm.registry.MRecipes abstract class MatteryCookingRecipe( - val id: ResourceLocation, + private val id: ResourceLocation, val input: Ingredient, val output: Ingredient, val count: Int = 1, diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/PainterRecipe.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/PainterRecipe.kt index a9a5cbcee..8fff7cc78 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/PainterRecipe.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/PainterRecipe.kt @@ -31,7 +31,7 @@ import ru.dbotthepony.mc.otm.registry.MRecipes import java.util.function.Predicate abstract class AbstractPainterRecipe( - val id: ResourceLocation, + private val id: ResourceLocation, dyes: Map ) : IMatteryRecipe { override fun getId(): ResourceLocation { diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/PlatePressRecipe.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/PlatePressRecipe.kt index a5dd1c85e..143d7cd1d 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/PlatePressRecipe.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/PlatePressRecipe.kt @@ -24,7 +24,7 @@ import ru.dbotthepony.mc.otm.data.minRange import ru.dbotthepony.mc.otm.registry.MItems class PlatePressRecipe( - val id: ResourceLocation, + private val id: ResourceLocation, val input: Ingredient, val output: Ingredient, val count: Int = 1, diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/UpgradeRecipe.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/UpgradeRecipe.kt index c5f969c7a..a946509eb 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/UpgradeRecipe.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/recipe/UpgradeRecipe.kt @@ -1,16 +1,12 @@ package ru.dbotthepony.mc.otm.recipe import com.google.common.collect.ImmutableList -import com.google.gson.JsonObject -import com.google.gson.JsonPrimitive import com.mojang.serialization.Codec import com.mojang.serialization.codecs.RecordCodecBuilder import net.minecraft.core.NonNullList import net.minecraft.core.RegistryAccess import net.minecraft.nbt.CompoundTag -import net.minecraft.network.FriendlyByteBuf import net.minecraft.resources.ResourceLocation -import net.minecraft.util.GsonHelper import net.minecraft.util.StringRepresentable import net.minecraft.world.inventory.CraftingContainer import net.minecraft.world.item.ItemStack @@ -25,67 +21,65 @@ import net.minecraftforge.common.crafting.IShapedRecipe import ru.dbotthepony.mc.otm.container.util.stream import ru.dbotthepony.mc.otm.core.nbt.set import ru.dbotthepony.mc.otm.core.registryName -import ru.dbotthepony.mc.otm.core.set import ru.dbotthepony.mc.otm.core.tagNotNull -import ru.dbotthepony.mc.otm.core.util.readBinaryJson -import ru.dbotthepony.mc.otm.core.util.writeBinaryJson -import ru.dbotthepony.mc.otm.core.collect.stream import ru.dbotthepony.mc.otm.data.Codec2RecipeSerializer +import ru.dbotthepony.mc.otm.data.RecipePair +import ru.dbotthepony.mc.otm.data.codec import java.util.stream.Stream class UpgradeRecipe( - val parent: ShapedRecipe, + val parent: RecipePair, copyPaths: Stream, val source: ResourceLocation, -) : CraftingRecipe, IShapedRecipe by parent { - constructor(parent: ShapedRecipe, copyPaths: Collection, source: ResourceLocation) : this(parent, copyPaths.stream(), source) +) : CraftingRecipe, IShapedRecipe by parent.recipe { + constructor(parent: RecipePair, copyPaths: Collection, source: ResourceLocation) : this(parent, copyPaths.stream(), source) override fun matches(p_44002_: CraftingContainer, p_44003_: Level): Boolean { - return parent.matches(p_44002_, p_44003_) + return parent.recipe.matches(p_44002_, p_44003_) } override fun canCraftInDimensions(p_43999_: Int, p_44000_: Int): Boolean { - return parent.canCraftInDimensions(p_43999_, p_44000_) + return parent.recipe.canCraftInDimensions(p_43999_, p_44000_) } override fun getResultItem(p_267052_: RegistryAccess): ItemStack { - return parent.getResultItem(p_267052_) + return parent.recipe.getResultItem(p_267052_) } override fun getRemainingItems(p_44004_: CraftingContainer): NonNullList { - return parent.getRemainingItems(p_44004_) + return parent.recipe.getRemainingItems(p_44004_) } override fun getIngredients(): NonNullList { - return parent.ingredients + return parent.recipe.ingredients } override fun isSpecial(): Boolean { - return parent.isSpecial + return parent.recipe.isSpecial } override fun showNotification(): Boolean { - return parent.showNotification() + return parent.recipe.showNotification() } override fun getGroup(): String { - return parent.group + return parent.recipe.group } override fun getToastSymbol(): ItemStack { - return parent.toastSymbol + return parent.recipe.toastSymbol } override fun isIncomplete(): Boolean { - return parent.isIncomplete + return parent.recipe.isIncomplete } override fun getType(): RecipeType<*> { - return parent.type + return parent.recipe.type } override fun category(): CraftingBookCategory { - return parent.category() + return parent.recipe.category() } enum class OpType : StringRepresentable { @@ -191,7 +185,7 @@ class UpgradeRecipe( val copyPaths: ImmutableList = copyPaths.collect(ImmutableList.toImmutableList()) override fun assemble(pInv: CraftingContainer, registryAccess: RegistryAccess): ItemStack { - val result = parent.assemble(pInv, registryAccess) + val result = parent.recipe.assemble(pInv, registryAccess) if (result.isEmpty) { return result @@ -225,7 +219,7 @@ class UpgradeRecipe( val CODEC = Codec2RecipeSerializer { p -> RecordCodecBuilder.create { it.group( - p.wrap(ShapedRecipe.Serializer.SHAPED_RECIPE).fieldOf("parent").forGetter(UpgradeRecipe::parent), + ShapedRecipe.Serializer.SHAPED_RECIPE.codec(p).fieldOf("parent").forGetter(UpgradeRecipe::parent), COPY_PATHS_CODEC.fieldOf("copyPaths").forGetter(UpgradeRecipe::copyPaths), ResourceLocation.CODEC.fieldOf("source").forGetter(UpgradeRecipe::source) ).apply(it, ::UpgradeRecipe) 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 f422251f6..7a6eb77ef 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/triggers/MCriterionTrigger.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/triggers/MCriterionTrigger.kt @@ -30,7 +30,7 @@ import java.util.function.Predicate // allows to support both 1.20.1 and 1.20.2 with ease // and has slightly less memory footprint than vanilla SimpleCriterionTrigger -abstract class MCriterionTrigger.AbstractInstance>(val id: ResourceLocation) : CriterionTrigger { +abstract class MCriterionTrigger.AbstractInstance>(private val id: ResourceLocation) : CriterionTrigger { override fun getId(): ResourceLocation { return id }