fix(resource-pack-entry-widgets): utilize hash codes for less predictable ordering
This commit is contained in:
parent
241b5b2d5d
commit
4e87ddf84f
@ -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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user