Tiny rework of Protocol Version API (#1260)

This commit is contained in:
Maxim Breitman
2024-03-02 06:42:02 +03:00
committed by GitHub
parent af09677f60
commit 82189a6a21
3 changed files with 56 additions and 29 deletions

View File

@@ -21,8 +21,28 @@ import java.util.Set;
* 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,
@@ -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);
}
}
@@ -191,6 +211,35 @@ public enum ProtocolVersion implements Ordered<ProtocolVersion> {
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.
*
@@ -201,35 +250,13 @@ public enum ProtocolVersion implements Ordered<ProtocolVersion> {
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;
return false;
}
/**
@@ -238,7 +265,7 @@ public enum ProtocolVersion implements Ordered<ProtocolVersion> {
* @return if the protocol is legacy
*/
public boolean isLegacy() {
return this == LEGACY;
return false;
}
@Override