diff --git a/recipes/Ingredient.java b/recipes/Ingredient.java index 6f596c23..46004c88 100644 --- a/recipes/Ingredient.java +++ b/recipes/Ingredient.java @@ -98,7 +98,7 @@ public class Ingredient throw new RecipeError( input + " : Needs at least Namespace and Name." ); } - public ItemStack getItemStack() throws RegistrationError + public ItemStack getItemStack() throws RegistrationError, MissingIngredientError { if ( isAir ) throw new RegistrationError( "Found blank item and expected a real item." ); @@ -117,7 +117,7 @@ public class Ingredient if ( o instanceof Block ) return new ItemStack( (Block) o, 1, meta ); - throw new RegistrationError( "Unable to find item: " + toString() ); + throw new MissingIngredientError( "Unable to find item: " + toString() ); } @Override diff --git a/recipes/MissingIngredientError.java b/recipes/MissingIngredientError.java new file mode 100644 index 00000000..868eb72b --- /dev/null +++ b/recipes/MissingIngredientError.java @@ -0,0 +1,11 @@ +package appeng.recipes; + +public class MissingIngredientError extends Exception { + + private static final long serialVersionUID = -998858343831371697L; + + public MissingIngredientError(String n) { + super( n ); + } + +} diff --git a/recipes/RecipeHandler.java b/recipes/RecipeHandler.java index 899dc405..5a7ceb17 100644 --- a/recipes/RecipeHandler.java +++ b/recipes/RecipeHandler.java @@ -1,10 +1,16 @@ package appeng.recipes; +import java.io.DataInputStream; +import java.io.InputStream; import java.util.HashMap; import java.util.LinkedList; import java.util.List; +import net.minecraft.client.Minecraft; +import net.minecraft.util.ResourceLocation; + import appeng.core.AELog; +import appeng.core.AppEng; import appeng.recipes.handlers.CraftHandler; import appeng.recipes.handlers.Grind; import appeng.recipes.handlers.Shaped; @@ -13,9 +19,13 @@ import appeng.recipes.handlers.Smelt; public class RecipeHandler { - HashMap aliases = new HashMap(); - List Handlers = new LinkedList(); + HashMap aliases = new HashMap(); + List Handlers = new LinkedList(); + List tokens = new LinkedList(); + boolean crash = true; + boolean erroronmissing = true; + private void addCrafting(CraftHandler ch) { Handlers.add( ch ); @@ -23,18 +33,37 @@ public class RecipeHandler public void registerHandlers() { - for (CraftHandler ch : Handlers) + try { - try + for (CraftHandler ch : Handlers) { - ch.register(); - } - catch (RegistrationError e) - { - AELog.warning( "Unable to regsiter a recipe." ); - AELog.error( e ); + try + { + ch.register(); + } + catch (RegistrationError e) + { + AELog.warning( "Unable to regsiter a recipe." ); + AELog.error( e ); + if ( crash ) throw e; + } + catch (MissingIngredientError e) + { + if ( erroronmissing ) + { + AELog.warning( "Unable to regsiter a recipe." ); + AELog.error( e ); + if ( crash ) throw e; + } + } } } + catch( Throwable e ) + { + AELog.error( e ); + if ( crash ) + throw new RuntimeException(e); + } } public String alias(String in) @@ -46,88 +75,92 @@ public class RecipeHandler return in; } - - List tokens = new LinkedList(); - - public void parseRecipes(String file) + + public void parseRecipes(String path) { - file = org.apache.commons.lang3.StringUtils.join( new String[] { "", "alias=", " mc", " -> minecraft", "", "alias=", " ae2", " -> appliedenergistics2", - "", "smelt=", " ae2:ItemMaterial.IronDust", " -> mc:iron_ingot", "", "smelt=", " ae2:ItemMaterial.GoldDust", " -> mc:gold_ingot", "", "craft=", - " mc:stick mc:stick mc:stick,", " _ _ mc:stick,", " _ _ mc:stick", " -> ae2:BlockGrinder", "" }, "\n" ); - - int len = file.length(); - - boolean inQuote = false; - - String token = ""; - int line = 0; - - for (int x = 0; x < len; x++) + try { - char c = file.charAt( x ); - - if ( c == '\n' ) - line++; - - if ( inQuote ) + ResourceLocation r = new ResourceLocation(AppEng.instance.modid, path ); + InputStream in = Minecraft.getMinecraft().getResourceManager().getResource(r).getInputStream(); + DataInputStream reader = new DataInputStream(in); + + boolean inQuote = false; + + String token = ""; + int line = 0; + + while ( in.available() > 0) { - switch (c) + char c = reader.readChar(); + + if ( c == '\n' ) + line++; + + if ( inQuote ) { - case '"': - inQuote = !inQuote; - break; - default: - token = token + c; - } - } - else - { - switch (c) - { - case '"': - inQuote = !inQuote; - break; - case ',': - - if ( token.length() > 0 ) + switch (c) { - tokens.add( token ); - tokens.add( "," ); + case '"': + inQuote = !inQuote; + break; + default: + token = token + c; } - token = ""; - break; - - case '=': - - processTokens( line ); - - if ( token.length() > 0 ) - tokens.add( token ); - token = ""; - - break; - - case '\n': - case '\t': - case '\r': - case ' ': - - if ( token.length() > 0 ) - tokens.add( token ); - token = ""; - - break; - default: - token = token + c; } + else + { + switch (c) + { + case '"': + inQuote = !inQuote; + break; + case ',': + + if ( token.length() > 0 ) + { + tokens.add( token ); + tokens.add( "," ); + } + token = ""; + break; + + case '=': + + processTokens( path,line ); + + if ( token.length() > 0 ) + tokens.add( token ); + token = ""; + + break; + + case '\n': + case '\t': + case '\r': + case ' ': + + if ( token.length() > 0 ) + tokens.add( token ); + token = ""; + + break; + default: + token = token + c; + } + } + } - + processTokens( path,line ); + } + catch( Throwable e ) + { + AELog.error( e ); + if ( crash ) + throw new RuntimeException(e); } - processTokens( line ); - } - private void processTokens(int line) + private void processTokens(String file, int line) throws RecipeError { try { @@ -137,7 +170,7 @@ public class RecipeHandler int split = tokens.indexOf( "->" ); if ( split != -1 ) { - String operation = tokens.remove( 0 ); + String operation = tokens.remove( 0 ).toLowerCase(); if ( operation.equals( "alias" ) ) { @@ -146,6 +179,31 @@ public class RecipeHandler else throw new RecipeError( "Alias must have exactly 1 input and 1 output." ); } + else if ( operation.equals( "crash" )&& ( tokens.get( 0 ).equals("true") || tokens.get( 0 ).equals("false") )) + { + if ( tokens.size() == 1 ) + { + crash = tokens.get(0).equals("true"); + } + else + throw new RecipeError( "crash must be true or false explicitly." ); + } + else if ( operation.equals( "erroronmissing" ) ) + { + if ( tokens.size() == 1 && ( tokens.get( 0 ).equals("true") || tokens.get( 0 ).equals("false") )) + { + erroronmissing = tokens.get(0).equals("true"); + } + else + throw new RecipeError( "erroronmissing must be true or false explicitly." ); + } + else if ( operation.equals( "import" ) ) + { + if ( tokens.size() == 1 ) + parseRecipes( tokens.get(0) ); + else + throw new RecipeError( "Import must have exactly 1 input." ); + } else { List pre = tokens.subList( 0, split - 1 ); @@ -178,8 +236,9 @@ public class RecipeHandler } catch (RecipeError e) { - AELog.warning( "Recipe Error near line:" + line + " with: " + tokens.toString() ); + AELog.warning( "Recipe Error near line:" + line + " in "+file+" with: " + tokens.toString() ); AELog.error( e ); + if ( crash ) throw e; } tokens.clear(); diff --git a/recipes/handlers/CraftHandler.java b/recipes/handlers/CraftHandler.java index cc1733fd..e9169d37 100644 --- a/recipes/handlers/CraftHandler.java +++ b/recipes/handlers/CraftHandler.java @@ -3,6 +3,7 @@ package appeng.recipes.handlers; import java.util.List; import appeng.recipes.Ingredient; +import appeng.recipes.MissingIngredientError; import appeng.recipes.RecipeError; import appeng.recipes.RegistrationError; @@ -13,7 +14,7 @@ public class CraftHandler { } - public void register() throws RegistrationError + public void register() throws RegistrationError, MissingIngredientError { } diff --git a/recipes/handlers/Smelt.java b/recipes/handlers/Smelt.java index 12f7c586..d62e1c72 100644 --- a/recipes/handlers/Smelt.java +++ b/recipes/handlers/Smelt.java @@ -3,6 +3,7 @@ package appeng.recipes.handlers; import java.util.List; import appeng.recipes.Ingredient; +import appeng.recipes.MissingIngredientError; import appeng.recipes.RecipeError; import appeng.recipes.RegistrationError; import cpw.mods.fml.common.registry.GameRegistry; @@ -31,7 +32,7 @@ public class Smelt extends CraftHandler } @Override - public void register() throws RegistrationError + public void register() throws RegistrationError, MissingIngredientError { if ( in.getItemStack().getItem() == null ) throw new RegistrationError( in.toString() + ": Smelting Input is not a valid item." );