From 09f484f95670d83018d198fa23fb22ddec16e1db Mon Sep 17 00:00:00 2001 From: Hugo Kerstens Date: Thu, 31 Mar 2016 19:50:04 +0200 Subject: [PATCH 1/2] Implement new protocol version registry --- .../api/protocol/ProtocolPipeline.java | 6 +-- .../api/protocol/ProtocolRegistry.java | 4 +- .../api/protocol/ProtocolVersion.java | 47 ++++++++++++++++--- .../us/myles/ViaVersion/boss/ViaBossBar.java | 2 +- .../commands/defaultsubs/ListSubCmd.java | 3 +- 5 files changed, 48 insertions(+), 14 deletions(-) diff --git a/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolPipeline.java b/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolPipeline.java index a51beaf52..f7a4f7a19 100644 --- a/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolPipeline.java +++ b/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolPipeline.java @@ -82,11 +82,11 @@ public class ProtocolPipeline extends Protocol { String packet = "UNKNOWN"; // For 1.8/1.9 server version, eventually we'll probably get an API for this... - if (ProtocolRegistry.SERVER_PROTOCOL >= ProtocolVersion.V1_8 && - ProtocolRegistry.SERVER_PROTOCOL <= ProtocolVersion.V1_9_2) { + if (ProtocolRegistry.SERVER_PROTOCOL >= ProtocolVersion.v1_8.getId() && + ProtocolRegistry.SERVER_PROTOCOL <= ProtocolVersion.v1_9_2.getId()) { PacketType type; - if (ProtocolRegistry.SERVER_PROTOCOL == ProtocolVersion.V1_8) { + if (ProtocolRegistry.SERVER_PROTOCOL == ProtocolVersion.v1_8.getId()) { if (direction == Direction.INCOMING) { type = PacketType.findNewPacket(state, direction, originalID); } else { diff --git a/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolRegistry.java b/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolRegistry.java index 81fe8e874..f11e3c84b 100644 --- a/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolRegistry.java +++ b/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolRegistry.java @@ -14,8 +14,8 @@ public class ProtocolRegistry { static { // Register built in protocols - registerProtocol(new Protocol1_9TO1_8(), Collections.singletonList(ProtocolVersion.V1_9), ProtocolVersion.V1_8); - registerProtocol(new Protocol1_9_1TO1_9(), Arrays.asList(ProtocolVersion.V1_9_1, ProtocolVersion.V1_9_2), ProtocolVersion.V1_9); + registerProtocol(new Protocol1_9TO1_8(), Collections.singletonList(ProtocolVersion.v1_9.getId()), ProtocolVersion.v1_8.getId()); + registerProtocol(new Protocol1_9_1TO1_9(), Arrays.asList(ProtocolVersion.v1_9_1.getId(), ProtocolVersion.v1_9_2.getId()), ProtocolVersion.v1_9.getId()); } /** diff --git a/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolVersion.java b/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolVersion.java index c0221ff73..1bae3a5b9 100644 --- a/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolVersion.java +++ b/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolVersion.java @@ -1,13 +1,46 @@ package us.myles.ViaVersion.api.protocol; +import lombok.Data; +import lombok.NonNull; + +import java.util.*; + +@Data public class ProtocolVersion { - /* Defined protocol constants */ - public static final int V1_7_1 = 4; - public static final int V1_7_6 = 5; + private static final Map versions = new HashMap<>(); - public static final int V1_8 = 47; + public static final ProtocolVersion v1_7_1; + public static final ProtocolVersion v1_7_6; + public static final ProtocolVersion v1_8; + public static final ProtocolVersion v1_9; + public static final ProtocolVersion v1_9_1; + public static final ProtocolVersion v1_9_2; - public static final int V1_9 = 107; - public static final int V1_9_1 = 108; // used for 1.9.1 - public static final int V1_9_2 = 109; + private final int id; + private final String name; + + static { + register(v1_7_1 = new ProtocolVersion(4, "1.7-1.7.5")); + register(v1_7_6 = new ProtocolVersion(5, "1.7.6-1.7.10")); + register(v1_8 = new ProtocolVersion(4, "1.8.x")); + register(v1_9 = new ProtocolVersion(107, "1.9")); + register(v1_9_1 = new ProtocolVersion(108, "1.9.1")); + register(v1_9_2 = new ProtocolVersion(109, "1.9.2")); + } + + public static void register(@NonNull ProtocolVersion protocol) { + versions.put(protocol.getId(), protocol); + } + + public static boolean isRegistered(int id) { + return versions.containsKey(id); + } + + public static ProtocolVersion getProtocol(int id) { + return versions.get(id); + } + + public static List getProtocols() { + return Collections.unmodifiableList(new ArrayList(versions.values())); + } } diff --git a/src/main/java/us/myles/ViaVersion/boss/ViaBossBar.java b/src/main/java/us/myles/ViaVersion/boss/ViaBossBar.java index 076b7ef13..39c6202c6 100644 --- a/src/main/java/us/myles/ViaVersion/boss/ViaBossBar.java +++ b/src/main/java/us/myles/ViaVersion/boss/ViaBossBar.java @@ -154,7 +154,7 @@ public class ViaBossBar implements BossBar { } private void sendPacket(UUID uuid, ByteBuf buf) { - if (!ViaVersion.getInstance().isPorted(uuid) || !(ViaVersion.getInstance().getPlayerVersion(uuid) >= ProtocolVersion.V1_9)) { + if (!ViaVersion.getInstance().isPorted(uuid) || !(ViaVersion.getInstance().getPlayerVersion(uuid) >= ProtocolVersion.v1_9.getId())) { players.remove(uuid); return; } diff --git a/src/main/java/us/myles/ViaVersion/commands/defaultsubs/ListSubCmd.java b/src/main/java/us/myles/ViaVersion/commands/defaultsubs/ListSubCmd.java index af5605b57..35ac81994 100644 --- a/src/main/java/us/myles/ViaVersion/commands/defaultsubs/ListSubCmd.java +++ b/src/main/java/us/myles/ViaVersion/commands/defaultsubs/ListSubCmd.java @@ -5,6 +5,7 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import us.myles.ViaVersion.api.ViaVersion; import us.myles.ViaVersion.api.command.ViaSubCommand; +import us.myles.ViaVersion.api.protocol.ProtocolVersion; import java.util.*; @@ -38,7 +39,7 @@ public class ListSubCmd extends ViaSubCommand { Map> sorted = new TreeMap<>(playerVersions); for (Map.Entry> entry : sorted.entrySet()) - sender.sendMessage(String.format(color("&8[&6%s&8]: &b%s"), entry.getKey(), entry.getValue())); //TODO: Make versions like [1.8,1.9,1.9.1,1.9.2,etc] instead of protocol id + sender.sendMessage(String.format(color("&8[&6%s&8]: &b%s"), ProtocolVersion.getProtocol(entry.getKey()).getName(), entry.getValue())); sorted.clear(); return true; From 0a69d62cbe2087216dc84a5beb7fca605c63781a Mon Sep 17 00:00:00 2001 From: Hugo Kerstens Date: Thu, 31 Mar 2016 23:26:13 +0200 Subject: [PATCH 2/2] Fixed typo (blame Mats) --- .../java/us/myles/ViaVersion/api/protocol/ProtocolVersion.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolVersion.java b/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolVersion.java index 1bae3a5b9..b8bcf6c01 100644 --- a/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolVersion.java +++ b/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolVersion.java @@ -22,7 +22,7 @@ public class ProtocolVersion { static { register(v1_7_1 = new ProtocolVersion(4, "1.7-1.7.5")); register(v1_7_6 = new ProtocolVersion(5, "1.7.6-1.7.10")); - register(v1_8 = new ProtocolVersion(4, "1.8.x")); + register(v1_8 = new ProtocolVersion(47, "1.8.x")); register(v1_9 = new ProtocolVersion(107, "1.9")); register(v1_9_1 = new ProtocolVersion(108, "1.9.1")); register(v1_9_2 = new ProtocolVersion(109, "1.9.2"));