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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
package gregtech.api.recipe;
import static gregtech.api.util.GTRecipeBuilder.ENABLE_COLLISION_CHECK;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import org.jetbrains.annotations.Unmodifiable;
import gregtech.api.interfaces.IRecipeMap;
import gregtech.api.interfaces.tileentity.IHasWorldObjectAndCoords;
import gregtech.api.util.FieldsAreNonnullByDefault;
import gregtech.api.util.GTRecipe;
import gregtech.api.util.GTRecipeBuilder;
import gregtech.api.util.MethodsReturnNonnullByDefault;
/**
* Manages list of recipes. Its functionalities are split
* between {@link RecipeMapBackend} and {@link RecipeMapFrontend}.
*
* @param <B> Type of {@link RecipeMapBackend}
*/
@SuppressWarnings({ "unused", "UnusedReturnValue" })
@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
@FieldsAreNonnullByDefault
public final class RecipeMap<B extends RecipeMapBackend> implements IRecipeMap {
/**
* All the recipemap instances. key=unlocalized name, value=instance.
*/
public static final Map<String, RecipeMap<?>> ALL_RECIPE_MAPS = new HashMap<>();
private final B backend;
private final RecipeMapFrontend frontend;
/**
* Unique unlocalized name of this recipemap. Used for identifier, localization key for NEI tab name, etc.
*/
public final String unlocalizedName;
private final RecipeCategory defaultRecipeCategory;
/**
* Use {@link RecipeMapBuilder} to instantiate.
*/
RecipeMap(String unlocalizedName, B backend, RecipeMapFrontend frontend) {
this.unlocalizedName = unlocalizedName;
this.backend = backend;
this.frontend = frontend;
this.defaultRecipeCategory = new RecipeCategory(this);
backend.setRecipeMap(this);
if (ALL_RECIPE_MAPS.containsKey(unlocalizedName)) {
throw new IllegalArgumentException(
"Cannot register recipemap with duplicated unlocalized name: " + unlocalizedName);
}
ALL_RECIPE_MAPS.put(unlocalizedName, this);
}
public B getBackend() {
return backend;
}
public RecipeMapFrontend getFrontend() {
return frontend;
}
/**
* @return All the recipes belonging to this recipemap.
*/
@Unmodifiable
public Collection<GTRecipe> getAllRecipes() {
return backend.getAllRecipes();
}
/**
* @return List of registered recipe categories associated with this recipemap.
*/
public List<RecipeCategory> getAssociatedCategories() {
return RecipeCategory.ALL_RECIPE_CATEGORIES.values()
.stream()
.filter(category -> category.recipeMap == this)
.collect(Collectors.toList());
}
public RecipeCategory getDefaultRecipeCategory() {
return defaultRecipeCategory;
}
/**
* @return Amperage of this recipemap. Note that recipes store EU/t with amperage included,
* e.g. Arc Furnace recipe with 90 EU/t means 30 EU/t (LV) with 3 amperage.
*/
public int getAmperage() {
return frontend.getUIProperties().amperage;
}
@Override
public void addDownstream(IRecipeMap downstream) {
backend.addDownstream(downstream);
}
// region add recipe
@Deprecated
@Nullable
public GTRecipe addRecipe(GTRecipe aRecipe) {
return addRecipe(aRecipe, true, false, false);
}
@Deprecated
@Nullable
public GTRecipe addRecipe(GTRecipe aRecipe, boolean aCheckForCollisions, boolean aFakeRecipe, boolean aHidden) {
aRecipe.mHidden = aHidden;
aRecipe.mFakeRecipe = aFakeRecipe;
if (aRecipe.mFluidInputs.length < backend.properties.minFluidInputs
&& aRecipe.mInputs.length < backend.properties.minItemInputs) return null;
if (aCheckForCollisions && ENABLE_COLLISION_CHECK && backend.checkCollision(aRecipe)) return null;
return backend.compileRecipe(aRecipe);
}
/**
* Only used for fake Recipe Handlers to show something in NEI, do not use this for adding actual Recipes!
* findRecipe won't find fake Recipes, containsInput WILL find fake Recipes
*/
@Deprecated
@Nullable
public GTRecipe addFakeRecipe(boolean aCheckForCollisions, @Nullable ItemStack[] aInputs,
@Nullable ItemStack[] aOutputs, @Nullable Object aSpecial, @Nullable FluidStack[] aFluidInputs,
@Nullable FluidStack[] aFluidOutputs, int aDuration, int aEUt, int aSpecialValue, ItemStack[][] aAlt,
boolean hidden) {
return addFakeRecipe(
aCheckForCollisions,
new GTRecipe.GTRecipe_WithAlt(
false,
aInputs,
aOutputs,
aSpecial,
null,
aFluidInputs,
aFluidOutputs,
aDuration,
aEUt,
aSpecialValue,
aAlt),
hidden);
}
/**
* Only used for fake Recipe Handlers to show something in NEI, do not use this for adding actual Recipes!
* findRecipe won't find fake Recipes, containsInput WILL find fake Recipes
*/
@Deprecated
@Nullable
public GTRecipe addFakeRecipe(boolean aCheckForCollisions, GTRecipe aRecipe) {
return addRecipe(aRecipe, aCheckForCollisions, true, false);
}
@Deprecated
@Nullable
public GTRecipe addFakeRecipe(boolean aCheckForCollisions, GTRecipe aRecipe, boolean hidden) {
return addRecipe(aRecipe, aCheckForCollisions, true, hidden);
}
@Nonnull
@Override
public Collection<GTRecipe> doAdd(GTRecipeBuilder builder) {
return backend.doAdd(builder);
}
public GTRecipe add(GTRecipe aRecipe) {
return backend.compileRecipe(aRecipe);
}
// endregion
/**
* @return if this Item is a valid Input for any for the Recipes
*/
public boolean containsInput(@Nullable ItemStack aStack) {
return aStack != null && backend.containsInput(aStack);
}
/**
* @return if this Fluid is a valid Input for any for the Recipes
*/
public boolean containsInput(@Nullable FluidStack aFluid) {
return aFluid != null && containsInput(aFluid.getFluid());
}
/**
* @return if this Fluid is a valid Input for any for the Recipes
*/
public boolean containsInput(@Nullable Fluid aFluid) {
return aFluid != null && backend.containsInput(aFluid);
}
// region find recipe
/**
* @return Entrypoint for fluent API for finding recipe.
*/
public FindRecipeQuery findRecipeQuery() {
return new FindRecipeQuery(this);
}
@Nullable
public GTRecipe findRecipe(@Nullable IHasWorldObjectAndCoords aTileEntity, boolean aNotUnificated, long aVoltage,
@Nullable FluidStack[] aFluids, @Nullable ItemStack... aInputs) {
return findRecipe(aTileEntity, null, aNotUnificated, aVoltage, aFluids, null, aInputs);
}
@Nullable
public GTRecipe findRecipe(@Nullable IHasWorldObjectAndCoords aTileEntity, boolean aNotUnificated,
boolean aDontCheckStackSizes, long aVoltage, @Nullable FluidStack[] aFluids, @Nullable ItemStack... aInputs) {
return findRecipe(aTileEntity, null, aNotUnificated, aDontCheckStackSizes, aVoltage, aFluids, null, aInputs);
}
@Nullable
public GTRecipe findRecipe(@Nullable IHasWorldObjectAndCoords aTileEntity, @Nullable GTRecipe aRecipe,
boolean aNotUnificated, long aVoltage, @Nullable FluidStack[] aFluids, @Nullable ItemStack aSpecialSlot,
@Nullable ItemStack... aInputs) {
return findRecipe(aTileEntity, aRecipe, aNotUnificated, false, aVoltage, aFluids, aSpecialSlot, aInputs);
}
@Nullable
public GTRecipe findRecipe(@Nullable IHasWorldObjectAndCoords aTileEntity, @Nullable GTRecipe aRecipe,
boolean aNotUnificated, boolean aDontCheckStackSizes, long aVoltage, @Nullable FluidStack[] aFluids,
@Nullable ItemStack aSpecialSlot, @Nullable ItemStack... aInputs) {
return findRecipeQuery().items(aInputs != null ? aInputs : new ItemStack[0])
.fluids(aFluids != null ? aFluids : new FluidStack[0])
.specialSlot(aSpecialSlot)
.voltage(aVoltage)
.cachedRecipe(aRecipe)
.notUnificated(aNotUnificated)
.dontCheckStackSizes(aDontCheckStackSizes)
.find();
}
// endregion
@Override
public String toString() {
return "RecipeMap{" + "unlocalizedName='"
+ unlocalizedName
+ '\''
+ ", ownerMod="
+ defaultRecipeCategory.ownerMod.getModId()
+ '}';
}
private static final Pattern LEGACY_IDENTIFIER_PATTERN = Pattern.compile("(.+)_[0-9]+_[0-9]+_[0-9]+_[0-9]+_[0-9]+");
/**
* Gets recipemap instance from old mUniqueIdentifier format. This is only for backward compat, where tiles
* saved recipemap with mUniqueIdentifier.
*
* @param legacyIdentifier mUniqueIdentifier, in %s_%d_%d_%d_%d_%d format
* @return Found recipemap, can be null
*/
@Nullable
public static RecipeMap<?> getFromOldIdentifier(String legacyIdentifier) {
Matcher matcher = LEGACY_IDENTIFIER_PATTERN.matcher(legacyIdentifier);
if (!matcher.find()) {
// It can be new format
return ALL_RECIPE_MAPS.get(legacyIdentifier);
}
return ALL_RECIPE_MAPS.get(matcher.group(1));
}
}
|