Skip to content

Commit

Permalink
feat: switch to using SizedIngredients provided by neo
Browse files Browse the repository at this point in the history
  • Loading branch information
klikli-dev committed Jun 16, 2024
1 parent f4a535d commit 82731e9
Show file tree
Hide file tree
Showing 18 changed files with 122 additions and 227 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ protected boolean craft(RecipeHolder<DigestionRecipe> pRecipe) {
//consume the input stacks
//the double loop may not be necessary, it may be OK to just take one from each slot (because recipe matches only if exact items match, not if more items are present)
//however this costs almost nothing extra and is safer so we do it.
for (var ingredient : pRecipe.value().getIngredientsWithCount()) {
for (var ingredient : pRecipe.value().getSizedIngredients()) {
for (int i = 0; i < this.inputInventorySupplier.get().getSlots(); i++) {
if (ingredient.ingredient().test(this.inputInventorySupplier.get().getStackInSlot(i))) {
this.inputInventorySupplier.get().extractItem(i, ingredient.count(), false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import net.minecraft.world.level.Level;
import net.neoforged.neoforge.fluids.FluidStack;
import net.neoforged.neoforge.fluids.crafting.FluidIngredient;
import net.neoforged.neoforge.fluids.crafting.SizedFluidIngredient;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -34,33 +35,29 @@ public class AccumulationRecipe implements Recipe<ItemHandlerWithFluidRecipeInpu
public static final int DEFAULT_TIME = 100;

public static final MapCodec<AccumulationRecipe> CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group(
FluidIngredient.CODEC.fieldOf("evaporant").forGetter((r) -> r.evaporant),
Codec.INT.fieldOf("evaporantAmount").forGetter((r) -> r.evaporantAmount),
SizedFluidIngredient.NESTED_CODEC.fieldOf("evaporant").forGetter((r) -> r.evaporant),
Ingredient.CODEC.optionalFieldOf("solute").forGetter(r -> Optional.ofNullable(r.solute)),
FluidStack.CODEC.fieldOf("result").forGetter(r -> r.result),
Codec.INT.optionalFieldOf("time", DEFAULT_TIME).forGetter(r -> r.time)
).apply(instance, (evaporant, evaporantAmount, solute, result, accumulation_time) -> new AccumulationRecipe(evaporant, evaporantAmount, solute.orElse(null), result, accumulation_time))
).apply(instance, (evaporant, solute, result, accumulation_time) -> new AccumulationRecipe(evaporant, solute.orElse(null), result, accumulation_time))
);

public static final StreamCodec<RegistryFriendlyByteBuf, AccumulationRecipe> STREAM_CODEC = StreamCodec.composite(
FluidIngredient.STREAM_CODEC,
SizedFluidIngredient.STREAM_CODEC,
r -> r.evaporant,
ByteBufCodecs.INT,
r -> r.evaporantAmount,
ByteBufCodecs.optional(Ingredient.CONTENTS_STREAM_CODEC),
r -> Optional.ofNullable(r.solute),
FluidStack.STREAM_CODEC,
r -> r.result,
ByteBufCodecs.INT,
r -> r.time,
(evaporant, evaporantAmount, solute, result, accumulation_time) -> new AccumulationRecipe(evaporant, evaporantAmount, solute.orElse(null), result, accumulation_time)
(evaporant, solute, result, accumulation_time) -> new AccumulationRecipe(evaporant, solute.orElse(null), result, accumulation_time)
);

/**
* The fluid to evaporate to obtain the result.
*/
protected final FluidIngredient evaporant;
protected final int evaporantAmount;
protected final SizedFluidIngredient evaporant;
/**
* The (optional) item to dissolve in the evaporant to obtain the result.
*/
Expand All @@ -72,9 +69,8 @@ public class AccumulationRecipe implements Recipe<ItemHandlerWithFluidRecipeInpu
protected final FluidStack result;
protected final int time;

public AccumulationRecipe(FluidIngredient evaporant, int evaporantAmount, @Nullable Ingredient solute, FluidStack result, int time) {
public AccumulationRecipe(SizedFluidIngredient evaporant, @Nullable Ingredient solute, FluidStack result, int time) {
this.evaporant = evaporant;
this.evaporantAmount = evaporantAmount;
this.solute = solute;
this.result = result;
this.time = time;
Expand All @@ -93,7 +89,7 @@ public boolean isSpecial() {
@Override
public boolean matches(@NotNull ItemHandlerWithFluidRecipeInput pContainer, @NotNull Level pLevel) {
var fluid = pContainer.getTank().getFluidInTank(0);
boolean evaporantMatches = this.evaporant.test(fluid) && fluid.getAmount() >= this.evaporantAmount;
boolean evaporantMatches = this.evaporant.test(fluid);
//noinspection DataFlowIssue: we are checking this.hasSolute so solute is not null!
boolean soluteMatches =
pContainer.getItem(0).isEmpty() && !this.hasSolute() || //if recipe requires no solute and container does not have one we're ok
Expand Down Expand Up @@ -145,12 +141,12 @@ public int getTime() {
return this.time;
}

public FluidIngredient getEvaporant() {
public SizedFluidIngredient getEvaporant() {
return this.evaporant;
}

public int getEvaporantAmount() {
return this.evaporantAmount;
return this.evaporant.amount();
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,47 +22,45 @@
import net.minecraft.world.item.crafting.RecipeSerializer;
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraft.world.level.Level;
import net.neoforged.neoforge.common.crafting.SizedIngredient;
import org.jetbrains.annotations.NotNull;

import java.util.List;


public class CalcinationRecipe implements Recipe<ItemHandlerRecipeInput> {

public static final int DEFAULT_TIME = 100;

public static final MapCodec<CalcinationRecipe> CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group(
Ingredient.CODEC.fieldOf("ingredient").forGetter((r) -> r.ingredient),
Codec.INT.fieldOf("ingredientCount").forGetter((r) -> r.ingredientCount),
SizedIngredient.NESTED_CODEC.fieldOf("ingredient").forGetter((r) -> r.ingredient),
ItemStack.STRICT_CODEC.fieldOf("result").forGetter(r -> r.result),
Codec.INT.optionalFieldOf("time", DEFAULT_TIME).forGetter(r -> r.time)
).apply(instance, CalcinationRecipe::new)
);

public static final StreamCodec<RegistryFriendlyByteBuf, CalcinationRecipe> STREAM_CODEC = StreamCodec.composite(
Ingredient.CONTENTS_STREAM_CODEC,
SizedIngredient.STREAM_CODEC,
r -> r.ingredient,
ByteBufCodecs.INT,
r -> r.ingredientCount,
ItemStack.OPTIONAL_STREAM_CODEC,
r -> r.result,
ByteBufCodecs.INT,
r -> r.time,
CalcinationRecipe::new
);

protected final Ingredient ingredient;
protected final int ingredientCount;
protected final SizedIngredient ingredient;
protected final ItemStack result;
protected final int time;

public CalcinationRecipe(Ingredient pIngredient, int ingredientCount, ItemStack pResult, int time) {
public CalcinationRecipe(SizedIngredient pIngredient, ItemStack pResult, int time) {
this.ingredient = pIngredient;
this.ingredientCount = ingredientCount;
this.result = pResult;
this.time = time;
}

public int getIngredientCount() {
return this.ingredientCount;
return this.ingredient.count();
}

@Override
Expand All @@ -78,7 +76,7 @@ public boolean isSpecial() {
@Override
public boolean matches(ItemHandlerRecipeInput input, @NotNull Level pLevel) {
var stack = input.getItem(0);
return this.ingredient.test(stack) && stack.getCount() >= this.ingredientCount;
return this.ingredient.test(stack);
}

@Override
Expand All @@ -99,7 +97,7 @@ public boolean canCraftInDimensions(int pWidth, int pHeight) {
@Override
public @NotNull NonNullList<Ingredient> getIngredients() {
NonNullList<Ingredient> nonnulllist = NonNullList.create();
nonnulllist.add(this.ingredient);
nonnulllist.add(this.ingredient.ingredient());
return nonnulllist;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@


import com.klikli_dev.theurgy.content.recipe.input.ItemHandlerWithFluidRecipeInput;
import com.klikli_dev.theurgy.datagen.recipe.IngredientWithCount;
import com.klikli_dev.theurgy.registry.ItemRegistry;
import com.klikli_dev.theurgy.registry.RecipeSerializerRegistry;
import com.klikli_dev.theurgy.registry.RecipeTypeRegistry;
Expand All @@ -26,7 +25,8 @@
import net.minecraft.world.item.crafting.RecipeSerializer;
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraft.world.level.Level;
import net.neoforged.neoforge.fluids.crafting.FluidIngredient;
import net.neoforged.neoforge.common.crafting.SizedIngredient;
import net.neoforged.neoforge.fluids.crafting.SizedFluidIngredient;
import org.jetbrains.annotations.NotNull;

import java.util.List;
Expand All @@ -35,41 +35,36 @@ public class DigestionRecipe implements Recipe<ItemHandlerWithFluidRecipeInput>
public static final int DEFAULT_TIME = 200;

public static final MapCodec<DigestionRecipe> CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group(
FluidIngredient.CODEC.fieldOf("fluid").forGetter((r) -> r.fluid),
Codec.INT.fieldOf("fluidAmount").forGetter((r) -> r.fluidAmount),
IngredientWithCount.CODEC.listOf().fieldOf("ingredients").forGetter(r -> r.ingredientsWithCount),
SizedFluidIngredient.NESTED_CODEC.fieldOf("fluid").forGetter((r) -> r.fluid),
SizedIngredient.NESTED_CODEC.listOf().fieldOf("ingredients").forGetter(r -> r.sizedIngredients),
ItemStack.STRICT_CODEC.fieldOf("result").forGetter(r -> r.result),
Codec.INT.optionalFieldOf("time", DEFAULT_TIME).forGetter(r -> r.time)
).apply(instance, DigestionRecipe::new)
);

public static final StreamCodec<RegistryFriendlyByteBuf, DigestionRecipe> STREAM_CODEC = StreamCodec.composite(
FluidIngredient.STREAM_CODEC,
SizedFluidIngredient.STREAM_CODEC,
r -> r.fluid,
ByteBufCodecs.INT,
r -> r.fluidAmount,
IngredientWithCount.STREAM_CODEC.apply(ByteBufCodecs.list()),
r -> r.ingredientsWithCount,
SizedIngredient.STREAM_CODEC.apply(ByteBufCodecs.list()),
r -> r.sizedIngredients,
ItemStack.OPTIONAL_STREAM_CODEC,
r -> r.result,
ByteBufCodecs.INT,
r -> r.time,
DigestionRecipe::new
);

protected final FluidIngredient fluid;
protected final int fluidAmount;
protected final SizedFluidIngredient fluid;

protected final List<IngredientWithCount> ingredientsWithCount;
protected final List<SizedIngredient> sizedIngredients;
protected final NonNullList<Ingredient> ingredients;
protected final ItemStack result;
protected final int time;

public DigestionRecipe(FluidIngredient fluid, int fluidAmount, List<IngredientWithCount> ingredientsWithCount, ItemStack result, int time) {
public DigestionRecipe(SizedFluidIngredient fluid, List<SizedIngredient> sizedIngredients, ItemStack result, int time) {
this.fluid = fluid;
this.fluidAmount = fluidAmount;
this.ingredientsWithCount = ingredientsWithCount;
this.ingredients = ingredientsWithCount.stream().map(IngredientWithCount::ingredient).collect(NonNullList::create, NonNullList::add, NonNullList::addAll);
this.sizedIngredients = sizedIngredients;
this.ingredients = sizedIngredients.stream().map(SizedIngredient::ingredient).collect(NonNullList::create, NonNullList::add, NonNullList::addAll);
this.result = result;
this.time = time;
}
Expand All @@ -87,13 +82,13 @@ public boolean isSpecial() {
@Override
public boolean matches(ItemHandlerWithFluidRecipeInput pContainer, @NotNull Level pLevel) {
var fluid = pContainer.getTank().getFluidInTank(0);
var fluidMatches = this.fluid.test(fluid) && fluid.getAmount() >= this.fluidAmount;
var fluidMatches = this.fluid.test(fluid);
if (!fluidMatches)
return false;

IntList visited = new IntArrayList();
//first check for each ingredient if we have it in the container
for (var ingredient : this.ingredientsWithCount) {
for (var ingredient : this.sizedIngredients) {
var found = false;
for (int i = 0; i < pContainer.size(); i++) {
//skip already visited slots to not "double dip"
Expand Down Expand Up @@ -144,8 +139,8 @@ public boolean canCraftInDimensions(int pWidth, int pHeight) {
return this.ingredients;
}

public List<IngredientWithCount> getIngredientsWithCount() {
return this.ingredientsWithCount;
public List<SizedIngredient> getSizedIngredients() {
return this.sizedIngredients;
}

@Override
Expand All @@ -158,12 +153,12 @@ public List<IngredientWithCount> getIngredientsWithCount() {
return RecipeSerializerRegistry.DIGESTION.get();
}

public FluidIngredient getFluid() {
public SizedFluidIngredient getFluid() {
return this.fluid;
}

public int getFluidAmount() {
return this.fluidAmount;
return this.fluid.amount();
}

public ItemStack getResult() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,36 @@
import net.minecraft.world.item.crafting.RecipeSerializer;
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraft.world.level.Level;
import net.neoforged.neoforge.common.crafting.SizedIngredient;
import org.jetbrains.annotations.NotNull;

public class DistillationRecipe implements Recipe<ItemHandlerRecipeInput> {

public static final int DEFAULT_TIME = 100;

public static final MapCodec<DistillationRecipe> CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group(
Ingredient.CODEC.fieldOf("ingredient").forGetter((r) -> r.ingredient),
Codec.INT.fieldOf("ingredientCount").forGetter((r) -> r.ingredientCount),
SizedIngredient.NESTED_CODEC.fieldOf("ingredient").forGetter((r) -> r.ingredient),
ItemStack.STRICT_CODEC.fieldOf("result").forGetter(r -> r.result),
Codec.INT.optionalFieldOf("time", DEFAULT_TIME).forGetter(r -> r.time)
).apply(instance, DistillationRecipe::new)
);

public static final StreamCodec<RegistryFriendlyByteBuf, DistillationRecipe> STREAM_CODEC = StreamCodec.composite(
Ingredient.CONTENTS_STREAM_CODEC,
SizedIngredient.STREAM_CODEC,
r -> r.ingredient,
ByteBufCodecs.INT,
r -> r.ingredientCount,
ItemStack.OPTIONAL_STREAM_CODEC,
r -> r.result,
ByteBufCodecs.INT,
r -> r.time,
DistillationRecipe::new
);

protected final Ingredient ingredient;
protected final int ingredientCount;
protected final SizedIngredient ingredient;
protected final ItemStack result;
protected final int time;

public DistillationRecipe(Ingredient pIngredient, int ingredientCount, ItemStack pResult, int time) {
public DistillationRecipe(SizedIngredient pIngredient, ItemStack pResult, int time) {
this.ingredient = pIngredient;
this.ingredientCount = ingredientCount;
this.result = pResult;
this.time = time;
}
Expand All @@ -73,7 +69,7 @@ public boolean isSpecial() {
@Override
public boolean matches(@NotNull ItemHandlerRecipeInput pContainer, @NotNull Level pLevel) {
var stack = pContainer.getItem(0);
return this.ingredient.test(stack) && stack.getCount() >= this.ingredientCount;
return this.ingredient.test(stack);
}

@Override
Expand All @@ -94,16 +90,16 @@ public boolean canCraftInDimensions(int pWidth, int pHeight) {
@Override
public @NotNull NonNullList<Ingredient> getIngredients() {
NonNullList<Ingredient> nonnulllist = NonNullList.create();
nonnulllist.add(this.ingredient);
nonnulllist.add(this.ingredient.ingredient());
return nonnulllist;
}

public Ingredient getIngredient() {
public SizedIngredient getIngredient() {
return this.ingredient;
}

public int getIngredientCount() {
return this.ingredientCount;
return this.ingredient.count();
}

@Override
Expand Down
Loading

0 comments on commit 82731e9

Please sign in to comment.