summaryrefslogtreecommitdiff
path: root/ihl/recipes/UniversalRecipeManager.java
blob: 169d6f64f62de0fc5073c527f3959213c695d0f6 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package ihl.recipes;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import ic2.api.recipe.IRecipeInput;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidStack;
import ihl.utils.IHLUtils;

public class UniversalRecipeManager {

	public static Map<String, UniversalRecipeManager> machineRecipeManagers = new HashMap<String, UniversalRecipeManager>();
	public final String machine;

	public UniversalRecipeManager(String machine1) {
		machine = machine1;
		if (machineRecipeManagers.containsKey(machine1)) {
			throw new IllegalArgumentException("Recipe manager for " + machine1 + " already exist!");
		}
		machineRecipeManagers.put(machine1, this);
	}

	private final Map<UniversalRecipeInput, UniversalRecipeOutput> recipes = new HashMap<UniversalRecipeInput, UniversalRecipeOutput>();
	private final Map<String, UniversalRecipeInput> keywordMap = new HashMap<String, UniversalRecipeInput>();

	public void addRecipe(UniversalRecipeInput input, UniversalRecipeOutput output) {
		if (input == null) {
			throw new NullPointerException("The recipe input is null");
		} else {
			if (output.getFluidOutputs() == null || output.getItemOutputs() == null
					|| (output.getFluidOutputs().size() == 0 && output.getItemOutputs().size() == 0)) {
				throw new NullPointerException("The output is empty");
			}
		}

		Iterator<UniversalRecipeInput> var8 = this.recipes.keySet().iterator();

		while (var8.hasNext()) {
			UniversalRecipeInput existingInput = (UniversalRecipeInput) var8.next();
			if (existingInput.matches(input)) {
				StringBuffer ssError = new StringBuffer(255);
				ssError.append("Ambiguous recipe. \n");
				ssError.append("Existing input: \n");
				Iterator<IRecipeInput> iii1 = existingInput.getItemInputs().iterator();
				Iterator<IRecipeInputFluid> fii1 = existingInput.getFluidInputs().iterator();
				while (iii1 != null && iii1.hasNext()) {
					ssError.append(iii1.next().toString());
					ssError.append(" \n");
				}
				while (fii1 != null && fii1.hasNext()) {
					ssError.append(fii1.next().toString());
					ssError.append(" \n");
				}
				ssError.append("New input: \n");
				Iterator<IRecipeInput> iii2 = input.getItemInputs().iterator();
				Iterator<IRecipeInputFluid> fii2 = input.getFluidInputs().iterator();
				while (iii2 != null && iii2.hasNext()) {
					ssError.append(iii2.next().toString());
					ssError.append(" \n");
				}
				while (fii2 != null && fii2.hasNext()) {
					ssError.append(fii2.next().toString());
					ssError.append(" \n");
				}
				throw new RuntimeException(ssError.toString());
			}
		}

		this.recipes.put(input, output);
	}

	public void addRecipe(String keyword, UniversalRecipeInput input, UniversalRecipeOutput output) {
		this.addRecipe(input, output);
		this.keywordMap.put(keyword, input);
	}

	public UniversalRecipeOutput getOutputFor(List<FluidStack> fluidInputs, List<ItemStack> itemInputs,
			boolean adjustInput, boolean inputAffectOutput) {
		if (fluidInputs == null && itemInputs == null) {
			return null;
		} else {
			Iterator<Entry<UniversalRecipeInput, UniversalRecipeOutput>> i$ = this.recipes.entrySet().iterator();

			while (true) {
				if (i$.hasNext()) {
					Entry<UniversalRecipeInput, UniversalRecipeOutput> entry = i$.next();
					UniversalRecipeInput recipeInput = entry.getKey();

					if (!recipeInput.matches(fluidInputs, itemInputs)) {
						continue;
					}

					if (recipeInput.adjustAmounts(fluidInputs, itemInputs, true, false)) {
						UniversalRecipeOutput output = entry.getValue();
						if (inputAffectOutput) {
							int multiplier = recipeInput.getMultiplierAndAdjustAmounts(fluidInputs, itemInputs);
							return output.copyWithMultiplier(multiplier);
						} else if (adjustInput) {
							recipeInput.adjustAmounts(fluidInputs, itemInputs, true, true);
						}
						return output;
					}
				}

				return null;
			}
		}
	}

	public Map<UniversalRecipeInput, UniversalRecipeOutput> getRecipes() {
		return this.recipes;
	}

	public UniversalRecipeInput getRecipeInput(List<FluidStack> fluidInputs1, List<ItemStack> itemInputs1) {
		{
			Iterator<Entry<UniversalRecipeInput, UniversalRecipeOutput>> i$ = this.recipes.entrySet().iterator();

			while (true) {
				if (i$.hasNext()) {
					Entry<UniversalRecipeInput, UniversalRecipeOutput> entry = i$.next();
					UniversalRecipeInput recipeInput = entry.getKey();

					if (!recipeInput.matches(fluidInputs1, itemInputs1)) {
						continue;
					}

					if (recipeInput.adjustAmounts(fluidInputs1, itemInputs1, true, false)) {
						return recipeInput;
					}
				}

				return null;
			}
		}
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public UniversalRecipeOutput getOutputFor(List[] input, boolean adjustInput, boolean inputAffectOutput) {
		return this.getOutputFor(input[0], input[1], adjustInput, inputAffectOutput);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public UniversalRecipeInput getRecipeInput(List[] input) {
		return this.getRecipeInput(input[0], input[1]);
	}

	public void removeRecipeByInput(UniversalRecipeInput uRecipeInput) {
		List<FluidStack> fluidInputs = IHLUtils.convertRecipeInputToFluidStackList(uRecipeInput.getFluidInputs());
		List<ItemStack> itemInputs = IHLUtils.convertRecipeInputToItemStackList(uRecipeInput.getItemInputs());
		{
			Iterator<Entry<UniversalRecipeInput, UniversalRecipeOutput>> i$ = this.recipes.entrySet().iterator();
			while (i$.hasNext()) {
				Entry<UniversalRecipeInput, UniversalRecipeOutput> entry = i$.next();
				UniversalRecipeInput recipeInput = entry.getKey();
				if (recipeInput.matches(fluidInputs, itemInputs)) {
					i$.remove();
					break;
				}
			}
		}
	}

	public void removeRecipeByOutput(UniversalRecipeOutput uRecipeOutput) {
		Iterator<Entry<UniversalRecipeInput, UniversalRecipeOutput>> i$ = this.recipes.entrySet().iterator();
		while (i$.hasNext()) {
			Entry<UniversalRecipeInput, UniversalRecipeOutput> entry = i$.next();
			UniversalRecipeOutput recipeOutput = entry.getValue();
			if (recipeOutputHasCommonEntries(recipeOutput, uRecipeOutput)) {
				i$.remove();
			}
		}
	}

	public boolean recipeOutputHasCommonEntries(UniversalRecipeOutput out, UniversalRecipeOutput out1) {
		List<FluidStack> fluidOutputs = out.getFluidOutputs();
		List<RecipeOutputItemStack> itemOutputs = out.getItemOutputs();
		if (!fluidOutputs.isEmpty() && !out1.getFluidOutputs().isEmpty()) {
			FluidStack fs1 = out1.getFluidOutputs().get(0);
			Iterator<FluidStack> fi = fluidOutputs.iterator();
			while (fi.hasNext()) {
				FluidStack fs = fi.next();
				if (fs.getFluid() == fs1.getFluid()) {
					return true;
				}
			}
		}
		if (!itemOutputs.isEmpty() && !out1.getItemOutputs().isEmpty()) {
			RecipeOutputItemStack is1 = out1.getItemOutputs().get(0);
			Iterator<RecipeOutputItemStack> ii = itemOutputs.iterator();
			while (ii.hasNext()) {
				RecipeOutputItemStack is = ii.next();
				if (is.matches(is1)) {
					return true;
				}
			}
		}
		return false;
	}
}