From 05be819d18b3f5256717017549807b5a3ea3ffb9 Mon Sep 17 00:00:00 2001 From: pahimar Date: Wed, 3 Jul 2013 16:26:10 -0400 Subject: [PATCH] Move the recipe stuff to the more logical location (RecipeRegistry vs DynEMC) --- ee3_common/com/pahimar/ee3/emc/DynEMC.java | 153 +----------------- .../ee3/item/crafting/RecipeRegistry.java | 143 ++++++++++++++-- 2 files changed, 134 insertions(+), 162 deletions(-) diff --git a/ee3_common/com/pahimar/ee3/emc/DynEMC.java b/ee3_common/com/pahimar/ee3/emc/DynEMC.java index 3354780d..3a4a2783 100644 --- a/ee3_common/com/pahimar/ee3/emc/DynEMC.java +++ b/ee3_common/com/pahimar/ee3/emc/DynEMC.java @@ -1,42 +1,22 @@ package com.pahimar.ee3.emc; -import java.util.ArrayList; import java.util.Iterator; -import java.util.List; -import java.util.NoSuchElementException; import java.util.Set; -import net.minecraft.item.Item; -import net.minecraft.item.ItemStack; -import net.minecraft.item.crafting.IRecipe; -import net.minecraftforge.oredict.OreDictionary; - -import com.google.common.collect.HashMultimap; -import com.google.common.collect.Multimap; import com.pahimar.ee3.core.util.LogHelper; -import com.pahimar.ee3.core.util.OreStack; -import com.pahimar.ee3.core.util.RecipeHelper; import com.pahimar.ee3.emc.graph.WeightedDirectedGraph; import com.pahimar.ee3.emc.graph.WeightedEdge; import com.pahimar.ee3.item.CustomWrappedStack; import com.pahimar.ee3.item.crafting.RecipeRegistry; -import com.pahimar.ee3.item.crafting.RecipesPotions; -import com.pahimar.ee3.item.crafting.RecipesSmelting; -import com.pahimar.ee3.item.crafting.RecipesVanilla; public class DynEMC { private static DynEMC dynEMC = null; - - private ArrayList discoveredStacks; - private ArrayList stacksWithoutRecipes; private WeightedDirectedGraph graph; private DynEMC() { - discoveredStacks = new ArrayList(); - stacksWithoutRecipes = new ArrayList(); graph = new WeightedDirectedGraph(); init(); @@ -53,143 +33,14 @@ public class DynEMC { private void init() { + @SuppressWarnings("unused") RecipeRegistry recipeManager = RecipeRegistry.getInstance(); - - Multimap> recipes = HashMultimap.create(); - - // Add potion recipes - recipes.putAll(RecipesPotions.getPotionRecipes()); - - // Add smelting recipes in the vanilla smelting manager - recipes.putAll(RecipesSmelting.getSmeltingRecipes()); - - // Add recipes in the vanilla crafting manager - recipes.putAll(RecipesVanilla.getVanillaRecipes()); - - // Add recipes gathered via IMC - // TODO Gather IMC recipes - - // Populate the discovered stacks list with all stacks that we are involved in a recipe we are aware of - discoverStacks(recipes); - // Add items that have no recipe, using the list of discovered stacks to determine if it's in a recipe or not - for (CustomWrappedStack stack : stacksWithoutRecipes) { - recipes.put(stack, new ArrayList()); - } - - // Iterate through every recipe in the map, and add them to the registry - Set recipeKeySet = recipes.keySet(); - Iterator recipeKeySetIterator = recipeKeySet.iterator(); - CustomWrappedStack recipeOutput = null; - - while (recipeKeySetIterator.hasNext()) { - recipeOutput = recipeKeySetIterator.next(); - - for (List recipeInputs : recipes.get(recipeOutput)) { - recipeManager.addRecipe(recipeOutput, recipeInputs); - } - } + populateGraph(); } - private void discoverStacks(Multimap> recipes) { - - Set recipeKeySet = recipes.keySet(); - Iterator recipeKeySetIterator = recipeKeySet.iterator(); - CustomWrappedStack recipeOutput = null; - - while (recipeKeySetIterator.hasNext()) { - recipeOutput = recipeKeySetIterator.next(); - - if (!discoveredStacks.contains(new CustomWrappedStack(recipeOutput.getWrappedStack()))) { - discoveredStacks.add(new CustomWrappedStack(recipeOutput.getWrappedStack())); - } - - for (List recipeInputs : recipes.get(recipeOutput)) { - for (CustomWrappedStack recipeInput : recipeInputs) { - - if (!discoveredStacks.contains(new CustomWrappedStack(recipeInput.getWrappedStack()))) { - discoveredStacks.add(new CustomWrappedStack(recipeInput.getWrappedStack())); - } - } - } - } - - ArrayList subItemList = new ArrayList(); - - for (int i = 0; i < Item.itemsList.length; i++) { - if (Item.itemsList[i] != null) { - if (Item.itemsList[i].getHasSubtypes()) { - - subItemList.clear(); - Item.itemsList[i].getSubItems(i, Item.itemsList[i].getCreativeTab(), subItemList); - - for (ItemStack itemStack : subItemList) { - if (itemStack != null) { - - CustomWrappedStack customWrappedStack = new CustomWrappedStack(itemStack); - - if (!discoveredStacks.contains(customWrappedStack)) { - discoveredStacks.add(customWrappedStack); - stacksWithoutRecipes.add(customWrappedStack); - } - } - } - } - else { - - CustomWrappedStack customWrappedStack = new CustomWrappedStack(new ItemStack(Item.itemsList[i])); - - if (!discoveredStacks.contains(customWrappedStack)) { - discoveredStacks.add(customWrappedStack); - stacksWithoutRecipes.add(customWrappedStack); - } - } - } - } - } - - @SuppressWarnings("unused") private void populateGraph() { - for (CustomWrappedStack customWrappedStack : discoveredStacks) { - - ArrayList recipes = RecipeHelper.getReverseRecipes(customWrappedStack); - - for (IRecipe recipe : recipes) { - - ArrayList recipeInputs = RecipeHelper.getCollatedRecipeInputs(recipe); - - for (CustomWrappedStack wrappedRecipeInput : recipeInputs) { - - float weight = wrappedRecipeInput.getStackSize(); - - CustomWrappedStack recipeInput = null; - - if (wrappedRecipeInput.getWrappedStack() instanceof ItemStack) { - ItemStack itemStack = (ItemStack) wrappedRecipeInput.getWrappedStack(); - - if (OreDictionary.getOreID(itemStack) != -1) { - recipeInput = new CustomWrappedStack(new OreStack(itemStack)); - } - else { - recipeInput = new CustomWrappedStack(itemStack); - } - } - else if (wrappedRecipeInput.getWrappedStack() instanceof OreStack) { - recipeInput = new CustomWrappedStack((OreStack) wrappedRecipeInput.getWrappedStack()); - } - - try { - if (recipeInput != null) { - graph.addEdge(customWrappedStack, recipeInput, weight); - } - } - catch (NoSuchElementException e) { - LogHelper.severe(e.getMessage() + ";\nFrom: [" + customWrappedStack + "]\nTo: [" + wrappedRecipeInput + "]"); - } - } - } - } } public int size() { diff --git a/ee3_common/com/pahimar/ee3/item/crafting/RecipeRegistry.java b/ee3_common/com/pahimar/ee3/item/crafting/RecipeRegistry.java index 9a79dadb..8ff983c9 100644 --- a/ee3_common/com/pahimar/ee3/item/crafting/RecipeRegistry.java +++ b/ee3_common/com/pahimar/ee3/item/crafting/RecipeRegistry.java @@ -4,7 +4,9 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.List; +import java.util.Set; +import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import com.google.common.collect.HashMultimap; @@ -20,23 +22,122 @@ public class RecipeRegistry { private static RecipeRegistry recipeRegistry = null; private Multimap> recipeMap; - - public List wildCardList; + private ArrayList discoveredStacks; + private ArrayList recipelessStacks; + private List wildCardStacks; private RecipeRegistry() { recipeMap = HashMultimap.create(); - wildCardList = RecipeHelper.populateWildCards(); + wildCardStacks = RecipeHelper.populateWildCards(); + discoveredStacks = new ArrayList(); + recipelessStacks = new ArrayList(); } public static RecipeRegistry getInstance() { if (recipeRegistry == null) { recipeRegistry = new RecipeRegistry(); + recipeRegistry.init(); } return recipeRegistry; } + + private void init() { + + Multimap> recipes = HashMultimap.create(); + + // Add potion recipes + recipes.putAll(RecipesPotions.getPotionRecipes()); + + // Add smelting recipes in the vanilla smelting manager + recipes.putAll(RecipesSmelting.getSmeltingRecipes()); + + // Add recipes in the vanilla crafting manager + recipes.putAll(RecipesVanilla.getVanillaRecipes()); + + // Add recipes gathered via IMC + // TODO Gather IMC recipes + + // Populate the discovered stacks list with all stacks that we are involved in a recipe we are aware of + discoverStacks(recipes); + + // Add items that have no recipe, using the list of discovered stacks to determine if it's in a recipe or not + for (CustomWrappedStack stack : recipelessStacks) { + recipes.put(stack, new ArrayList()); + } + + // Iterate through every recipe in the map, and add them to the registry + Set recipeKeySet = recipes.keySet(); + Iterator recipeKeySetIterator = recipeKeySet.iterator(); + CustomWrappedStack recipeOutput = null; + + while (recipeKeySetIterator.hasNext()) { + recipeOutput = recipeKeySetIterator.next(); + + for (List recipeInputs : recipes.get(recipeOutput)) { + addRecipe(recipeOutput, recipeInputs); + } + } + } + + private void discoverStacks(Multimap> recipes) { + + Set recipeKeySet = recipes.keySet(); + Iterator recipeKeySetIterator = recipeKeySet.iterator(); + CustomWrappedStack recipeOutput = null; + + while (recipeKeySetIterator.hasNext()) { + recipeOutput = recipeKeySetIterator.next(); + + if (!discoveredStacks.contains(new CustomWrappedStack(recipeOutput.getWrappedStack()))) { + discoveredStacks.add(new CustomWrappedStack(recipeOutput.getWrappedStack())); + } + + for (List recipeInputs : recipes.get(recipeOutput)) { + for (CustomWrappedStack recipeInput : recipeInputs) { + + if (!discoveredStacks.contains(new CustomWrappedStack(recipeInput.getWrappedStack()))) { + discoveredStacks.add(new CustomWrappedStack(recipeInput.getWrappedStack())); + } + } + } + } + + ArrayList subItemList = new ArrayList(); + + for (int i = 0; i < Item.itemsList.length; i++) { + if (Item.itemsList[i] != null) { + if (Item.itemsList[i].getHasSubtypes()) { + + subItemList.clear(); + Item.itemsList[i].getSubItems(i, Item.itemsList[i].getCreativeTab(), subItemList); + + for (ItemStack itemStack : subItemList) { + if (itemStack != null) { + + CustomWrappedStack customWrappedStack = new CustomWrappedStack(itemStack); + + if (!discoveredStacks.contains(customWrappedStack)) { + discoveredStacks.add(customWrappedStack); + recipelessStacks.add(customWrappedStack); + } + } + } + } + else { + + CustomWrappedStack customWrappedStack = new CustomWrappedStack(new ItemStack(Item.itemsList[i])); + + if (!discoveredStacks.contains(customWrappedStack)) { + discoveredStacks.add(customWrappedStack); + recipelessStacks.add(customWrappedStack); + } + } + } + } + } public boolean hasRecipe(CustomWrappedStack customWrappedStack) { @@ -48,26 +149,26 @@ public class RecipeRegistry { return hasRecipe(new CustomWrappedStack(itemStack)); } - public int countRecipes(CustomWrappedStack customWrappedStack) { + public int countRecipesFor(CustomWrappedStack customWrappedStack) { Collection> keys = recipeMap.get(customWrappedStack); return keys.size(); } - public int countRecipes(ItemStack itemStack) { + public int countRecipesFor(ItemStack itemStack) { - return countRecipes(new CustomWrappedStack(itemStack)); + return countRecipesFor(new CustomWrappedStack(itemStack)); } - public Collection> getRecipes(CustomWrappedStack customWrappedStack) { + public Collection> getRecipesFor(CustomWrappedStack customWrappedStack) { return recipeMap.get(customWrappedStack); } - public Collection> getRecipes(ItemStack itemStack) { + public Collection> getRecipesFor(ItemStack itemStack) { - return getRecipes(new CustomWrappedStack(itemStack)); + return getRecipesFor(new CustomWrappedStack(itemStack)); } /* @@ -94,8 +195,8 @@ public class RecipeRegistry { wrappedInputStack = (CustomWrappedStack) object; } - if (wildCardList.contains(wrappedInputStack)) { - Iterator wildIter = wildCardList.iterator(); + if (wildCardStacks.contains(wrappedInputStack)) { + Iterator wildIter = wildCardStacks.iterator(); while (wildIter.hasNext()) { CustomWrappedStack wildCard = wildIter.next(); if (wildCard.equals(wrappedInputStack)) { @@ -166,4 +267,24 @@ public class RecipeRegistry { return stringBuilder.toString(); } + + public Multimap> getRecipeMappings() { + + return recipeMap; + } + + public List getDiscoveredStacks() { + + return discoveredStacks; + } + + public List getRecipelessStacks() { + + return recipelessStacks; + } + + public List getWildCardStacks() { + + return wildCardStacks; + } }