From d8e80366e121c11ff78076b22bc20ae6855c3a5d Mon Sep 17 00:00:00 2001 From: pahimar Date: Sat, 1 Jun 2013 21:49:21 -0400 Subject: [PATCH] Fix edge weights not getting assigned properly, still some edge exceptions to handle in the graph construction --- .../com/pahimar/ee3/core/util/ItemUtil.java | 25 ++++----- .../pahimar/ee3/core/util/RecipeHelper.java | 23 ++++---- ee3_common/com/pahimar/ee3/emc/DynEMC.java | 53 +++++++++---------- .../com/pahimar/ee3/emc/EmcBlackList.java | 12 ++--- .../com/pahimar/ee3/emc/EquivalencyGroup.java | 32 +++++------ ...ckWrapper.java => CustomWrappedStack.java} | 47 +++++++--------- 6 files changed, 87 insertions(+), 105 deletions(-) rename ee3_common/com/pahimar/ee3/item/{CustomStackWrapper.java => CustomWrappedStack.java} (71%) diff --git a/ee3_common/com/pahimar/ee3/core/util/ItemUtil.java b/ee3_common/com/pahimar/ee3/core/util/ItemUtil.java index e4fcfdfe..441a9398 100644 --- a/ee3_common/com/pahimar/ee3/core/util/ItemUtil.java +++ b/ee3_common/com/pahimar/ee3/core/util/ItemUtil.java @@ -9,7 +9,7 @@ import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraftforge.oredict.OreDictionary; -import com.pahimar.ee3.item.CustomStackWrapper; +import com.pahimar.ee3.item.CustomWrappedStack; import com.pahimar.ee3.item.ModItems; import com.pahimar.ee3.lib.Colours; import com.pahimar.ee3.lib.Strings; @@ -91,10 +91,9 @@ public class ItemUtil { return false; } - @SuppressWarnings({ "rawtypes", "unchecked" }) - public static ArrayList collateStacks(List unCollatedStacks) { + public static ArrayList collateStacks(List unCollatedStacks) { - ArrayList collatedStacks = new ArrayList(); + ArrayList collatedStacks = new ArrayList(); for (int i = 0; i < unCollatedStacks.size(); i++) { @@ -105,21 +104,15 @@ public class ItemUtil { boolean found = false; for (int j = 0; j < collatedStacks.size(); j++) { - if ((unCollatedStacks.get(i) instanceof ItemStack) && (collatedStacks.get(j) instanceof ItemStack)) { - ItemStack unCollatedStack = (ItemStack) unCollatedStacks.get(i); - ItemStack collatedStack = (ItemStack) collatedStacks.get(j); - - if (compare(unCollatedStack, collatedStack)) { - ((ItemStack) collatedStacks.get(j)).stackSize += 1; + if (unCollatedStacks.get(i).getItemStack() != null && collatedStacks.get(j).getItemStack() != null) { + if (compare(unCollatedStacks.get(i).getItemStack(), collatedStacks.get(j).getItemStack())) { + collatedStacks.get(j).setStackSize(collatedStacks.get(j).getStackSize() + 1); found = true; } } - else if ((unCollatedStacks.get(i) instanceof OreStack) && (collatedStacks.get(j) instanceof OreStack)) { - OreStack unCollatedStack = (OreStack) unCollatedStacks.get(i); - OreStack collatedStack = (OreStack) collatedStacks.get(j); - - if (OreStack.compareStacks(unCollatedStack, collatedStack)) { - ((OreStack) collatedStacks.get(j)).stackSize += 1; + else if (unCollatedStacks.get(i).getOreStack() != null && collatedStacks.get(j).getOreStack() != null) { + if (OreStack.compareStacks(unCollatedStacks.get(i).getOreStack(), collatedStacks.get(j).getOreStack())) { + collatedStacks.get(j).setStackSize(collatedStacks.get(j).getStackSize() + 1); found = true; } } diff --git a/ee3_common/com/pahimar/ee3/core/util/RecipeHelper.java b/ee3_common/com/pahimar/ee3/core/util/RecipeHelper.java index 6865f2a0..76d31335 100644 --- a/ee3_common/com/pahimar/ee3/core/util/RecipeHelper.java +++ b/ee3_common/com/pahimar/ee3/core/util/RecipeHelper.java @@ -13,7 +13,7 @@ import net.minecraft.item.crafting.ShapelessRecipes; import net.minecraftforge.oredict.ShapedOreRecipe; import net.minecraftforge.oredict.ShapelessOreRecipe; -import com.pahimar.ee3.item.CustomStackWrapper; +import com.pahimar.ee3.item.CustomWrappedStack; import cpw.mods.fml.common.registry.GameRegistry; @@ -37,9 +37,9 @@ public class RecipeHelper { * Could be an ItemStack or an Arraylist */ @SuppressWarnings({ "rawtypes" }) - public static ArrayList getRecipeInputs(IRecipe recipe) { + public static ArrayList getRecipeInputs(IRecipe recipe) { - ArrayList recipeInputs = new ArrayList(); + ArrayList recipeInputs = new ArrayList(); if (recipe instanceof ShapedRecipes) { @@ -51,7 +51,7 @@ public class RecipeHelper { ItemStack itemStack = shapedRecipe.recipeItems[i]; itemStack.stackSize = 1; - recipeInputs.add(new CustomStackWrapper(itemStack)); + recipeInputs.add(new CustomWrappedStack(itemStack)); } } } @@ -65,7 +65,7 @@ public class RecipeHelper { ItemStack itemStack = (ItemStack) object; itemStack.stackSize = 1; - recipeInputs.add(new CustomStackWrapper(itemStack)); + recipeInputs.add(new CustomWrappedStack(itemStack)); } } } @@ -85,7 +85,7 @@ public class RecipeHelper { OreStack oreStack = new OreStack((ItemStack) shapedOreRecipeList.get(0)); oreStack.stackSize = 1; - recipeInputs.add(new CustomStackWrapper(oreStack)); + recipeInputs.add(new CustomWrappedStack(oreStack)); } } /* @@ -96,7 +96,7 @@ public class RecipeHelper { ItemStack itemStack = (ItemStack) shapedOreRecipe.getInput()[i]; itemStack.stackSize = 1; - recipeInputs.add(new CustomStackWrapper(itemStack)); + recipeInputs.add(new CustomWrappedStack(itemStack)); } } } @@ -113,14 +113,14 @@ public class RecipeHelper { OreStack oreStack = new OreStack((ItemStack) shapelessOreRecipeList.get(0)); oreStack.stackSize = 1; - recipeInputs.add(new CustomStackWrapper(oreStack)); + recipeInputs.add(new CustomWrappedStack(oreStack)); } } else if (object instanceof ItemStack) { ItemStack itemStack = (ItemStack) object; itemStack.stackSize = 1; - recipeInputs.add(new CustomStackWrapper(itemStack)); + recipeInputs.add(new CustomWrappedStack(itemStack)); } } } @@ -128,13 +128,12 @@ public class RecipeHelper { return recipeInputs; } - @SuppressWarnings("unchecked") - public static ArrayList getCollatedRecipeInputs(IRecipe recipe) { + public static ArrayList getCollatedRecipeInputs(IRecipe recipe) { return ItemUtil.collateStacks(getRecipeInputs(recipe)); } - public static ArrayList getReverseRecipes(CustomStackWrapper customStackWrapper) { + public static ArrayList getReverseRecipes(CustomWrappedStack customStackWrapper) { return getReverseRecipes(customStackWrapper.getItemStack()); } diff --git a/ee3_common/com/pahimar/ee3/emc/DynEMC.java b/ee3_common/com/pahimar/ee3/emc/DynEMC.java index bf20475c..a5c02400 100644 --- a/ee3_common/com/pahimar/ee3/emc/DynEMC.java +++ b/ee3_common/com/pahimar/ee3/emc/DynEMC.java @@ -18,19 +18,19 @@ 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.CustomStackWrapper; +import com.pahimar.ee3.item.CustomWrappedStack; public class DynEMC { private static DynEMC dynEMC = null; - private ArrayList discoveredItems; - private WeightedDirectedGraph graph; + private ArrayList discoveredItems; + private WeightedDirectedGraph graph; private DynEMC() { - discoveredItems = new ArrayList(); - graph = new WeightedDirectedGraph(); + discoveredItems = new ArrayList(); + graph = new WeightedDirectedGraph(); init(); } @@ -44,7 +44,7 @@ public class DynEMC { return dynEMC; } - public List getDiscoveredItems() { + public List getDiscoveredItems() { return discoveredItems; } @@ -62,7 +62,7 @@ public class DynEMC { */ for (String oreName : OreDictionary.getOreNames()) { - CustomStackWrapper customWrappedStack = new CustomStackWrapper(new OreStack(oreName)); + CustomWrappedStack customWrappedStack = new CustomWrappedStack(new OreStack(oreName)); if (!discoveredItems.contains(customWrappedStack)) { discoveredItems.add(customWrappedStack); @@ -75,17 +75,17 @@ public class DynEMC { if (craftingResult != null) { if (craftingResult.getItemDamage() == OreDictionary.WILDCARD_VALUE) { - CustomStackWrapper wrappedCraftingResult = new CustomStackWrapper(craftingResult); + CustomWrappedStack wrappedCraftingResult = new CustomWrappedStack(craftingResult); if (!discoveredItems.contains(wrappedCraftingResult)) { discoveredItems.add(wrappedCraftingResult); } } - for (CustomStackWrapper wrappedRecipeInput : RecipeHelper.getCollatedRecipeInputs((IRecipe) recipe)) { + for (CustomWrappedStack wrappedRecipeInput : RecipeHelper.getCollatedRecipeInputs((IRecipe) recipe)) { if ((wrappedRecipeInput.getItemStack() != null) && (wrappedRecipeInput.getItemStack().getItemDamage() == OreDictionary.WILDCARD_VALUE)) { - wrappedRecipeInput.setWrappedStackSize(1); + wrappedRecipeInput.setStackSize(1); if (!discoveredItems.contains(wrappedRecipeInput)) { discoveredItems.add(wrappedRecipeInput); } @@ -109,7 +109,7 @@ public class DynEMC { for (ItemStack itemStack : subItems) { if (itemStack != null) { - CustomStackWrapper customStackWrapper = new CustomStackWrapper(itemStack); + CustomWrappedStack customStackWrapper = new CustomWrappedStack(itemStack); if (!discoveredItems.contains(customStackWrapper)) { discoveredItems.add(customStackWrapper); @@ -121,7 +121,7 @@ public class DynEMC { ItemStack itemStack = new ItemStack(Item.itemsList[i]); - CustomStackWrapper customStackWrapper = new CustomStackWrapper(itemStack); + CustomWrappedStack customStackWrapper = new CustomWrappedStack(itemStack); if (!discoveredItems.contains(customStackWrapper)) { discoveredItems.add(customStackWrapper); } @@ -133,42 +133,41 @@ public class DynEMC { * Now that we have discovered as many items as possible, trim out the * items that are black listed */ - for (CustomStackWrapper customStackWrapper : EmcBlackList.getInstance().getBlackListStacks()) { + for (CustomWrappedStack customStackWrapper : EmcBlackList.getInstance().getBlackListStacks()) { while (discoveredItems.contains(customStackWrapper)) { discoveredItems.remove(customStackWrapper); } } - for (CustomStackWrapper customWrappedStack : discoveredItems) { + for (CustomWrappedStack customWrappedStack : discoveredItems) { if (!graph.nodeExists(customWrappedStack)) { graph.addNode(customWrappedStack); } } - for (CustomStackWrapper customWrappedStack : discoveredItems) { + for (CustomWrappedStack customWrappedStack : discoveredItems) { ArrayList recipes = RecipeHelper.getReverseRecipes(customWrappedStack); for (IRecipe recipe : recipes) { - ArrayList recipeInputs = RecipeHelper.getCollatedRecipeInputs(recipe); - - System.out.println(recipeInputs); + ArrayList recipeInputs = RecipeHelper.getCollatedRecipeInputs(recipe); - for (CustomStackWrapper wrappedRecipeInput : recipeInputs) { + for (CustomWrappedStack wrappedRecipeInput : recipeInputs) { if (wrappedRecipeInput.getItemStack() != null) { ItemStack itemStack = wrappedRecipeInput.getItemStack(); + itemStack.stackSize = wrappedRecipeInput.getStackSize(); if (OreDictionary.getOreID(itemStack) != -1) { - wrappedRecipeInput = new CustomStackWrapper(new OreStack(itemStack)); + wrappedRecipeInput = new CustomWrappedStack(new OreStack(itemStack)); } } - float weight = wrappedRecipeInput.getWrappedStackSize(); - wrappedRecipeInput.setWrappedStackSize(1); + float weight = wrappedRecipeInput.getStackSize(); + wrappedRecipeInput.setStackSize(1); try { graph.addEdge(customWrappedStack, wrappedRecipeInput, weight); @@ -187,9 +186,9 @@ public class DynEMC { StringBuilder stringBuilder = new StringBuilder(); LogHelper.log(Level.INFO, "***** START NODES *****"); - Iterator nodeIter = graph.iterator(); + Iterator nodeIter = graph.iterator(); while (nodeIter.hasNext()) { - CustomStackWrapper node = nodeIter.next(); + CustomWrappedStack node = nodeIter.next(); LogHelper.log(Level.INFO, "Node: " + node); } LogHelper.log(Level.INFO, "***** END NODES *****"); @@ -197,9 +196,9 @@ public class DynEMC { LogHelper.log(Level.INFO, "***** START EDGES FROM *****"); nodeIter = graph.iterator(); while (nodeIter.hasNext()) { - CustomStackWrapper node = nodeIter.next(); - Set> edgesFrom = graph.edgesFrom(node); - for (WeightedEdge edge : edgesFrom) { + CustomWrappedStack node = nodeIter.next(); + Set> edgesFrom = graph.edgesFrom(node); + for (WeightedEdge edge : edgesFrom) { LogHelper.log(Level.INFO, "Crafting Output: " + node); LogHelper.log(Level.INFO, "Crafting Input: " + edge.getTarget()); LogHelper.log(Level.INFO, "Weight: " + edge.getWeight()); diff --git a/ee3_common/com/pahimar/ee3/emc/EmcBlackList.java b/ee3_common/com/pahimar/ee3/emc/EmcBlackList.java index 68fe9ef1..f3ebc078 100644 --- a/ee3_common/com/pahimar/ee3/emc/EmcBlackList.java +++ b/ee3_common/com/pahimar/ee3/emc/EmcBlackList.java @@ -8,13 +8,13 @@ import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraftforge.oredict.OreDictionary; -import com.pahimar.ee3.item.CustomStackWrapper; +import com.pahimar.ee3.item.CustomWrappedStack; public class EmcBlackList { private static EmcBlackList emcBlackList = null; - private ArrayList stackBlackList = new ArrayList(); + private ArrayList stackBlackList = new ArrayList(); private EmcBlackList() { @@ -31,14 +31,14 @@ public class EmcBlackList { return emcBlackList; } - public List getBlackListStacks() { + public List getBlackListStacks() { return stackBlackList; } public void add(ItemStack itemStack) { - CustomStackWrapper customStackWrapper = new CustomStackWrapper(itemStack); + CustomWrappedStack customStackWrapper = new CustomWrappedStack(itemStack); if (!stackBlackList.contains(customStackWrapper)) { stackBlackList.add(customStackWrapper); @@ -72,7 +72,7 @@ public class EmcBlackList { itemStack.stackSize = 1; } - return stackBlackList.contains(new CustomStackWrapper(itemStack)); + return stackBlackList.contains(new CustomWrappedStack(itemStack)); } public boolean contains(Item item) { @@ -97,7 +97,7 @@ public class EmcBlackList { public void remove(ItemStack itemStack) { - CustomStackWrapper customStackWrapper = new CustomStackWrapper(itemStack); + CustomWrappedStack customStackWrapper = new CustomWrappedStack(itemStack); while (stackBlackList.contains(customStackWrapper)) { stackBlackList.remove(customStackWrapper); diff --git a/ee3_common/com/pahimar/ee3/emc/EquivalencyGroup.java b/ee3_common/com/pahimar/ee3/emc/EquivalencyGroup.java index 5c31dd63..b00300a9 100644 --- a/ee3_common/com/pahimar/ee3/emc/EquivalencyGroup.java +++ b/ee3_common/com/pahimar/ee3/emc/EquivalencyGroup.java @@ -5,64 +5,64 @@ import java.util.List; import net.minecraft.item.ItemStack; -import com.pahimar.ee3.item.CustomStackWrapper; +import com.pahimar.ee3.item.CustomWrappedStack; public class EquivalencyGroup { - private List equivalentItems; + private List equivalentItems; public EquivalencyGroup() { - equivalentItems = new ArrayList(); + equivalentItems = new ArrayList(); } public EquivalencyGroup(List equivalentItems) { - this.equivalentItems = new ArrayList(); + this.equivalentItems = new ArrayList(); for (ItemStack itemStack : equivalentItems) { - this.equivalentItems.add(new CustomStackWrapper(itemStack)); + this.equivalentItems.add(new CustomWrappedStack(itemStack)); } } - public List getMembers() { + public List getMembers() { return equivalentItems; } public boolean containsMember(ItemStack itemStack) { - return containsMember(new CustomStackWrapper(itemStack)); + return containsMember(new CustomWrappedStack(itemStack)); } - public boolean containsMember(CustomStackWrapper customStackWrapper) { + public boolean containsMember(CustomWrappedStack customStackWrapper) { return equivalentItems.contains(customStackWrapper); } public void addMember(ItemStack itemStack) { - this.addMember(new CustomStackWrapper(itemStack)); + this.addMember(new CustomWrappedStack(itemStack)); } - public void addMember(CustomStackWrapper customStackWrapper) { + public void addMember(CustomWrappedStack customStackWrapper) { if (!containsMember(customStackWrapper)) { equivalentItems.add(customStackWrapper); } } - public void setEquivalentItems(List equivalentItems) { + public void setEquivalentItems(List equivalentItems) { this.equivalentItems = equivalentItems; } public void removeMember(ItemStack itemStack) { - removeMember(new CustomStackWrapper(itemStack)); + removeMember(new CustomWrappedStack(itemStack)); } - public void removeMember(CustomStackWrapper customStackWrapper) { + public void removeMember(CustomWrappedStack customStackWrapper) { while (containsMember(customStackWrapper)) { equivalentItems.remove(customStackWrapper); @@ -71,7 +71,7 @@ public class EquivalencyGroup { public void clearMembers() { - equivalentItems = new ArrayList(); + equivalentItems = new ArrayList(); } @Override @@ -92,7 +92,7 @@ public class EquivalencyGroup { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("Equivalent Group Members: "); - for (CustomStackWrapper customStackWrapper : equivalentItems) { + for (CustomWrappedStack customStackWrapper : equivalentItems) { stringBuilder.append(String.format("%s ", customStackWrapper)); } @@ -104,7 +104,7 @@ public class EquivalencyGroup { int hashCode = 1; - for (CustomStackWrapper customStackWrapper : equivalentItems) { + for (CustomWrappedStack customStackWrapper : equivalentItems) { hashCode = 37 * hashCode + customStackWrapper.hashCode(); } diff --git a/ee3_common/com/pahimar/ee3/item/CustomStackWrapper.java b/ee3_common/com/pahimar/ee3/item/CustomWrappedStack.java similarity index 71% rename from ee3_common/com/pahimar/ee3/item/CustomStackWrapper.java rename to ee3_common/com/pahimar/ee3/item/CustomWrappedStack.java index 863e1057..f0222748 100644 --- a/ee3_common/com/pahimar/ee3/item/CustomStackWrapper.java +++ b/ee3_common/com/pahimar/ee3/item/CustomWrappedStack.java @@ -6,51 +6,42 @@ import net.minecraftforge.oredict.OreDictionary; import com.pahimar.ee3.core.util.ItemUtil; import com.pahimar.ee3.core.util.OreStack; -public class CustomStackWrapper { +public class CustomWrappedStack { + private int stackSize; private ItemStack itemStack; private OreStack oreStack; - public CustomStackWrapper(ItemStack itemStack) { + public CustomWrappedStack(ItemStack itemStack) { this.itemStack = itemStack; this.oreStack = null; + stackSize = itemStack.stackSize; if (this.itemStack != null) { this.itemStack.stackSize = 1; } } - public CustomStackWrapper(OreStack oreStack) { + public CustomWrappedStack(OreStack oreStack) { this.itemStack = null; this.oreStack = oreStack; + stackSize = oreStack.stackSize; if (this.oreStack != null) { this.oreStack.stackSize = 1; } } - public int getWrappedStackSize() { + public int getStackSize() { - if (itemStack != null) { - return itemStack.stackSize; - } - else if (oreStack != null) { - return oreStack.stackSize; - } - - return -1; + return stackSize; } - public void setWrappedStackSize(int stackSize) { + public void setStackSize(int stackSize) { - if (itemStack != null) { - itemStack.stackSize = stackSize; - } - else if (oreStack != null) { - oreStack.stackSize = stackSize; - } + this.stackSize = stackSize; } public ItemStack getItemStack() { @@ -66,19 +57,19 @@ public class CustomStackWrapper { @Override public boolean equals(Object object) { - if (!(object instanceof CustomStackWrapper)) { + if (!(object instanceof CustomWrappedStack)) { return false; } - CustomStackWrapper customWrappedStack = (CustomStackWrapper) object; + CustomWrappedStack customWrappedStack = (CustomWrappedStack) object; if (itemStack != null) { if (customWrappedStack.itemStack != null) { - return ItemUtil.compare(this.itemStack, customWrappedStack.itemStack); + return (ItemUtil.compare(this.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)) { + if (ItemUtil.compare(itemStack, oreDictItemStack) && stackSize == oreDictItemStack.stackSize) { return true; } } @@ -87,13 +78,13 @@ public class CustomStackWrapper { else if (oreStack != null) { if (customWrappedStack.itemStack != null) { for (ItemStack oreDictItemStack : OreDictionary.getOres(oreStack.oreName)) { - if (ItemUtil.compare(customWrappedStack.itemStack, oreDictItemStack)) { + if (ItemUtil.compare(customWrappedStack.itemStack, oreDictItemStack) && stackSize == oreDictItemStack.stackSize) { return true; } } } else if (customWrappedStack.oreStack != null) { - return oreStack.equals(customWrappedStack.oreStack); + return (oreStack.equals(customWrappedStack.oreStack) && stackSize == oreStack.stackSize); } } @@ -117,6 +108,8 @@ public class CustomStackWrapper { public int hashCode() { int hashCode = 1; + + hashCode = 37 * hashCode + stackSize; if (itemStack != null) { hashCode = 37 * hashCode + itemStack.itemID; @@ -127,12 +120,10 @@ public class CustomStackWrapper { else { hashCode = 37 * hashCode + itemStack.getItemDamage(); } - - hashCode = 37 * hashCode + itemStack.stackSize; + hashCode = 37 * hashCode + itemStack.getItemName().hashCode(); } else if (oreStack != null) { - hashCode = 37 * hashCode + oreStack.stackSize; hashCode = 37 * hashCode + oreStack.oreName.hashCode(); }