fix(resource-pack-entry-widgets): utilize hash codes for less predictable ordering
All checks were successful
ci/woodpecker/push/docs Pipeline was successful
ci/woodpecker/push/jfmod Pipeline was successful

This commit is contained in:
Johannes Frohnmeyer 2024-07-20 17:27:03 +02:00
parent 241b5b2d5d
commit 4e87ddf84f
Signed by: Johannes
GPG Key ID: E76429612C2929F4

View File

@ -4,12 +4,31 @@ import io.gitlab.jfronny.libjf.entrywidgets.api.v0.ResourcePackEntryWidget;
import net.fabricmc.loader.api.entrypoint.EntrypointContainer;
import java.util.Comparator;
import java.util.function.Function;
/**
* Shuffles the entrypoints in the resource pack entry widget.
* Although the order is (intentionally) not guaranteed, it is consistent.
*/
public class EntrypointComparator implements Comparator<EntrypointContainer<ResourcePackEntryWidget>> {
@Override
public int compare(EntrypointContainer<ResourcePackEntryWidget> o1, EntrypointContainer<ResourcePackEntryWidget> o2) {
return Comparator.<EntrypointContainer<ResourcePackEntryWidget>, String>comparing(entrypoint -> entrypoint.getProvider().getMetadata().getId())
.thenComparing(s -> s.getEntrypoint().getClass().getName())
.compare(o1, o2);
return compareByValues(o1, o2,
s -> s.getProvider().getMetadata().getId().hashCode(),
s -> s.getProvider().getMetadata().getId(),
s -> s.getEntrypoint().getClass().getName()
);
}
@SuppressWarnings({"rawtypes", "unchecked"})
@SafeVarargs
private static <T> int compareByValues(T left, T right, Function<T, Comparable>... extractors) {
for (Function<T, Comparable> extractor : extractors) {
int result = extractor.apply(left).compareTo(extractor.apply(right));
if (result != 0) {
return result;
}
}
return 0;
}
}