diff --git a/src/main/java/com/gregtechceu/gtceu/api/recipe/ingredient/FluidIngredient.java b/src/main/java/com/gregtechceu/gtceu/api/recipe/ingredient/FluidIngredient.java index a915c6cff6..7163a778ce 100644 --- a/src/main/java/com/gregtechceu/gtceu/api/recipe/ingredient/FluidIngredient.java +++ b/src/main/java/com/gregtechceu/gtceu/api/recipe/ingredient/FluidIngredient.java @@ -128,25 +128,23 @@ public boolean equals(Object obj) { Value[] myValues = this.values.clone(); Value[] otherValues = other.values.clone(); - Arrays.parallelSort(myValues, VALUE_COMPARATOR); - Arrays.parallelSort(otherValues, VALUE_COMPARATOR); - - for (Value value1 : myValues) { - for (Value value2 : otherValues) { - if (value1 instanceof TagValue first) { - if (!(value2 instanceof TagValue second)) { - return false; - } - if (first.tag != second.tag) { - return false; - } - } else if (value1 instanceof FluidValue first) { - if (!(value2 instanceof FluidValue second)) { - return false; - } - if (first.fluid != second.fluid) { - return false; - } + Arrays.sort(myValues, VALUE_COMPARATOR); + Arrays.sort(otherValues, VALUE_COMPARATOR); + + for (int i = 0; i < myValues.length; i++) { + if (myValues[i] instanceof TagValue first) { + if (!(otherValues[i] instanceof TagValue second)) { + return false; + } + if (first.compareTo(second) != 0) { + return false; + } + } else if (myValues[i] instanceof FluidValue first) { + if (!(otherValues[i] instanceof FluidValue second)) { + return false; + } + if (first.compareTo(second) != 0) { + return false; } } } @@ -326,7 +324,7 @@ public interface Value { JsonObject serialize(); } - public record TagValue(TagKey tag) implements Value { + public record TagValue(TagKey tag) implements Value, Comparable { @Override public Collection getFluids() { @@ -347,9 +345,14 @@ public JsonObject serialize() { jsonObject.addProperty("tag", this.tag.location().toString()); return jsonObject; } + + @Override + public int compareTo(TagValue other) { + return this.tag.location().compareTo(other.tag.location()); + } } - public record FluidValue(Fluid fluid) implements Value { + public record FluidValue(Fluid fluid) implements Value, Comparable { @Override public Collection getFluids() { @@ -362,28 +365,41 @@ public JsonObject serialize() { jsonObject.addProperty("fluid", BuiltInRegistries.FLUID.getKey(this.fluid).toString()); return jsonObject; } + + @Override + public int compareTo(FluidValue other) { + return FLUID_COMPARATOR.compare(this.fluid, other.fluid); + } } public static final Comparator FLUID_COMPARATOR = Comparator.comparing(BuiltInRegistries.FLUID::getKey); - public static final Comparator VALUE_COMPARATOR = new Comparator<>() { + /** + * Comparator for Values. First sort types: TagValue, then FluidValue, then other Values. + * Within the type groups, sort by tag or key. + * For other Values we have no comparator to check against, so we don't sort them at all. This might be a problem + * if anyone ever tries to use multiple Values that are not TagValues or FluidValues in this.value. + */ + public static final Comparator VALUE_COMPARATOR = (value1, value2) -> { + + if (value1 instanceof TagValue tagValue1) { + if (value2 instanceof TagValue tagValue2) { + return tagValue1.compareTo(tagValue2); + } else { + return 1; + } + } - @Override - public int compare(FluidIngredient.Value value1, FluidIngredient.Value value2) { - if (value1 instanceof FluidIngredient.TagValue first) { - if (!(value2 instanceof FluidIngredient.TagValue second)) { - return 1; - } - if (first.tag() != second.tag()) { - return 1; - } - } else if (value1 instanceof FluidIngredient.FluidValue first) { - if (!(value2 instanceof FluidIngredient.FluidValue second)) { - return 1; - } - return FLUID_COMPARATOR.compare(first.fluid, second.fluid); + if (value1 instanceof FluidValue fluidValue1) { + if (value2 instanceof FluidValue fluidValue2) { + return fluidValue1.compareTo(fluidValue2); + } else if (value2 instanceof TagValue) { + return -1; + } else { + return 1; } - return 0; } + + return 0; }; }