Skip to content

Commit

Permalink
Preliminary porting work on FluidIngredient
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxNeedsSnacks committed Oct 15, 2024
1 parent bc39e14 commit 2073b15
Show file tree
Hide file tree
Showing 18 changed files with 430 additions and 585 deletions.
19 changes: 13 additions & 6 deletions src/main/java/net/neoforged/neoforge/common/NeoForgeMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import net.minecraft.world.entity.ai.attributes.RangedAttribute;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.crafting.display.SlotDisplay;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.GameRules;
import net.minecraft.world.level.LevelReader;
Expand Down Expand Up @@ -143,11 +144,12 @@
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.EmptyFluidIngredient;
import net.neoforged.neoforge.fluids.crafting.FluidIngredientType;
import net.neoforged.neoforge.fluids.crafting.IntersectionFluidIngredient;
import net.neoforged.neoforge.fluids.crafting.SingleFluidIngredient;
import net.neoforged.neoforge.fluids.crafting.TagFluidIngredient;
import net.neoforged.neoforge.fluids.crafting.SimpleFluidIngredient;
import net.neoforged.neoforge.fluids.crafting.display.FluidSlotDisplay;
import net.neoforged.neoforge.fluids.crafting.display.FluidStackSlotDisplay;
import net.neoforged.neoforge.fluids.crafting.display.FluidTagSlotDisplay;
import net.neoforged.neoforge.forge.snapshots.ForgeSnapshotsMod;
import net.neoforged.neoforge.internal.versions.neoforge.NeoForgeVersion;
import net.neoforged.neoforge.network.DualStackUtils;
Expand Down Expand Up @@ -353,6 +355,12 @@ public class NeoForgeMod {
*/
public static final Holder<HolderSetType> NOT_HOLDER_SET = HOLDER_SET_TYPES.register("not", NotHolderSet.Type::new);

private static final DeferredRegister<SlotDisplay.Type<?>> SLOT_DISPLAY_TYPES = DeferredRegister.create(Registries.SLOT_DISPLAY, NeoForgeVersion.MOD_ID);

public static final DeferredHolder<SlotDisplay.Type<?>, SlotDisplay.Type<FluidSlotDisplay>> FLUID_SLOT_DISPLAY = SLOT_DISPLAY_TYPES.register("fluid", () -> new SlotDisplay.Type<>(FluidSlotDisplay.MAP_CODEC, FluidSlotDisplay.STREAM_CODEC));
public static final DeferredHolder<SlotDisplay.Type<?>, SlotDisplay.Type<FluidStackSlotDisplay>> FLUID_STACK_SLOT_DISPLAY = SLOT_DISPLAY_TYPES.register("fluid_stack", () -> new SlotDisplay.Type<>(FluidStackSlotDisplay.MAP_CODEC, FluidStackSlotDisplay.STREAM_CODEC));
public static final DeferredHolder<SlotDisplay.Type<?>, SlotDisplay.Type<FluidTagSlotDisplay>> FLUID_TAG_SLOT_DISPLAY = SLOT_DISPLAY_TYPES.register("fluid_tag", () -> new SlotDisplay.Type<>(FluidTagSlotDisplay.MAP_CODEC, FluidTagSlotDisplay.STREAM_CODEC));

private static final DeferredRegister<IngredientType<?>> INGREDIENT_TYPES = DeferredRegister.create(NeoForgeRegistries.Keys.INGREDIENT_TYPES, NeoForgeVersion.MOD_ID);

public static final DeferredHolder<IngredientType<?>, IngredientType<CompoundIngredient>> COMPOUND_INGREDIENT_TYPE = INGREDIENT_TYPES.register("compound", () -> new IngredientType<>(CompoundIngredient.CODEC));
Expand All @@ -363,9 +371,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<SingleFluidIngredient>> SINGLE_FLUID_INGREDIENT_TYPE = FLUID_INGREDIENT_TYPES.register("single", () -> new FluidIngredientType<>(SingleFluidIngredient.CODEC));
public static final DeferredHolder<FluidIngredientType<?>, FluidIngredientType<TagFluidIngredient>> TAG_FLUID_INGREDIENT_TYPE = FLUID_INGREDIENT_TYPES.register("tag", () -> new FluidIngredientType<>(TagFluidIngredient.CODEC));
public static final DeferredHolder<FluidIngredientType<?>, FluidIngredientType<EmptyFluidIngredient>> EMPTY_FLUID_INGREDIENT_TYPE = FLUID_INGREDIENT_TYPES.register("empty", () -> new FluidIngredientType<>(EmptyFluidIngredient.CODEC));
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<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 Expand Up @@ -547,6 +553,7 @@ public NeoForgeMod(IEventBus modEventBus, Dist dist, ModContainer container) {
VANILLA_FLUID_TYPES.register(modEventBus);
ENTITY_PREDICATE_CODECS.register(modEventBus);
ITEM_SUB_PREDICATES.register(modEventBus);
SLOT_DISPLAY_TYPES.register(modEventBus);
INGREDIENT_TYPES.register(modEventBus);
CONDITION_CODECS.register(modEventBus);
GLOBAL_LOOT_MODIFIER_SERIALIZERS.register(modEventBus);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;
import net.minecraft.core.Holder;
import net.minecraft.world.level.material.Fluid;
import net.neoforged.neoforge.common.NeoForgeMod;
import net.neoforged.neoforge.common.crafting.CompoundIngredient;
import net.neoforged.neoforge.common.util.NeoForgeExtraCodecs;
Expand All @@ -22,7 +24,7 @@
* @see CompoundIngredient CompoundIngredient, its item equivalent
*/
public final class CompoundFluidIngredient extends FluidIngredient {
public static final MapCodec<CompoundFluidIngredient> CODEC = NeoForgeExtraCodecs.aliasedFieldOf(FluidIngredient.LIST_CODEC_NON_EMPTY, "children", "ingredients").xmap(CompoundFluidIngredient::new, CompoundFluidIngredient::children);
public static final MapCodec<CompoundFluidIngredient> CODEC = NeoForgeExtraCodecs.aliasedFieldOf(FluidIngredient.CODEC.listOf(1, Integer.MAX_VALUE), "children", "ingredients").xmap(CompoundFluidIngredient::new, CompoundFluidIngredient::children);

private final List<FluidIngredient> children;

Expand All @@ -37,8 +39,6 @@ public CompoundFluidIngredient(List<? extends FluidIngredient> children) {
* Creates a compound ingredient from the given list of ingredients.
*/
public static FluidIngredient of(FluidIngredient... children) {
if (children.length == 0)
return FluidIngredient.empty();
if (children.length == 1)
return children[0];

Expand All @@ -49,21 +49,15 @@ public static FluidIngredient of(FluidIngredient... children) {
* Creates a compound ingredient from the given list of ingredients.
*/
public static FluidIngredient of(List<FluidIngredient> children) {
if (children.isEmpty())
return FluidIngredient.empty();
if (children.size() == 1)
return children.getFirst();

return new CompoundFluidIngredient(children);
}

public static FluidIngredient of(Stream<FluidIngredient> stream) {
return of(stream.toList());
}

@Override
public Stream<FluidStack> generateStacks() {
return children.stream().flatMap(FluidIngredient::generateStacks);
public Stream<Holder<Fluid>> generateFluids() {
return children.stream().flatMap(FluidIngredient::generateFluids);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.HolderSetCodec;
import net.minecraft.world.item.crafting.display.SlotDisplay;
import net.minecraft.world.level.material.Fluid;
import net.neoforged.neoforge.common.NeoForgeMod;
import net.neoforged.neoforge.common.crafting.DataComponentIngredient;
import net.neoforged.neoforge.fluids.FluidStack;
import net.neoforged.neoforge.fluids.FluidType;
import net.neoforged.neoforge.fluids.crafting.display.FluidStackSlotDisplay;

/**
* Fluid ingredient that matches the given set of fluids, additionally performing either a
Expand All @@ -39,7 +41,7 @@ public class DataComponentFluidIngredient extends FluidIngredient {
public static final MapCodec<DataComponentFluidIngredient> CODEC = RecordCodecBuilder.mapCodec(
builder -> builder
.group(
HolderSetCodec.create(Registries.FLUID, BuiltInRegistries.FLUID.holderByNameCodec(), false).fieldOf("fluids").forGetter(DataComponentFluidIngredient::fluids),
HolderSetCodec.create(Registries.FLUID, BuiltInRegistries.FLUID.holderByNameCodec(), false).fieldOf("fluids").forGetter(DataComponentFluidIngredient::fluidSet),
DataComponentPredicate.CODEC.fieldOf("components").forGetter(DataComponentFluidIngredient::components),
Codec.BOOL.optionalFieldOf("strict", false).forGetter(DataComponentFluidIngredient::isStrict))
.apply(builder, DataComponentFluidIngredient::new));
Expand Down Expand Up @@ -70,8 +72,15 @@ public boolean test(FluidStack stack) {
}
}

public Stream<FluidStack> generateStacks() {
return Stream.of(stacks);
public Stream<Holder<Fluid>> generateFluids() {
return fluids.stream();
}

@Override
public SlotDisplay display() {
return new SlotDisplay.Composite(Stream.of(stacks)
.map(stack -> (SlotDisplay) new FluidStackSlotDisplay(stack))
.toList());
}

@Override
Expand All @@ -98,7 +107,7 @@ public boolean equals(Object obj) {
&& other.strict == this.strict;
}

public HolderSet<Fluid> fluids() {
public HolderSet<Fluid> fluidSet() {
return fluids;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import com.mojang.serialization.codecs.RecordCodecBuilder;
import java.util.Objects;
import java.util.stream.Stream;
import net.minecraft.core.Holder;
import net.minecraft.world.level.material.Fluid;
import net.neoforged.neoforge.common.NeoForgeMod;
import net.neoforged.neoforge.common.crafting.DifferenceIngredient;
import net.neoforged.neoforge.fluids.FluidStack;
Expand All @@ -23,8 +25,8 @@ public final class DifferenceFluidIngredient extends FluidIngredient {
public static final MapCodec<DifferenceFluidIngredient> CODEC = RecordCodecBuilder.mapCodec(
builder -> builder
.group(
FluidIngredient.CODEC_NON_EMPTY.fieldOf("base").forGetter(DifferenceFluidIngredient::base),
FluidIngredient.CODEC_NON_EMPTY.fieldOf("subtracted").forGetter(DifferenceFluidIngredient::subtracted))
FluidIngredient.CODEC.fieldOf("base").forGetter(DifferenceFluidIngredient::base),
FluidIngredient.CODEC.fieldOf("subtracted").forGetter(DifferenceFluidIngredient::subtracted))
.apply(builder, DifferenceFluidIngredient::new));
private final FluidIngredient base;
private final FluidIngredient subtracted;
Expand All @@ -35,8 +37,8 @@ public DifferenceFluidIngredient(FluidIngredient base, FluidIngredient subtracte
}

@Override
public Stream<FluidStack> generateStacks() {
return base.generateStacks().filter(subtracted.negate());
public Stream<Holder<Fluid>> generateFluids() {
return base.fluids().stream().filter(e -> !subtracted().fluids().contains(e));
}

@Override
Expand Down

This file was deleted.

Loading

0 comments on commit 2073b15

Please sign in to comment.