Changes to RecipeHelper to solve the case where recipes have individual item stacks with a stack size > 1, and a general method to ensure items without recipes (but that are accessibly via the vanilla Items list) are added to the recipe registry as having no recipe

This commit is contained in:
pahimar 2013-07-03 16:13:38 -04:00
parent 1591f537c3
commit 9a482adcff
2 changed files with 89 additions and 92 deletions

View file

@ -94,8 +94,16 @@ public class RecipeHelper {
ShapedRecipes shapedRecipe = (ShapedRecipes) recipe;
for (int i = 0; i < shapedRecipe.recipeItems.length; i++) {
if (shapedRecipe.recipeItems[i] instanceof ItemStack) {
recipeInputs.add(new CustomWrappedStack(shapedRecipe.recipeItems[i]));
ItemStack itemStack = shapedRecipe.recipeItems[i].copy();
if (itemStack.stackSize > 1) {
itemStack.stackSize = 1;
}
recipeInputs.add(new CustomWrappedStack(itemStack));
}
}
}
@ -104,8 +112,16 @@ public class RecipeHelper {
ShapelessRecipes shapelessRecipe = (ShapelessRecipes) recipe;
for (Object object : shapelessRecipe.recipeItems) {
if (object instanceof ItemStack) {
recipeInputs.add(new CustomWrappedStack(object));
ItemStack itemStack = ((ItemStack) object).copy();
if (itemStack.stackSize > 1) {
itemStack.stackSize = 1;
}
recipeInputs.add(new CustomWrappedStack(itemStack));
}
}
}
@ -117,9 +133,19 @@ public class RecipeHelper {
/*
* If the element is a list, then it is an OreStack
*/
if (shapedOreRecipe.getInput()[i] instanceof ArrayList || shapedOreRecipe.getInput()[i] instanceof ItemStack) {
if (shapedOreRecipe.getInput()[i] instanceof ArrayList) {
recipeInputs.add(new CustomWrappedStack(shapedOreRecipe.getInput()[i]));
}
else if (shapedOreRecipe.getInput()[i] instanceof ItemStack) {
ItemStack itemStack = ((ItemStack) shapedOreRecipe.getInput()[i]).copy();
if (itemStack.stackSize > 1) {
itemStack.stackSize = 1;
}
recipeInputs.add(new CustomWrappedStack(itemStack));
}
}
}
else if (recipe instanceof ShapelessOreRecipe) {
@ -127,9 +153,20 @@ public class RecipeHelper {
ShapelessOreRecipe shapelessOreRecipe = (ShapelessOreRecipe) recipe;
for (Object object : shapelessOreRecipe.getInput()) {
if (object instanceof ArrayList || object instanceof ItemStack) {
if (object instanceof ArrayList) {
recipeInputs.add(new CustomWrappedStack(object));
}
else if (object instanceof ItemStack) {
ItemStack itemStack = ((ItemStack) object).copy();
if (itemStack.stackSize > 1) {
itemStack.stackSize = 1;
}
recipeInputs.add(new CustomWrappedStack(itemStack));
}
}
}

View file

@ -8,7 +8,6 @@ import java.util.Set;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.CraftingManager;
import net.minecraft.item.crafting.IRecipe;
import net.minecraftforge.oredict.OreDictionary;
@ -29,12 +28,15 @@ public class DynEMC {
private static DynEMC dynEMC = null;
private ArrayList<CustomWrappedStack> discoveredItems;
private ArrayList<CustomWrappedStack> discoveredStacks;
private ArrayList<CustomWrappedStack> stacksWithoutRecipes;
private WeightedDirectedGraph<CustomWrappedStack> graph;
private DynEMC() {
discoveredItems = new ArrayList<CustomWrappedStack>();
discoveredStacks = new ArrayList<CustomWrappedStack>();
stacksWithoutRecipes = new ArrayList<CustomWrappedStack>();
graph = new WeightedDirectedGraph<CustomWrappedStack>();
init();
@ -54,9 +56,6 @@ public class DynEMC {
RecipeRegistry recipeManager = RecipeRegistry.getInstance();
Multimap<CustomWrappedStack, List<CustomWrappedStack>> recipes = HashMultimap.create();
Set<CustomWrappedStack> recipeKeySet = null;
Iterator<CustomWrappedStack> recipeKeySetIterator = null;
CustomWrappedStack recipeOutput = null;
// Add potion recipes
recipes.putAll(RecipesPotions.getPotionRecipes());
@ -70,128 +69,89 @@ public class DynEMC {
// Add recipes gathered via IMC
// TODO Gather IMC recipes
// Add items that have no recipe
// 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<CustomWrappedStack>());
}
// Iterate through every recipe in the map, and add them to the registry
recipeKeySet = recipes.keySet();
recipeKeySetIterator = recipeKeySet.iterator();
recipeOutput = null;
Set<CustomWrappedStack> recipeKeySet = recipes.keySet();
Iterator<CustomWrappedStack> recipeKeySetIterator = recipeKeySet.iterator();
CustomWrappedStack recipeOutput = null;
while (recipeKeySetIterator.hasNext()) {
recipeOutput = recipeKeySetIterator.next();
for (List<CustomWrappedStack> recipeInputs : recipes.get(recipeOutput)) {
recipeManager.addRecipe(recipeOutput, recipeInputs);
}
}
LogHelper.debug(recipeManager.toString());
}
@SuppressWarnings("unused")
private void populateItemList() {
ArrayList<ItemStack> subItems = new ArrayList<ItemStack>();
/*
* Add all entries from the OreDictionary
*/
for (String oreName : OreDictionary.getOreNames()) {
CustomWrappedStack customWrappedStack = new CustomWrappedStack(new OreStack(oreName));
if (!discoveredItems.contains(customWrappedStack)) {
discoveredItems.add(customWrappedStack);
private void discoverStacks(Multimap<CustomWrappedStack, List<CustomWrappedStack>> recipes) {
Set<CustomWrappedStack> recipeKeySet = recipes.keySet();
Iterator<CustomWrappedStack> 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 (Object recipe : CraftingManager.getInstance().getRecipeList()) {
if (recipe instanceof IRecipe) {
ItemStack craftingResult = ((IRecipe) recipe).getRecipeOutput();
if (craftingResult != null) {
if (craftingResult.getItemDamage() == OreDictionary.WILDCARD_VALUE) {
CustomWrappedStack wrappedCraftingResult = new CustomWrappedStack(craftingResult);
if (!discoveredItems.contains(wrappedCraftingResult)) {
discoveredItems.add(wrappedCraftingResult);
}
}
for (CustomWrappedStack wrappedRecipeInput : RecipeHelper.getCollatedRecipeInputs((IRecipe) recipe)) {
if (wrappedRecipeInput.getWrappedStack() instanceof ItemStack) {
ItemStack wrappedItemStack = (ItemStack) wrappedRecipeInput.getWrappedStack();
if (wrappedItemStack.getItemDamage() == OreDictionary.WILDCARD_VALUE) {
wrappedRecipeInput.setStackSize(1);
if (!discoveredItems.contains(wrappedRecipeInput)) {
discoveredItems.add(wrappedRecipeInput);
}
}
}
for (List<CustomWrappedStack> recipeInputs : recipes.get(recipeOutput)) {
for (CustomWrappedStack recipeInput : recipeInputs) {
if (!discoveredStacks.contains(new CustomWrappedStack(recipeInput.getWrappedStack()))) {
discoveredStacks.add(new CustomWrappedStack(recipeInput.getWrappedStack()));
}
}
}
}
/*
* For every possible item (and sub item), add them to the discovered
* items list
*/
ArrayList<ItemStack> subItemList = new ArrayList<ItemStack>();
for (int i = 0; i < Item.itemsList.length; i++) {
if (Item.itemsList[i] != null) {
if (Item.itemsList[i].getHasSubtypes()) {
subItems.clear();
Item.itemsList[i].getSubItems(i, Item.itemsList[i].getCreativeTab(), subItems);
subItemList.clear();
Item.itemsList[i].getSubItems(i, Item.itemsList[i].getCreativeTab(), subItemList);
for (ItemStack itemStack : subItems) {
for (ItemStack itemStack : subItemList) {
if (itemStack != null) {
CustomWrappedStack customWrappedStack = new CustomWrappedStack(itemStack);
if (!discoveredItems.contains(customWrappedStack)) {
discoveredItems.add(customWrappedStack);
if (!discoveredStacks.contains(customWrappedStack)) {
discoveredStacks.add(customWrappedStack);
stacksWithoutRecipes.add(customWrappedStack);
}
}
}
}
else {
ItemStack itemStack = new ItemStack(Item.itemsList[i]);
CustomWrappedStack customWrappedStack = new CustomWrappedStack(itemStack);
if (!discoveredItems.contains(customWrappedStack)) {
discoveredItems.add(customWrappedStack);
CustomWrappedStack customWrappedStack = new CustomWrappedStack(new ItemStack(Item.itemsList[i]));
if (!discoveredStacks.contains(customWrappedStack)) {
discoveredStacks.add(customWrappedStack);
stacksWithoutRecipes.add(customWrappedStack);
}
}
}
}
/**
* Now that we have discovered as many items as possible, trim out the
* items that are black listed
*/
for (CustomWrappedStack customWrappedStack : EmcBlackList.getInstance().getBlackListStacks()) {
while (discoveredItems.contains(customWrappedStack)) {
discoveredItems.remove(customWrappedStack);
}
}
for (CustomWrappedStack customWrappedStack : discoveredItems) {
if (!graph.nodeExists(customWrappedStack)) {
graph.addNode(customWrappedStack);
}
}
}
@SuppressWarnings("unused")
private void populateGraph() {
for (CustomWrappedStack customWrappedStack : discoveredItems) {
for (CustomWrappedStack customWrappedStack : discoveredStacks) {
ArrayList<IRecipe> recipes = RecipeHelper.getReverseRecipes(customWrappedStack);