Rework and expose ProtocolVersion to the API

This commit is contained in:
Crypnotic
2018-11-15 02:29:58 -06:00
parent 74ee716480
commit 6b4c809c39
52 changed files with 423 additions and 309 deletions

View File

@@ -0,0 +1,138 @@
package com.velocitypowered.api.network;
import com.google.common.collect.ImmutableMap;
import com.velocitypowered.api.proxy.server.ServerInfo;
import java.util.HashMap;
import java.util.Map;
/**
* Represents each Minecraft protocol version
*/
public enum ProtocolVersion {
UNKNOWN(-1, "Unknown"),
LEGACY(-1, "Legacy"),
MINECRAFT_1_8(47, "1.8"),
MINECRAFT_1_9(107, "1.9"),
MINECRAFT_1_9_1(108, "1.9.1"),
MINECRAFT_1_9_2(109, "1.9.2"),
MINECRAFT_1_9_4(110, "1.9.4"),
MINECRAFT_1_10(210, "1.10"),
MINECRAFT_1_11(315, "1.11"),
MINECRAFT_1_11_1(316, "1.11.1"),
MINECRAFT_1_12(335, "1.12"),
MINECRAFT_1_12_1(338, "1.12.1"),
MINECRAFT_1_12_2(340, "1.12.2"),
MINECRAFT_1_13(393, "1.13"),
MINECRAFT_1_13_1(401, "1.13.1"),
MINECRAFT_1_13_2(404, "1.13.2");
private final int protocol;
private final String name;
/**
* Represents the lowest supported version
*/
public static final ProtocolVersion MINIMUM_VERSION = MINECRAFT_1_8;
/**
* Represents the highest supported version
*/
public static final ProtocolVersion MAXIMUM_VERSION = MINECRAFT_1_13_2;
/**
* The user-friendly representation of the lowest and highest supported versions
*/
public static final String SUPPORTED_VERSION_STRING = String.format("%s-%s", MINIMUM_VERSION, MAXIMUM_VERSION);
/**
* A map linking the protocol version number to its {@link ProtocolVersion} representation
*/
public static final ImmutableMap<Integer, ProtocolVersion> ID_TO_PROTOCOL_CONSTANT;
static {
Map<Integer, ProtocolVersion> versions = new HashMap<>();
for (ProtocolVersion version : values()) {
versions.put(version.protocol, version);
}
ID_TO_PROTOCOL_CONSTANT = ImmutableMap.copyOf(versions);
}
ProtocolVersion(int protocol, String name) {
this.protocol = protocol;
this.name = name;
}
/**
* Returns the protocol as an int
*
* @return the protocol version
*/
public int getProtocol() {
return protocol;
}
/**
* Returns the user-friendly name for this protocol
*
* @return the protocol name
*/
public String getName() {
return name;
}
/**
* 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);
}
/**
* 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
*
* @return if the protocol is unknown
*/
public boolean isUnknown() {
return this == UNKNOWN;
}
/**
* Returns whether this {@link ProtocolVersion} is a legacy protocol
*
* @return if the protocol is legacy
*/
public boolean isLegacy() {
return this == LEGACY;
}
@Override
public String toString() {
return name;
}
}

View File

@@ -1,5 +1,7 @@
package com.velocitypowered.api.proxy;
import com.velocitypowered.api.network.ProtocolVersion;
import java.net.InetSocketAddress;
import java.util.Optional;
@@ -34,5 +36,5 @@ public interface InboundConnection {
*
* @return the protocol version the connection uses
*/
int getProtocolVersion();
ProtocolVersion getProtocolVersion();
}

View File

@@ -10,6 +10,8 @@ import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import com.velocitypowered.api.network.ProtocolVersion;
import net.kyori.text.Component;
import org.checkerframework.checker.nullness.qual.Nullable;