Skip to content

Commit 24b9991

Browse files
authored
Fix NeoForge fluid ingredient types not being registered (#1783)
1 parent 709add9 commit 24b9991

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

src/main/java/net/neoforged/neoforge/common/NeoForgeMod.java

+1
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,7 @@ public NeoForgeMod(IEventBus modEventBus, Dist dist, ModContainer container) {
535535
ITEM_SUB_PREDICATES.register(modEventBus);
536536
SLOT_DISPLAY_TYPES.register(modEventBus);
537537
INGREDIENT_TYPES.register(modEventBus);
538+
FLUID_INGREDIENT_TYPES.register(modEventBus);
538539
CONDITION_CODECS.register(modEventBus);
539540
GLOBAL_LOOT_MODIFIER_SERIALIZERS.register(modEventBus);
540541
NeoForge.EVENT_BUS.addListener(this::serverStopping);

src/main/java/net/neoforged/neoforge/fluids/crafting/FluidIngredientCodecs.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ public class FluidIngredientCodecs {
2424
static Codec<FluidIngredient> codec() {
2525
return Codec.xor(
2626
NeoForgeRegistries.FLUID_INGREDIENT_TYPES.byNameCodec().<FluidIngredient>dispatch("neoforge:ingredient_type", FluidIngredient::getType, FluidIngredientType::codec),
27-
SimpleFluidIngredient.CODEC).xmap(either -> either.map(i -> i, i -> i), ingredient -> switch (ingredient) {
27+
// Use lazy: SimpleFluidIngredient.CODEC is initialized after FluidIngredient.CODEC which lives in a superclass.
28+
Codec.lazyInitialized(() -> SimpleFluidIngredient.CODEC))
29+
.xmap(either -> either.map(i -> i, i -> i), ingredient -> switch (ingredient) {
2830
case SimpleFluidIngredient simple -> Either.right(simple);
2931
default -> Either.left(ingredient);
3032
});

tests/src/junit/java/net/neoforged/neoforge/unittest/FluidIngredientTests.java

+29
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package net.neoforged.neoforge.unittest;
77

8+
import com.mojang.serialization.JsonOps;
89
import java.util.List;
910
import java.util.stream.Stream;
1011
import net.minecraft.core.component.DataComponentPatch;
@@ -17,6 +18,7 @@
1718
import net.neoforged.neoforge.fluids.crafting.CompoundFluidIngredient;
1819
import net.neoforged.neoforge.fluids.crafting.DataComponentFluidIngredient;
1920
import net.neoforged.neoforge.fluids.crafting.FluidIngredient;
21+
import net.neoforged.neoforge.fluids.crafting.IntersectionFluidIngredient;
2022
import net.neoforged.testframework.junit.EphemeralTestServerProvider;
2123
import org.assertj.core.api.Assertions;
2224
import org.junit.jupiter.api.Test;
@@ -74,6 +76,7 @@ void fluidIngredientComponentMatchingWorks(boolean strict, MinecraftServer serve
7476
.isEqualTo(!strict);
7577
}
7678

79+
@Test
7780
void singleFluidIngredientIgnoresSizeAndData(MinecraftServer server) {
7881
var ingredient = FluidIngredient.of(Fluids.WATER);
7982

@@ -85,4 +88,30 @@ void singleFluidIngredientIgnoresSizeAndData(MinecraftServer server) {
8588
.withFailMessage("Single fluid ingredient should match regardless of fluid data!")
8689
.isTrue();
8790
}
91+
92+
@Test
93+
void customFluidIngredientSerialization(MinecraftServer server) {
94+
var ingredient1 = FluidIngredient.of(Fluids.WATER, Fluids.LAVA);
95+
var ingredient2 = FluidIngredient.of(Fluids.LAVA);
96+
var intersection = IntersectionFluidIngredient.of(ingredient1, ingredient2);
97+
98+
var ops = server.registryAccess().createSerializationContext(JsonOps.INSTANCE);
99+
var result = FluidIngredient.CODEC.encodeStart(ops, intersection)
100+
.getOrThrow(error -> new RuntimeException("Failed to serialize ingredient: " + error));
101+
102+
var deserialized = FluidIngredient.CODEC.decode(ops, result)
103+
.getOrThrow(error -> new RuntimeException("Failed to deserialize ingredient: " + error))
104+
.getFirst();
105+
106+
if (!(deserialized instanceof IntersectionFluidIngredient)) {
107+
throw new AssertionError("Deserialized ingredient is not an IntersectionFluidIngredient! Got " + deserialized);
108+
}
109+
110+
if (deserialized.test(new FluidStack(Fluids.WATER, 1))) {
111+
throw new AssertionError("Deserialized ingredient should not match water!");
112+
}
113+
if (!deserialized.test(new FluidStack(Fluids.LAVA, 1))) {
114+
throw new AssertionError("Deserialized ingredient should match lava!");
115+
}
116+
}
88117
}

0 commit comments

Comments
 (0)