Skip to content

Commit

Permalink
Fix SimpleFluidIngredient's StreamCodec, cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxNeedsSnacks committed Oct 15, 2024
1 parent 2073b15 commit a351612
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 26 deletions.
3 changes: 2 additions & 1 deletion src/main/java/net/neoforged/neoforge/common/NeoForgeMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -371,7 +372,7 @@ public class NeoForgeMod {
public static final DeferredHolder<IngredientType<?>, IngredientType<CustomDisplayIngredient>> CUSTOM_DISPLAY_INGREDIENT = INGREDIENT_TYPES.register("custom_display", () -> new IngredientType<>(CustomDisplayIngredient.CODEC));

private static final DeferredRegister<FluidIngredientType<?>> FLUID_INGREDIENT_TYPES = DeferredRegister.create(NeoForgeRegistries.Keys.FLUID_INGREDIENT_TYPES, NeoForgeVersion.MOD_ID);
public static final DeferredHolder<FluidIngredientType<?>, FluidIngredientType<SimpleFluidIngredient>> SIMPLE_FLUID_INGREDIENT_TYPE = FLUID_INGREDIENT_TYPES.register("simple", () -> new FluidIngredientType<>(SimpleFluidIngredient.MAP_CODEC));
public static final DeferredHolder<FluidIngredientType<?>, FluidIngredientType<SimpleFluidIngredient>> SIMPLE_FLUID_INGREDIENT_TYPE = FLUID_INGREDIENT_TYPES.register("simple", FluidIngredientCodecs::simpleType);
public static final DeferredHolder<FluidIngredientType<?>, FluidIngredientType<CompoundFluidIngredient>> COMPOUND_FLUID_INGREDIENT_TYPE = FLUID_INGREDIENT_TYPES.register("compound", () -> new FluidIngredientType<>(CompoundFluidIngredient.CODEC));
public static final DeferredHolder<FluidIngredientType<?>, FluidIngredientType<DataComponentFluidIngredient>> DATA_COMPONENT_FLUID_INGREDIENT_TYPE = FLUID_INGREDIENT_TYPES.register("components", () -> new FluidIngredientType<>(DataComponentFluidIngredient.CODEC));
public static final DeferredHolder<FluidIngredientType<?>, FluidIngredientType<DifferenceFluidIngredient>> DIFFERENCE_FLUID_INGREDIENT_TYPE = FLUID_INGREDIENT_TYPES.register("difference", () -> new FluidIngredientType<>(DifferenceFluidIngredient.CODEC));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<FluidIngredient> codec() {
return Codec.xor(
NeoForgeRegistries.FLUID_INGREDIENT_TYPES.byNameCodec().<FluidIngredient>dispatch("neoforge:ingredient_type", FluidIngredient::getType, FluidIngredientType::codec),
Expand All @@ -26,4 +34,26 @@ static StreamCodec<RegistryFriendlyByteBuf, FluidIngredient> streamCodec() {
return ByteBufCodecs.registry(NeoForgeRegistries.Keys.FLUID_INGREDIENT_TYPES)
.dispatch(FluidIngredient::getType, FluidIngredientType::streamCodec);
}

@ApiStatus.Internal
public static FluidIngredientType<SimpleFluidIngredient> simpleType() {
MapCodec<SimpleFluidIngredient> erroringMapCodec = new MapCodec<>() {
@Override
public <T> Stream<T> keys(DynamicOps<T> dynamicOps) {
return Stream.empty();
}

@Override
public <T> DataResult<SimpleFluidIngredient> decode(DynamicOps<T> ops, MapLike<T> mapLike) {
return DataResult.error(() -> "Simple fluid ingredients cannot be decoded using map syntax!");
}

@Override
public <T> RecordBuilder<T> encode(SimpleFluidIngredient ingredient, DynamicOps<T> ops, RecordBuilder<T> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,29 +28,14 @@
* though in JSON, it is still written <b>without</b> a type field, see {@link FluidIngredientCodecs#codec()}
*/
public class SimpleFluidIngredient extends FluidIngredient {
public static final Codec<HolderSet<Fluid>> HOLDER_SET_NO_EMPTY_FLUID = HolderSetCodec.create(
private static final Codec<HolderSet<Fluid>> HOLDER_SET_NO_EMPTY_FLUID = HolderSetCodec.create(
Registries.FLUID, FluidStack.FLUID_NON_EMPTY_CODEC, false);

public static final Codec<SimpleFluidIngredient> CODEC = ExtraCodecs.nonEmptyHolderSet(HOLDER_SET_NO_EMPTY_FLUID)
static final Codec<SimpleFluidIngredient> 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<SimpleFluidIngredient> MAP_CODEC = new MapCodec<>() {
@Override
public <T> Stream<T> keys(DynamicOps<T> dynamicOps) {
return Stream.empty();
}

@Override
public <T> DataResult<SimpleFluidIngredient> decode(DynamicOps<T> ops, MapLike<T> mapLike) {
return DataResult.error(() -> "Simple fluid ingredients cannot be decoded using map syntax!");
}

@Override
public <T> RecordBuilder<T> encode(SimpleFluidIngredient ingredient, DynamicOps<T> ops, RecordBuilder<T> 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<RegistryFriendlyByteBuf, SimpleFluidIngredient> CONTENTS_STREAM_CODEC = ByteBufCodecs.holderSet(Registries.FLUID)
.map(SimpleFluidIngredient::new, SimpleFluidIngredient::fluidSet);

private final HolderSet<Fluid> values;

Expand Down

0 comments on commit a351612

Please sign in to comment.