2015-03-17 13:43:41 +01:00
package mekanism.common ;
import java.util.List ;
import mekanism.common.recipe.RecipeHandler ;
import mekanism.common.recipe.RecipeHandler.Recipe ;
2016-01-13 00:41:15 +01:00
import mekanism.common.recipe.ShapedMekanismRecipe ;
import mekanism.common.recipe.ShapelessMekanismRecipe ;
2015-03-17 13:43:41 +01:00
import mekanism.common.recipe.inputs.MachineInput ;
import mekanism.common.recipe.machines.MachineRecipe ;
2016-01-13 00:41:15 +01:00
import mekanism.common.util.RecipeUtils ;
import net.minecraft.item.ItemStack ;
2015-03-17 13:43:41 +01:00
import cpw.mods.fml.common.Mod.EventHandler ;
import cpw.mods.fml.common.event.FMLInterModComms.IMCMessage ;
2016-01-13 00:41:15 +01:00
import cpw.mods.fml.common.registry.GameRegistry ;
2015-03-17 13:43:41 +01:00
public class IMCHandler
{
@EventHandler
public void onIMCEvent ( List < IMCMessage > messages )
{
for ( IMCMessage msg : messages )
{
if ( msg . isNBTMessage ( ) )
{
try {
boolean found = false ;
2015-07-15 00:59:55 +02:00
boolean delete = false ;
2016-01-12 03:13:36 +01:00
String message = msg . key ;
2016-01-13 00:41:15 +01:00
if ( message . equals ( " ShapedMekanismRecipe " ) )
{
ShapedMekanismRecipe recipe = ShapedMekanismRecipe . create ( msg . getNBTValue ( ) ) ;
if ( recipe ! = null )
{
GameRegistry . addRecipe ( recipe ) ;
Mekanism . logger . info ( " [Mekanism] " + msg . getSender ( ) + " added a shaped recipe to the recipe list. " ) ;
}
else {
Mekanism . logger . error ( " [Mekanism] " + msg . getSender ( ) + " attempted to add an invalid shaped recipe. " ) ;
}
found = true ;
}
else if ( message . equals ( " ShapelessMekanismRecipe " ) )
{
ShapelessMekanismRecipe recipe = ShapelessMekanismRecipe . create ( msg . getNBTValue ( ) ) ;
if ( recipe ! = null )
{
GameRegistry . addRecipe ( recipe ) ;
Mekanism . logger . info ( " [Mekanism] " + msg . getSender ( ) + " added a shapeless recipe to the recipe list. " ) ;
}
else {
Mekanism . logger . error ( " [Mekanism] " + msg . getSender ( ) + " attempted to add an invalid shapeless recipe. " ) ;
}
found = true ;
}
else if ( message . equals ( " DeleteMekanismRecipes " ) | | message . equals ( " RemoveMekanismRecipes " ) )
{
ItemStack stack = RecipeUtils . loadRecipeItemStack ( msg . getNBTValue ( ) ) ;
if ( stack ! = null )
{
RecipeUtils . removeRecipes ( stack ) ;
Mekanism . logger . info ( " [Mekanism] " + msg . getSender ( ) + " removed a Mekanism recipe from the recipe list. " ) ;
}
else {
Mekanism . logger . error ( " [Mekanism] " + msg . getSender ( ) + " attempted to remove a Mekanism recipe with an invalid output. " ) ;
}
found = true ;
}
2015-07-15 00:59:55 +02:00
2016-01-12 03:13:36 +01:00
if ( message . startsWith ( " Delete " ) | | message . startsWith ( " Remove " ) )
2015-07-15 00:59:55 +02:00
{
2016-01-12 03:13:36 +01:00
message = message . replace ( " Delete " , " " ) . replace ( " Remove " , " " ) ;
2015-07-15 00:59:55 +02:00
delete = true ;
}
2015-03-17 13:43:41 +01:00
for ( Recipe type : Recipe . values ( ) )
{
if ( msg . key . equalsIgnoreCase ( type . getRecipeName ( ) + " Recipe " ) )
{
MachineInput input = type . createInput ( msg . getNBTValue ( ) ) ;
if ( input ! = null & & input . isValid ( ) )
{
MachineRecipe recipe = type . createRecipe ( input , msg . getNBTValue ( ) ) ;
if ( recipe ! = null & & recipe . recipeOutput ! = null )
{
2015-07-15 00:59:55 +02:00
if ( delete )
{
RecipeHandler . removeRecipe ( type , recipe ) ;
Mekanism . logger . info ( " [Mekanism] " + msg . getSender ( ) + " removed recipe of type " + type . getRecipeName ( ) + " from the recipe list. " ) ;
}
else {
RecipeHandler . addRecipe ( type , recipe ) ;
Mekanism . logger . info ( " [Mekanism] " + msg . getSender ( ) + " added recipe of type " + type . getRecipeName ( ) + " to the recipe list. " ) ;
}
2015-03-17 13:43:41 +01:00
}
else {
2016-01-13 00:41:15 +01:00
Mekanism . logger . error ( " [Mekanism] " + msg . getSender ( ) + " attempted to " + ( delete ? " remove " : " add " ) + " recipe of type " + type . getRecipeName ( ) + " with an invalid output. " ) ;
2015-03-17 13:43:41 +01:00
}
}
else {
2016-01-13 00:41:15 +01:00
Mekanism . logger . error ( " [Mekanism] " + msg . getSender ( ) + " attempted to " + ( delete ? " remove " : " add " ) + " recipe of type " + type . getRecipeName ( ) + " with an invalid input. " ) ;
2015-03-17 13:43:41 +01:00
}
found = true ;
break ;
}
}
if ( ! found )
{
Mekanism . logger . error ( " [Mekanism] " + msg . getSender ( ) + " sent unknown IMC message with key ' " + msg . key + " .' " ) ;
}
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
}
}
}
}