From 44e8056fb3e96a2eab3b9583dbd45aff931eff5b Mon Sep 17 00:00:00 2001 From: Myles Date: Sun, 19 Jun 2016 22:35:49 +0100 Subject: [PATCH] Add more versions & use ProtocolSupport API if not ported. --- .../us/myles/ViaVersion/ViaVersionPlugin.java | 14 ++++++-- .../myles/ViaVersion/api/ViaVersionAPI.java | 4 ++- .../api/protocol/ProtocolVersion.java | 18 ++++++++++ .../ViaVersion/util/ProtocolSupportUtil.java | 33 +++++++++++++++++++ 4 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 src/main/java/us/myles/ViaVersion/util/ProtocolSupportUtil.java diff --git a/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java b/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java index e16e25321..19a3939bc 100644 --- a/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java +++ b/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java @@ -9,7 +9,6 @@ import lombok.Getter; import lombok.NonNull; import org.bukkit.Bukkit; import org.bukkit.entity.Player; -import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.java.JavaPlugin; import us.myles.ViaVersion.api.Pair; @@ -29,6 +28,7 @@ import us.myles.ViaVersion.protocols.base.ProtocolInfo; import us.myles.ViaVersion.update.UpdateListener; import us.myles.ViaVersion.update.UpdateUtil; import us.myles.ViaVersion.util.ListWrapper; +import us.myles.ViaVersion.util.ProtocolSupportUtil; import us.myles.ViaVersion.util.ReflectionUtil; import java.lang.reflect.Field; @@ -338,17 +338,25 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaVersionAPI { @Override public int getPlayerVersion(@NonNull Player player) { if (!isPorted(player)) - return ProtocolRegistry.SERVER_PROTOCOL; + return getExternalVersion(player); return portedPlayers.get(player.getUniqueId()).get(ProtocolInfo.class).getProtocolVersion(); } @Override public int getPlayerVersion(@NonNull UUID uuid) { if (!isPorted(uuid)) - return ProtocolRegistry.SERVER_PROTOCOL; + return getExternalVersion(Bukkit.getPlayer(uuid)); return portedPlayers.get(uuid).get(ProtocolInfo.class).getProtocolVersion(); } + private int getExternalVersion(Player player) { + if (!isProtocolSupport()) { + return ProtocolRegistry.SERVER_PROTOCOL; + } else { + return ProtocolSupportUtil.getProtocolVersion(player); + } + } + @Override public boolean isPorted(UUID playerUUID) { return portedPlayers.containsKey(playerUUID); diff --git a/src/main/java/us/myles/ViaVersion/api/ViaVersionAPI.java b/src/main/java/us/myles/ViaVersion/api/ViaVersionAPI.java index 5e2dca295..57743784d 100644 --- a/src/main/java/us/myles/ViaVersion/api/ViaVersionAPI.java +++ b/src/main/java/us/myles/ViaVersion/api/ViaVersionAPI.java @@ -21,6 +21,7 @@ public interface ViaVersionAPI { /** * Get protocol number from a player + * Will also retrieve version from ProtocolSupport if it's being used. * * @param player Bukkit player object * @return Protocol ID, For example (47=1.8-1.8.8, 107=1.9, 108=1.9.1) @@ -120,8 +121,9 @@ public interface ViaVersionAPI { /** * Gets if the server uses spigot - * + *

* Note: Will only work after ViaVersion load + * * @return True if spigot */ boolean isSpigot(); 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 2fb6c0468..bc2ce650b 100644 --- a/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolVersion.java +++ b/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolVersion.java @@ -9,6 +9,13 @@ import java.util.*; public class ProtocolVersion { private static final Map versions = new HashMap<>(); + public static final ProtocolVersion v1_4_6; + public static final ProtocolVersion v1_5_1; + public static final ProtocolVersion v1_5_2; + public static final ProtocolVersion v_1_6_1; + public static final ProtocolVersion v_1_6_2; + public static final ProtocolVersion v_1_6_3; + public static final ProtocolVersion v_1_6_4; public static final ProtocolVersion v1_7_1; public static final ProtocolVersion v1_7_6; public static final ProtocolVersion v1_8; @@ -17,11 +24,21 @@ public class ProtocolVersion { public static final ProtocolVersion v1_9_2; public static final ProtocolVersion v1_9_3; public static final ProtocolVersion v1_10; + public static final ProtocolVersion unknown; private final int id; private final String name; static { + // Before netty rewrite + register(v1_4_6 = new ProtocolVersion(51, "1.4.6")); + register(v1_5_1 = new ProtocolVersion(60, "1.5.1")); + register(v1_5_2 = new ProtocolVersion(61, "1.5.2")); + register(v_1_6_1 = new ProtocolVersion(73, "1.6.1")); + register(v_1_6_2 = new ProtocolVersion(74, "1.6.2")); + register(v_1_6_3 = new ProtocolVersion(77, "1.6.3")); + register(v_1_6_4 = new ProtocolVersion(78, "1.6.4")); + // After netty rewrite 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(47, "1.8.x")); @@ -30,6 +47,7 @@ public class ProtocolVersion { register(v1_9_2 = new ProtocolVersion(109, "1.9.2")); register(v1_9_3 = new ProtocolVersion(110, "1.9.3/4")); register(v1_10 = new ProtocolVersion(210, "1.10")); + register(unknown = new ProtocolVersion(-1, "UNKNOWN")); } public static void register(@NonNull ProtocolVersion protocol) { diff --git a/src/main/java/us/myles/ViaVersion/util/ProtocolSupportUtil.java b/src/main/java/us/myles/ViaVersion/util/ProtocolSupportUtil.java new file mode 100644 index 000000000..66458a0a5 --- /dev/null +++ b/src/main/java/us/myles/ViaVersion/util/ProtocolSupportUtil.java @@ -0,0 +1,33 @@ +package us.myles.ViaVersion.util; + +import org.bukkit.entity.Player; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +public class ProtocolSupportUtil { + private static Method protocolVersionMethod = null; + private static Method getIdMethod = null; + + static { + try { + protocolVersionMethod = Class.forName("protocolsupport.api.ProtocolSupportAPI").getMethod("getProtocolVersion", Player.class); + getIdMethod = Class.forName("protocolsupport.api.ProtocolVersion").getMethod("getId"); + } catch (Exception e) { + // ProtocolSupport not installed. + } + } + + public static int getProtocolVersion(Player player) { + if (protocolVersionMethod == null) return -1; + try { + Object version = protocolVersionMethod.invoke(null, player); + return (int) getIdMethod.invoke(version); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + return -1; + } +}