From 50ec92e8ee8dcb8816f888cccab9ff95d977edf2 Mon Sep 17 00:00:00 2001 From: PrototypeTrousers Date: Sun, 21 Aug 2022 17:30:21 -0300 Subject: [PATCH] Remove machine fluid filtering Fixes #1147 --- .../capability/impl/AbstractRecipeLogic.java | 4 +- .../WorkableTieredMetaTileEntity.java | 27 +---- .../java/gregtech/api/recipes/RecipeMap.java | 98 ------------------- .../machines/RecipeMapFluidCanner.java | 5 - .../common/items/armor/PowerlessJetpack.java | 13 ++- 5 files changed, 11 insertions(+), 136 deletions(-) diff --git a/src/main/java/gregtech/api/capability/impl/AbstractRecipeLogic.java b/src/main/java/gregtech/api/capability/impl/AbstractRecipeLogic.java index 4a0e2cd3091..de549b016a9 100644 --- a/src/main/java/gregtech/api/capability/impl/AbstractRecipeLogic.java +++ b/src/main/java/gregtech/api/capability/impl/AbstractRecipeLogic.java @@ -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; @@ -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(); diff --git a/src/main/java/gregtech/api/metatileentity/WorkableTieredMetaTileEntity.java b/src/main/java/gregtech/api/metatileentity/WorkableTieredMetaTileEntity.java index 87d61f2cd64..6bc2a3b46bf 100644 --- a/src/main/java/gregtech/api/metatileentity/WorkableTieredMetaTileEntity.java +++ b/src/main/java/gregtech/api/metatileentity/WorkableTieredMetaTileEntity.java @@ -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; @@ -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 fluidKeyCache; - protected final RecipeLogicEnergy workable; protected final RecipeMap recipeMap; protected final ICubeRenderer renderer; @@ -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); @@ -133,21 +125,6 @@ protected FluidTankList createExportFluidHandler() { return new FluidTankList(false, fluidExports); } - protected boolean canInputFluid(FluidStack inputFluid) { - RecipeMap recipeMap = workable.getRecipeMap(); - List 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 tooltip, boolean advanced) { super.addInformation(stack, player, tooltip, advanced); diff --git a/src/main/java/gregtech/api/recipes/RecipeMap.java b/src/main/java/gregtech/api/recipes/RecipeMap.java index 10529a4521c..f4231719669 100644 --- a/src/main/java/gregtech/api/recipes/RecipeMap.java +++ b/src/main/java/gregtech/api/recipes/RecipeMap.java @@ -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; @@ -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 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() @@ -301,22 +287,6 @@ public Recipe findRecipe(long voltage, List inputs, List }); } - public boolean acceptsFluid(List 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 = new ObjectArrayList<>(); - buildFromFluidStacks(list, fluidInputs); - return canInsertFluid(list, lookup); - } - @Nullable public Recipe find(@Nonnull List items, @Nonnull List fluids, @Nonnull Predicate canHandle) { // First, check if items and fluids are valid. @@ -326,9 +296,7 @@ public Recipe find(@Nonnull List items, @Nonnull List flu if (items.size() == 0 && fluids.size() == 0) { return null; } - // Filter out empty fluids. - // Build input. List> list = new ObjectArrayList<>(items.size() + fluids.size()); if (items.size() > 0) { buildFromItemStacks(list, uniqueItems(items)); @@ -404,72 +372,6 @@ public static List uniqueIngredientsList(List 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> 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> fluidIngredients, @Nonnull Branch branchMap, int index, int count, long skip) { - List wr = fluidIngredients.get(index); - // Iterate over current level of nodes. - for (AbstractMapIngredient t : wr) { - Either 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> 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 * diff --git a/src/main/java/gregtech/api/recipes/machines/RecipeMapFluidCanner.java b/src/main/java/gregtech/api/recipes/machines/RecipeMapFluidCanner.java index 47f79a732ab..a53f983b5ee 100644 --- a/src/main/java/gregtech/api/recipes/machines/RecipeMapFluidCanner.java +++ b/src/main/java/gregtech/api/recipes/machines/RecipeMapFluidCanner.java @@ -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 inputs, List fluidInputs, int outputFluidTankCapacity, boolean exactVoltage) { diff --git a/src/main/java/gregtech/common/items/armor/PowerlessJetpack.java b/src/main/java/gregtech/common/items/armor/PowerlessJetpack.java index a484e933f4d..2f5fdc39312 100644 --- a/src/main/java/gregtech/common/items/armor/PowerlessJetpack.java +++ b/src/main/java/gregtech/common/items/armor/PowerlessJetpack.java @@ -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 FUELS = RecipeMaps.COMBUSTION_GENERATOR_FUELS.getRecipeList(); - public final int tankCapacity = 16000; private Recipe previousRecipe = null; @@ -194,9 +193,9 @@ public void findNewRecipe(@Nonnull ItemStack stack) { currentRecipe = previousRecipe; return; } else if (fluidStack != null) { - Collection 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; } @@ -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