aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/api/util/recipe/RecipeInputRequirements.java
blob: 590c10410151ca1073df2982231ba647fed5b9c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package gregtech.api.util.recipe;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.annotation.Nonnull;

import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;

import gregtech.api.util.GT_Recipe;
import gregtech.api.util.item.ItemHolder;

public class RecipeInputRequirements {

    protected Map<ItemHolder, Long> itemInputs = new HashMap<>();
    protected Set<ItemHolder> itemInputsMet = new HashSet<>();
    protected boolean metAllItem = false;
    protected Map<Fluid, Long> fluidInputs = new HashMap<>();
    protected Set<Fluid> fluidInputsMet = new HashSet<>();
    protected boolean metAllFluid = false;

    public RecipeInputRequirements(@Nonnull GT_Recipe recipe) {
        this(recipe.mInputs, recipe.mFluidInputs);
    }

    public RecipeInputRequirements(@Nonnull ItemStack[] itemInputs, @Nonnull FluidStack[] fluidInputs) {
        for (ItemStack item : itemInputs) {
            if (item == null) continue;
            ItemHolder itemIH = new ItemHolder(item);
            this.itemInputs.put(itemIH, this.itemInputs.getOrDefault(itemIH, 0L) + item.stackSize);
        }

        for (FluidStack fluid : fluidInputs) {
            if (fluid == null) continue;
            this.fluidInputs.put(fluid.getFluid(), this.fluidInputs.getOrDefault(fluid.getFluid(), 0L) + fluid.amount);
        }
    }

    /**
     * 
     * @param itemInputs we have and want to fill this request
     * @return {@code true} when all item inputs are met
     */
    public boolean tryToFillItemRequirements(Map<ItemHolder, Long> itemInputs) {
        if (metAllItem) return metAllItem;
        for (Entry<ItemHolder, Long> entry : itemInputs.entrySet()) {
            if (itemInputsMet.contains(entry.getKey())) continue;
            if (!this.itemInputs.containsKey(entry.getKey())) continue;
            if (this.itemInputs.get(entry.getKey()) > entry.getValue()) continue;
            itemInputsMet.add(entry.getKey());
        }
        metAllItem = itemInputsMet.containsAll(this.itemInputs.keySet());
        return metAllItem;
    }

    /**
     * 
     * @param fluidInputs we have and want to fill this request
     * @return {@code true} when all fluid inputs are met
     */
    public boolean tryToFillFluidRequirements(Map<Fluid, Long> fluidInputs) {
        if (metAllFluid) return metAllFluid;
        for (Entry<Fluid, Long> entry : fluidInputs.entrySet()) {
            if (fluidInputsMet.contains(entry.getKey())) continue;
            if (!this.fluidInputs.containsKey(entry.getKey())) continue;
            if (this.fluidInputs.get(entry.getKey()) > entry.getValue()) continue;
            fluidInputsMet.add(entry.getKey());
        }
        metAllFluid = fluidInputsMet.containsAll(this.fluidInputs.keySet());
        return metAllFluid;
    }
}