Simplify the custom wrapped stack wrapper, and workon a RecipeManager

This commit is contained in:
pahimar 2013-06-12 16:01:45 -04:00
parent 976df1d39b
commit 87f1108632
4 changed files with 224 additions and 95 deletions

View file

@ -10,6 +10,7 @@ import net.minecraft.item.crafting.FurnaceRecipes;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.item.crafting.ShapedRecipes;
import net.minecraft.item.crafting.ShapelessRecipes;
import net.minecraftforge.oredict.OreDictionary;
import net.minecraftforge.oredict.ShapedOreRecipe;
import net.minecraftforge.oredict.ShapelessOreRecipe;
@ -28,6 +29,52 @@ import cpw.mods.fml.common.registry.GameRegistry;
*/
public class RecipeHelper {
/**
* Discovers all instances of ItemStacks with wild card meta values in the vanilla Crafting Manager
*
* @return A list of CustomWrappedStacks that contains all wild card meta ItemStacks in the vanilla Crafting Manager
*/
public static ArrayList<CustomWrappedStack> discoverWildCards() {
ArrayList<CustomWrappedStack> wildCards = new ArrayList<CustomWrappedStack>();
for (Object recipe : CraftingManager.getInstance().getRecipeList()) {
if (recipe instanceof IRecipe) {
if (((IRecipe) recipe).getRecipeOutput() instanceof ItemStack) {
CustomWrappedStack recipeOutput = new CustomWrappedStack(((IRecipe) recipe).getRecipeOutput());
ArrayList<CustomWrappedStack> recipeInputs = RecipeHelper.getRecipeInputs((IRecipe) recipe);
ItemStack itemStack = null;
if (recipeOutput.getWrappedStack() instanceof ItemStack) {
itemStack = (ItemStack) recipeOutput.getWrappedStack();
if (itemStack.getItemDamage() == OreDictionary.WILDCARD_VALUE && !wildCards.contains(recipeOutput)) {
wildCards.add(recipeOutput);
}
}
for (CustomWrappedStack inputStack : recipeInputs) {
if (inputStack.getWrappedStack() instanceof ItemStack) {
itemStack = (ItemStack) inputStack.getWrappedStack();
if (itemStack.getItemDamage() == OreDictionary.WILDCARD_VALUE && !wildCards.contains(inputStack)) {
wildCards.add(inputStack);
}
}
}
}
}
}
return wildCards;
}
/**
* Returns a list of elements that constitute the input in a crafting recipe
*
@ -40,15 +87,17 @@ public class RecipeHelper {
public static ArrayList<CustomWrappedStack> getRecipeInputs(IRecipe recipe) {
ArrayList<CustomWrappedStack> recipeInputs = new ArrayList<CustomWrappedStack>();
ItemStack itemStack = null;
OreStack oreStack = null;
if (recipe instanceof ShapedRecipes) {
ShapedRecipes shapedRecipe = (ShapedRecipes) recipe;
for (int i = 0; i < shapedRecipe.recipeItems.length; i++) {
if (shapedRecipe.recipeItems[i] != null) {
if (shapedRecipe.recipeItems[i] instanceof ItemStack) {
ItemStack itemStack = shapedRecipe.recipeItems[i];
itemStack = shapedRecipe.recipeItems[i];
itemStack.stackSize = 1;
recipeInputs.add(new CustomWrappedStack(itemStack));
@ -62,7 +111,7 @@ public class RecipeHelper {
for (Object object : shapelessRecipe.recipeItems) {
if (object instanceof ItemStack) {
ItemStack itemStack = (ItemStack) object;
itemStack = (ItemStack) object;
itemStack.stackSize = 1;
recipeInputs.add(new CustomWrappedStack(itemStack));
@ -82,7 +131,7 @@ public class RecipeHelper {
if (!shapedOreRecipeList.isEmpty()) {
OreStack oreStack = new OreStack((ItemStack) shapedOreRecipeList.get(0));
oreStack = new OreStack((ItemStack) shapedOreRecipeList.get(0));
oreStack.stackSize = 1;
recipeInputs.add(new CustomWrappedStack(oreStack));
@ -93,7 +142,7 @@ public class RecipeHelper {
*/
else if (shapedOreRecipe.getInput()[i] instanceof ItemStack) {
ItemStack itemStack = (ItemStack) shapedOreRecipe.getInput()[i];
itemStack = (ItemStack) shapedOreRecipe.getInput()[i];
itemStack.stackSize = 1;
recipeInputs.add(new CustomWrappedStack(itemStack));
@ -110,14 +159,14 @@ public class RecipeHelper {
if (!shapelessOreRecipeList.isEmpty()) {
OreStack oreStack = new OreStack((ItemStack) shapelessOreRecipeList.get(0));
oreStack = new OreStack((ItemStack) shapelessOreRecipeList.get(0));
oreStack.stackSize = 1;
recipeInputs.add(new CustomWrappedStack(oreStack));
}
}
else if (object instanceof ItemStack) {
ItemStack itemStack = (ItemStack) object;
itemStack = (ItemStack) object;
itemStack.stackSize = 1;
recipeInputs.add(new CustomWrappedStack(itemStack));

View file

@ -18,6 +18,7 @@ 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.RecipeManager;
public class DynEMC {
@ -49,56 +50,8 @@ public class DynEMC {
}
private void init() {
}
/**
* Discovers all instances of ItemStacks with wild card meta values in the vanilla Crafting Manager
* @return A list of CustomWrappedStacks that contains all wild card meta ItemStacks in the vanilla Crafting Manager
*/
private ArrayList<CustomWrappedStack> findWildCards() {
ArrayList<CustomWrappedStack> wildCards = new ArrayList<CustomWrappedStack>();
for (Object recipe : CraftingManager.getInstance().getRecipeList()) {
if (recipe instanceof IRecipe) {
if (((IRecipe) recipe).getRecipeOutput() instanceof ItemStack) {
CustomWrappedStack recipeOutput = new CustomWrappedStack(((IRecipe) recipe).getRecipeOutput());
ArrayList<CustomWrappedStack> recipeInputs = RecipeHelper.getRecipeInputs((IRecipe) recipe);
ItemStack itemStack = null;
if (recipeOutput.getWrappedStack() instanceof ItemStack) {
itemStack = (ItemStack) recipeOutput.getWrappedStack();
if (itemStack.getItemDamage() == OreDictionary.WILDCARD_VALUE && OreDictionary.getOreID(itemStack) == -1) {
if (!wildCards.contains(recipeOutput)) {
wildCards.add(recipeOutput);
}
}
}
for (CustomWrappedStack inputStack : recipeInputs) {
if (inputStack.getWrappedStack() instanceof ItemStack) {
itemStack = (ItemStack) inputStack.getWrappedStack();
if (itemStack.getItemDamage() == OreDictionary.WILDCARD_VALUE && OreDictionary.getOreID(itemStack) == -1) {
if (!wildCards.contains(inputStack)) {
wildCards.add(inputStack);
}
}
}
}
}
}
}
return wildCards;
RecipeManager recipeManager = RecipeManager.getInstance();
}
private void populateItemList() {
@ -134,9 +87,9 @@ public class DynEMC {
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);
}

View file

@ -11,81 +11,124 @@ public class CustomWrappedStack {
private int stackSize;
private ItemStack itemStack;
private OreStack oreStack;
public CustomWrappedStack(ItemStack itemStack) {
this.itemStack = itemStack;
this.oreStack = null;
stackSize = itemStack.stackSize;
/**
* Creates a new CustomWrappedStack object which wraps the given input. Valid inputs would be ItemStacks or OreStacks.
* If something other than an ItemStack or an OreStack is used as input, nothing is wrapped and the size of the wrapped
* stack is set to -1 to indicate an invalid wrapped stack.
*
* @param object The newly created wrapped stack object
*/
public CustomWrappedStack(Object object) {
if (this.itemStack != null) {
this.itemStack.stackSize = 1;
}
}
public CustomWrappedStack(OreStack oreStack) {
this.itemStack = null;
this.oreStack = oreStack;
stackSize = oreStack.stackSize;
if (this.oreStack != null) {
this.oreStack.stackSize = 1;
/*
* We are given an ItemStack to wrap
*/
if (object instanceof ItemStack) {
ItemStack itemStack = (ItemStack) object;
/*
* If the ItemStack does not exist in the OreDictionary, wrap it as an ItemStack
*/
if (OreDictionary.getOreID(itemStack) == -1) {
this.itemStack = itemStack;
oreStack = null;
stackSize = itemStack.stackSize;
this.itemStack.stackSize = 1;
}
/*
* Else the ItemStack exists in the OreDictionary, so wrap it as an OreStack instead of an ItemStack
*/
else {
this.itemStack = null;
oreStack = new OreStack(itemStack);
stackSize = oreStack.stackSize;
oreStack.stackSize = 1;
}
}
/*
* We are given an OreStack to wrap
*/
else if (object instanceof OreStack) {
itemStack = null;
oreStack = (OreStack) object;
stackSize = oreStack.stackSize;
oreStack.stackSize = 1;
}
/*
* Else, we are given something we cannot wrap
*/
else {
stackSize = -1;
}
}
/**
* Returns the stack size of the wrapped stack, or -1 if we wrapped an invalid input
*
* @return The size of the wrapped stack
*/
public int getStackSize() {
return stackSize;
}
/**
* Sets the size of the wrapped stack
*
* @param stackSize The new size of the wrapped stack
*/
public void setStackSize(int stackSize) {
this.stackSize = stackSize;
}
/**
* Returns the wrapped stack
*
* @return The wrapped ItemStack or OreStack, or null if something other than an ItemStack or OreStack was used to create this object
*/
public Object getWrappedStack() {
if (itemStack != null) {
return itemStack;
}
else {
else if (oreStack != null) {
return oreStack;
}
return null;
}
@Override
public boolean equals(Object object) {
if (!(object instanceof CustomWrappedStack)) {
if (!(object instanceof CustomWrappedStack))
return false;
}
CustomWrappedStack customWrappedStack = (CustomWrappedStack) object;
if (itemStack != null) {
if (customWrappedStack.itemStack != null) {
return (ItemUtil.compare(this.itemStack, customWrappedStack.itemStack) && stackSize == customWrappedStack.itemStack.stackSize);
}
if (customWrappedStack.itemStack != null)
return ItemUtil.compare(itemStack, customWrappedStack.itemStack) && stackSize == customWrappedStack.itemStack.stackSize;
else if (customWrappedStack.oreStack != null) {
for (ItemStack oreDictItemStack : OreDictionary.getOres(customWrappedStack.oreStack.oreName)) {
if (ItemUtil.compare(itemStack, oreDictItemStack) && stackSize == oreDictItemStack.stackSize) {
if (ItemUtil.compare(itemStack, oreDictItemStack) && stackSize == oreDictItemStack.stackSize)
return true;
}
}
}
}
else if (oreStack != null) {
if (customWrappedStack.itemStack != null) {
for (ItemStack oreDictItemStack : OreDictionary.getOres(oreStack.oreName)) {
if (ItemUtil.compare(customWrappedStack.itemStack, oreDictItemStack) && stackSize == oreDictItemStack.stackSize) {
if (ItemUtil.compare(customWrappedStack.itemStack, oreDictItemStack) && stackSize == oreDictItemStack.stackSize)
return true;
}
}
}
else if (customWrappedStack.oreStack != null) {
return (oreStack.equals(customWrappedStack.oreStack) && stackSize == oreStack.stackSize);
}
else if (customWrappedStack.oreStack != null)
return oreStack.equals(customWrappedStack.oreStack) && stackSize == oreStack.stackSize;
}
return false;
@ -108,7 +151,7 @@ public class CustomWrappedStack {
public int hashCode() {
int hashCode = 1;
hashCode = 37 * hashCode + stackSize;
if (itemStack != null) {
@ -120,7 +163,7 @@ public class CustomWrappedStack {
else {
hashCode = 37 * hashCode + itemStack.getItemDamage();
}
hashCode = 37 * hashCode + itemStack.getItemName().hashCode();
}
else if (oreStack != null) {

View file

@ -0,0 +1,84 @@
package com.pahimar.ee3.item.crafting;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import net.minecraft.item.ItemStack;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.pahimar.ee3.core.util.RecipeHelper;
import com.pahimar.ee3.item.CustomWrappedStack;
public class RecipeManager {
private static RecipeManager recipeManager = null;
private Multimap<CustomWrappedStack, List<CustomWrappedStack>> recipeMap;
private List<CustomWrappedStack> wildCardStacks;
private RecipeManager() {
recipeMap = HashMultimap.create();
wildCardStacks = RecipeHelper.discoverWildCards();
}
public static RecipeManager getInstance() {
if (recipeManager == null) {
recipeManager = new RecipeManager();
}
return recipeManager;
}
public boolean hasRecipe(CustomWrappedStack customWrappedStack) {
return recipeMap.containsKey(customWrappedStack);
}
public boolean hasRecipe(ItemStack itemStack) {
return hasRecipe(new CustomWrappedStack(itemStack));
}
public int countRecipes(CustomWrappedStack customWrappedStack) {
Collection<List<CustomWrappedStack>> keys = recipeMap.get(customWrappedStack);
return keys.size();
}
public int countRecipes(ItemStack itemStack) {
return countRecipes(new CustomWrappedStack(itemStack));
}
public Collection<List<CustomWrappedStack>> getRecipes(CustomWrappedStack customWrappedStack) {
return recipeMap.get(customWrappedStack);
}
public Collection<List<CustomWrappedStack>> getRecipes(ItemStack itemStack) {
return getRecipes(new CustomWrappedStack(itemStack));
}
public void addRecipe(CustomWrappedStack recipeOutput, List<?> recipeInputs) {
ArrayList<CustomWrappedStack> collatedStacks = new ArrayList<CustomWrappedStack>();
CustomWrappedStack wrappedInput = null;
/**
* For every input in the input list, check to see if we have discovered
* it already - If we have, add it to the one we already have - If we
* have not, add it to the collection of discovered items
*/
for (Object object : recipeInputs) {
}
}
}