From a351612a5335b161c919730bbfeb7860010600cf Mon Sep 17 00:00:00 2001 From: "max." Date: Tue, 15 Oct 2024 19:48:57 +0200 Subject: [PATCH] Fix SimpleFluidIngredient's StreamCodec, cleanup --- .../neoforge/common/NeoForgeMod.java | 3 +- .../crafting/FluidIngredientCodecs.java | 32 ++++++++++++++++++- .../crafting/SimpleFluidIngredient.java | 31 ++++-------------- 3 files changed, 40 insertions(+), 26 deletions(-) diff --git a/src/main/java/net/neoforged/neoforge/common/NeoForgeMod.java b/src/main/java/net/neoforged/neoforge/common/NeoForgeMod.java index e88b5a76423..5352186d87a 100644 --- a/src/main/java/net/neoforged/neoforge/common/NeoForgeMod.java +++ b/src/main/java/net/neoforged/neoforge/common/NeoForgeMod.java @@ -144,6 +144,7 @@ import net.neoforged.neoforge.fluids.crafting.CompoundFluidIngredient; import net.neoforged.neoforge.fluids.crafting.DataComponentFluidIngredient; import net.neoforged.neoforge.fluids.crafting.DifferenceFluidIngredient; +import net.neoforged.neoforge.fluids.crafting.FluidIngredientCodecs; import net.neoforged.neoforge.fluids.crafting.FluidIngredientType; import net.neoforged.neoforge.fluids.crafting.IntersectionFluidIngredient; import net.neoforged.neoforge.fluids.crafting.SimpleFluidIngredient; @@ -371,7 +372,7 @@ public class NeoForgeMod { public static final DeferredHolder, IngredientType> CUSTOM_DISPLAY_INGREDIENT = INGREDIENT_TYPES.register("custom_display", () -> new IngredientType<>(CustomDisplayIngredient.CODEC)); private static final DeferredRegister> FLUID_INGREDIENT_TYPES = DeferredRegister.create(NeoForgeRegistries.Keys.FLUID_INGREDIENT_TYPES, NeoForgeVersion.MOD_ID); - public static final DeferredHolder, FluidIngredientType> SIMPLE_FLUID_INGREDIENT_TYPE = FLUID_INGREDIENT_TYPES.register("simple", () -> new FluidIngredientType<>(SimpleFluidIngredient.MAP_CODEC)); + public static final DeferredHolder, FluidIngredientType> SIMPLE_FLUID_INGREDIENT_TYPE = FLUID_INGREDIENT_TYPES.register("simple", FluidIngredientCodecs::simpleType); public static final DeferredHolder, FluidIngredientType> COMPOUND_FLUID_INGREDIENT_TYPE = FLUID_INGREDIENT_TYPES.register("compound", () -> new FluidIngredientType<>(CompoundFluidIngredient.CODEC)); public static final DeferredHolder, FluidIngredientType> DATA_COMPONENT_FLUID_INGREDIENT_TYPE = FLUID_INGREDIENT_TYPES.register("components", () -> new FluidIngredientType<>(DataComponentFluidIngredient.CODEC)); public static final DeferredHolder, FluidIngredientType> DIFFERENCE_FLUID_INGREDIENT_TYPE = FLUID_INGREDIENT_TYPES.register("difference", () -> new FluidIngredientType<>(DifferenceFluidIngredient.CODEC)); diff --git a/src/main/java/net/neoforged/neoforge/fluids/crafting/FluidIngredientCodecs.java b/src/main/java/net/neoforged/neoforge/fluids/crafting/FluidIngredientCodecs.java index 6dff22e70d0..ce9299a7889 100644 --- a/src/main/java/net/neoforged/neoforge/fluids/crafting/FluidIngredientCodecs.java +++ b/src/main/java/net/neoforged/neoforge/fluids/crafting/FluidIngredientCodecs.java @@ -7,12 +7,20 @@ import com.mojang.datafixers.util.Either; import com.mojang.serialization.Codec; +import com.mojang.serialization.DataResult; +import com.mojang.serialization.DynamicOps; +import com.mojang.serialization.MapCodec; +import com.mojang.serialization.MapLike; +import com.mojang.serialization.RecordBuilder; +import java.util.stream.Stream; import net.minecraft.network.RegistryFriendlyByteBuf; import net.minecraft.network.codec.ByteBufCodecs; import net.minecraft.network.codec.StreamCodec; import net.neoforged.neoforge.registries.NeoForgeRegistries; +import org.jetbrains.annotations.ApiStatus; -class FluidIngredientCodecs { +@ApiStatus.Internal +public class FluidIngredientCodecs { static Codec codec() { return Codec.xor( NeoForgeRegistries.FLUID_INGREDIENT_TYPES.byNameCodec().dispatch("neoforge:ingredient_type", FluidIngredient::getType, FluidIngredientType::codec), @@ -26,4 +34,26 @@ static StreamCodec streamCodec() { return ByteBufCodecs.registry(NeoForgeRegistries.Keys.FLUID_INGREDIENT_TYPES) .dispatch(FluidIngredient::getType, FluidIngredientType::streamCodec); } + + @ApiStatus.Internal + public static FluidIngredientType simpleType() { + MapCodec erroringMapCodec = new MapCodec<>() { + @Override + public Stream keys(DynamicOps dynamicOps) { + return Stream.empty(); + } + + @Override + public DataResult decode(DynamicOps ops, MapLike mapLike) { + return DataResult.error(() -> "Simple fluid ingredients cannot be decoded using map syntax!"); + } + + @Override + public RecordBuilder encode(SimpleFluidIngredient ingredient, DynamicOps ops, RecordBuilder builder) { + return builder.withErrorsFrom(DataResult.error(() -> "Simple fluid ingredients cannot be encoded using map syntax! Please use vanilla syntax (namespaced:item or #tag) instead!")); + } + }; + + return new FluidIngredientType<>(erroringMapCodec, SimpleFluidIngredient.CONTENTS_STREAM_CODEC); + } } diff --git a/src/main/java/net/neoforged/neoforge/fluids/crafting/SimpleFluidIngredient.java b/src/main/java/net/neoforged/neoforge/fluids/crafting/SimpleFluidIngredient.java index 9b20cf829ac..49eb07bae8e 100644 --- a/src/main/java/net/neoforged/neoforge/fluids/crafting/SimpleFluidIngredient.java +++ b/src/main/java/net/neoforged/neoforge/fluids/crafting/SimpleFluidIngredient.java @@ -6,15 +6,13 @@ package net.neoforged.neoforge.fluids.crafting; import com.mojang.serialization.Codec; -import com.mojang.serialization.DataResult; -import com.mojang.serialization.DynamicOps; -import com.mojang.serialization.MapCodec; -import com.mojang.serialization.MapLike; -import com.mojang.serialization.RecordBuilder; import java.util.stream.Stream; import net.minecraft.core.Holder; import net.minecraft.core.HolderSet; import net.minecraft.core.registries.Registries; +import net.minecraft.network.RegistryFriendlyByteBuf; +import net.minecraft.network.codec.ByteBufCodecs; +import net.minecraft.network.codec.StreamCodec; import net.minecraft.resources.HolderSetCodec; import net.minecraft.util.ExtraCodecs; import net.minecraft.world.level.material.Fluid; @@ -30,29 +28,14 @@ * though in JSON, it is still written without a type field, see {@link FluidIngredientCodecs#codec()} */ public class SimpleFluidIngredient extends FluidIngredient { - public static final Codec> HOLDER_SET_NO_EMPTY_FLUID = HolderSetCodec.create( + private static final Codec> HOLDER_SET_NO_EMPTY_FLUID = HolderSetCodec.create( Registries.FLUID, FluidStack.FLUID_NON_EMPTY_CODEC, false); - public static final Codec CODEC = ExtraCodecs.nonEmptyHolderSet(HOLDER_SET_NO_EMPTY_FLUID) + static final Codec CODEC = ExtraCodecs.nonEmptyHolderSet(HOLDER_SET_NO_EMPTY_FLUID) .xmap(SimpleFluidIngredient::new, SimpleFluidIngredient::fluidSet); - // Note: This map codec explicitly always errors, since we want to force people to use HolderSet syntax! - public static final MapCodec MAP_CODEC = new MapCodec<>() { - @Override - public Stream keys(DynamicOps dynamicOps) { - return Stream.empty(); - } - - @Override - public DataResult decode(DynamicOps ops, MapLike mapLike) { - return DataResult.error(() -> "Simple fluid ingredients cannot be decoded using map syntax!"); - } - - @Override - public RecordBuilder encode(SimpleFluidIngredient ingredient, DynamicOps ops, RecordBuilder builder) { - return builder.withErrorsFrom(DataResult.error(() -> "Simple fluid ingredients cannot be encoded using map syntax! Please use vanilla syntax (namespaced:item or #tag) instead!")); - } - }; + static final StreamCodec CONTENTS_STREAM_CODEC = ByteBufCodecs.holderSet(Registries.FLUID) + .map(SimpleFluidIngredient::new, SimpleFluidIngredient::fluidSet); private final HolderSet values;