Skip to content

Commit

Permalink
Use drain/extract for Recipe#matches (#2358)
Browse files Browse the repository at this point in the history
  • Loading branch information
serenibyss committed Feb 19, 2024
1 parent e5f0194 commit f43a676
Showing 1 changed file with 48 additions and 2 deletions.
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

0 comments on commit f43a676

Please sign in to comment.