From 31cb594f4deb47fed407173dde02f9d742c7e7b7 Mon Sep 17 00:00:00 2001 From: AlgorithmX2 Date: Fri, 25 Jul 2014 17:49:49 -0500 Subject: [PATCH] Moved Recipe Registration to Init, Added Recipe Baking. --- core/Registration.java | 30 +++++++++++++---- recipes/GroupIngredient.java | 10 ++++++ recipes/Ingredient.java | 11 +++++++ recipes/IngredientSet.java | 13 +++++++- recipes/RecipeHandler.java | 7 +++- recipes/game/IRecipeBakeable.java | 12 +++++++ recipes/game/ShapedRecipe.java | 55 +++++++++++++++++-------------- recipes/game/ShapelessRecipe.java | 54 +++++++++++++++--------------- recipes/handlers/Shaped.java | 2 +- recipes/handlers/Shapeless.java | 4 +-- 10 files changed, 135 insertions(+), 63 deletions(-) create mode 100644 recipes/game/IRecipeBakeable.java diff --git a/core/Registration.java b/core/Registration.java index 5a2fc98d..a97f6b3b 100644 --- a/core/Registration.java +++ b/core/Registration.java @@ -140,6 +140,7 @@ import appeng.parts.PartPlacement; import appeng.recipes.AEItemResolver; import appeng.recipes.RecipeHandler; import appeng.recipes.game.FacadeRecipe; +import appeng.recipes.game.IRecipeBakeable; import appeng.recipes.game.ShapedRecipe; import appeng.recipes.game.ShapelessRecipe; import appeng.recipes.handlers.Crusher; @@ -561,7 +562,10 @@ public class Registration // default settings.. ((P2PTunnelRegistry) AEApi.instance().registries().p2pTunnel()).configure(); - // NetworkRegistry.instance().registerGuiHandler( AppEng.instance, GuiBridge.GUI_Handler ); + recipeHandler.injectRecipes(); + + if ( AEConfig.instance.isFeatureEnabled( AEFeature.enableFacadeCrafting ) ) + CraftingManager.getInstance().getRecipeList().add( new FacadeRecipe() ); } public void PostInit(FMLPostInitializationEvent event) @@ -689,12 +693,26 @@ public class Registration * Whitelist AE2 */ mr.whiteListTileEntity( AEBaseTile.class ); + + bakeRecipes(); + } - recipeHandler.registerHandlers(); - - if ( AEConfig.instance.isFeatureEnabled( AEFeature.enableFacadeCrafting ) ) - CraftingManager.getInstance().getRecipeList().add( new FacadeRecipe() ); - + private void bakeRecipes() + { + for ( Object o : CraftingManager.getInstance().getRecipeList() ) + { + if ( o instanceof IRecipeBakeable ) + { + try + { + ( (IRecipeBakeable) o ).bake(); + } + catch (Throwable e) + { + AELog.error( e ); + } + } + } } private void registerSpatial(boolean force) diff --git a/recipes/GroupIngredient.java b/recipes/GroupIngredient.java index 569688b8..fffe5ae6 100644 --- a/recipes/GroupIngredient.java +++ b/recipes/GroupIngredient.java @@ -17,6 +17,7 @@ public class GroupIngredient implements IIngredient int qty = 0; final String name; final List ingredients; + ItemStack[] baked; boolean isInside = false; @@ -57,6 +58,9 @@ public class GroupIngredient implements IIngredient @Override public ItemStack[] getItemStackSet() throws RegistrationError, MissingIngredientError { + if ( baked != null ) + return baked; + if ( isInside ) return new ItemStack[0]; @@ -107,4 +111,10 @@ public class GroupIngredient implements IIngredient return false; } + @Override + public void bake() throws RegistrationError, MissingIngredientError + { + baked = getItemStackSet(); + } + } diff --git a/recipes/Ingredient.java b/recipes/Ingredient.java index d93feb60..da4bd9e5 100644 --- a/recipes/Ingredient.java +++ b/recipes/Ingredient.java @@ -26,6 +26,8 @@ public class Ingredient implements IIngredient final public int qty; + ItemStack[] baked; + public Ingredient(RecipeHandler handler, String input, int qty) throws RecipeError, MissedIngredientSet { // works no matter wat! @@ -159,6 +161,9 @@ public class Ingredient implements IIngredient @Override public ItemStack[] getItemStackSet() throws RegistrationError, MissingIngredientError { + if ( baked != null ) + return baked; + if ( nameSpace.equalsIgnoreCase( "oreDictionary" ) ) { List ores = OreDictionary.getOres( itemName ); @@ -211,4 +216,10 @@ public class Ingredient implements IIngredient return isAir; } + @Override + public void bake() throws RegistrationError, MissingIngredientError + { + baked = getItemStackSet(); + } + } diff --git a/recipes/IngredientSet.java b/recipes/IngredientSet.java index 7e384812..6a8a151f 100644 --- a/recipes/IngredientSet.java +++ b/recipes/IngredientSet.java @@ -16,7 +16,8 @@ public class IngredientSet implements IIngredient int qty = 0; final String name; final List items; - + ItemStack[] baked; + public IngredientSet(ResolverResultSet rr) { name = rr.name; items = rr.results; @@ -44,6 +45,9 @@ public class IngredientSet implements IIngredient @Override public ItemStack[] getItemStackSet() throws RegistrationError, MissingIngredientError { + if ( baked != null ) + return baked; + if ( isInside ) return new ItemStack[0]; @@ -75,4 +79,11 @@ public class IngredientSet implements IIngredient { return false; } + + @Override + public void bake() throws RegistrationError, MissingIngredientError + { + baked = getItemStackSet(); + } + } diff --git a/recipes/RecipeHandler.java b/recipes/RecipeHandler.java index 8b66ba0f..0fa131ca 100644 --- a/recipes/RecipeHandler.java +++ b/recipes/RecipeHandler.java @@ -12,6 +12,7 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import com.google.common.collect.HashMultimap; +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader; import net.minecraft.item.ItemStack; import appeng.api.AEApi; @@ -32,6 +33,7 @@ import appeng.items.misc.ItemCrystalSeed; import appeng.items.parts.ItemMultiPart; import appeng.recipes.handlers.IWebsiteSeralizer; import appeng.recipes.handlers.OreRegistration; +import cpw.mods.fml.common.LoaderState; import cpw.mods.fml.common.registry.GameRegistry; import cpw.mods.fml.common.registry.GameRegistry.UniqueIdentifier; @@ -77,8 +79,11 @@ public class RecipeHandler implements IRecipeHandler } @Override - public void registerHandlers() + public void injectRecipes() { + if ( cpw.mods.fml.common.Loader.instance().hasReachedState( LoaderState.POSTINITIALIZATION )) + throw new RuntimeException("Recipes must now be loaded in Init."); + HashMap processed = new HashMap(); try { diff --git a/recipes/game/IRecipeBakeable.java b/recipes/game/IRecipeBakeable.java new file mode 100644 index 00000000..c93edc2e --- /dev/null +++ b/recipes/game/IRecipeBakeable.java @@ -0,0 +1,12 @@ +package appeng.recipes.game; + +import appeng.api.exceptions.MissingIngredientError; +import appeng.api.exceptions.RegistrationError; + + +public interface IRecipeBakeable +{ + + void bake() throws RegistrationError, MissingIngredientError; + +} diff --git a/recipes/game/ShapedRecipe.java b/recipes/game/ShapedRecipe.java index 735730a9..73191ab4 100644 --- a/recipes/game/ShapedRecipe.java +++ b/recipes/game/ShapedRecipe.java @@ -3,13 +3,16 @@ package appeng.recipes.game; import java.util.ArrayList; import java.util.HashMap; +import appeng.api.exceptions.MissingIngredientError; +import appeng.api.exceptions.RegistrationError; +import appeng.api.recipes.IIngredient; import net.minecraft.inventory.InventoryCrafting; import net.minecraft.item.ItemStack; import net.minecraft.item.crafting.IRecipe; import net.minecraft.world.World; import net.minecraftforge.oredict.OreDictionary; -public class ShapedRecipe implements IRecipe +public class ShapedRecipe implements IRecipe, IRecipeBakeable { // Added in for future ease of change, but hard coded for now. @@ -82,21 +85,9 @@ public class ShapedRecipe implements IRecipe Character chr = (Character) recipe[idx]; Object in = recipe[idx + 1]; - if ( in instanceof ItemStack ) + if ( in instanceof IIngredient ) { - itemMap.put( chr, ((ItemStack) in).copy() ); - } - else if ( in instanceof ItemStack[] ) - { - ItemStack[] a = (ItemStack[]) in; - if ( a.length == 1 ) - itemMap.put( chr, a[0] ); - else - itemMap.put( chr, a ); - } - else if ( in instanceof String ) - { - itemMap.put( chr, OreDictionary.getOres( (String) in ) ); + itemMap.put( chr, in ); } else { @@ -183,20 +174,24 @@ public class ShapedRecipe implements IRecipe ItemStack slot = inv.getStackInRowAndColumn( x, y ); - if ( target instanceof ItemStack ) - { - if ( !checkItemEquals( (ItemStack) target, slot ) ) - { - return false; - } - } - else if ( target instanceof ItemStack[] ) + if ( target instanceof IIngredient ) { boolean matched = false; - for (ItemStack item : (ItemStack[]) target) + try { - matched = matched || checkItemEquals( item, slot ); + for (ItemStack item : ((IIngredient) target).getItemStackSet() ) + { + matched = matched || checkItemEquals( item, slot ); + } + } + catch (RegistrationError e) + { + // :P + } + catch (MissingIngredientError e) + { + // :P } if ( !matched ) @@ -270,4 +265,14 @@ public class ShapedRecipe implements IRecipe return input; } + @Override + public void bake() throws RegistrationError, MissingIngredientError + { + for ( Object o : getInput() ) + { + if ( o instanceof IIngredient ) + ((IIngredient)o).bake(); + } + } + } \ No newline at end of file diff --git a/recipes/game/ShapelessRecipe.java b/recipes/game/ShapelessRecipe.java index 18e46692..9c5dac1f 100644 --- a/recipes/game/ShapelessRecipe.java +++ b/recipes/game/ShapelessRecipe.java @@ -3,13 +3,16 @@ package appeng.recipes.game; import java.util.ArrayList; import java.util.Iterator; +import appeng.api.exceptions.MissingIngredientError; +import appeng.api.exceptions.RegistrationError; +import appeng.api.recipes.IIngredient; import net.minecraft.inventory.InventoryCrafting; import net.minecraft.item.ItemStack; import net.minecraft.item.crafting.IRecipe; import net.minecraft.world.World; import net.minecraftforge.oredict.OreDictionary; -public class ShapelessRecipe implements IRecipe +public class ShapelessRecipe implements IRecipe, IRecipeBakeable { private ItemStack output = null; @@ -19,21 +22,9 @@ public class ShapelessRecipe implements IRecipe output = result.copy(); for (Object in : recipe) { - if ( in instanceof ItemStack ) + if ( in instanceof IIngredient ) { - input.add( ((ItemStack) in).copy() ); - } - else if ( in instanceof ItemStack[] ) - { - ItemStack[] a = (ItemStack[]) in; - if ( a.length == 1 ) - input.add( a[0] ); - else - input.add( a ); - } - else if ( in instanceof String ) - { - input.add( OreDictionary.getOres( (String) in ) ); + input.add( in ); } else { @@ -87,22 +78,22 @@ public class ShapelessRecipe implements IRecipe Object next = req.next(); - if ( next instanceof ItemStack ) + if ( next instanceof IIngredient ) { - match = checkItemEquals( (ItemStack) next, slot ); - } - else if ( next instanceof ItemStack[] ) - { - for (ItemStack item : (ItemStack[]) next) + try { - match = match || checkItemEquals( item, slot ); + for (ItemStack item : ((IIngredient) next).getItemStackSet() ) + { + match = match || checkItemEquals( item, slot ); + } } - } - else if ( next instanceof ArrayList ) - { - for (ItemStack item : (ArrayList) next) + catch (RegistrationError e) { - match = match || checkItemEquals( item, slot ); + // :P + } + catch (MissingIngredientError e) + { + // :P } } @@ -141,4 +132,13 @@ public class ShapelessRecipe implements IRecipe return this.input; } + @Override + public void bake() throws RegistrationError, MissingIngredientError + { + for ( Object o : getInput() ) + { + if ( o instanceof IIngredient ) + ((IIngredient)o).bake(); + } + } } \ No newline at end of file diff --git a/recipes/handlers/Shaped.java b/recipes/handlers/Shaped.java index e2750d8b..f68bb498 100644 --- a/recipes/handlers/Shaped.java +++ b/recipes/handlers/Shaped.java @@ -69,7 +69,7 @@ public class Shaped implements ICraftHandler, IWebsiteSeralizer { row = row + first; args.add( first ); - args.add( inputs.get( y ).get( x ).getItemStackSet() ); + args.add( inputs.get( y ).get( x ) ); first++; } diff --git a/recipes/handlers/Shapeless.java b/recipes/handlers/Shapeless.java index 6a514444..3af9255e 100644 --- a/recipes/handlers/Shapeless.java +++ b/recipes/handlers/Shapeless.java @@ -43,7 +43,7 @@ public class Shapeless implements ICraftHandler, IWebsiteSeralizer { List args = new ArrayList(); for (IIngredient i : inputs) - args.add( i.getItemStackSet() ); + args.add( i ); ItemStack outIS = output.getItemStack(); @@ -54,7 +54,7 @@ public class Shapeless implements ICraftHandler, IWebsiteSeralizer catch (Throwable e) { AELog.error( e ); - throw new RegistrationError( "Erro while adding shapeless recipe." ); + throw new RegistrationError( "Error while adding shapeless recipe." ); } }