Skip to content

Commit

Permalink
Tiny rework of Protocol Version API (PaperMC#1260)
Browse files Browse the repository at this point in the history
  • Loading branch information
breitwan authored Mar 2, 2024
1 parent af09677 commit 82189a6
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,28 @@
* Represents each Minecraft protocol version.
*/
public enum ProtocolVersion implements Ordered<ProtocolVersion> {
UNKNOWN(-1, "Unknown"),
LEGACY(-2, "Legacy"),
UNKNOWN(-1, "Unknown") {
@Override
public boolean isUnknown() {
return true;
}

@Override
public boolean isSupported() {
return false;
}
},
LEGACY(-2, "Legacy") {
@Override
public boolean isLegacy() {
return true;
}

@Override
public boolean isSupported() {
return false;
}
},
MINECRAFT_1_7_2(4,
"1.7.2", "1.7.3", "1.7.4", "1.7.5"),
MINECRAFT_1_7_6(5,
Expand Down Expand Up @@ -118,7 +138,7 @@ public enum ProtocolVersion implements Ordered<ProtocolVersion> {
static {
Set<ProtocolVersion> versions = EnumSet.noneOf(ProtocolVersion.class);
for (ProtocolVersion value : values()) {
if (!value.isUnknown() && !value.isLegacy()) {
if (value.isSupported()) {
versions.add(value);
}
}
Expand Down Expand Up @@ -192,13 +212,12 @@ public List<String> getVersionsSupportedBy() {
}

/**
* Gets the {@link ProtocolVersion} for the given protocol.
* Returns whether this {@link ProtocolVersion} is supported.
*
* @param protocol the protocol as an int
* @return the protocol version
* @return if the protocol supported
*/
public static ProtocolVersion getProtocolVersion(int protocol) {
return ID_TO_PROTOCOL_CONSTANT.getOrDefault(protocol, UNKNOWN);
public boolean isSupported() {
return true;
}

/**
Expand All @@ -208,9 +227,7 @@ public static ProtocolVersion getProtocolVersion(int protocol) {
* @return if the protocol supported
*/
public static boolean isSupported(int protocol) {
ProtocolVersion version = ID_TO_PROTOCOL_CONSTANT.get(protocol);

return version != null && !version.isUnknown();
return getProtocolVersion(protocol).isSupported();
}

/**
Expand All @@ -220,7 +237,17 @@ public static boolean isSupported(int protocol) {
* @return if the protocol supported
*/
public static boolean isSupported(ProtocolVersion version) {
return version != null && !version.isUnknown();
return version != null && version.isSupported();
}

/**
* Gets the {@link ProtocolVersion} for the given protocol.
*
* @param protocol the protocol as an int
* @return the protocol version
*/
public static ProtocolVersion getProtocolVersion(int protocol) {
return ID_TO_PROTOCOL_CONSTANT.getOrDefault(protocol, UNKNOWN);
}

/**
Expand All @@ -229,7 +256,7 @@ public static boolean isSupported(ProtocolVersion version) {
* @return if the protocol is unknown
*/
public boolean isUnknown() {
return this == UNKNOWN;
return false;
}

/**
Expand All @@ -238,7 +265,7 @@ public boolean isUnknown() {
* @return if the protocol is legacy
*/
public boolean isLegacy() {
return this == LEGACY;
return false;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public boolean handle(final HandshakePacket handshake) {
}

private void handleLogin(HandshakePacket handshake, InitialInboundConnection ic) {
if (!ProtocolVersion.isSupported(handshake.getProtocolVersion())) {
if (!handshake.getProtocolVersion().isSupported()) {
// Bump connection into correct protocol state so that we can send the disconnect packet.
connection.setState(StateRegistry.LOGIN);
ic.disconnectQuietly(Component.translatable()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ private CompletableFuture<ServerPing> attemptPingPassthrough(VelocityInboundConn
*/
public CompletableFuture<ServerPing> getInitialPing(VelocityInboundConnection connection) {
VelocityConfiguration configuration = server.getConfiguration();
ProtocolVersion shownVersion = ProtocolVersion.isSupported(connection.getProtocolVersion())
ProtocolVersion shownVersion = connection.getProtocolVersion().isSupported()
? connection.getProtocolVersion() : ProtocolVersion.MAXIMUM_VERSION;
PingPassthroughMode passthroughMode = configuration.getPingPassthrough();

Expand Down

0 comments on commit 82189a6

Please sign in to comment.