Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand Down Expand Up @@ -326,7 +324,7 @@ public interface Value {
JsonObject serialize();
}

public record TagValue(TagKey<Fluid> tag) implements Value {
public record TagValue(TagKey<Fluid> tag) implements Value, Comparable<TagValue> {

@Override
public Collection<Fluid> getFluids() {
Expand All @@ -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<FluidValue> {

@Override
public Collection<Fluid> getFluids() {
Expand All @@ -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> FLUID_COMPARATOR = Comparator.comparing(BuiltInRegistries.FLUID::getKey);

public static final Comparator<FluidIngredient.Value> 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<FluidIngredient.Value> 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;
};
}
Loading