From 35578372a1d6d4b82163d9793ce7ace205136af8 Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Wed, 14 Feb 2024 22:45:34 +0100 Subject: [PATCH] Fix isWorkingPipe and cursed ProtocolVersion comparator --- .../api/protocol/version/ProtocolVersion.java | 19 ++++++++++--------- .../protocol/ProtocolManagerImpl.java | 2 +- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/api/src/main/java/com/viaversion/viaversion/api/protocol/version/ProtocolVersion.java b/api/src/main/java/com/viaversion/viaversion/api/protocol/version/ProtocolVersion.java index 05b5dd8fe..aa0beb754 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/protocol/version/ProtocolVersion.java +++ b/api/src/main/java/com/viaversion/viaversion/api/protocol/version/ProtocolVersion.java @@ -84,7 +84,7 @@ public class ProtocolVersion implements Comparable { public static final ProtocolVersion v1_20_2 = register(764, "1.20.2"); public static final ProtocolVersion v1_20_3 = register(765, "1.20.3/1.20.4", new SubVersionRange("1.20", 3, 4)); public static final ProtocolVersion v1_20_5 = register(766, 177, "1.20.5"); - public static final ProtocolVersion unknown = register(-1, "UNKNOWN"); + public static final ProtocolVersion unknown = new ProtocolVersion(VersionType.SPECIAL, -1, -1, "UNKNOWN", null); public static ProtocolVersion register(int version, String name) { return register(version, -1, name); @@ -108,10 +108,8 @@ public class ProtocolVersion implements Comparable { * @param protocolVersion protocol version to register */ public static void register(ProtocolVersion protocolVersion) { - if (protocolVersion != unknown) { - VERSION_LIST.add(protocolVersion); - VERSION_LIST.sort(ProtocolVersion::compareTo); - } + VERSION_LIST.add(protocolVersion); + VERSION_LIST.sort(ProtocolVersion::compareTo); final Int2ObjectMap versions = VERSIONS.computeIfAbsent(protocolVersion.versionType, $ -> new Int2ObjectOpenHashMap<>()); versions.put(protocolVersion.version, protocolVersion); @@ -463,11 +461,14 @@ public class ProtocolVersion implements Comparable { @Override public int compareTo(final ProtocolVersion other) { - if (customComparator() != null) { + // Cursed custom comparators + if (this.versionType == VersionType.SPECIAL && customComparator() != null) { return customComparator().compare(this, other); - } else if (other.customComparator() != null) { - return other.customComparator().compare(other, this); - } else if (this.versionType != other.versionType) { + } else if (other.versionType == VersionType.SPECIAL && other.customComparator() != null) { + return other.customComparator().compare(this, other); + } + + if (this.versionType != other.versionType) { // Compare by version type first since version ids have reset multiple times return this.versionType.ordinal() < other.versionType.ordinal() ? -1 : 1; } else if (this.version != other.version) { diff --git a/common/src/main/java/com/viaversion/viaversion/protocol/ProtocolManagerImpl.java b/common/src/main/java/com/viaversion/viaversion/protocol/ProtocolManagerImpl.java index 33bbea7ad..e7899c7b0 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocol/ProtocolManagerImpl.java +++ b/common/src/main/java/com/viaversion/viaversion/protocol/ProtocolManagerImpl.java @@ -386,7 +386,7 @@ public class ProtocolManagerImpl implements ProtocolManager { public boolean isWorkingPipe() { for (Object2ObjectMap map : registryMap.values()) { for (ProtocolVersion protocolVersion : serverProtocolVersion.supportedProtocolVersions()) { - if (map.containsKey(protocolVersion.getVersion())) { + if (map.containsKey(protocolVersion)) { return true; } }