From ad3465f0d3ab9e296d772ff81d8cda442bb46992 Mon Sep 17 00:00:00 2001 From: creeper123123321 Date: Mon, 26 Nov 2018 15:58:17 -0200 Subject: [PATCH] Fix Velocity platform breaking changes --- .../handlers/VelocityChannelInitializer.java | 10 ++-- .../handlers/VelocityServerHandler.java | 15 ++++-- .../platform/VelocityViaInjector.java | 4 +- .../providers/VelocityVersionProvider.java | 48 +++++-------------- 4 files changed, 27 insertions(+), 50 deletions(-) diff --git a/velocity/src/main/java/us/myles/ViaVersion/velocity/handlers/VelocityChannelInitializer.java b/velocity/src/main/java/us/myles/ViaVersion/velocity/handlers/VelocityChannelInitializer.java index 306bcf667..056021abf 100644 --- a/velocity/src/main/java/us/myles/ViaVersion/velocity/handlers/VelocityChannelInitializer.java +++ b/velocity/src/main/java/us/myles/ViaVersion/velocity/handlers/VelocityChannelInitializer.java @@ -2,20 +2,18 @@ package us.myles.ViaVersion.velocity.handlers; import io.netty.channel.Channel; import io.netty.channel.ChannelInitializer; -import lombok.NonNull; -import lombok.RequiredArgsConstructor; +import lombok.AllArgsConstructor; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.protocol.ProtocolPipeline; import java.lang.reflect.Method; -@RequiredArgsConstructor +@AllArgsConstructor public class VelocityChannelInitializer extends ChannelInitializer { - @NonNull private ChannelInitializer original; - private Method initChannel; + private static Method initChannel; - { + static { try { initChannel = ChannelInitializer.class.getDeclaredMethod("initChannel", Channel.class); initChannel.setAccessible(true); diff --git a/velocity/src/main/java/us/myles/ViaVersion/velocity/handlers/VelocityServerHandler.java b/velocity/src/main/java/us/myles/ViaVersion/velocity/handlers/VelocityServerHandler.java index 51bb600d1..0a2f6074c 100644 --- a/velocity/src/main/java/us/myles/ViaVersion/velocity/handlers/VelocityServerHandler.java +++ b/velocity/src/main/java/us/myles/ViaVersion/velocity/handlers/VelocityServerHandler.java @@ -4,6 +4,7 @@ import com.velocitypowered.api.event.PostOrder; import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.event.player.ServerConnectedEvent; import com.velocitypowered.api.event.player.ServerPreConnectEvent; +import com.velocitypowered.api.network.ProtocolVersion; import us.myles.ViaVersion.api.Pair; import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.boss.BossBar; @@ -28,8 +29,10 @@ public class VelocityServerHandler { static { try { - setProtocolVersion = Class.forName("com.velocitypowered.proxy.connection.MinecraftConnection").getDeclaredMethod("setProtocolVersion", int.class); - setNextProtocolVersion = Class.forName("com.velocitypowered.proxy.connection.MinecraftConnection").getDeclaredMethod("setNextProtocolVersion", int.class); + setProtocolVersion = Class.forName("com.velocitypowered.proxy.connection.MinecraftConnection") + .getDeclaredMethod("setProtocolVersion", ProtocolVersion.class); + setNextProtocolVersion = Class.forName("com.velocitypowered.proxy.connection.MinecraftConnection") + .getDeclaredMethod("setNextProtocolVersion", ProtocolVersion.class); } catch (NoSuchMethodException | ClassNotFoundException e) { e.printStackTrace(); } @@ -49,7 +52,9 @@ public class VelocityServerHandler { // Check if ViaVersion can support that version Object connection = ReflectionUtil.invoke(e.getPlayer(), "getConnection"); - setNextProtocolVersion.invoke(connection, protocols == null ? user.get(ProtocolInfo.class).getProtocolVersion() : protocolId); + setNextProtocolVersion.invoke(connection, ProtocolVersion.getProtocolVersion(protocols == null + ? user.get(ProtocolInfo.class).getProtocolVersion() + : protocolId)); } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e1) { e1.printStackTrace(); @@ -128,8 +133,8 @@ public class VelocityServerHandler { } Object connection = ReflectionUtil.invoke(e.getPlayer(), "getConnection"); - int version = (int) ReflectionUtil.invoke(connection,"getNextProtocolVersion"); - setProtocolVersion.invoke(ReflectionUtil.invoke(e.getPlayer(), "getConnection"), version); + ProtocolVersion version = (ProtocolVersion) ReflectionUtil.invoke(connection,"getNextProtocolVersion"); + setProtocolVersion.invoke(connection, version); } } user.getVelocityLock().writeLock().unlock(); diff --git a/velocity/src/main/java/us/myles/ViaVersion/velocity/platform/VelocityViaInjector.java b/velocity/src/main/java/us/myles/ViaVersion/velocity/platform/VelocityViaInjector.java index 4673d23c4..daa63940d 100644 --- a/velocity/src/main/java/us/myles/ViaVersion/velocity/platform/VelocityViaInjector.java +++ b/velocity/src/main/java/us/myles/ViaVersion/velocity/platform/VelocityViaInjector.java @@ -29,8 +29,8 @@ public class VelocityViaInjector implements ViaInjector { return getLowestSupportedProtocolVersion(); } - public static int getLowestSupportedProtocolVersion() throws Exception { - return ReflectionUtil.getStatic(Class.forName("com.velocitypowered.proxy.protocol.ProtocolConstants"), "MINIMUM_GENERIC_VERSION", int.class); + public static int getLowestSupportedProtocolVersion() { + return com.velocitypowered.api.network.ProtocolVersion.MINIMUM_VERSION.getProtocol(); } @Override diff --git a/velocity/src/main/java/us/myles/ViaVersion/velocity/providers/VelocityVersionProvider.java b/velocity/src/main/java/us/myles/ViaVersion/velocity/providers/VelocityVersionProvider.java index 30fd4add9..3b05ce5f2 100644 --- a/velocity/src/main/java/us/myles/ViaVersion/velocity/providers/VelocityVersionProvider.java +++ b/velocity/src/main/java/us/myles/ViaVersion/velocity/providers/VelocityVersionProvider.java @@ -6,42 +6,30 @@ import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.protocol.ProtocolVersion; import us.myles.ViaVersion.protocols.base.ProtocolInfo; import us.myles.ViaVersion.protocols.base.VersionProvider; -import us.myles.ViaVersion.util.ReflectionUtil; +import us.myles.ViaVersion.velocity.platform.VelocityViaInjector; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class VelocityVersionProvider extends VersionProvider { - private static Class ref ; - - static { - try { - ref = Class.forName("com.velocitypowered.proxy.protocol.ProtocolConstants"); - } catch (Exception e) { - Via.getPlatform().getLogger().severe("Could not detect the ProtocolConstants class"); - e.printStackTrace(); - } - } @Override public int getServerProtocol(UserConnection user) throws Exception { - if (ref == null) - return super.getServerProtocol(user); // TODO Have one constant list forever until restart? (Might limit plugins if they change this) - Object list = ReflectionUtil.getStatic(ref, "SUPPORTED_VERSIONS", Object.class); - List sorted = new ArrayList((List) ReflectionUtil.invoke(list, "asList")); + List sorted = new ArrayList<>(com.velocitypowered.api.network.ProtocolVersion.ID_TO_PROTOCOL_CONSTANT.keySet()); + sorted.remove(Integer.valueOf(-1)); // Unknown/legacy Collections.sort(sorted); - ProtocolInfo info = user.get(ProtocolInfo.class); + int playerVersion = user.get(ProtocolInfo.class).getProtocolVersion(); // Bungee supports it - if (sorted.contains(info.getProtocolVersion())) - return info.getProtocolVersion(); + if (sorted.contains(playerVersion)) + return playerVersion; // Older than bungee supports, get the lowest version - if (info.getProtocolVersion() < sorted.get(0)) { - return getLowestSupportedVersion(); + if (playerVersion < sorted.get(0)) { + return VelocityViaInjector.getLowestSupportedProtocolVersion(); } // Loop through all protocols to get the closest protocol id that bungee supports (and that viaversion does too) @@ -49,25 +37,11 @@ public class VelocityVersionProvider extends VersionProvider { // TODO: This needs a better fix, i.e checking ProtocolRegistry to see if it would work. // This is more of a workaround for snapshot support by bungee. for (Integer protocol : Lists.reverse(sorted)) { - if (info.getProtocolVersion() > protocol && ProtocolVersion.isRegistered(protocol)) + if (playerVersion > protocol && ProtocolVersion.isRegistered(protocol)) return protocol; } - Via.getPlatform().getLogger().severe("Panic, no protocol id found for " + info.getProtocolVersion()); - return info.getProtocolVersion(); - } - - public static int getLowestSupportedVersion() { - List list; - try { - return ReflectionUtil.getStatic( - Class.forName("com.velocitypowered.proxy.protocol.ProtocolConstants"), - "MINIMUM_GENERIC_VERSION", - int.class); - } catch (NoSuchFieldException | IllegalAccessException | ClassNotFoundException e) { - e.printStackTrace(); - } - // Fallback - return -1; + Via.getPlatform().getLogger().severe("Panic, no protocol id found for " + playerVersion); + return playerVersion; } }