Added Basic Quartz and Tool Recipes.

Added Custom Recipes.
This commit is contained in:
AlgorithmX2
2014-02-10 23:51:25 -06:00
parent 077572acc9
commit 08e6a656b8
14 changed files with 879 additions and 95 deletions
+190 -73
View File
@@ -1,102 +1,139 @@
package appeng.recipes;
import java.io.DataInputStream;
import java.io.InputStream;
import java.io.BufferedReader;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map.Entry;
import net.minecraft.client.Minecraft;
import net.minecraft.util.ResourceLocation;
import appeng.api.recipes.IRecipeHandler;
import appeng.api.recipes.IRecipeLoader;
import appeng.core.AELog;
import appeng.core.AppEng;
import appeng.recipes.handlers.CraftHandler;
import appeng.recipes.handlers.Grind;
import appeng.recipes.handlers.OreRegistration;
import appeng.recipes.handlers.Pureify;
import appeng.recipes.handlers.Shaped;
import appeng.recipes.handlers.Shapeless;
import appeng.recipes.handlers.Smelt;
public class RecipeHandler
public class RecipeHandler implements IRecipeHandler
{
HashMap<String, String> aliases = new HashMap<String,String>();
List<CraftHandler> Handlers = new LinkedList<CraftHandler>();
List<String> tokens = new LinkedList<String>();
final public List<String> tokens = new LinkedList<String>();
final RecipeData data;
boolean crash = true;
boolean erroronmissing = true;
private void addCrafting(CraftHandler ch)
{
Handlers.add( ch );
public RecipeHandler() {
data = new RecipeData();
}
RecipeHandler(RecipeHandler parent) {
data = parent.data;
}
private void addCrafting(CraftHandler ch)
{
data.Handlers.add( ch );
}
@Override
public void registerHandlers()
{
HashMap<Class, Integer> processed = new HashMap<Class, Integer>();
try
{
for (CraftHandler ch : Handlers)
for (CraftHandler ch : data.Handlers)
{
try
{
ch.register();
Class clz = ch.getClass();
Integer i = processed.get( clz );
if ( i == null )
processed.put( clz, 1 );
else
processed.put( clz, i + 1 );
}
catch (RegistrationError e)
{
AELog.warning( "Unable to regsiter a recipe." );
AELog.error( e );
if ( crash ) throw e;
if ( data.crash )
throw e;
}
catch (MissingIngredientError e)
{
if ( erroronmissing )
if ( data.erroronmissing )
{
AELog.warning( "Unable to regsiter a recipe." );
AELog.error( e );
if ( crash ) throw e;
AELog.error( e );
if ( data.crash )
throw e;
}
}
}
}
catch( Throwable e )
catch (Throwable e)
{
AELog.error( e );
if ( crash )
throw new RuntimeException(e);
if ( data.crash )
throw new RuntimeException( e );
}
for (Entry<Class, Integer> e : processed.entrySet())
{
AELog.info( "Recipes Loading: " + e.getKey().getSimpleName() + ": " + e.getValue() + " loaded." );
}
}
public String alias(String in)
{
String out = aliases.get( in );
String out = data.aliases.get( in );
if ( out != null )
return out;
return in;
}
public void parseRecipes(String path)
@Override
public void parseRecipes(IRecipeLoader loader, String path)
{
try
{
ResourceLocation r = new ResourceLocation(AppEng.instance.modid, path );
InputStream in = Minecraft.getMinecraft().getResourceManager().getResource(r).getInputStream();
DataInputStream reader = new DataInputStream(in);
BufferedReader reader = null;
try
{
reader = loader.getFile( path );
}
catch (Exception err)
{
AELog.warning( "Error Loading Recipe File:" + path );
AELog.error( err );
return;
}
boolean inQuote = false;
boolean inComment = false;
String token = "";
int line = 0;
while ( in.available() > 0)
int val = -1;
while ((val = reader.read()) != -1)
{
char c = reader.readChar();
char c = (char) val;
if ( c == '\n' )
line++;
if ( inQuote )
if ( inComment )
{
if ( c == '\n' || c == '\r' )
inComment = false;
}
else if ( inQuote )
{
switch (c)
{
@@ -115,7 +152,7 @@ public class RecipeHandler
inQuote = !inQuote;
break;
case ',':
if ( token.length() > 0 )
{
tokens.add( token );
@@ -123,44 +160,49 @@ public class RecipeHandler
}
token = "";
break;
case '=':
processTokens( path,line );
processTokens( loader, path, line );
if ( token.length() > 0 )
tokens.add( token );
token = "";
break;
case '#':
inComment = true;
// then add a token if you can...
case '\n':
case '\t':
case '\r':
case ' ':
if ( token.length() > 0 )
tokens.add( token );
token = "";
break;
default:
token = token + c;
}
}
}
processTokens( path,line );
reader.close();
processTokens( loader, path, line );
}
catch( Throwable e )
catch (Throwable e)
{
AELog.error( e );
if ( crash )
throw new RuntimeException(e);
if ( data.crash )
throw new RuntimeException( e );
}
}
private void processTokens(String file, int line) throws RecipeError
private void processTokens(IRecipeLoader loader, String file, int line) throws RecipeError
{
try
{
@@ -175,34 +217,38 @@ public class RecipeHandler
if ( operation.equals( "alias" ) )
{
if ( tokens.size() == 3 && tokens.indexOf( "->" ) == 1 )
aliases.put( tokens.get( 0 ), tokens.get( 2 ) );
data.aliases.put( tokens.get( 0 ), tokens.get( 2 ) );
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") ))
else if ( operation.equals( "group" ) )
{
if ( tokens.size() == 1 )
List<String> pre = tokens.subList( 0, split - 1 );
List<String> post = tokens.subList( split, tokens.size() );
List<List<Ingredient>> inputs = parseLines( pre );
if ( inputs.size() >= 1 && post.size() == 1 )
{
crash = tokens.get(0).equals("true");
}
else
throw new RecipeError( "crash must be true or false explicitly." );
throw new RecipeError( "Group must have exactly 1 output, and 1 or more inputs." );
}
else if ( operation.equals( "erroronmissing" ) )
else if ( operation.equals( "ore" ) )
{
if ( tokens.size() == 1 && ( tokens.get( 0 ).equals("true") || tokens.get( 0 ).equals("false") ))
List<String> pre = tokens.subList( 0, split - 1 );
List<String> post = tokens.subList( split, tokens.size() );
List<List<Ingredient>> inputs = parseLines( pre );
if ( inputs.size() == 1 && inputs.get( 0 ).size() > 0 && post.size() == 1 )
{
erroronmissing = tokens.get(0).equals("true");
CraftHandler ch = new OreRegistration( inputs.get( 0 ), post.get( 0 ) );
addCrafting( ch );
}
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." );
throw new RecipeError( "Group must have exactly 1 output, and 1 or more inputs in a single row." );
}
else
{
@@ -216,8 +262,12 @@ public class RecipeHandler
if ( operation.equals( "shaped" ) )
ch = new Shaped();
else if ( operation.equals( "shapeless" ) )
ch = new Shapeless();
else if ( operation.equals( "smelt" ) )
ch = new Smelt();
else if ( operation.equals( "pureify" ) )
ch = new Pureify();
else if ( operation.equals( "grind" ) )
ch = new Grind();
@@ -231,14 +281,45 @@ public class RecipeHandler
}
}
else
throw new RecipeError( tokens.toString() + "; recipe without an output." );
{
String operation = tokens.remove( 0 ).toLowerCase();
if ( operation.equals( "crash" ) && (tokens.get( 0 ).equals( "true" ) || tokens.get( 0 ).equals( "false" )) )
{
if ( tokens.size() == 1 )
{
data.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" )) )
{
data.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 )
(new RecipeHandler( this )).parseRecipes( loader, tokens.get( 0 ) );
else
throw new RecipeError( "Import must have exactly 1 input." );
}
else
throw new RecipeError( operation + ": " + tokens.toString() + "; recipe without an output." );
}
}
catch (RecipeError e)
{
AELog.warning( "Recipe Error near line:" + line + " in "+file+" with: " + tokens.toString() );
AELog.warning( "Recipe Error near line:" + line + " in " + file + " with: " + tokens.toString() );
AELog.error( e );
if ( crash ) throw e;
if ( data.crash )
throw e;
}
tokens.clear();
@@ -249,17 +330,38 @@ public class RecipeHandler
List<List<Ingredient>> out = new LinkedList<List<Ingredient>>();
List<Ingredient> cList = new LinkedList<Ingredient>();
boolean hasQty = false;
int qty = 1;
for (String v : subList)
{
if ( v.equals( "," ) )
{
if ( hasQty )
throw new RecipeError( "Qty found with no item." );
if ( !cList.isEmpty() )
out.add( cList );
cList = new LinkedList<Ingredient>();
}
else
{
cList.add( new Ingredient( this, v ) );
if ( isNumber( v ) )
{
if ( hasQty )
throw new RecipeError( "Qty found with no item." );
hasQty = true;
qty = Integer.parseInt( v );
}
else
{
if ( hasQty )
{
cList.add( new Ingredient( this, v, qty ) );
hasQty = false;
}
else
cList.add( new Ingredient( this, v, 1 ) );
}
}
}
@@ -268,4 +370,19 @@ public class RecipeHandler
return out;
}
private boolean isNumber(String v)
{
if ( v.length() <= 0 )
return false;
int l = v.length();
for (int x = 0; x < l; x++)
{
if ( !Character.isDigit( v.charAt( x ) ) )
return false;
}
return true;
}
}