Recipe Engine Added, not quite done yet tho.

This commit is contained in:
AlgorithmX2 2014-02-09 21:16:36 -06:00
parent df1ba5282a
commit 4a87ceac4f
18 changed files with 530 additions and 441 deletions

View file

@ -108,6 +108,7 @@ import appeng.me.cache.SecurityCache;
import appeng.me.cache.SpatialPylonCache;
import appeng.me.cache.TickManagerCache;
import appeng.me.storage.AEExternalHandler;
import appeng.recipes.RecipeHandler;
import appeng.recipes.ores.OreDictionaryHandler;
import com.google.common.collect.ArrayListMultimap;
@ -125,9 +126,11 @@ public class Registration
final public static Registration instance = new Registration();
public RecipeHandler recipeHandler;
public BiomeGenBase storageBiome;
private Registration() {
recipeHandler = new RecipeHandler();
}
final private Multimap<AEFeature, Class> featuresToEntities = ArrayListMultimap.create();
@ -304,6 +307,7 @@ public class Registration
addFeature( ToolReplicatorCard.class );
addFeature( BlockItemGen.class );
addFeature( BlockChunkloader.class );
}
private AEItemDefinition addFeature(Class c, Object... Args)
@ -398,6 +402,8 @@ public class Registration
public void Init(FMLInitializationEvent event)
{
recipeHandler.parseRecipes( "" );
IPartHelper ph = AEApi.instance().partHelper();
ph.registerNewLayer( "appeng.api.parts.layers.LayerIEnergySink", "ic2.api.energy.tile.IEnergySink" );
ph.registerNewLayer( "appeng.api.parts.layers.LayerISidedInventory", "net.minecraft.inventory.ISidedInventory" );
@ -501,5 +507,6 @@ public class Registration
if ( AEConfig.instance.isFeatureEnabled( AEFeature.CertusQuartzWorldGen ) )
GameRegistry.registerWorldGenerator( new QuartzWorldGen(), 0 );
recipeHandler.registerHandlers();
}
}

View file

@ -36,10 +36,13 @@ public class ItemMaterial extends AEBaseItem implements IStorageComponent, IUpgr
HashMap<Integer, MaterialType> dmgToMaterial = new HashMap();
public static ItemMaterial instance;
public ItemMaterial() {
super( ItemMaterial.class );
setfeature( EnumSet.of( AEFeature.Core ) );
setHasSubtypes( true );
instance = this;
}
class SlightlyBetterSort implements Comparator<String>

View file

@ -41,11 +41,14 @@ public class ItemPart extends AEBaseItem implements IPartItem, IItemGroup
HashMap<Integer, PartTypeIst> dmgToPart = new HashMap();
public static ItemPart instance;
public ItemPart() {
super( ItemPart.class );
setfeature( EnumSet.of( AEFeature.Core ) );
AEApi.instance().partHelper().setItemBusRenderer( this );
setHasSubtypes( true );
instance = this;
}
public ItemStackSrc createPart(PartType mat, Enum varient)
@ -91,6 +94,16 @@ public class ItemPart extends AEBaseItem implements IPartItem, IItemGroup
return null;
}
public int getDamageByType(PartType t)
{
for (Entry<Integer, PartTypeIst> pt : dmgToPart.entrySet())
{
if ( pt.getValue().part == t )
return pt.getKey();
}
return -1;
}
public PartType getTypeByStack(ItemStack is)
{
if ( is == null )

View file

@ -1,13 +0,0 @@
package appeng.recipes;
import net.minecraft.item.ItemStack;
import net.minecraftforge.oredict.ShapedOreRecipe;
public class AEShapedOreRecipe extends ShapedOreRecipe
{
public AEShapedOreRecipe(ItemStack result, Object... recipe) {
super( result, recipe );
}
}

View file

@ -1,286 +0,0 @@
package appeng.recipes;
import java.util.ArrayList;
import java.util.HashMap;
import net.minecraft.block.Block;
import net.minecraft.init.Items;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.world.World;
import net.minecraftforge.oredict.OreDictionary;
import appeng.api.AEApi;
/*
* Basically the ore dictionary crafting recipe, with a slight twist
* of adding quartz to the lists even tho they are not in the ore dictionary.
*
* Yes this is silly, but it seemed simpler then re-writing half the class to make a simple alteration.
*/
public class AEShapedQuartzRecipe implements IRecipe
{
// Added in for future ease of change, but hard coded for now.
private static final int MAX_CRAFT_GRID_WIDTH = 3;
private static final int MAX_CRAFT_GRID_HEIGHT = 3;
private ItemStack output = null;
private Object[] input = null;
private int width = 0;
private int height = 0;
private boolean mirrored = true;
public int getWidth()
{
return width;
}
public int getHeight()
{
return height;
}
public Object[] getIngredients()
{
return input;
}
public AEShapedQuartzRecipe(ItemStack result, boolean dust, Object... recipe) {
output = result.copy();
String shape = "";
int idx = 0;
if ( recipe[idx] instanceof Boolean )
{
mirrored = (Boolean) recipe[idx];
if ( recipe[idx + 1] instanceof Object[] )
{
recipe = (Object[]) recipe[idx + 1];
}
else
{
idx = 1;
}
}
if ( recipe[idx] instanceof String[] )
{
String[] parts = ((String[]) recipe[idx++]);
for (String s : parts)
{
width = s.length();
shape += s;
}
height = parts.length;
}
else
{
while (recipe[idx] instanceof String)
{
String s = (String) recipe[idx++];
shape += s;
width = s.length();
height++;
}
}
if ( width * height != shape.length() )
{
String ret = "Invalid shaped ore recipe: ";
for (Object tmp : recipe)
{
ret += tmp + ", ";
}
ret += output;
throw new RuntimeException( ret );
}
HashMap<Character, Object> itemMap = new HashMap<Character, Object>();
for (; idx < recipe.length; idx += 2)
{
Character chr = (Character) recipe[idx];
Object in = recipe[idx + 1];
if ( in instanceof ItemStack )
{
itemMap.put( chr, ((ItemStack) in).copy() );
}
else if ( in instanceof Item )
{
itemMap.put( chr, new ItemStack( (Item) in ) );
}
else if ( in instanceof Block )
{
itemMap.put( chr, new ItemStack( (Block) in, 1, OreDictionary.WILDCARD_VALUE ) );
}
else if ( in instanceof String )
{
ArrayList<ItemStack> is = OreDictionary.getOres( (String) in );
ArrayList copy = new ArrayList();
copy.addAll( is );
if ( in.equals( "crystalQuartz" ) || in.equals( "dustQuartz" ) )
{
if ( dust )
{
copy.add( AEApi.instance().materials().materialCertusQuartzDust.stack( 1 ) );
copy.add( AEApi.instance().materials().materialNetherQuartzDust.stack( 1 ) );
}
else
{
copy.add( AEApi.instance().materials().materialCertusQuartzCrystal.stack( 1 ) );
copy.add( new ItemStack( Items.quartz ) );
}
}
itemMap.put( chr, copy );
}
else
{
String ret = "Invalid shaped ore recipe: ";
for (Object tmp : recipe)
{
ret += tmp + ", ";
}
ret += output;
throw new RuntimeException( ret );
}
}
input = new Object[width * height];
int x = 0;
for (char chr : shape.toCharArray())
{
input[x++] = itemMap.get( chr );
}
}
@Override
public ItemStack getCraftingResult(InventoryCrafting var1)
{
return output.copy();
}
@Override
public int getRecipeSize()
{
return input.length;
}
@Override
public ItemStack getRecipeOutput()
{
return output;
}
@Override
public boolean matches(InventoryCrafting inv, World world)
{
for (int x = 0; x <= MAX_CRAFT_GRID_WIDTH - width; x++)
{
for (int y = 0; y <= MAX_CRAFT_GRID_HEIGHT - height; ++y)
{
if ( checkMatch( inv, x, y, false ) )
{
return true;
}
if ( mirrored && checkMatch( inv, x, y, true ) )
{
return true;
}
}
}
return false;
}
private boolean checkMatch(InventoryCrafting inv, int startX, int startY, boolean mirror)
{
for (int x = 0; x < MAX_CRAFT_GRID_WIDTH; x++)
{
for (int y = 0; y < MAX_CRAFT_GRID_HEIGHT; y++)
{
int subX = x - startX;
int subY = y - startY;
Object target = null;
if ( subX >= 0 && subY >= 0 && subX < width && subY < height )
{
if ( mirror )
{
target = input[width - subX - 1 + subY * width];
}
else
{
target = input[subX + subY * width];
}
}
ItemStack slot = inv.getStackInRowAndColumn( x, y );
if ( target instanceof ItemStack )
{
if ( !checkItemEquals( (ItemStack) target, slot ) )
{
return false;
}
}
else if ( target instanceof ArrayList )
{
boolean matched = false;
for (ItemStack item : (ArrayList<ItemStack>) target)
{
matched = matched || checkItemEquals( item, slot );
}
if ( !matched )
{
return false;
}
}
else if ( target == null && slot != null )
{
return false;
}
}
}
return true;
}
private boolean checkItemEquals(ItemStack target, ItemStack input)
{
if ( input == null && target != null || input != null && target == null )
{
return false;
}
return (target.getItem() == input.getItem() && (target.getItemDamage() == OreDictionary.WILDCARD_VALUE || target.getItemDamage() == input
.getItemDamage()));
}
public AEShapedQuartzRecipe setMirrored(boolean mirror)
{
mirrored = mirror;
return this;
}
/**
* Returns the input for this recipe, any mod accessing this value should never manipulate the values in this array
* as it will effect the recipe itself.
*
* @return The recipes input vales.
*/
public Object[] getInput()
{
return this.input;
}
}

View file

@ -1,12 +0,0 @@
package appeng.recipes;
import net.minecraft.item.ItemStack;
import net.minecraftforge.oredict.ShapelessOreRecipe;
public class AEShapelessOreRecipe extends ShapelessOreRecipe
{
public AEShapelessOreRecipe(ItemStack output, Object... t) {
super( output, t );
}
}

129
recipes/Ingredient.java Normal file
View file

@ -0,0 +1,129 @@
package appeng.recipes;
import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraftforge.oredict.OreDictionary;
import appeng.core.AppEng;
import appeng.items.materials.MaterialType;
import appeng.items.parts.ItemPart;
import appeng.items.parts.PartType;
public class Ingredient
{
final public boolean isAir;
final public String nameSpace;
final public String itemName;
final public int meta;
public Ingredient(RecipeHandler handler, String input) throws RecipeError {
if ( input.equals( "_" ) )
{
isAir = true;
nameSpace = "";
itemName = "";
meta = OreDictionary.WILDCARD_VALUE;
return;
}
isAir = false;
String[] parts = input.split( ":" );
if ( parts.length >= 2 )
{
nameSpace = handler.alias( parts[0] );
String tmpName = handler.alias( parts[1] );
if ( parts.length != 3 )
{
int sel = 0;
if ( nameSpace.equals( "oreDictionary" ) )
{
if ( parts.length == 3 )
throw new RecipeError( "Cannot specify meta when using ore dictionary." );
sel = OreDictionary.WILDCARD_VALUE;
}
else if ( nameSpace.equals( AppEng.modid ) )
{
try
{
if ( tmpName.startsWith( "ItemMaterial." ) )
{
String materialName = tmpName.substring( tmpName.indexOf( "." ) + 1 );
MaterialType mt = MaterialType.valueOf( materialName );
tmpName = tmpName.substring( 0, tmpName.indexOf( "." ) );
sel = mt.damageValue;
}
if ( tmpName.startsWith( "ItemPart." ) )
{
String partName = tmpName.substring( tmpName.indexOf( "." ) + 1 );
PartType pt = PartType.valueOf( partName );
tmpName = tmpName.substring( 0, tmpName.indexOf( "." ) );
sel = ItemPart.instance.getDamageByType( pt );
}
}
catch (IllegalArgumentException e)
{
throw new RecipeError( tmpName + " is not a valid ae2 item defintion." );
}
}
meta = sel;
}
else
{
if ( parts[2].equals( "*" ) )
{
meta = OreDictionary.WILDCARD_VALUE;
}
else
{
try
{
meta = Integer.parseInt( parts[2] );
}
catch (NumberFormatException e)
{
throw new RecipeError( "Invalid Metadata." );
}
}
}
itemName = tmpName;
}
else
throw new RecipeError( input + " : Needs at least Namespace and Name." );
}
public ItemStack getItemStack() throws RegistrationError
{
if ( isAir )
throw new RegistrationError( "Found blank item and expected a real item." );
Object o = Item.itemRegistry.getObject( nameSpace + ":" + itemName );
if ( o instanceof Item )
return new ItemStack( (Item) o, 1, meta );
if ( o instanceof Block )
return new ItemStack( (Block) o, 1, meta );
o = Item.itemRegistry.getObject( nameSpace + ":item." + itemName );
if ( o instanceof Item )
return new ItemStack( (Item) o, 1, meta );
o = Item.itemRegistry.getObject( nameSpace + ":tile." + itemName );
if ( o instanceof Block )
return new ItemStack( (Block) o, 1, meta );
throw new RegistrationError( "Unable to find item: " + toString() );
}
@Override
public String toString()
{
return nameSpace + ":" + itemName + ":" + meta;
}
}

View file

@ -1,36 +0,0 @@
package appeng.recipes;
import java.util.Iterator;
public class Recipe
{
final int size;
final RecipeType type;
final RecipeItem items[];
public Recipe(RecipeType rt, Iterator<RecipeItem> ilist) throws RecipeError {
type = rt;
items = new RecipeItem[rt.getSize()];
int itemCount = 0;
while (itemCount < rt.getSize() && ilist.hasNext())
{
items[itemCount++] = ilist.next();
}
if ( ilist.hasNext() )
throw new RecipeError( "Invalid Number of Items in recipe." );
if ( rt.isShaped() )
{
if ( itemCount != rt.getSize() )
throw new RecipeError( "Invalid Number of Items in recipe." );
size = rt.getSize();
}
else
size = itemCount;
}
}

212
recipes/RecipeHandler.java Normal file
View file

@ -0,0 +1,212 @@
package appeng.recipes;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import appeng.core.AELog;
import appeng.recipes.handlers.CraftHandler;
import appeng.recipes.handlers.Grind;
import appeng.recipes.handlers.Shaped;
import appeng.recipes.handlers.Smelt;
public class RecipeHandler
{
HashMap<String, String> aliases = new HashMap();
List<CraftHandler> Handlers = new LinkedList();
private void addCrafting(CraftHandler ch)
{
Handlers.add( ch );
}
public void registerHandlers()
{
for (CraftHandler ch : Handlers)
{
try
{
ch.register();
}
catch (RegistrationError e)
{
AELog.warning( "Unable to regsiter a recipe." );
AELog.error( e );
}
}
}
public String alias(String in)
{
String out = aliases.get( in );
if ( out != null )
return out;
return in;
}
List<String> tokens = new LinkedList();
public void parseRecipes(String file)
{
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++)
{
char c = file.charAt( x );
if ( c == '\n' )
line++;
if ( inQuote )
{
switch (c)
{
case '"':
inQuote = !inQuote;
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( 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( line );
}
private void processTokens(int line)
{
try
{
if ( tokens.isEmpty() )
return;
int split = tokens.indexOf( "->" );
if ( split != -1 )
{
String operation = tokens.remove( 0 );
if ( operation.equals( "alias" ) )
{
if ( tokens.size() == 3 && tokens.indexOf( "->" ) == 1 )
aliases.put( tokens.get( 0 ), tokens.get( 2 ) );
else
throw new RecipeError( "Alias must have exactly 1 input and 1 output." );
}
else
{
List<String> pre = tokens.subList( 0, split - 1 );
List<String> post = tokens.subList( split, tokens.size() );
List<List<Ingredient>> inputs = parseLines( pre );
List<List<Ingredient>> outputs = parseLines( post );
CraftHandler ch = null;
if ( operation.equals( "shaped" ) )
ch = new Shaped();
else if ( operation.equals( "smelt" ) )
ch = new Smelt();
else if ( operation.equals( "grind" ) )
ch = new Grind();
if ( ch != null )
{
ch.setup( inputs, outputs );
addCrafting( ch );
}
else
throw new RecipeError( "Invalid crafting type: " + operation );
}
}
else
throw new RecipeError( tokens.toString() + "; recipe without an output." );
}
catch (RecipeError e)
{
AELog.warning( "Recipe Error near line:" + line + " with: " + tokens.toString() );
AELog.error( e );
}
tokens.clear();
}
private List<List<Ingredient>> parseLines(List<String> subList) throws RecipeError
{
List<List<Ingredient>> out = new LinkedList<List<Ingredient>>();
List<Ingredient> cList = new LinkedList<Ingredient>();
for (String v : subList)
{
if ( v.equals( "," ) )
{
if ( !cList.isEmpty() )
out.add( cList );
cList = new LinkedList<Ingredient>();
}
else
{
cList.add( new Ingredient( this, v ) );
}
}
if ( !cList.isEmpty() )
out.add( cList );
return out;
}
}

View file

@ -1,36 +0,0 @@
package appeng.recipes;
import java.util.List;
import net.minecraft.item.ItemStack;
import net.minecraftforge.oredict.OreDictionary;
public class RecipeItem
{
final private ItemStack is[];
final private int oreID;
RecipeItem(ItemStack in) {
oreID = -1;
is = new ItemStack[1];
is[0] = in;
}
RecipeItem(String oreName) {
oreID = OreDictionary.getOreID( oreName );
List<ItemStack> ores = OreDictionary.getOres( oreID );
is = ores.toArray( new ItemStack[ores.size()] );
}
int getOreID()
{
return oreID;
}
ItemStack[] getValidItems()
{
return is;
}
}

View file

@ -1,7 +0,0 @@
package appeng.recipes;
public class RecipeParser
{
}

View file

@ -1,51 +0,0 @@
package appeng.recipes;
public enum RecipeType
{
// Shapeless
Shape0x0,
// Shaped 1x1
Shape1x1(1, 1),
// Shaped 2x2
Shape1x2(1, 2), Shape2x1(2, 1), Shape2x2(2, 2),
// Shaped 3x3
Shape2x3(2, 3), Shape3x2(3, 2), Shape3x3(3, 3);
boolean shapeless;
final int width;
final int height;
final int size;
private RecipeType(int w, int h) {
width = w;
height = h;
size = w * h;
shapeless = false;
}
private RecipeType() {
this( 3, 3 );
shapeless = true;
}
public boolean isShaped()
{
return shapeless;
}
public int getWidth()
{
return width;
}
public int getHeight()
{
return height;
}
public int getSize()
{
return size;
}
}

View file

@ -0,0 +1,12 @@
package appeng.recipes;
public class RegistrationError extends Exception
{
private static final long serialVersionUID = -6602870588617670263L;
public RegistrationError(String n) {
super( n );
}
}

View file

@ -0,0 +1,20 @@
package appeng.recipes.handlers;
import java.util.List;
import appeng.recipes.Ingredient;
import appeng.recipes.RecipeError;
import appeng.recipes.RegistrationError;
public class CraftHandler
{
public void setup(List<List<Ingredient>> input, List<List<Ingredient>> output) throws RecipeError
{
}
public void register() throws RegistrationError
{
}
}

View file

@ -0,0 +1,6 @@
package appeng.recipes.handlers;
public class Grind extends CraftHandler
{
}

View file

@ -0,0 +1,46 @@
package appeng.recipes.handlers;
import java.util.List;
import appeng.recipes.Ingredient;
import appeng.recipes.RecipeError;
import appeng.recipes.RegistrationError;
public class Shaped extends CraftHandler
{
private int rows;
private int cols;
List<List<Ingredient>> inputs;
Ingredient output;
@Override
public void setup(List<List<Ingredient>> input, List<List<Ingredient>> output) throws RecipeError
{
if ( output.size() == 1 && output.get( 0 ).size() == 1 )
{
rows = inputs.size();
if ( rows > 0 && inputs.size() <= 3 )
{
cols = inputs.get( 0 ).size();
for (int x = 0; x < inputs.size(); x++)
if ( inputs.get( x ).size() != cols )
throw new RecipeError( "all rows in a shaped crafting recipe must contain the same number of ingredients." );
inputs = input;
this.output = output.get( 0 ).get( 0 );
}
else
throw new RecipeError( "shaped crafting recpies must have 1-3 rows." );
}
else
throw new RecipeError( "Crafting must produce a single output." );
}
@Override
public void register() throws RegistrationError
{
}
}

View file

@ -0,0 +1,37 @@
package appeng.recipes.handlers;
import java.util.List;
import appeng.recipes.Ingredient;
import appeng.recipes.RecipeError;
import appeng.recipes.RegistrationError;
public class Shapeless extends CraftHandler
{
List<Ingredient> inputs;
Ingredient output;
@Override
public void setup(List<List<Ingredient>> input, List<List<Ingredient>> output) throws RecipeError
{
if ( output.size() == 1 && output.get( 0 ).size() == 1 )
{
if ( inputs.size() == 1 )
{
inputs = input.get( 0 );
this.output = output.get( 0 ).get( 0 );
}
else
throw new RecipeError( "Shapeless crafting recipes cannot have rows." );
}
else
throw new RecipeError( "Crafting must produce a single output." );
}
@Override
public void register() throws RegistrationError
{
}
}

View file

@ -0,0 +1,45 @@
package appeng.recipes.handlers;
import java.util.List;
import appeng.recipes.Ingredient;
import appeng.recipes.RecipeError;
import appeng.recipes.RegistrationError;
import cpw.mods.fml.common.registry.GameRegistry;
public class Smelt extends CraftHandler
{
Ingredient in;
Ingredient out;
@Override
public void setup(List<List<Ingredient>> input, List<List<Ingredient>> output) throws RecipeError
{
if ( input.size() == 1 && output.size() == 1 )
{
List<Ingredient> inputList = input.get( 0 );
List<Ingredient> outputList = output.get( 0 );
if ( inputList.size() == 1 && outputList.size() == 1 )
{
in = inputList.get( 0 );
out = outputList.get( 0 );
return;
}
}
throw new RecipeError( "Smelting recipe can only have a single input and output." );
}
@Override
public void register() throws RegistrationError
{
if ( in.getItemStack().getItem() == null )
throw new RegistrationError( in.toString() + ": Smelting Input is not a valid item." );
if ( out.getItemStack().getItem() == null )
throw new RegistrationError( out.toString() + ": Smelting Output is not a valid item." );
GameRegistry.addSmelting( in.getItemStack(), out.getItemStack(), 0 );
}
}