Skip to content

Commit

Permalink
[CR] Duplicate ARL + RecipeMap logic instead of creating overloads
Browse files Browse the repository at this point in the history
  • Loading branch information
josiah-roberts committed Dec 29, 2023
1 parent ad45cec commit 1faffbc
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

import static gregtech.api.GTValues.ULV;
import static gregtech.api.recipes.logic.OverclockingLogic.*;
Expand Down Expand Up @@ -513,26 +512,12 @@ protected static int getMinTankCapacity(@NotNull IMultipleTankHandler tanks) {
*/
@Nullable
protected Recipe findRecipe(long maxVoltage, IItemHandlerModifiable inputs, IMultipleTankHandler fluidInputs) {
return this.findRecipe(maxVoltage, inputs, fluidInputs, null);
}

/**
* Find a recipe using inputs
*
* @param maxVoltage the maximum voltage the recipe can have
* @param inputs the item inputs used to search for the recipe
* @param fluidInputs the fluid inputs used to search for the recipe
* @param extraPredicate an optional extra predicate to apply when searching
* @return the recipe if found, otherwise null
*/
@Nullable
protected Recipe findRecipe(long maxVoltage, IItemHandlerModifiable inputs, IMultipleTankHandler fluidInputs, Predicate<Recipe> extraPredicate) {
RecipeMap<?> map = getRecipeMap();
if (map == null || !isRecipeMapValid(map)) {
return null;
}

return map.findRecipe(maxVoltage, inputs, fluidInputs, extraPredicate);
return map.findRecipe(maxVoltage, inputs, fluidInputs);
}

/**
Expand Down
32 changes: 3 additions & 29 deletions src/main/java/gregtech/api/recipes/RecipeMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -507,17 +507,7 @@ protected ValidationResult<Recipe> postValidateRecipe(@NotNull ValidationResult<

@Nullable
public Recipe findRecipe(long voltage, IItemHandlerModifiable inputs, IMultipleTankHandler fluidInputs) {
return this.findRecipe(voltage, GTUtility.itemHandlerToList(inputs), GTUtility.fluidHandlerToList(fluidInputs), null);
}

@Nullable
public Recipe findRecipe(long voltage, IItemHandlerModifiable inputs, IMultipleTankHandler fluidInputs, Predicate<Recipe> extraPredicate) {
return this.findRecipe(voltage, GTUtility.itemHandlerToList(inputs), GTUtility.fluidHandlerToList(fluidInputs), extraPredicate);
}

@Nullable
public Recipe findRecipe(long voltage, List<ItemStack> inputs, List<FluidStack> fluidInputs, Predicate<Recipe> extraPredicate) {
return findRecipe(voltage, inputs, fluidInputs, false, extraPredicate);
return this.findRecipe(voltage, GTUtility.itemHandlerToList(inputs), GTUtility.fluidHandlerToList(fluidInputs));
}

/**
Expand All @@ -530,7 +520,7 @@ public Recipe findRecipe(long voltage, List<ItemStack> inputs, List<FluidStack>
*/
@Nullable
public Recipe findRecipe(long voltage, List<ItemStack> inputs, List<FluidStack> fluidInputs) {
return findRecipe(voltage, inputs, fluidInputs, false, null);
return findRecipe(voltage, inputs, fluidInputs, false);
}

/**
Expand All @@ -545,22 +535,6 @@ public Recipe findRecipe(long voltage, List<ItemStack> inputs, List<FluidStack>
@Nullable
public Recipe findRecipe(long voltage, final List<ItemStack> inputs, final List<FluidStack> fluidInputs,
boolean exactVoltage) {
return findRecipe(voltage, inputs, fluidInputs, exactVoltage, null);
}

/**
* Finds a Recipe matching the Fluid and/or ItemStack Inputs.
*
* @param voltage Voltage of the Machine or Long.MAX_VALUE if it has no Voltage
* @param inputs the Item Inputs
* @param fluidInputs the Fluid Inputs
* @param exactVoltage should require exact voltage matching on recipe. used by craftweaker
* @param extraPredicate optional extra filter predicate
* @return the Recipe it has found or null for no matching Recipe
*/
@Nullable
public Recipe findRecipe(long voltage, final List<ItemStack> inputs, final List<FluidStack> fluidInputs,
boolean exactVoltage, Predicate<Recipe> extraPredicate) {
final List<ItemStack> items = inputs.stream().filter(s -> !s.isEmpty()).collect(Collectors.toList());
final List<FluidStack> fluids = fluidInputs.stream().filter(f -> f != null && f.amount != 0)
.collect(Collectors.toList());
Expand All @@ -574,7 +548,7 @@ public Recipe findRecipe(long voltage, final List<ItemStack> inputs, final List<
// there is not enough voltage to consider the recipe valid
return false;
}
return recipe.matches(false, inputs, fluidInputs) && (extraPredicate == null || extraPredicate.test(recipe));
return recipe.matches(false, inputs, fluidInputs);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
import gregtech.api.recipes.Recipe;
import gregtech.api.recipes.RecipeBuilder;

import gregtech.api.recipes.RecipeMap;

import gregtech.api.util.GTUtility;

import net.minecraft.item.ItemStack;
import net.minecraft.util.math.MathHelper;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.IFluidTank;
Expand All @@ -19,6 +24,7 @@
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.stream.Collectors;

public class LargeTurbineWorkableHandler extends MultiblockFuelRecipeLogic {

Expand Down Expand Up @@ -82,7 +88,7 @@ private int getParallel(Recipe recipe, double totalHolderEfficiencyCoefficient,
(Math.abs(recipe.getEUt()) * totalHolderEfficiencyCoefficient));
}

private boolean canDoRecipeConsideringParallel(Recipe recipe) {
private boolean canDoRecipeWithParallel(Recipe recipe) {
IRotorHolder rotorHolder = ((MetaTileEntityLargeTurbine) metaTileEntity).getRotorHolder();
if (rotorHolder == null || !rotorHolder.hasRotor())
return false;
Expand All @@ -101,13 +107,26 @@ private boolean canDoRecipeConsideringParallel(Recipe recipe) {

@Override
protected boolean checkPreviousRecipe() {
return super.checkPreviousRecipe() && canDoRecipeConsideringParallel(this.previousRecipe);
return super.checkPreviousRecipe() && canDoRecipeWithParallel(this.previousRecipe);
}

@Override
protected @Nullable Recipe findRecipe(long maxVoltage, IItemHandlerModifiable inputs,
IMultipleTankHandler fluidInputs) {
return super.findRecipe(maxVoltage, inputs, fluidInputs, this::canDoRecipeConsideringParallel);
RecipeMap<?> map = getRecipeMap();
if (map == null || !isRecipeMapValid(map)) {
return null;
}

final List<ItemStack> items = GTUtility.itemHandlerToList(inputs).stream().filter(s -> !s.isEmpty()).collect(
Collectors.toList());
final List<FluidStack> fluids = GTUtility.fluidHandlerToList(fluidInputs).stream().filter(f -> f != null && f.amount != 0)
.collect(Collectors.toList());

return map.find(items, fluids, recipe -> {
if (recipe.getEUt() > maxVoltage) return false;
return recipe.matches(false, inputs, fluidInputs) && this.canDoRecipeWithParallel(recipe);
});
}

@Override
Expand Down

0 comments on commit 1faffbc

Please sign in to comment.