From e8ec768d4c566061a69d3fbeed35ac608817dcfe Mon Sep 17 00:00:00 2001 From: AlgorithmX2 Date: Wed, 5 Mar 2014 13:34:56 -0600 Subject: [PATCH] Website Recipe Exporter. --- core/features/AEFeature.java | 2 +- recipes/RecipeData.java | 4 + recipes/RecipeHandler.java | 116 ++++++++++++++++++ recipes/{Recipes => game}/ShapedRecipe.java | 2 +- .../{Recipes => game}/ShapelessRecipe.java | 2 +- recipes/handlers/Grind.java | 16 ++- recipes/handlers/IWebsiteSeralizer.java | 9 ++ recipes/handlers/Macerator.java | 16 ++- recipes/handlers/OreRegistration.java | 6 + recipes/handlers/Pulverizer.java | 17 ++- recipes/handlers/Shaped.java | 31 ++++- recipes/handlers/Shapeless.java | 30 ++++- recipes/handlers/Smelt.java | 17 ++- 13 files changed, 257 insertions(+), 11 deletions(-) rename recipes/{Recipes => game}/ShapedRecipe.java (99%) rename recipes/{Recipes => game}/ShapelessRecipe.java (98%) create mode 100644 recipes/handlers/IWebsiteSeralizer.java diff --git a/core/features/AEFeature.java b/core/features/AEFeature.java index eaa93a0ce..a4c0432a6 100644 --- a/core/features/AEFeature.java +++ b/core/features/AEFeature.java @@ -48,7 +48,7 @@ public enum AEFeature DuplicateItems("Misc", false), Profiler("Services", false), VersionChecker("Services"), Debug("Misc", false), Creative("Misc"), - Logging("Misc"), IntegrationLogging("Misc", false), CustomRecipes("Crafting", false), + Logging("Misc"), IntegrationLogging("Misc", false), CustomRecipes("Crafting", false), WebsiteRecipes("Misc",false), inWorldSingularity("Crafting"), inWorldFluix("Crafting"), inWorldPurification("Crafting"); diff --git a/recipes/RecipeData.java b/recipes/RecipeData.java index 8792a0010..adb59caf4 100644 --- a/recipes/RecipeData.java +++ b/recipes/RecipeData.java @@ -1,8 +1,10 @@ package appeng.recipes; import java.util.HashMap; +import java.util.HashSet; import java.util.LinkedList; import java.util.List; +import java.util.Set; import appeng.api.recipes.ICraftHandler; @@ -17,5 +19,7 @@ public class RecipeData public boolean crash = true; public boolean exceptions = true; public boolean erroronmissing = true; + + public Set knownItem = new HashSet(); } diff --git a/recipes/RecipeHandler.java b/recipes/RecipeHandler.java index 07ed869ef..a908cd692 100644 --- a/recipes/RecipeHandler.java +++ b/recipes/RecipeHandler.java @@ -1,10 +1,20 @@ package appeng.recipes; import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map.Entry; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; + +import cpw.mods.fml.common.registry.GameRegistry; +import cpw.mods.fml.common.registry.GameRegistry.UniqueIdentifier; + +import net.minecraft.item.ItemStack; import appeng.api.AEApi; import appeng.api.exceptions.MissingIngredientError; @@ -15,7 +25,12 @@ import appeng.api.recipes.ICraftHandler; import appeng.api.recipes.IIngredient; import appeng.api.recipes.IRecipeHandler; import appeng.api.recipes.IRecipeLoader; +import appeng.core.AEConfig; import appeng.core.AELog; +import appeng.core.features.AEFeature; +import appeng.items.materials.ItemMaterial; +import appeng.items.parts.ItemPart; +import appeng.recipes.handlers.IWebsiteSeralizer; import appeng.recipes.handlers.OreRegistration; public class RecipeHandler implements IRecipeHandler @@ -37,6 +52,28 @@ public class RecipeHandler implements IRecipeHandler data.Handlers.add( ch ); } + public List findRecipe( ItemStack output ) + { + List out = new LinkedList(); + + for ( ICraftHandler ch : data.Handlers ) + { + try + { + if ( ch instanceof IWebsiteSeralizer && ch.canCraft(output)) + { + out.add((IWebsiteSeralizer)ch); + } + } + catch (Throwable t ) + { + AELog.error(t); + } + } + + return out; + } + @Override public void registerHandlers() { @@ -89,6 +126,81 @@ public class RecipeHandler implements IRecipeHandler { AELog.info( "Recipes Loading: " + e.getKey().getSimpleName() + ": " + e.getValue() + " loaded." ); } + + + if ( AEConfig.instance.isFeatureEnabled(AEFeature.WebsiteRecipes) ) + { + try { + ZipOutputStream out = new ZipOutputStream(new FileOutputStream("recipes.zip")); + + for ( String s : data.knownItem ) + { + try + { + Ingredient i = new Ingredient( this, s, 1 ); + for ( ItemStack is : i.getItemStackSet() ) + { + List recipes = findRecipe(is); + if ( !recipes.isEmpty() ) + { + int offset =0; + String realName = getName(is); + + for ( IWebsiteSeralizer ws : recipes ) + { + out.putNextEntry(new ZipEntry(realName+"_"+offset+".txt")); + offset++; + out.write(ws.getPattern(this).getBytes()); + } + + } + } + } + catch( Throwable t ) + { + // :P + } + } + + out.close(); + + } catch (FileNotFoundException e1) { + AELog.error(e1); + } catch (IOException e1) { + AELog.error(e1); + } + + } + } + + public String getName( IIngredient i ) + { + try + { + return getName( i.getItemStack() ); + } + catch (Throwable t ) + { + // :P + } + + return i.getNameSpace()+":"+i.getItemName(); + } + + public String getName(ItemStack is) { + + UniqueIdentifier id = GameRegistry.findUniqueIdentifierFor(is.getItem()); + String realName = id.modId+":"+id.name; + + if ( is.getItem() instanceof ItemMaterial ) + realName += ":"+( (ItemMaterial)is.getItem() ).getType(is).name(); + else if ( is.getItem() instanceof ItemPart ) + realName += ":"+( (ItemPart)is.getItem() ).getTypeByStack(is).name(); + else if ( is.getItemDamage() > 0 ) + realName += ":"+is.getItemDamage(); + + return realName; + } public String alias(String in) @@ -380,8 +492,11 @@ public class RecipeHandler implements IRecipeHandler private IIngredient findIngrident(String v, int qty) throws RecipeError { GroupIngredient gi = data.groups.get( v ); + if ( gi != null ) return gi.copy( qty ); + + data.knownItem.add( v ); return new Ingredient( this, v, qty ); } @@ -399,4 +514,5 @@ public class RecipeHandler implements IRecipeHandler return true; } + } diff --git a/recipes/Recipes/ShapedRecipe.java b/recipes/game/ShapedRecipe.java similarity index 99% rename from recipes/Recipes/ShapedRecipe.java rename to recipes/game/ShapedRecipe.java index 7c2c18205..4ff1b8b32 100644 --- a/recipes/Recipes/ShapedRecipe.java +++ b/recipes/game/ShapedRecipe.java @@ -1,4 +1,4 @@ -package appeng.recipes.Recipes; +package appeng.recipes.game; import java.util.ArrayList; import java.util.HashMap; diff --git a/recipes/Recipes/ShapelessRecipe.java b/recipes/game/ShapelessRecipe.java similarity index 98% rename from recipes/Recipes/ShapelessRecipe.java rename to recipes/game/ShapelessRecipe.java index eddca79f7..6b2698767 100644 --- a/recipes/Recipes/ShapelessRecipe.java +++ b/recipes/game/ShapelessRecipe.java @@ -1,4 +1,4 @@ -package appeng.recipes.Recipes; +package appeng.recipes.game; import java.util.ArrayList; import java.util.Iterator; diff --git a/recipes/handlers/Grind.java b/recipes/handlers/Grind.java index 542fb4ede..e1f6a26b8 100644 --- a/recipes/handlers/Grind.java +++ b/recipes/handlers/Grind.java @@ -9,8 +9,10 @@ import appeng.api.exceptions.RecipeError; import appeng.api.exceptions.RegistrationError; import appeng.api.recipes.ICraftHandler; import appeng.api.recipes.IIngredient; +import appeng.recipes.RecipeHandler; +import appeng.util.Platform; -public class Grind implements ICraftHandler +public class Grind implements ICraftHandler, IWebsiteSeralizer { IIngredient pro_input; @@ -39,4 +41,16 @@ public class Grind implements ICraftHandler AEApi.instance().registries().grinder().addRecipe( is, pro_output[0].getItemStack(), 8 ); } + @Override + public boolean canCraft(ItemStack output) throws RegistrationError, MissingIngredientError { + return Platform.isSameItemPrecise( pro_output[0].getItemStack(),output ); + } + + @Override + public String getPattern( RecipeHandler h ) { + return "grind\n"+ + h.getName(pro_input)+"\n"+ + h.getName(pro_output[0]); + } + } diff --git a/recipes/handlers/IWebsiteSeralizer.java b/recipes/handlers/IWebsiteSeralizer.java new file mode 100644 index 000000000..e9355809e --- /dev/null +++ b/recipes/handlers/IWebsiteSeralizer.java @@ -0,0 +1,9 @@ +package appeng.recipes.handlers; + +import appeng.recipes.RecipeHandler; + +public interface IWebsiteSeralizer { + + String getPattern( RecipeHandler han ); + +} diff --git a/recipes/handlers/Macerator.java b/recipes/handlers/Macerator.java index 89a28ddfa..5aaaafc7f 100644 --- a/recipes/handlers/Macerator.java +++ b/recipes/handlers/Macerator.java @@ -10,8 +10,10 @@ import appeng.api.recipes.ICraftHandler; import appeng.api.recipes.IIngredient; import appeng.core.AppEng; import appeng.integration.abstraction.IIC2; +import appeng.recipes.RecipeHandler; +import appeng.util.Platform; -public class Macerator implements ICraftHandler +public class Macerator implements ICraftHandler, IWebsiteSeralizer { IIngredient pro_input; @@ -44,4 +46,16 @@ public class Macerator implements ICraftHandler } } + @Override + public boolean canCraft(ItemStack output) throws RegistrationError, MissingIngredientError { + return Platform.isSameItemPrecise( pro_output[0].getItemStack(),output ); + } + + @Override + public String getPattern( RecipeHandler h ) { + return "macerator\n"+ + h.getName(pro_input)+"\n"+ + h.getName(pro_output[0]); + } + } diff --git a/recipes/handlers/OreRegistration.java b/recipes/handlers/OreRegistration.java index 4f4afbcd0..b0ec7fffc 100644 --- a/recipes/handlers/OreRegistration.java +++ b/recipes/handlers/OreRegistration.java @@ -38,4 +38,10 @@ public class OreRegistration implements ICraftHandler List> output) throws RecipeError { } + + @Override + public boolean canCraft(ItemStack output) throws RegistrationError, MissingIngredientError { + return false; + } + } diff --git a/recipes/handlers/Pulverizer.java b/recipes/handlers/Pulverizer.java index 706165fbf..a6bf127f8 100644 --- a/recipes/handlers/Pulverizer.java +++ b/recipes/handlers/Pulverizer.java @@ -9,9 +9,11 @@ import appeng.api.exceptions.RecipeError; import appeng.api.exceptions.RegistrationError; import appeng.api.recipes.ICraftHandler; import appeng.api.recipes.IIngredient; +import appeng.recipes.RecipeHandler; +import appeng.util.Platform; import cpw.mods.fml.common.event.FMLInterModComms; -public class Pulverizer implements ICraftHandler +public class Pulverizer implements ICraftHandler, IWebsiteSeralizer { IIngredient pro_input; @@ -49,4 +51,17 @@ public class Pulverizer implements ICraftHandler FMLInterModComms.sendMessage( "ThermalExpansion", "PulverizerRecipe", toSend ); } } + + @Override + public boolean canCraft(ItemStack output) throws RegistrationError, MissingIngredientError { + return Platform.isSameItemPrecise( pro_output[0].getItemStack(),output ); + } + + @Override + public String getPattern( RecipeHandler h ) { + return "pulverizer\n"+ + h.getName(pro_input)+"\n"+ + h.getName(pro_output[0]); + } + } diff --git a/recipes/handlers/Shaped.java b/recipes/handlers/Shaped.java index 72d54557c..b3029b557 100644 --- a/recipes/handlers/Shaped.java +++ b/recipes/handlers/Shaped.java @@ -10,10 +10,12 @@ import appeng.api.exceptions.RegistrationError; import appeng.api.recipes.ICraftHandler; import appeng.api.recipes.IIngredient; import appeng.core.AELog; -import appeng.recipes.Recipes.ShapedRecipe; +import appeng.recipes.RecipeHandler; +import appeng.recipes.game.ShapedRecipe; +import appeng.util.Platform; import cpw.mods.fml.common.registry.GameRegistry; -public class Shaped implements ICraftHandler +public class Shaped implements ICraftHandler, IWebsiteSeralizer { private int rows; @@ -87,4 +89,29 @@ public class Shaped implements ICraftHandler throw new RegistrationError( "Error while adding shaped recipe." ); } } + + @Override + public boolean canCraft(ItemStack reqOutput) throws RegistrationError, MissingIngredientError { + return Platform.isSameItemPrecise( output.getItemStack(),reqOutput ); + } + + @Override + public String getPattern( RecipeHandler h ) { + String o = "shaped "+output.getQty()+"\n"; + + o += h.getName(output)+"\n"; + + for ( int y = 0; y < rows; y++ ) + for ( int x= 0; x< cols; x++ ) + { + IIngredient i = inputs.get(y).get(x); + if ( i.isAir() ) + o += "air"+( x +1 == cols ? "\n" : " " ); + else + o += h.getName(i)+( x +1 == cols ? "\n" : " " ); + } + + return o.trim(); + } + } diff --git a/recipes/handlers/Shapeless.java b/recipes/handlers/Shapeless.java index 3ec629a3d..5c0f50c66 100644 --- a/recipes/handlers/Shapeless.java +++ b/recipes/handlers/Shapeless.java @@ -10,10 +10,12 @@ import appeng.api.exceptions.RegistrationError; import appeng.api.recipes.ICraftHandler; import appeng.api.recipes.IIngredient; import appeng.core.AELog; -import appeng.recipes.Recipes.ShapelessRecipe; +import appeng.recipes.RecipeHandler; +import appeng.recipes.game.ShapelessRecipe; +import appeng.util.Platform; import cpw.mods.fml.common.registry.GameRegistry; -public class Shapeless implements ICraftHandler +public class Shapeless implements ICraftHandler, IWebsiteSeralizer { List inputs; @@ -55,4 +57,28 @@ public class Shapeless implements ICraftHandler throw new RegistrationError( "Erro while adding shapeless recipe." ); } } + + @Override + public boolean canCraft(ItemStack reqOutput) throws RegistrationError, MissingIngredientError { + return Platform.isSameItemPrecise( output.getItemStack(),reqOutput ); + } + + @Override + public String getPattern( RecipeHandler h) { + String o = "shapeless "+output.getQty()+"\n"; + + o += h.getName(output)+"\n"; + + for ( int y = 0; y < inputs.size(); y++ ) + { + IIngredient i = inputs.get(y); + if ( i.isAir() ) + o += "air"+( y +1 == inputs.size() ? "\n" : " " ); + else + o += h.getName(i)+( y +1 == inputs.size() ? "\n" : " " ); + } + + return o.trim(); + } + } diff --git a/recipes/handlers/Smelt.java b/recipes/handlers/Smelt.java index 35d77752e..488f73938 100644 --- a/recipes/handlers/Smelt.java +++ b/recipes/handlers/Smelt.java @@ -2,14 +2,18 @@ package appeng.recipes.handlers; import java.util.List; +import net.minecraft.item.ItemStack; + import appeng.api.exceptions.MissingIngredientError; import appeng.api.exceptions.RecipeError; import appeng.api.exceptions.RegistrationError; import appeng.api.recipes.ICraftHandler; import appeng.api.recipes.IIngredient; +import appeng.recipes.RecipeHandler; +import appeng.util.Platform; import cpw.mods.fml.common.registry.GameRegistry; -public class Smelt implements ICraftHandler +public class Smelt implements ICraftHandler, IWebsiteSeralizer { IIngredient in; @@ -44,4 +48,15 @@ public class Smelt implements ICraftHandler GameRegistry.addSmelting( in.getItemStack(), out.getItemStack(), 0 ); } + @Override + public boolean canCraft(ItemStack reqOutput) throws RegistrationError, MissingIngredientError { + return Platform.isSameItemPrecise( out.getItemStack(),reqOutput ); + } + + @Override + public String getPattern( RecipeHandler h ) { + return "smelt "+out.getQty()+"\n"+ + h.getName(in)+"\n"+ + h.getName(out); + } }