Error control, missing Item Control

Import ( not done ).
And recursive file importing.
This commit is contained in:
AlgorithmX2
2014-02-10 17:15:43 -06:00
parent 4a87ceac4f
commit 21c254f09f
5 changed files with 159 additions and 87 deletions
+2 -2
View File
@@ -98,7 +98,7 @@ public class Ingredient
throw new RecipeError( input + " : Needs at least Namespace and Name." ); throw new RecipeError( input + " : Needs at least Namespace and Name." );
} }
public ItemStack getItemStack() throws RegistrationError public ItemStack getItemStack() throws RegistrationError, MissingIngredientError
{ {
if ( isAir ) if ( isAir )
throw new RegistrationError( "Found blank item and expected a real item." ); throw new RegistrationError( "Found blank item and expected a real item." );
@@ -117,7 +117,7 @@ public class Ingredient
if ( o instanceof Block ) if ( o instanceof Block )
return new ItemStack( (Block) o, 1, meta ); return new ItemStack( (Block) o, 1, meta );
throw new RegistrationError( "Unable to find item: " + toString() ); throw new MissingIngredientError( "Unable to find item: " + toString() );
} }
@Override @Override
+11
View File
@@ -0,0 +1,11 @@
package appeng.recipes;
public class MissingIngredientError extends Exception {
private static final long serialVersionUID = -998858343831371697L;
public MissingIngredientError(String n) {
super( n );
}
}
+139 -80
View File
@@ -1,10 +1,16 @@
package appeng.recipes; package appeng.recipes;
import java.io.DataInputStream;
import java.io.InputStream;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import net.minecraft.client.Minecraft;
import net.minecraft.util.ResourceLocation;
import appeng.core.AELog; import appeng.core.AELog;
import appeng.core.AppEng;
import appeng.recipes.handlers.CraftHandler; import appeng.recipes.handlers.CraftHandler;
import appeng.recipes.handlers.Grind; import appeng.recipes.handlers.Grind;
import appeng.recipes.handlers.Shaped; import appeng.recipes.handlers.Shaped;
@@ -13,8 +19,12 @@ import appeng.recipes.handlers.Smelt;
public class RecipeHandler public class RecipeHandler
{ {
HashMap<String, String> aliases = new HashMap(); HashMap<String, String> aliases = new HashMap<String,String>();
List<CraftHandler> Handlers = new LinkedList(); List<CraftHandler> Handlers = new LinkedList<CraftHandler>();
List<String> tokens = new LinkedList<String>();
boolean crash = true;
boolean erroronmissing = true;
private void addCrafting(CraftHandler ch) private void addCrafting(CraftHandler ch)
{ {
@@ -23,18 +33,37 @@ public class RecipeHandler
public void registerHandlers() public void registerHandlers()
{ {
for (CraftHandler ch : Handlers) try
{ {
try for (CraftHandler ch : Handlers)
{ {
ch.register(); try
} {
catch (RegistrationError e) ch.register();
{ }
AELog.warning( "Unable to regsiter a recipe." ); catch (RegistrationError e)
AELog.error( 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) public String alias(String in)
@@ -47,87 +76,91 @@ public class RecipeHandler
return in; return in;
} }
List<String> tokens = new LinkedList(); public void parseRecipes(String path)
public void parseRecipes(String file)
{ {
file = org.apache.commons.lang3.StringUtils.join( new String[] { "", "alias=", " mc", " -> minecraft", "", "alias=", " ae2", " -> appliedenergistics2", try
"", "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++)
{ {
char c = file.charAt( x ); ResourceLocation r = new ResourceLocation(AppEng.instance.modid, path );
InputStream in = Minecraft.getMinecraft().getResourceManager().getResource(r).getInputStream();
DataInputStream reader = new DataInputStream(in);
if ( c == '\n' ) boolean inQuote = false;
line++;
if ( inQuote ) String token = "";
int line = 0;
while ( in.available() > 0)
{ {
switch (c) char c = reader.readChar();
{
case '"':
inQuote = !inQuote;
break;
default:
token = token + c;
}
}
else
{
switch (c)
{
case '"':
inQuote = !inQuote;
break;
case ',':
if ( token.length() > 0 ) if ( c == '\n' )
line++;
if ( inQuote )
{
switch (c)
{ {
tokens.add( token ); case '"':
tokens.add( "," ); 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 try
{ {
@@ -137,7 +170,7 @@ public class RecipeHandler
int split = tokens.indexOf( "->" ); int split = tokens.indexOf( "->" );
if ( split != -1 ) if ( split != -1 )
{ {
String operation = tokens.remove( 0 ); String operation = tokens.remove( 0 ).toLowerCase();
if ( operation.equals( "alias" ) ) if ( operation.equals( "alias" ) )
{ {
@@ -146,6 +179,31 @@ public class RecipeHandler
else else
throw new RecipeError( "Alias must have exactly 1 input and 1 output." ); 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 else
{ {
List<String> pre = tokens.subList( 0, split - 1 ); List<String> pre = tokens.subList( 0, split - 1 );
@@ -178,8 +236,9 @@ public class RecipeHandler
} }
catch (RecipeError e) 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 ); AELog.error( e );
if ( crash ) throw e;
} }
tokens.clear(); tokens.clear();
+2 -1
View File
@@ -3,6 +3,7 @@ package appeng.recipes.handlers;
import java.util.List; import java.util.List;
import appeng.recipes.Ingredient; import appeng.recipes.Ingredient;
import appeng.recipes.MissingIngredientError;
import appeng.recipes.RecipeError; import appeng.recipes.RecipeError;
import appeng.recipes.RegistrationError; import appeng.recipes.RegistrationError;
@@ -13,7 +14,7 @@ public class CraftHandler
{ {
} }
public void register() throws RegistrationError public void register() throws RegistrationError, MissingIngredientError
{ {
} }
+2 -1
View File
@@ -3,6 +3,7 @@ package appeng.recipes.handlers;
import java.util.List; import java.util.List;
import appeng.recipes.Ingredient; import appeng.recipes.Ingredient;
import appeng.recipes.MissingIngredientError;
import appeng.recipes.RecipeError; import appeng.recipes.RecipeError;
import appeng.recipes.RegistrationError; import appeng.recipes.RegistrationError;
import cpw.mods.fml.common.registry.GameRegistry; import cpw.mods.fml.common.registry.GameRegistry;
@@ -31,7 +32,7 @@ public class Smelt extends CraftHandler
} }
@Override @Override
public void register() throws RegistrationError public void register() throws RegistrationError, MissingIngredientError
{ {
if ( in.getItemStack().getItem() == null ) if ( in.getItemStack().getItem() == null )
throw new RegistrationError( in.toString() + ": Smelting Input is not a valid item." ); throw new RegistrationError( in.toString() + ": Smelting Input is not a valid item." );