3
0
Mirror von https://github.com/PaperMC/Velocity.git synchronisiert 2024-11-16 21:10:30 +01:00

Tiny rework of Protocol Version API (#1260)

Dieser Commit ist enthalten in:
Maxim Breitman 2024-03-02 06:42:02 +03:00 committet von GitHub
Ursprung af09677f60
Commit 82189a6a21
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: B5690EEEBB952194
3 geänderte Dateien mit 56 neuen und 29 gelöschten Zeilen

Datei anzeigen

@ -21,8 +21,28 @@ import java.util.Set;
* Represents each Minecraft protocol version. * Represents each Minecraft protocol version.
*/ */
public enum ProtocolVersion implements Ordered<ProtocolVersion> { public enum ProtocolVersion implements Ordered<ProtocolVersion> {
UNKNOWN(-1, "Unknown"), UNKNOWN(-1, "Unknown") {
LEGACY(-2, "Legacy"), @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, MINECRAFT_1_7_2(4,
"1.7.2", "1.7.3", "1.7.4", "1.7.5"), "1.7.2", "1.7.3", "1.7.4", "1.7.5"),
MINECRAFT_1_7_6(5, MINECRAFT_1_7_6(5,
@ -118,7 +138,7 @@ public enum ProtocolVersion implements Ordered<ProtocolVersion> {
static { static {
Set<ProtocolVersion> versions = EnumSet.noneOf(ProtocolVersion.class); Set<ProtocolVersion> versions = EnumSet.noneOf(ProtocolVersion.class);
for (ProtocolVersion value : values()) { for (ProtocolVersion value : values()) {
if (!value.isUnknown() && !value.isLegacy()) { if (value.isSupported()) {
versions.add(value); versions.add(value);
} }
} }
@ -191,6 +211,35 @@ public enum ProtocolVersion implements Ordered<ProtocolVersion> {
return ImmutableList.copyOf(names); return ImmutableList.copyOf(names);
} }
/**
* Returns whether this {@link ProtocolVersion} is supported.
*
* @return if the protocol supported
*/
public boolean isSupported() {
return true;
}
/**
* Returns whether the protocol is supported.
*
* @param protocol the protocol as an int
* @return if the protocol supported
*/
public static boolean isSupported(int protocol) {
return getProtocolVersion(protocol).isSupported();
}
/**
* Returns whether the {@link ProtocolVersion} is supported.
*
* @param version the protocol version
* @return if the protocol supported
*/
public static boolean isSupported(ProtocolVersion version) {
return version != null && version.isSupported();
}
/** /**
* Gets the {@link ProtocolVersion} for the given protocol. * Gets the {@link ProtocolVersion} for the given protocol.
* *
@ -201,35 +250,13 @@ public enum ProtocolVersion implements Ordered<ProtocolVersion> {
return ID_TO_PROTOCOL_CONSTANT.getOrDefault(protocol, UNKNOWN); return ID_TO_PROTOCOL_CONSTANT.getOrDefault(protocol, UNKNOWN);
} }
/**
* Returns whether the protocol is supported.
*
* @param protocol the protocol as an int
* @return if the protocol supported
*/
public static boolean isSupported(int protocol) {
ProtocolVersion version = ID_TO_PROTOCOL_CONSTANT.get(protocol);
return version != null && !version.isUnknown();
}
/**
* Returns whether the {@link ProtocolVersion} is supported.
*
* @param version the protocol version
* @return if the protocol supported
*/
public static boolean isSupported(ProtocolVersion version) {
return version != null && !version.isUnknown();
}
/** /**
* Returns whether this {@link ProtocolVersion} is unknown to the proxy. * Returns whether this {@link ProtocolVersion} is unknown to the proxy.
* *
* @return if the protocol is unknown * @return if the protocol is unknown
*/ */
public boolean isUnknown() { public boolean isUnknown() {
return this == UNKNOWN; return false;
} }
/** /**
@ -238,7 +265,7 @@ public enum ProtocolVersion implements Ordered<ProtocolVersion> {
* @return if the protocol is legacy * @return if the protocol is legacy
*/ */
public boolean isLegacy() { public boolean isLegacy() {
return this == LEGACY; return false;
} }
@Override @Override

Datei anzeigen

@ -117,7 +117,7 @@ public class HandshakeSessionHandler implements MinecraftSessionHandler {
} }
private void handleLogin(HandshakePacket handshake, InitialInboundConnection ic) { 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. // Bump connection into correct protocol state so that we can send the disconnect packet.
connection.setState(StateRegistry.LOGIN); connection.setState(StateRegistry.LOGIN);
ic.disconnectQuietly(Component.translatable() ic.disconnectQuietly(Component.translatable()

Datei anzeigen

@ -143,7 +143,7 @@ public class ServerListPingHandler {
*/ */
public CompletableFuture<ServerPing> getInitialPing(VelocityInboundConnection connection) { public CompletableFuture<ServerPing> getInitialPing(VelocityInboundConnection connection) {
VelocityConfiguration configuration = server.getConfiguration(); VelocityConfiguration configuration = server.getConfiguration();
ProtocolVersion shownVersion = ProtocolVersion.isSupported(connection.getProtocolVersion()) ProtocolVersion shownVersion = connection.getProtocolVersion().isSupported()
? connection.getProtocolVersion() : ProtocolVersion.MAXIMUM_VERSION; ? connection.getProtocolVersion() : ProtocolVersion.MAXIMUM_VERSION;
PingPassthroughMode passthroughMode = configuration.getPingPassthrough(); PingPassthroughMode passthroughMode = configuration.getPingPassthrough();