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

Use drain/extract for Recipe#matches #2358

Merged
merged 1 commit into from
Feb 4, 2024
Merged
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
50 changes: 48 additions & 2 deletions src/main/java/gregtech/api/recipes/Recipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,54 @@ public static Recipe trimRecipeOutputs(Recipe currentRecipe, RecipeMap<?> recipe

public final boolean matches(boolean consumeIfSuccessful, IItemHandlerModifiable inputs,
IMultipleTankHandler fluidInputs) {
return matches(consumeIfSuccessful, GTUtility.itemHandlerToList(inputs),
GTUtility.fluidHandlerToList(fluidInputs));
Pair<Boolean, int[]> fluids = null;
Pair<Boolean, int[]> items = null;

if (fluidInputs.getFluidTanks().size() > 0) {
fluids = matchesFluid(GTUtility.fluidHandlerToList(fluidInputs));
if (!fluids.getKey()) {
return false;
}
}

if (inputs.getSlots() > 0) {
items = matchesItems(GTUtility.itemHandlerToList(inputs));
if (!items.getKey()) {
return false;
}
}

if (consumeIfSuccessful) {
if (fluids != null) {
int[] fluidAmountInTank = fluids.getValue();
var backedList = fluidInputs.getFluidTanks();

for (int i = 0; i < fluidAmountInTank.length; i++) {
var tank = backedList.get(i);
FluidStack fluidStack = tank.getFluid();
int fluidAmount = fluidAmountInTank[i];

if (fluidStack == null || fluidStack.amount == fluidAmount) {
continue;
}
tank.drain(Math.abs(fluidAmount - fluidStack.amount), true);
}
}
if (items != null) {
int[] itemAmountInSlot = items.getValue();
for (int i = 0; i < itemAmountInSlot.length; i++) {
ItemStack itemInSlot = inputs.getStackInSlot(i);
int itemAmount = itemAmountInSlot[i];

if (itemInSlot.isEmpty() || itemInSlot.getCount() == itemAmount) {
continue;
}
inputs.extractItem(i, Math.abs(itemAmount - itemInSlot.getCount()), false);
}
}
}

return true;
}

/**
Expand Down