2014-07-07 21:31:10 +02:00
package com.pahimar.ee3.recipe ;
2014-06-14 21:40:45 +02:00
2014-07-24 22:01:42 +02:00
import com.google.common.collect.ImmutableMultimap ;
2014-06-14 21:40:45 +02:00
import com.google.common.collect.Multimap ;
2016-05-20 03:10:55 +02:00
import com.google.common.collect.TreeMultimap ;
2014-07-07 21:31:10 +02:00
import com.pahimar.ee3.exchange.WrappedStack ;
2016-05-20 03:10:55 +02:00
import com.pahimar.ee3.reference.Comparators ;
2015-05-07 21:11:23 +02:00
import com.pahimar.ee3.util.LoaderHelper ;
2015-03-22 21:52:48 +01:00
import com.pahimar.ee3.util.LogHelper ;
2015-05-07 21:11:23 +02:00
import cpw.mods.fml.common.Loader ;
2015-11-19 21:24:40 +01:00
import org.apache.logging.log4j.Marker ;
import org.apache.logging.log4j.MarkerManager ;
2014-06-14 21:40:45 +02:00
2016-05-20 03:10:55 +02:00
import java.util.Collection ;
import java.util.Set ;
import java.util.TreeSet ;
2014-06-14 21:40:45 +02:00
2015-11-19 21:24:40 +01:00
public class RecipeRegistry {
public static final Marker RECIPE_MARKER = MarkerManager . getMarker ( " EE3_RECIPE " , LogHelper . MOD_MARKER ) ;
2016-05-20 19:56:53 +02:00
public static final RecipeRegistry INSTANCE = new RecipeRegistry ( ) ;
2014-06-14 21:40:45 +02:00
2016-05-20 03:10:55 +02:00
private Multimap < WrappedStack , Set < WrappedStack > > recipeMap ;
private ImmutableMultimap < WrappedStack , Set < WrappedStack > > immutableRecipeMap ;
2014-06-14 21:40:45 +02:00
2015-11-19 21:24:40 +01:00
private RecipeRegistry ( ) {
2016-05-20 03:10:55 +02:00
recipeMap = TreeMultimap . create ( WrappedStack . COMPARATOR , Comparators . WRAPPED_STACK_SET_COMPARATOR ) ;
2014-06-14 21:40:45 +02:00
}
2016-05-20 03:10:55 +02:00
public void addRecipe ( Object recipeOutput , Collection < ? > recipeInputList ) {
2015-11-19 21:24:40 +01:00
2015-04-19 21:01:35 +02:00
// Wrap the recipe output
WrappedStack wrappedRecipeOutput = WrappedStack . wrap ( recipeOutput ) ;
2015-11-19 21:24:40 +01:00
if ( wrappedRecipeOutput = = null ) {
2014-07-22 21:56:39 +02:00
return ;
}
2016-05-20 03:10:55 +02:00
Set < WrappedStack > wrappedRecipeInputList = new TreeSet < > ( ) ;
2015-05-07 21:11:23 +02:00
StringBuilder stringBuilder = new StringBuilder ( ) ;
2016-05-19 21:54:30 +02:00
2015-11-19 21:24:40 +01:00
for ( Object recipeInputObject : recipeInputList ) {
2016-05-19 21:54:30 +02:00
2015-04-19 21:01:35 +02:00
WrappedStack wrappedInputObject = WrappedStack . wrap ( recipeInputObject ) ;
2016-05-19 21:54:30 +02:00
2015-11-19 21:24:40 +01:00
if ( wrappedInputObject ! = null ) {
2015-04-19 21:01:35 +02:00
wrappedRecipeInputList . add ( wrappedInputObject ) ;
2015-05-07 21:11:23 +02:00
stringBuilder . append ( wrappedInputObject ) ;
stringBuilder . append ( " " ) ;
2016-05-19 21:54:30 +02:00
}
else {
2014-07-22 21:56:39 +02:00
return ;
}
}
2016-05-19 21:54:30 +02:00
// Check to see if we already have this recipe in the map
boolean existsAlready = false ;
2016-05-20 03:10:55 +02:00
for ( Set < WrappedStack > recipeInputs : recipeMap . get ( wrappedRecipeOutput ) ) {
2016-05-19 21:54:30 +02:00
if ( recipeInputs . containsAll ( wrappedRecipeInputList ) & & wrappedRecipeInputList . containsAll ( recipeInputs ) ) {
existsAlready = true ;
}
}
2014-07-23 04:12:04 +02:00
// Add the recipe mapping only if we don't already have it
2016-05-19 21:54:30 +02:00
if ( ! existsAlready ) {
2015-11-19 21:24:40 +01:00
LogHelper . trace ( RECIPE_MARKER , " [{}] Mod with ID '{}' added recipe (Output: {}, Inputs: {}) " , LoaderHelper . getLoaderState ( ) , Loader . instance ( ) . activeModContainer ( ) . getModId ( ) , wrappedRecipeOutput , stringBuilder . toString ( ) . trim ( ) ) ;
2014-07-23 04:12:04 +02:00
recipeMap . put ( wrappedRecipeOutput , wrappedRecipeInputList ) ;
}
2014-07-22 21:56:39 +02:00
}
2015-11-19 21:24:40 +01:00
public void registerVanillaRecipes ( ) {
2016-05-19 21:54:30 +02:00
2016-05-27 22:03:14 +02:00
new RecipesVanilla ( ) . registerRecipes ( ) ;
new RecipesFluidContainers ( ) . registerRecipes ( ) ;
new RecipesPotions ( ) . registerRecipes ( ) ;
2014-06-14 21:40:45 +02:00
}
2016-05-20 03:10:55 +02:00
public Multimap < WrappedStack , Set < WrappedStack > > getRecipeMappings ( ) {
2015-11-19 21:24:40 +01:00
if ( immutableRecipeMap = = null ) {
2016-05-20 19:56:53 +02:00
immutableRecipeMap = ImmutableMultimap . copyOf ( INSTANCE . recipeMap ) ;
2015-02-02 04:17:51 +01:00
}
return immutableRecipeMap ;
2014-06-14 21:40:45 +02:00
}
2015-03-22 21:52:48 +01:00
2015-11-19 21:24:40 +01:00
public void dumpRecipeRegistryToLog ( ) {
for ( WrappedStack wrappedStack : getRecipeMappings ( ) . keySet ( ) ) {
2015-03-22 21:52:48 +01:00
StringBuilder stringBuilder = new StringBuilder ( ) ;
2015-04-14 20:45:16 +02:00
stringBuilder . append ( String . format ( " Output: %s, Inputs: " , wrappedStack . toString ( ) ) ) ;
2016-05-20 03:10:55 +02:00
for ( Set < WrappedStack > listStacks : getRecipeMappings ( ) . get ( wrappedStack ) ) {
2015-11-19 21:24:40 +01:00
for ( WrappedStack listStack : listStacks ) {
2015-03-22 21:52:48 +01:00
stringBuilder . append ( listStack . toString ( ) + " " ) ;
}
}
2015-11-19 21:24:40 +01:00
LogHelper . info ( RECIPE_MARKER , stringBuilder . toString ( ) ) ;
2015-03-22 21:52:48 +01:00
}
}
2014-06-14 21:40:45 +02:00
}