From 8466e4458fb7f99efdddeb837b8a3a0ba1de2db4 Mon Sep 17 00:00:00 2001 From: gishicrafter Date: Sun, 11 Nov 2012 18:52:30 +0900 Subject: [PATCH 1/2] Made RefineryRecipe sortable --- .../api/recipes/RefineryRecipe.java | 86 +++++++++++++++++-- 1 file changed, 81 insertions(+), 5 deletions(-) diff --git a/common/buildcraft/api/recipes/RefineryRecipe.java b/common/buildcraft/api/recipes/RefineryRecipe.java index 44ec2aaf..dc7374b8 100644 --- a/common/buildcraft/api/recipes/RefineryRecipe.java +++ b/common/buildcraft/api/recipes/RefineryRecipe.java @@ -9,15 +9,16 @@ package buildcraft.api.recipes; -import java.util.LinkedList; +import java.util.SortedSet; +import java.util.TreeSet; import net.minecraftforge.liquids.LiquidStack; -public class RefineryRecipe { +public class RefineryRecipe implements Comparable { - private static LinkedList recipes = new LinkedList(); + private static SortedSet recipes = new TreeSet(); public static void registerRefineryRecipe(RefineryRecipe recipe) { if (!recipes.contains(recipe)) { @@ -45,8 +46,24 @@ public class RefineryRecipe { this(new LiquidStack(ingredientId1, ingredientQty1, 0), new LiquidStack(ingredientId2, ingredientQty2, 0), new LiquidStack(resultId, resultQty, 0), energy, delay); } public RefineryRecipe(LiquidStack ingredient1, LiquidStack ingredient2, LiquidStack result, int energy, int delay) { - this.ingredient1 = ingredient1; - this.ingredient2 = ingredient2; + + // Sort starting materials. + if(ingredient1 != null && ingredient2 != null) { + if( (ingredient1.itemID > ingredient2.itemID) || (ingredient1.itemID == ingredient2.itemID && ingredient1.itemMeta > ingredient2.itemMeta) ) { + this.ingredient1 = ingredient2; + this.ingredient2 = ingredient1; + } else { + this.ingredient1 = ingredient1; + this.ingredient2 = ingredient2; + } + } else if(ingredient2 != null) { + this.ingredient1 = ingredient2; + this.ingredient2 = ingredient1; + } else { + this.ingredient1 = ingredient1; + this.ingredient2 = ingredient2; + } + this.result = result; this.energy = energy; this.delay = delay; @@ -77,4 +94,63 @@ public class RefineryRecipe { return false; } + + // Compares to only the types of source materials. + // We consider non-null < null in order that one-ingredient recipe is checked after + // the failure of matching two-ingredient recipes which include that liquid. + + public int compareTo(RefineryRecipe other) { + + if(other == null) { + return -1; + } else if (ingredient1 == null) { + if(other.ingredient1 == null) { + return 0; + } else { + return 1; + } + } else if(other.ingredient1 == null) { + return -1; + } else if(ingredient1.itemID != other.ingredient1.itemID) { + return ingredient1.itemID - other.ingredient1.itemID; + } else if(ingredient1.itemMeta != other.ingredient1.itemMeta) { + return ingredient1.itemMeta - other.ingredient1.itemMeta; + } else if(ingredient2 == null) { + if(other.ingredient2 == null) { + return 0; + } else { + return 1; + } + } else if(other.ingredient2 == null) { + return -1; + } else if(ingredient2.itemID != other.ingredient2.itemID) { + return ingredient2.itemID - other.ingredient2.itemID; + } else if(ingredient2.itemMeta != other.ingredient2.itemMeta) { + return ingredient2.itemMeta - other.ingredient2.itemMeta; + } + + return 0; + } + + + // equals() should be consistent with compareTo(). + + public boolean equals(Object obj) { + if(obj != null && obj instanceof RefineryRecipe) { + return this.compareTo((RefineryRecipe)obj) == 0; + } + return false; + } + + // hashCode() should be overridden because equals() was overridden. + + public int hashCode() { + if(ingredient1 == null) { + return 0; + } else if(ingredient2 == null) { + return ingredient1.itemID ^ ingredient1.itemMeta; + } + + return ingredient1.itemID ^ ingredient1.itemMeta ^ ingredient2.itemID ^ ingredient2.itemMeta; + } } From 4a485a0d98d1512d0a498ff3134bccdd05d5051a Mon Sep 17 00:00:00 2001 From: gishicrafter Date: Mon, 12 Nov 2012 06:43:24 +0900 Subject: [PATCH 2/2] added @override --- common/buildcraft/api/recipes/RefineryRecipe.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/common/buildcraft/api/recipes/RefineryRecipe.java b/common/buildcraft/api/recipes/RefineryRecipe.java index dc7374b8..3b9caa98 100644 --- a/common/buildcraft/api/recipes/RefineryRecipe.java +++ b/common/buildcraft/api/recipes/RefineryRecipe.java @@ -98,7 +98,7 @@ public class RefineryRecipe implements Comparable { // Compares to only the types of source materials. // We consider non-null < null in order that one-ingredient recipe is checked after // the failure of matching two-ingredient recipes which include that liquid. - + @Override public int compareTo(RefineryRecipe other) { if(other == null) { @@ -134,7 +134,7 @@ public class RefineryRecipe implements Comparable { // equals() should be consistent with compareTo(). - + @Override public boolean equals(Object obj) { if(obj != null && obj instanceof RefineryRecipe) { return this.compareTo((RefineryRecipe)obj) == 0; @@ -143,7 +143,7 @@ public class RefineryRecipe implements Comparable { } // hashCode() should be overridden because equals() was overridden. - + @Override public int hashCode() { if(ingredient1 == null) { return 0;