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

Remove machine fluid filtering #1151

Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import gregtech.api.recipes.logic.IParallelableRecipeLogic;
import gregtech.api.recipes.recipeproperties.CleanroomProperty;
import gregtech.api.recipes.recipeproperties.IRecipePropertyStorage;
import gregtech.api.recipes.recipeproperties.RecipePropertyStorage;
import gregtech.api.util.GTTransferUtils;
import gregtech.api.util.GTUtility;
import gregtech.common.ConfigHolder;
Expand Down Expand Up @@ -197,6 +196,9 @@ protected boolean canWorkWithInputs() {
// if the inputs were bad last time, check if they've changed before trying to find a new recipe.
if (this.invalidInputsForRecipes && !hasNotifiedInputs()) return false;
else {
//the change in inputs (especially by removal of ingredient by the player) might change the current valid recipe.
//and if the previous recipe produced fluids and the new recipe doesn't, then outputs are not full.
this.isOutputsFull = false;
this.invalidInputsForRecipes = false;
this.metaTileEntity.getNotifiedItemInputList().clear();
this.metaTileEntity.getNotifiedFluidInputList().clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
import gregtech.api.metatileentity.multiblock.ICleanroomProvider;
import gregtech.api.metatileentity.multiblock.ICleanroomReceiver;
import gregtech.api.recipes.FluidKey;
import gregtech.api.recipes.Recipe;
import gregtech.api.recipes.RecipeMap;
import gregtech.api.util.GTUtility;
import gregtech.client.renderer.ICubeRenderer;
import it.unimi.dsi.fastutil.objects.ObjectArraySet;
import net.minecraft.client.resources.I18n;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
Expand All @@ -22,24 +20,19 @@
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.world.World;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTank;
import net.minecraftforge.fluids.IFluidTank;
import net.minecraftforge.items.IItemHandlerModifiable;
import net.minecraftforge.items.ItemStackHandler;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.function.Function;

public abstract class WorkableTieredMetaTileEntity extends TieredMetaTileEntity implements IDataInfoProvider, ICleanroomReceiver {

protected static Set<FluidKey> fluidKeyCache;

protected final RecipeLogicEnergy workable;
protected final RecipeMap<?> recipeMap;
protected final ICubeRenderer renderer;
Expand Down Expand Up @@ -114,10 +107,9 @@ protected IItemHandlerModifiable createExportItemHandler() {
@Override
protected FluidTankList createImportFluidHandler() {
if (workable == null) return new FluidTankList(false);
FilteredFluidHandler[] fluidImports = new FilteredFluidHandler[workable.getRecipeMap().getMaxFluidInputs()];
NotifiableFluidTank[] fluidImports = new NotifiableFluidTank[workable.getRecipeMap().getMaxFluidInputs()];
for (int i = 0; i < fluidImports.length; i++) {
NotifiableFilteredFluidHandler filteredFluidHandler = new NotifiableFilteredFluidHandler(this.tankScalingFunction.apply(this.getTier()), this, false);
filteredFluidHandler.setFillPredicate(this::canInputFluid);
NotifiableFluidTank filteredFluidHandler = new NotifiableFluidTank(this.tankScalingFunction.apply(this.getTier()), this, false);
fluidImports[i] = filteredFluidHandler;
}
return new FluidTankList(false, fluidImports);
Expand All @@ -133,21 +125,6 @@ protected FluidTankList createExportFluidHandler() {
return new FluidTankList(false, fluidExports);
}

protected boolean canInputFluid(FluidStack inputFluid) {
RecipeMap<?> recipeMap = workable.getRecipeMap();
List<FluidStack> fluidInputs = new ArrayList<>();
for (IFluidTank fluidTank : this.importFluids.getFluidTanks()) {
FluidStack fluidStack = fluidTank.getFluid();
if (fluidStack != null && fluidStack.amount > 0){
if (fluidStack.isFluidEqual(inputFluid)) {
return true;
}
fluidInputs.add(fluidStack);
}
}
return recipeMap != null && recipeMap.acceptsFluid(fluidInputs, inputFluid);
}

@Override
public void addInformation(ItemStack stack, @Nullable World player, List<String> tooltip, boolean advanced) {
super.addInformation(stack, player, tooltip, advanced);
Expand Down
98 changes: 0 additions & 98 deletions src/main/java/gregtech/api/recipes/RecipeMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.SoundEvent;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fml.common.Optional.Method;
import net.minecraftforge.items.IItemHandlerModifiable;
Expand Down Expand Up @@ -175,19 +174,6 @@ public RecipeMap<?> getSmallRecipeMap() {
return smallRecipeMap;
}

/**
* This is alternative case when machine can input given fluid
* If this method returns true, machine will receive given fluid even if getRecipesForFluid doesn't have
* any recipe for this fluid
*/
public boolean canInputFluidForce(Fluid fluid) {
return false;
}

public Collection<Recipe> getRecipesForFluid(FluidStack fluid) {
return lookup.getRecipes(false).filter(r -> r.hasInputFluid(fluid)).collect(Collectors.toSet());
}

private static boolean foundInvalidRecipe = false;

//internal usage only, use buildAndRegister()
Expand Down Expand Up @@ -301,22 +287,6 @@ public Recipe findRecipe(long voltage, List<ItemStack> inputs, List<FluidStack>
});
}

public boolean acceptsFluid(List<FluidStack> fluidInputs, FluidStack fluid) {
if (canInputFluidForce(fluid.getFluid())) {
return true;
}
if (fluidInputs.isEmpty()) {
return fluidIngredientRoot.get(new MapFluidIngredient(fluid)) != null;
}
if (fluidInputs.contains(fluid)) {
return true;
}
fluidInputs.add(fluid);
List<List<AbstractMapIngredient>> list = new ObjectArrayList<>();
buildFromFluidStacks(list, fluidInputs);
return canInsertFluid(list, lookup);
}

@Nullable
public Recipe find(@Nonnull List<ItemStack> items, @Nonnull List<FluidStack> fluids, @Nonnull Predicate<Recipe> canHandle) {
// First, check if items and fluids are valid.
Expand All @@ -326,9 +296,7 @@ public Recipe find(@Nonnull List<ItemStack> items, @Nonnull List<FluidStack> flu
if (items.size() == 0 && fluids.size() == 0) {
return null;
}
// Filter out empty fluids.

// Build input.
List<List<AbstractMapIngredient>> list = new ObjectArrayList<>(items.size() + fluids.size());
if (items.size() > 0) {
buildFromItemStacks(list, uniqueItems(items));
Expand Down Expand Up @@ -404,72 +372,6 @@ public static List<GTRecipeInput> uniqueIngredientsList(List<GTRecipeInput> inpu
return list;
}

/**
* Returns a boolean indicating whether the given group of fluids resolves to a valid branch or recipe.
*
* @param fluidIngredients the ingredients part
* @param map the root branch to search from.
* @return a recipe
*/
private boolean canInsertFluid(@Nonnull List<List<AbstractMapIngredient>> fluidIngredients, @Nonnull Branch map) {
// Try each ingredient as a starting point, adding it to the skiplist.
boolean canInsert;
for (int i = 0; i < fluidIngredients.size(); i++) {
canInsert = recurseFluidTreeFindBranchOrRecipe(fluidIngredients, map, i, 0, (1L << i));
if (canInsert) {
return true;
}
}
return false;
}

/**
* Recursively finds either a recipe or a branch, and return it upon evaluating all the ingredients
*
* @param fluidIngredients the ingredients part
* @param branchMap the current branch of the tree
* @param index the index of the wrapper to get
* @param count how deep we are in recursion, < ingredients.length
* @param skip bitmap of ingredients to skip, i.e. which ingredients are used in the
* recursion.
* @return True if the current fluid ingredients resolve to a valid branch or recipe. False otherwise.
*/
private boolean recurseFluidTreeFindBranchOrRecipe(@Nonnull List<List<AbstractMapIngredient>> fluidIngredients, @Nonnull Branch branchMap, int index, int count, long skip) {
List<AbstractMapIngredient> wr = fluidIngredients.get(index);
// Iterate over current level of nodes.
for (AbstractMapIngredient t : wr) {
Either<Recipe, Branch> result = branchMap.getNodes().get(t);
if (result != null) {
if (result.left().isPresent() && count == fluidIngredients.size() - 1) {
return true;
} else if (result.right().isPresent()) {
if (count == fluidIngredients.size()) {
return true;
}
return diveFluidTreeFindBranchOrRecipe(fluidIngredients, result.right().get(), index, count, skip);
}
}
}
return false;
}

private boolean diveFluidTreeFindBranchOrRecipe(@Nonnull List<List<AbstractMapIngredient>> fluidIngredients, @Nonnull Branch branchMap, int index, int count, long skip) {
// We loop around fluidIngredients.size() if we reach the end.
int counter = (index + 1) % fluidIngredients.size();
while (counter != index) {
// Have we already used this ingredient? If so, skip this one.
if (((skip & (1L << counter)) == 0)) {
// Recursive call.
boolean found = recurseFluidTreeFindBranchOrRecipe(fluidIngredients, branchMap, counter, count + 1, skip | (1L << counter));
if (found) {
return true;
}
}
counter = (counter + 1) % fluidIngredients.size();
}
return false;
}

/**
* Recursively finds a recipe, top level. call this to find a recipe
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ public RecipeMapFluidCanner(String unlocalizedName, int minInputs, int maxInputs
super(unlocalizedName, minInputs, maxInputs, minOutputs, maxOutputs, minFluidInputs, maxFluidInputs, minFluidOutputs, maxFluidOutputs, defaultRecipe, isHidden);
}

@Override
public boolean canInputFluidForce(Fluid fluid) {
return true;
}

@Override
@Nullable
public Recipe findRecipe(long voltage, List<ItemStack> inputs, List<FluidStack> fluidInputs, int outputFluidTankCapacity, boolean exactVoltage) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,12 @@
import net.minecraftforge.fml.relauncher.SideOnly;

import javax.annotation.Nonnull;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

public class PowerlessJetpack implements ISpecialArmorLogic, IArmorLogic, IJetpack {

private static final Collection<Recipe> FUELS = RecipeMaps.COMBUSTION_GENERATOR_FUELS.getRecipeList();

public final int tankCapacity = 16000;

private Recipe previousRecipe = null;
Expand Down Expand Up @@ -194,9 +193,9 @@ public void findNewRecipe(@Nonnull ItemStack stack) {
currentRecipe = previousRecipe;
return;
} else if (fluidStack != null) {
Collection<Recipe> recipes = RecipeMaps.COMBUSTION_GENERATOR_FUELS.getRecipesForFluid(fluidStack);
if (!recipes.isEmpty()) {
previousRecipe = (Recipe) recipes.toArray()[0];
Recipe recipe = RecipeMaps.COMBUSTION_GENERATOR_FUELS.find(Collections.emptyList(), Collections.singletonList(fluidStack), (Objects::nonNull));
if (recipe != null) {
previousRecipe = recipe;
currentRecipe = previousRecipe;
return;
}
Expand Down Expand Up @@ -271,7 +270,7 @@ public ICapabilityProvider createProvider(ItemStack itemStack) {
return new FluidHandlerItemStack(itemStack, maxCapacity) {
@Override
public boolean canFillFluidType(FluidStack fluidStack) {
return !RecipeMaps.COMBUSTION_GENERATOR_FUELS.getRecipesForFluid(fluidStack).isEmpty();
return RecipeMaps.COMBUSTION_GENERATOR_FUELS.find(Collections.emptyList(), Collections.singletonList(fluidStack), (Objects::nonNull)) != null;
}

@Override
Expand Down