/
TCompRegistry.java
454 lines (405 loc) · 15.1 KB
/
TCompRegistry.java
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
package knightminer.tcomplement.library;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Nonnull;
import org.apache.logging.log4j.Logger;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import knightminer.tcomplement.library.events.TCompRegisterEvent;
import knightminer.tcomplement.library.steelworks.EmptyMixRecipe;
import knightminer.tcomplement.library.steelworks.HeatRecipe;
import knightminer.tcomplement.library.steelworks.HighOvenFuel;
import knightminer.tcomplement.library.steelworks.IHeatRecipe;
import knightminer.tcomplement.library.steelworks.IHighOvenFilter;
import knightminer.tcomplement.library.steelworks.IMixRecipe;
import knightminer.tcomplement.library.steelworks.MixAdditive;
import knightminer.tcomplement.library.steelworks.MixRecipe;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidStack;
import slimeknights.mantle.client.CreativeTab;
import slimeknights.mantle.util.RecipeMatch;
import slimeknights.mantle.util.RecipeMatchRegistry;
import slimeknights.tconstruct.library.TinkerRegistry;
import slimeknights.tconstruct.library.smeltery.MeltingRecipe;
public class TCompRegistry {
public static final Logger log = Util.getLogger("API");
/*-------------------------------------------------------------------------*\
| CREATIVE TABS |
\*-------------------------------------------------------------------------*/
public static CreativeTab tabGeneral = new CreativeTab("TCompGeneral", new ItemStack(Blocks.BRICK_BLOCK));
public static CreativeTab tabTools = new CreativeTab("TCompTools", new ItemStack(Items.IRON_PICKAXE));
/*-------------------------------------------------------------------------*\
| Melter |
\*-------------------------------------------------------------------------*/
// this is basically a wrapper for the Tinkers Registry allowing me to override recipes without affecting the smeltery
private static List<MeltingRecipe> meltingOverrides = Lists.newLinkedList();
private static List<IBlacklist> meltingBlacklist = Lists.newLinkedList();
/**
* Registers a melter override recipe. This is a recipe that exists only in the melter, typically used to replace a smeltery recipe
* @param recipe Recipe to register
*/
public static void registerMelterOverride(MeltingRecipe recipe) {
if(new TCompRegisterEvent.MelterOverrideRegisterEvent(recipe).fire()) {
meltingOverrides.add(recipe);
}
else {
try {
String input = recipe.input.getInputs().stream().findFirst().map(ItemStack::getUnlocalizedName).orElse("?");
log.debug("Registration of melting recipe for " + recipe.getResult().getUnlocalizedName() + " from " + input + " has been cancelled by event");
} catch(Exception e) {
log.error("Error when logging melting event", e);
}
}
}
/**
* Gets all melter overrides
* @return Immutable list of all melter overrides
*/
public static List<MeltingRecipe> getAllMeltingOverrides() {
return ImmutableList.copyOf(meltingOverrides);
}
/**
* Blacklists an input from being used for a normal smeltery recipe. This is not needed if an override is added with that input
* @param blacklist Blacklist entry
*/
public static void registerMelterBlacklist(IBlacklist blacklist) {
if(new TCompRegisterEvent.MelterBlackListRegisterEvent(blacklist).fire()) {
meltingBlacklist.add(blacklist);
}
else {
try {
log.debug("Registration of melter blacklist recipe has been cancelled by event");
} catch(Exception e) {
log.error("Error when logging melting event", e);
}
}
}
/**
* Registers a blacklist entry using a RecipeMatch entry
* @param blacklist RecipeMatch to blacklist
*/
public static void registerMelterBlacklist(RecipeMatch blacklist) {
registerMelterBlacklist(new RecipeMatchBlacklist(blacklist));
}
/**
* Checks if a melting recipe is hidden by the melter overrides or blacklist
* @param recipe Recipe to check
* @return true if the recipe would be hidden, false otherwise
*/
public static boolean isSmeltingHidden(MeltingRecipe recipe) {
List<ItemStack> inputs = recipe.input.getInputs();
// TODO: should probably validate that all inputs match for cases of list inputs, but probably not an issue
// check blacklist first, its probably quicker
for(IBlacklist blacklist : meltingBlacklist) {
if(inputs.stream().anyMatch(blacklist::matches)) {
return true;
}
}
// next try overrides
for(MeltingRecipe override : meltingOverrides) {
if(inputs.stream().anyMatch(override::matches)) {
return true;
}
}
return false;
}
/**
* Gets the melting recipe for a given item stack.
* This checks the overrides first, then runs though the blacklist before checking the smeltery registry
* @param stack Input stack
* @return recipe instance
*/
public static MeltingRecipe getMelting(ItemStack stack) {
// check if the recipe exists in our overrides
for(MeltingRecipe recipe : meltingOverrides) {
if(recipe.matches(stack)) {
return recipe;
}
}
// if not, check if it is a blacklisted melting recipe
for(IBlacklist blacklist : meltingBlacklist) {
if(blacklist.matches(stack)) {
return null;
}
}
// if not, use the Tinkers version
return TinkerRegistry.getMelting(stack);
}
/*-------------------------------------------------------------------------*\
| High Oven Mixes |
\*-------------------------------------------------------------------------*/
private static List<IMixRecipe> mixRegistry = Lists.newLinkedList();
private static Map<MixAdditive,RecipeMatchRegistry> mixAdditives = new EnumMap<>(MixAdditive.class);
static {
for(MixAdditive type : MixAdditive.values()) {
mixAdditives.put(type, new RecipeMatchRegistry());
}
}
/**
* Registers a new high oven mix recipe
* @param recipe Recipe to register
* @return Registered recipe for chaining
*/
@Nonnull
public static IMixRecipe registerMix(IMixRecipe recipe) {
if(new TCompRegisterEvent.HighOvenMixRegisterEvent(recipe).fire()) {
mixRegistry.add(recipe);
return recipe;
}
else try {
FluidStack output = recipe.getOutput();
if (output != null) {
log.debug("Registration of mix recipe for " + output.getUnlocalizedName() + " has been cancelled by event");
}
} catch(Exception e) {
log.error("Error when logging mix event", e);
}
// empty recipe so no null checks needed for chaining
return EmptyMixRecipe.INSTANCE;
}
/**
* Registers a new mix from the given input and output
* @param input Recipe fluid input
* @param output Recipe fluid output
* @return IMixRecipe instance
*/
@Nonnull
public static IMixRecipe registerMix(FluidStack input, FluidStack output) {
return registerMix(new MixRecipe(input, output));
}
/**
* Gets the mix recipe for the given set of inputs
* @param fluid FluidStack input
* @param oxidizer Item in oxidizer slot
* @param reducer Item in reducer slot
* @param purifier Item in purifier slot
* @return First mix recipe found, null if nothing found
*/
public static IMixRecipe getMixRecipe(FluidStack fluid, ItemStack oxidizer, ItemStack reducer, ItemStack purifier) {
for(IMixRecipe recipe : mixRegistry) {
if (recipe.matches(fluid, oxidizer, reducer, purifier)) {
return recipe;
}
}
return null;
}
/**
* Gets a heat recipe or a mix recipe for a filter based on its input and output
* @param input Fluid input
* @param output Fluid result
* @return First recipe found, null if nothing found
*/
public static IHighOvenFilter getFilter(FluidStack input, FluidStack output) {
for(IHeatRecipe recipe : heatRegistry) {
if (recipe.matches(input, output)) {
return recipe;
}
}
for(IMixRecipe recipe : mixRegistry) {
if (recipe.matches(input, output)) {
return recipe;
}
}
return null;
}
/**
* Registers a mix additive for the sake of the high oven slot checks
* @param additive Additive recipe match to register
* @param type Additive type
*/
public static void registerMixAdditive(@Nonnull RecipeMatch additive, @Nonnull MixAdditive type) {
mixAdditives.get(type).addRecipeMatch(additive);
}
/**
* Checks if the given stack is a valid mix additive
* @param stack Stack to check
* @param type Additive type
* @return True if a valid additive
*/
public static boolean isValidMixAdditive(@Nonnull ItemStack stack, @Nonnull MixAdditive type) {
return mixAdditives.get(type).matches(stack).isPresent();
}
/**
* Gets all high oven mix recipes
* @return Immutable list of all mix recipes
*/
public static List<IMixRecipe> getAllMixRecipes() {
return ImmutableList.copyOf(mixRegistry);
}
/*-------------------------------------------------------------------------*\
| High Oven Heat recipes |
\*-------------------------------------------------------------------------*/
private static List<IHeatRecipe> heatRegistry = Lists.newLinkedList();
/**
* Registers a new high oven heat recipe
* @param recipe Recipe to register
* @return Registered recipe for chaining
*/
public static void registerHeatRecipe(IHeatRecipe recipe) {
if(new TCompRegisterEvent.HighOvenHeatRegisterEvent(recipe).fire()) {
heatRegistry.add(recipe);
}
else try {
FluidStack output = recipe.getOutput();
if (output != null) {
log.debug("Registration of heat recipe for " + output.getUnlocalizedName() + " has been cancelled by event");
}
} catch(Exception e) {
log.error("Error when logging mix event", e);
}
}
public static void registerHeatRecipe(@Nonnull FluidStack input, @Nonnull FluidStack output, int temp) {
registerHeatRecipe(new HeatRecipe(input, output, temp));
}
/**
* Gets a heat recipe based on its input
* @param input Fluid input
* @return First heat recipe found, null if nothing found
*/
public static IHeatRecipe getHeatRecipe(FluidStack input) {
for(IHeatRecipe recipe : heatRegistry) {
if (recipe.matches(input)) {
return recipe;
}
}
return null;
}
/**
* Gets all high oven heat recipes
* @return Immutable list of all heat recipes
*/
public static List<IHeatRecipe> getAllHeatRecipes() {
return ImmutableList.copyOf(heatRegistry);
}
/*-------------------------------------------------------------------------*\
| High Oven Fuels |
\*-------------------------------------------------------------------------*/
private static List<HighOvenFuel> highOvenFuels = Lists.newLinkedList();
private static void registerFuel(HighOvenFuel fuel) {
if(new TCompRegisterEvent.HighOvenFuelRegisterEvent(fuel).fire()) {
highOvenFuels.add(fuel);
}
else try {
String input = fuel.getFuels().stream().findFirst().map(ItemStack::getUnlocalizedName).orElse("?");
log.debug("Registration of high oven fuel for " + input + " has been cancelled by event");
} catch(Exception e) {
log.error("Error when logging high oven fuel event", e);
}
}
/**
* Registers a new high oven fuel
* @param fuel Fuel recipe match entry
* @param time Fuel burn time in seconds
* @param rate Temperature increase per second
*/
public static void registerFuel(RecipeMatch fuel, int time, int rate) {
registerFuel(new HighOvenFuel(fuel, time, rate));
}
/**
* Registers a new high oven fuel
* @param fuel Fuel item stack
* @param time Fuel burn time in seconds
* @param rate Temperature increase per second
*/
public static void registerFuel(ItemStack fuel, int time, int rate) {
registerFuel(RecipeMatch.of(fuel), time, rate);
}
/**
* Registers a new high oven fuel
* @param fuel Fuel oredictionary name
* @param time Fuel burn time in seconds
* @param rate Temperature increase per second
*/
public static void registerFuel(String fuel, int time, int rate) {
registerFuel(RecipeMatch.of(fuel), time, rate);
}
/**
* Gets the HighOvenFuel object matching this item stack
* @param input Stack to search
* @return HighOvenFuel object containing time and rate information
*/
public static HighOvenFuel getHighOvenFuel(ItemStack input) {
for (HighOvenFuel fuel : highOvenFuels) {
if (fuel.matches(input)) {
return fuel;
}
}
return null;
}
/**
* Checks if the given fuel is valid
* @param input Stack to search
* @return true if valid, false otherwise
*/
public static boolean isHighOvenFuel(ItemStack input) {
return getHighOvenFuel(input) != null;
}
/**
* Gets a list of all high oven fuels for display in JEI
* @return ImmutableList of all high oven fuels
*/
public static List<HighOvenFuel> getAllHighOvenFuels() {
return ImmutableList.copyOf(highOvenFuels);
}
/*-------------------------------------------------------------------------*\
| High Oven Overrides |
\*-------------------------------------------------------------------------*/
private static List<MeltingRecipe> highOvenOverrides = Lists.newLinkedList();
/**
* Registers a melter override recipe. This is a recipe that exists only in the melter, typically used to replace a smeltery recipe
* @param recipe Recipe to register
*/
public static void registerHighOvenOverride(MeltingRecipe recipe) {
if(new TCompRegisterEvent.HighOvenOverrideRegisterEvent(recipe).fire()) {
highOvenOverrides.add(recipe);
}
else try {
String input = recipe.input.getInputs().stream().findFirst().map(ItemStack::getUnlocalizedName).orElse("?");
log.debug("Registration of melting recipe for " + recipe.getResult().getUnlocalizedName() + " from " + input + " has been cancelled by event");
} catch(Exception e) {
log.error("Error when logging melting event", e);
}
}
/**
* Gets all high oven overrides
* @return Immutable list of all melter overrides
*/
public static List<MeltingRecipe> getAllHighOvenOverrides() {
return ImmutableList.copyOf(highOvenOverrides);
}
/**
* Checks if a melting recipe is hidden by the melter overrides or blacklist
* @param recipe Recipe to check
* @return true if the recipe would be hidden, false otherwise
*/
public static boolean isOvenHidden(MeltingRecipe recipe) {
List<ItemStack> inputs = recipe.input.getInputs();
// TODO: should probably validate that all inputs match for cases of list inputs, but probably not an issue
// no blacklist, we can just try overrides
for(MeltingRecipe override : highOvenOverrides) {
if(inputs.stream().anyMatch(override::matches)) {
return true;
}
}
return false;
}
/**
* Gets the melting recipe for a given item stack for the high oven.
* This is the same as the TinkerRegistry method, but checks high oven overrides
* @param stack Input stack
* @return recipe instance
*/
public static MeltingRecipe getOvenMelting(ItemStack stack) {
// check if the recipe exists in our overrides
for(MeltingRecipe recipe : highOvenOverrides) {
if(recipe.matches(stack)) {
return recipe;
}
}
// if not, use the Tinkers version
return TinkerRegistry.getMelting(stack);
}
}