Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NeoForge fluid ingredient types not being registered #1783

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,7 @@ public NeoForgeMod(IEventBus modEventBus, Dist dist, ModContainer container) {
ITEM_SUB_PREDICATES.register(modEventBus);
SLOT_DISPLAY_TYPES.register(modEventBus);
INGREDIENT_TYPES.register(modEventBus);
FLUID_INGREDIENT_TYPES.register(modEventBus);
CONDITION_CODECS.register(modEventBus);
GLOBAL_LOOT_MODIFIER_SERIALIZERS.register(modEventBus);
NeoForge.EVENT_BUS.addListener(this::serverStopping);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ public class FluidIngredientCodecs {
static Codec<FluidIngredient> codec() {
return Codec.xor(
NeoForgeRegistries.FLUID_INGREDIENT_TYPES.byNameCodec().<FluidIngredient>dispatch("neoforge:ingredient_type", FluidIngredient::getType, FluidIngredientType::codec),
SimpleFluidIngredient.CODEC).xmap(either -> either.map(i -> i, i -> i), ingredient -> switch (ingredient) {
// Use lazy: SimpleFluidIngredient.CODEC is initialized after FluidIngredient.CODEC which lives in a superclass.
Codec.lazyInitialized(() -> SimpleFluidIngredient.CODEC))
.xmap(either -> either.map(i -> i, i -> i), ingredient -> switch (ingredient) {
case SimpleFluidIngredient simple -> Either.right(simple);
default -> Either.left(ingredient);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package net.neoforged.neoforge.unittest;

import com.mojang.serialization.JsonOps;
import java.util.List;
import java.util.stream.Stream;
import net.minecraft.core.component.DataComponentPatch;
Expand All @@ -17,6 +18,7 @@
import net.neoforged.neoforge.fluids.crafting.CompoundFluidIngredient;
import net.neoforged.neoforge.fluids.crafting.DataComponentFluidIngredient;
import net.neoforged.neoforge.fluids.crafting.FluidIngredient;
import net.neoforged.neoforge.fluids.crafting.IntersectionFluidIngredient;
import net.neoforged.testframework.junit.EphemeralTestServerProvider;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -74,6 +76,7 @@ void fluidIngredientComponentMatchingWorks(boolean strict, MinecraftServer serve
.isEqualTo(!strict);
}

@Test
void singleFluidIngredientIgnoresSizeAndData(MinecraftServer server) {
var ingredient = FluidIngredient.of(Fluids.WATER);

Expand All @@ -85,4 +88,30 @@ void singleFluidIngredientIgnoresSizeAndData(MinecraftServer server) {
.withFailMessage("Single fluid ingredient should match regardless of fluid data!")
.isTrue();
}

@Test
void customFluidIngredientSerialization(MinecraftServer server) {
var ingredient1 = FluidIngredient.of(Fluids.WATER, Fluids.LAVA);
var ingredient2 = FluidIngredient.of(Fluids.LAVA);
var intersection = IntersectionFluidIngredient.of(ingredient1, ingredient2);

var ops = server.registryAccess().createSerializationContext(JsonOps.INSTANCE);
var result = FluidIngredient.CODEC.encodeStart(ops, intersection)
.getOrThrow(error -> new RuntimeException("Failed to serialize ingredient: " + error));

var deserialized = FluidIngredient.CODEC.decode(ops, result)
.getOrThrow(error -> new RuntimeException("Failed to deserialize ingredient: " + error))
.getFirst();

if (!(deserialized instanceof IntersectionFluidIngredient)) {
throw new AssertionError("Deserialized ingredient is not an IntersectionFluidIngredient! Got " + deserialized);
}

if (deserialized.test(new FluidStack(Fluids.WATER, 1))) {
throw new AssertionError("Deserialized ingredient should not match water!");
}
if (!deserialized.test(new FluidStack(Fluids.LAVA, 1))) {
throw new AssertionError("Deserialized ingredient should match lava!");
}
}
}
Loading