aboutsummaryrefslogtreecommitdiff
path: root/src/Java/gtPlusPlus/xmod/gregtech/loaders/RecipeGen_FluidCanning.java
blob: fbf61598d34142602a6af28de4fec557722cb672 (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
package gtPlusPlus.xmod.gregtech.loaders;

import java.util.HashSet;
import java.util.Set;

import gregtech.api.GregTech_API;
import gregtech.api.enums.GT_Values;
import gregtech.api.enums.Materials;
import gregtech.api.util.GT_Recipe;
import gregtech.api.util.GT_Recipe.GT_Recipe_Map;
import gregtech.api.util.Recipe_GT;
import gtPlusPlus.api.interfaces.RunnableWithInfo;
import gtPlusPlus.api.objects.Logger;
import gtPlusPlus.core.material.Material;
import gtPlusPlus.core.material.MaterialGenerator;
import gtPlusPlus.core.util.reflect.ReflectionUtils;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidStack;

public class RecipeGen_FluidCanning extends RecipeGen_Base {

	public final static Set<RunnableWithInfo<Material>> mRecipeGenMap = new HashSet<RunnableWithInfo<Material>>();
	static {
		MaterialGenerator.mRecipeMapsToGenerate.put(mRecipeGenMap);
	}

	private final GT_Recipe recipe;
	private final boolean isValid;

	public boolean valid() {
		return isValid;
	}
	public RecipeGen_FluidCanning(boolean aExtracting, ItemStack aEmpty, ItemStack aFull, FluidStack aFluid) {
		this(aExtracting, aEmpty, aFull, aFluid, GT_Values.NF, null, null);
	}

	public RecipeGen_FluidCanning(boolean aExtracting, ItemStack aEmpty, ItemStack aFull, FluidStack aFluidIn, FluidStack aFluidOut) {
		this(aExtracting, aEmpty, aFull, aFluidIn, aFluidOut, null, null);
	}

	public RecipeGen_FluidCanning(boolean aExtracting, ItemStack aEmpty, ItemStack aFull, FluidStack aFluid, Integer aDuration, Integer aEUt) {
		this(aExtracting, aEmpty, aFull, aFluid, GT_Values.NF, aDuration, aEUt);
	}

	// Alternative Constructor
	public RecipeGen_FluidCanning(boolean aExtracting, ItemStack aEmpty, ItemStack aFull, FluidStack aFluidIn, FluidStack aFluidOut, Integer aDuration, Integer aEUt) {
		ItemStack aInput;
		ItemStack aOutput;
		FluidStack aFluidInput;
		FluidStack aFluidOutput;

		// Safety check on the duration
		if (aDuration == null || aDuration <= 0) {
			aDuration = (aFluidIn != null) ? (aFluidIn.amount / 62) : ((aFluidOut != null) ? (aFluidOut.amount / 62) : 10);
		}

		// Safety check on the EU
		if (aEUt == null || aEUt <= 0) {
			if (aExtracting) {
				aEUt = 2;
			}
			else {
				aEUt = 1;
			}
		}

		// Set Item stacks correctly, invert if extraction recipe.
		if (aExtracting) {
			aInput = aFull;
			aOutput = aEmpty;
			aFluidInput = GT_Values.NF;
			aFluidOutput = aFluidIn;			
		}
		else {
			aInput = aEmpty;
			aOutput = aFull;		
			aFluidInput = aFluidIn;
			aFluidOutput = aFluidOut != null ? aFluidOut : GT_Values.NF;			
		}

		//Check validity

		Recipe_GT aRecipe = new Recipe_GT(
				true,
				new ItemStack[] { aInput },
				new ItemStack[] { aOutput },
				null,
				new int[] {10000},
				new FluidStack[] { aFluidInput },
				new FluidStack[] { aFluidOutput },
				aDuration,
				aEUt,
				0);


		// Check Valid		
		boolean aTempValidityCheck = false;
		if (aExtracting) {
			if (aInput != null && aFluidOutput != null) {
				aTempValidityCheck = true;
			}
		}
		else {
			if (aInput != null && aOutput != null && (aFluidInput != null || aFluidOutput != null)) {
				aTempValidityCheck = true;				
			}
		}		


		if (aTempValidityCheck) {
			// Valid Recipe
			recipe = aRecipe;
			mRecipeGenMap.add(this);
			disableOptional = aExtracting;
			isValid = true;			
		}
		else {
			isValid = false;
			disableOptional = aExtracting;
			aRecipe.mEnabled = false;
			aRecipe.mHidden = true;
			recipe = null;
		}
	}

	@Override
	public void run() {
		generateRecipes();
	}

	private void generateRecipes() {
		if (isValid && recipe != null) {
			if (this.disableOptional) {
				addFluidExtractionRecipe(recipe);	
			}
			else {
				addFluidCannerRecipe(recipe);
			}
		}		
	}

	private final boolean addFluidExtractionRecipe(GT_Recipe aRecipe) {
		if (aRecipe != null) {			
			int aCount1 = GT_Recipe_Map.sFluidExtractionRecipes.mRecipeList.size();
			int aCount2 = aCount1;
			GT_Recipe_Map.sFluidExtractionRecipes.addRecipe(aRecipe);
			aCount1 = GT_Recipe_Map.sFluidExtractionRecipes.mRecipeList.size();
			return aCount1 > aCount2;
		}
		return false;
	}

	private final boolean addFluidCannerRecipe(GT_Recipe recipe2) {
		if (recipe2 != null) {
			int aCount1 = GT_Recipe_Map.sFluidCannerRecipes.mRecipeList.size();
			int aCount2 = aCount1;
			GT_Recipe_Map.sFluidCannerRecipes.addRecipe(recipe2);
			aCount1 = GT_Recipe_Map.sFluidCannerRecipes.mRecipeList.size();
			return aCount1 > aCount2;
		}
		return false;
	}

	private void dumpStack() {
		int parents = 2;
		for (int i=0;i<6;i++) {
			Logger.INFO((disableOptional ? "EXTRACTING" : "CANNING")+" DEBUG | "+(i == 0 ? "Called from: " : "Parent: ")+ReflectionUtils.getMethodName(i+parents));			
		}

	}

	private String buildLogString() {
		int solidSize = getMapSize(GT_Recipe_Map.sCannerRecipes);
		int fluidSize = getMapSize(GT_Recipe_Map.sFluidCannerRecipes);		
		return (disableOptional ? "EXTRACTING" : "CANNING")+" DEBUG | Solids: "+solidSize+" | Liquids: "+fluidSize+" | ";
	}

	private final int getMapSize(GT_Recipe_Map aMap) {
		return aMap.mRecipeList.size();
	}

}