Skip to content

Commit 3accd38

Browse files
Add ProtocolVersionList#isCloserTo
1 parent 054ec10 commit 3accd38

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/main/java/net/raphimc/vialoader/util/ProtocolVersionList.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,21 @@ public static List<ProtocolVersion> getProtocolsNewToOld() {
3131
return Collections.unmodifiableList(protocolVersions);
3232
}
3333

34+
/**
35+
* Returns true if first is closer to version than second
36+
*
37+
* @param version The version to compare to
38+
* @param first The first version
39+
* @param second The second version
40+
* @return true if first is closer to version than second
41+
*/
42+
public static boolean isCloserTo(final ProtocolVersion version, final ProtocolVersion first, final ProtocolVersion second) {
43+
if (version.getVersionType() == first.getVersionType() || version.getVersionType() == second.getVersionType()) {
44+
return Math.abs(version.getVersion() - first.getVersion()) < Math.abs(version.getVersion() - second.getVersion());
45+
} else {
46+
final int ordinal = version.getVersionType().ordinal();
47+
return Math.abs(ordinal - first.getVersionType().ordinal()) < Math.abs(ordinal - second.getVersionType().ordinal());
48+
}
49+
}
50+
3451
}

0 commit comments

Comments
 (0)