Make main source set compile

This commit is contained in:
DBotThePony 2023-12-31 22:07:28 +07:00
parent aa0a283e06
commit 347be35184
Signed by: DBot
GPG Key ID: DCC23B5715498507
11 changed files with 89 additions and 40 deletions

View File

@ -39,6 +39,9 @@ class Codec2RecipeSerializer<S : Recipe<*>>(
get() = checkNotNull(context.idStack.lastOrNull()) { "Not currently deserializing recipe" } get() = checkNotNull(context.idStack.lastOrNull()) { "Not currently deserializing recipe" }
val ingredients: Codec<Ingredient> get() = ActualIngredientCodec val ingredients: Codec<Ingredient> get() = ActualIngredientCodec
val isNetwork: Boolean
get() = context.isNetwork > 0
} }
private val codec = codec.invoke(Context()) private val codec = codec.invoke(Context())

View File

@ -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<R : Recipe<*>>(val recipe: R, val data: JsonObject)
fun <R : Recipe<*>> RecipeSerializer<R>.codec(context: Codec2RecipeSerializer<*>.Context): Codec<RecipePair<R>> {
return object : Codec<RecipePair<R>> {
override fun <T : Any> encode(input: RecipePair<R>, ops: DynamicOps<T>, prefix: T): DataResult<T> {
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 <T : Any> decode(ops: DynamicOps<T>, input: T): DataResult<Pair<RecipePair<R>, 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()))
}
}
}
}

View File

@ -106,7 +106,7 @@ class FluidCapsuleItem(val capacity: IntSupplier) : Item(Properties().stacksTo(6
actionResult.result actionResult.result
} else { } else {
val state = level.getBlockState(hitPos) 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) val actionResult = FluidUtil.tryPlaceFluid(player, level, hand, placePos, targetItem, fluid)
if (!actionResult.isSuccess) return InteractionResultHolder.pass(item) if (!actionResult.isSuccess) return InteractionResultHolder.pass(item)

View File

@ -1,9 +1,11 @@
package ru.dbotthepony.mc.otm.recipe package ru.dbotthepony.mc.otm.recipe
import com.google.gson.JsonObject
import com.mojang.serialization.Codec import com.mojang.serialization.Codec
import net.minecraft.core.NonNullList import net.minecraft.core.NonNullList
import net.minecraft.core.RegistryAccess import net.minecraft.core.RegistryAccess
import net.minecraft.network.FriendlyByteBuf import net.minecraft.network.FriendlyByteBuf
import net.minecraft.resources.ResourceLocation
import net.minecraft.world.inventory.CraftingContainer import net.minecraft.world.inventory.CraftingContainer
import net.minecraft.world.item.ItemStack import net.minecraft.world.item.ItemStack
import net.minecraft.world.item.crafting.CraftingBookCategory import net.minecraft.world.item.crafting.CraftingBookCategory
@ -99,16 +101,12 @@ class EnergyContainerRecipe(val parent: ShapedRecipe) : CraftingRecipe, IShapedR
} }
companion object : RecipeSerializer<EnergyContainerRecipe> { companion object : RecipeSerializer<EnergyContainerRecipe> {
private val codec by lazy { override fun fromNetwork(id: ResourceLocation, data: FriendlyByteBuf): EnergyContainerRecipe? {
RecipeSerializer.SHAPED_RECIPE.codec().xmap(::EnergyContainerRecipe, EnergyContainerRecipe::parent) return ShapedRecipe.Serializer.SHAPED_RECIPE.fromNetwork(id, data)?.let(::EnergyContainerRecipe)
} }
override fun codec(): Codec<EnergyContainerRecipe> { override fun fromJson(p_44103_: ResourceLocation, p_44104_: JsonObject): EnergyContainerRecipe {
return codec return EnergyContainerRecipe(ShapedRecipe.Serializer.SHAPED_RECIPE.fromJson(p_44103_, p_44104_))
}
override fun fromNetwork(data: FriendlyByteBuf): EnergyContainerRecipe? {
return ShapedRecipe.Serializer.SHAPED_RECIPE.fromNetwork(data)?.let(::EnergyContainerRecipe)
} }
override fun toNetwork(buff: FriendlyByteBuf, value: EnergyContainerRecipe) { override fun toNetwork(buff: FriendlyByteBuf, value: EnergyContainerRecipe) {

View File

@ -19,7 +19,7 @@ import ru.dbotthepony.mc.otm.data.Codec2RecipeSerializer
import ru.dbotthepony.mc.otm.item.tool.ExplosiveHammerItem import ru.dbotthepony.mc.otm.item.tool.ExplosiveHammerItem
import ru.dbotthepony.mc.otm.registry.MItems 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 { override fun getId(): ResourceLocation {
return id return id
} }

View File

@ -42,7 +42,7 @@ interface IMatterEntanglerRecipe : IMatteryRecipe<CraftingContainer> {
} }
open class MatterEntanglerRecipe( open class MatterEntanglerRecipe(
val id: ResourceLocation, private val id: ResourceLocation,
override val ingredients: IIngredientMatrix, override val ingredients: IIngredientMatrix,
override val matter: Decimal, override val matter: Decimal,
override val ticks: Double, override val ticks: Double,

View File

@ -24,7 +24,7 @@ import ru.dbotthepony.mc.otm.registry.MItems
import ru.dbotthepony.mc.otm.registry.MRecipes import ru.dbotthepony.mc.otm.registry.MRecipes
abstract class MatteryCookingRecipe( abstract class MatteryCookingRecipe(
val id: ResourceLocation, private val id: ResourceLocation,
val input: Ingredient, val input: Ingredient,
val output: Ingredient, val output: Ingredient,
val count: Int = 1, val count: Int = 1,

View File

@ -31,7 +31,7 @@ import ru.dbotthepony.mc.otm.registry.MRecipes
import java.util.function.Predicate import java.util.function.Predicate
abstract class AbstractPainterRecipe( abstract class AbstractPainterRecipe(
val id: ResourceLocation, private val id: ResourceLocation,
dyes: Map<out DyeColor?, Int> dyes: Map<out DyeColor?, Int>
) : IMatteryRecipe<Container> { ) : IMatteryRecipe<Container> {
override fun getId(): ResourceLocation { override fun getId(): ResourceLocation {

View File

@ -24,7 +24,7 @@ import ru.dbotthepony.mc.otm.data.minRange
import ru.dbotthepony.mc.otm.registry.MItems import ru.dbotthepony.mc.otm.registry.MItems
class PlatePressRecipe( class PlatePressRecipe(
val id: ResourceLocation, private val id: ResourceLocation,
val input: Ingredient, val input: Ingredient,
val output: Ingredient, val output: Ingredient,
val count: Int = 1, val count: Int = 1,

View File

@ -1,16 +1,12 @@
package ru.dbotthepony.mc.otm.recipe package ru.dbotthepony.mc.otm.recipe
import com.google.common.collect.ImmutableList 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.Codec
import com.mojang.serialization.codecs.RecordCodecBuilder import com.mojang.serialization.codecs.RecordCodecBuilder
import net.minecraft.core.NonNullList import net.minecraft.core.NonNullList
import net.minecraft.core.RegistryAccess import net.minecraft.core.RegistryAccess
import net.minecraft.nbt.CompoundTag import net.minecraft.nbt.CompoundTag
import net.minecraft.network.FriendlyByteBuf
import net.minecraft.resources.ResourceLocation import net.minecraft.resources.ResourceLocation
import net.minecraft.util.GsonHelper
import net.minecraft.util.StringRepresentable import net.minecraft.util.StringRepresentable
import net.minecraft.world.inventory.CraftingContainer import net.minecraft.world.inventory.CraftingContainer
import net.minecraft.world.item.ItemStack 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.container.util.stream
import ru.dbotthepony.mc.otm.core.nbt.set import ru.dbotthepony.mc.otm.core.nbt.set
import ru.dbotthepony.mc.otm.core.registryName 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.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.Codec2RecipeSerializer
import ru.dbotthepony.mc.otm.data.RecipePair
import ru.dbotthepony.mc.otm.data.codec
import java.util.stream.Stream import java.util.stream.Stream
class UpgradeRecipe( class UpgradeRecipe(
val parent: ShapedRecipe, val parent: RecipePair<ShapedRecipe>,
copyPaths: Stream<Op>, copyPaths: Stream<Op>,
val source: ResourceLocation, val source: ResourceLocation,
) : CraftingRecipe, IShapedRecipe<CraftingContainer> by parent { ) : CraftingRecipe, IShapedRecipe<CraftingContainer> by parent.recipe {
constructor(parent: ShapedRecipe, copyPaths: Collection<Op>, source: ResourceLocation) : this(parent, copyPaths.stream(), source) constructor(parent: RecipePair<ShapedRecipe>, copyPaths: Collection<Op>, source: ResourceLocation) : this(parent, copyPaths.stream(), source)
override fun matches(p_44002_: CraftingContainer, p_44003_: Level): Boolean { 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 { 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 { 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<ItemStack> { override fun getRemainingItems(p_44004_: CraftingContainer): NonNullList<ItemStack> {
return parent.getRemainingItems(p_44004_) return parent.recipe.getRemainingItems(p_44004_)
} }
override fun getIngredients(): NonNullList<Ingredient> { override fun getIngredients(): NonNullList<Ingredient> {
return parent.ingredients return parent.recipe.ingredients
} }
override fun isSpecial(): Boolean { override fun isSpecial(): Boolean {
return parent.isSpecial return parent.recipe.isSpecial
} }
override fun showNotification(): Boolean { override fun showNotification(): Boolean {
return parent.showNotification() return parent.recipe.showNotification()
} }
override fun getGroup(): String { override fun getGroup(): String {
return parent.group return parent.recipe.group
} }
override fun getToastSymbol(): ItemStack { override fun getToastSymbol(): ItemStack {
return parent.toastSymbol return parent.recipe.toastSymbol
} }
override fun isIncomplete(): Boolean { override fun isIncomplete(): Boolean {
return parent.isIncomplete return parent.recipe.isIncomplete
} }
override fun getType(): RecipeType<*> { override fun getType(): RecipeType<*> {
return parent.type return parent.recipe.type
} }
override fun category(): CraftingBookCategory { override fun category(): CraftingBookCategory {
return parent.category() return parent.recipe.category()
} }
enum class OpType : StringRepresentable { enum class OpType : StringRepresentable {
@ -191,7 +185,7 @@ class UpgradeRecipe(
val copyPaths: ImmutableList<Op> = copyPaths.collect(ImmutableList.toImmutableList()) val copyPaths: ImmutableList<Op> = copyPaths.collect(ImmutableList.toImmutableList())
override fun assemble(pInv: CraftingContainer, registryAccess: RegistryAccess): ItemStack { override fun assemble(pInv: CraftingContainer, registryAccess: RegistryAccess): ItemStack {
val result = parent.assemble(pInv, registryAccess) val result = parent.recipe.assemble(pInv, registryAccess)
if (result.isEmpty) { if (result.isEmpty) {
return result return result
@ -225,7 +219,7 @@ class UpgradeRecipe(
val CODEC = Codec2RecipeSerializer<UpgradeRecipe> { p -> val CODEC = Codec2RecipeSerializer<UpgradeRecipe> { p ->
RecordCodecBuilder.create { RecordCodecBuilder.create {
it.group( 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), COPY_PATHS_CODEC.fieldOf("copyPaths").forGetter(UpgradeRecipe::copyPaths),
ResourceLocation.CODEC.fieldOf("source").forGetter(UpgradeRecipe::source) ResourceLocation.CODEC.fieldOf("source").forGetter(UpgradeRecipe::source)
).apply(it, ::UpgradeRecipe) ).apply(it, ::UpgradeRecipe)

View File

@ -30,7 +30,7 @@ import java.util.function.Predicate
// allows to support both 1.20.1 and 1.20.2 with ease // allows to support both 1.20.1 and 1.20.2 with ease
// and has slightly less memory footprint than vanilla SimpleCriterionTrigger // and has slightly less memory footprint than vanilla SimpleCriterionTrigger
abstract class MCriterionTrigger<T : MCriterionTrigger<T>.AbstractInstance>(val id: ResourceLocation) : CriterionTrigger<T> { abstract class MCriterionTrigger<T : MCriterionTrigger<T>.AbstractInstance>(private val id: ResourceLocation) : CriterionTrigger<T> {
override fun getId(): ResourceLocation { override fun getId(): ResourceLocation {
return id return id
} }