From ec848d7201394f52a6dd34eb4c4c09cb7fc41bce Mon Sep 17 00:00:00 2001 From: Myles Date: Sat, 1 Oct 2016 00:10:54 +0100 Subject: [PATCH] Reflection for Bungee stuffs --- .../us/myles/ViaVersion/BungeePlugin.java | 22 ++++++++++++++----- .../bungee/handlers/BungeeEncodeHandler.java | 11 ++++------ 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/bungee/src/main/java/us/myles/ViaVersion/BungeePlugin.java b/bungee/src/main/java/us/myles/ViaVersion/BungeePlugin.java index 0872f8932..1a95df2a2 100644 --- a/bungee/src/main/java/us/myles/ViaVersion/BungeePlugin.java +++ b/bungee/src/main/java/us/myles/ViaVersion/BungeePlugin.java @@ -1,7 +1,6 @@ package us.myles.ViaVersion; import com.google.gson.JsonObject; -import net.md_5.bungee.UserConnection; import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.api.connection.ProxiedPlayer; @@ -15,6 +14,7 @@ import us.myles.ViaVersion.api.ViaAPI; import us.myles.ViaVersion.api.ViaVersionConfig; import us.myles.ViaVersion.api.command.ViaCommandSender; import us.myles.ViaVersion.api.configuration.ConfigurationProvider; +import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.platform.TaskId; import us.myles.ViaVersion.api.platform.ViaPlatform; import us.myles.ViaVersion.bungee.commands.BungeeCommand; @@ -25,7 +25,10 @@ import us.myles.ViaVersion.bungee.service.ProtocolDetectorService; import us.myles.ViaVersion.bungee.storage.BungeeStorage; import us.myles.ViaVersion.dump.PluginInfo; import us.myles.ViaVersion.util.GsonUtil; +import us.myles.ViaVersion.util.ReflectionUtil; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -153,7 +156,6 @@ public class BungeePlugin extends Plugin implements ViaPlatform, Listener { plugins.add(new PluginInfo(true, p.getDescription().getName(), p.getDescription().getVersion(), p.getDescription().getMain(), Arrays.asList(p.getDescription().getAuthor()))); platformSpecific.add("plugins", GsonUtil.getGson().toJsonTree(plugins)); - // TODO more? ProtocolLib things etc? return platformSpecific; } @@ -163,17 +165,25 @@ public class BungeePlugin extends Plugin implements ViaPlatform, Listener { Via.getManager().removePortedClient(e.getPlayer().getUniqueId()); } - // Set the handshake version every time someone connects to any server TODO reflection + // Set the handshake version every time someone connects to any server @EventHandler public void onServerConnect(ServerConnectEvent e) throws NoSuchFieldException, IllegalAccessException { - us.myles.ViaVersion.api.data.UserConnection user = Via.getManager().getConnection(e.getPlayer().getUniqueId()); + UserConnection user = Via.getManager().getConnection(e.getPlayer().getUniqueId()); if (!user.has(BungeeStorage.class)) { user.put(new BungeeStorage(user, e.getPlayer())); } int protocolId = ProtocolDetectorService.getProtocolId(e.getTarget().getName()); - UserConnection connection = (UserConnection) e.getPlayer(); - connection.getPendingConnection().getHandshake().setProtocolVersion(protocolId); + try { + Object pendingConnection = ReflectionUtil.invoke(e.getPlayer(), "getPendingConnection"); + Object handshake = ReflectionUtil.invoke(pendingConnection, "getHandshake"); + Method setProtocol = handshake.getClass().getDeclaredMethod("setProtocolVersion", int.class); + setProtocol.invoke(handshake, protocolId); + } catch (NoSuchMethodException e1) { + e1.printStackTrace(); + } catch (InvocationTargetException e1) { + e1.printStackTrace(); + } } } diff --git a/bungee/src/main/java/us/myles/ViaVersion/bungee/handlers/BungeeEncodeHandler.java b/bungee/src/main/java/us/myles/ViaVersion/bungee/handlers/BungeeEncodeHandler.java index 08bea9bda..157e196a2 100644 --- a/bungee/src/main/java/us/myles/ViaVersion/bungee/handlers/BungeeEncodeHandler.java +++ b/bungee/src/main/java/us/myles/ViaVersion/bungee/handlers/BungeeEncodeHandler.java @@ -5,7 +5,6 @@ import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.MessageToMessageEncoder; import net.md_5.bungee.api.connection.ProxiedPlayer; -import net.md_5.bungee.netty.ChannelWrapper; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.Pair; import us.myles.ViaVersion.api.Via; @@ -92,8 +91,7 @@ public class BungeeEncodeHandler extends MessageToMessageEncoder { super.exceptionCaught(ctx, cause); } - // TODO reflection - public void checkServerChange() throws NoSuchFieldException, IllegalAccessException { + public void checkServerChange() throws Exception { if (info.has(BungeeStorage.class)) { BungeeStorage storage = info.get(BungeeStorage.class); ProxiedPlayer player = storage.getPlayer(); @@ -111,10 +109,9 @@ public class BungeeEncodeHandler extends MessageToMessageEncoder { } int protocolId = ProtocolDetectorService.getProtocolId(serverName); - net.md_5.bungee.UserConnection connection = (net.md_5.bungee.UserConnection) player; - ChannelWrapper wrapper = ReflectionUtil.get(connection, "ch", ChannelWrapper.class); - wrapper.setVersion(protocolId); + Object wrapper = ReflectionUtil.get(player, "ch", Object.class); + wrapper.getClass().getDeclaredMethod("setVersion", int.class).invoke(wrapper, protocolId); us.myles.ViaVersion.api.data.UserConnection viaConnection = Via.getManager().getConnection(player.getUniqueId()); ProtocolInfo info = viaConnection.get(ProtocolInfo.class); @@ -127,7 +124,7 @@ public class BungeeEncodeHandler extends MessageToMessageEncoder { pipeline.add(prot.getValue()); } } - connection.init(); + ReflectionUtil.invoke(player, "init"); } } }