From 8445a6d9dbc4dd719f2abe979a76d0c30184c7b5 Mon Sep 17 00:00:00 2001 From: Matsv Date: Sat, 1 Oct 2016 14:27:39 +0200 Subject: [PATCH] Fix wrong handling if the client and child server have the same protocol id --- .../us/myles/ViaVersion/BungeePlugin.java | 19 +++++---- .../bungee/handlers/BungeeEncodeHandler.java | 39 +++++++++++-------- .../service/ProtocolDetectorService.java | 21 ++++++---- 3 files changed, 45 insertions(+), 34 deletions(-) diff --git a/bungee/src/main/java/us/myles/ViaVersion/BungeePlugin.java b/bungee/src/main/java/us/myles/ViaVersion/BungeePlugin.java index 2dd9f6470..f6cd0e614 100644 --- a/bungee/src/main/java/us/myles/ViaVersion/BungeePlugin.java +++ b/bungee/src/main/java/us/myles/ViaVersion/BungeePlugin.java @@ -174,6 +174,7 @@ public class BungeePlugin extends Plugin implements ViaPlatform, Listener { public void onServerConnect(ServerConnectEvent e) throws NoSuchFieldException, IllegalAccessException { UserConnection user = Via.getManager().getConnection(e.getPlayer().getUniqueId()); if (!user.has(BungeeStorage.class)) { + System.out.println("new storage"); user.put(new BungeeStorage(user, e.getPlayer())); } @@ -181,16 +182,14 @@ public class BungeePlugin extends Plugin implements ViaPlatform, Listener { List> protocols = ProtocolRegistry.getProtocolPath(user.get(ProtocolInfo.class).getProtocolVersion(), protocolId); // Check if ViaVersion can support that version - if (protocols != null) { - 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); - System.out.println("Changed server protocol id " + protocolId + " clientProtocol:" + user.get(ProtocolInfo.class).getProtocolVersion() + " path:" + protocols); - } catch (NoSuchMethodException | InvocationTargetException e1) { - e1.printStackTrace(); - } + 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, protocols == null ? user.get(ProtocolInfo.class).getProtocolVersion() : protocolId); + System.out.println("Changed server protocol id " + protocolId + " clientProtocol:" + user.get(ProtocolInfo.class).getProtocolVersion() + " path:" + protocols); + } catch (NoSuchMethodException | 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 a91f52c17..a4bbd4df7 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 @@ -99,7 +99,7 @@ public class BungeeEncodeHandler extends MessageToMessageEncoder { if (player.getServer() != null) { if (player.getServer() != null && !player.getServer().getInfo().getName().equals(storage.getCurrentServer())) { - System.out.println("Server change " + player.getServer().getInfo().getName()); + System.out.println("Server change " + player.getServer().getInfo().getName() + " curr" + storage.getCurrentServer()); String serverName = player.getServer().getInfo().getName(); storage.setCurrentServer(serverName); @@ -118,31 +118,36 @@ public class BungeeEncodeHandler extends MessageToMessageEncoder { List> protocols = ProtocolRegistry.getProtocolPath(info.getProtocolVersion(), protocolId); System.out.println(info.getProtocolVersion() + ">" + protocolId + " " + protocols); ProtocolPipeline pipeline = viaConnection.get(ProtocolInfo.class).getPipeline(); - if (protocols != null) { - viaConnection.clearStoredObjects(); - pipeline.cleanPipes(); + viaConnection.clearStoredObjects(); + pipeline.cleanPipes(); + + if (protocols != null) for (Pair prot : protocols) { pipeline.add(prot.getValue()); } - viaConnection.put(info); - viaConnection.setActive(true); - - // Init all protocols TODO check if this can get moved up to the previous for loop, and doesn't require the pipeline to already exist. - for (Pair protocol : protocols) { - protocol.getValue().init(viaConnection); - } - Object wrapper = ReflectionUtil.get(player, "ch", Object.class); - wrapper.getClass().getDeclaredMethod("setVersion", int.class).invoke(wrapper, protocolId); - ReflectionUtil.invoke(player, "init"); - } else { - viaConnection.setActive(false); + viaConnection.put(info); + viaConnection.put(storage); + + viaConnection.setActive(protocols != null); + + // Init all protocols TODO check if this can get moved up to the previous for loop, and doesn't require the pipeline to already exist. + for (Protocol protocol : pipeline.pipes()) { + protocol.init(viaConnection); } + + Object wrapper = ReflectionUtil.get(player, "ch", Object.class); + wrapper.getClass().getDeclaredMethod("setVersion", int.class).invoke(wrapper, protocolId); +// ReflectionUtil.invoke(player, "init"); + + Object entityMap = Class.forName("net.md_5.bungee.entitymap.EntityMap").getDeclaredMethod("getEntityMap", int.class).invoke(null, protocolId); + ReflectionUtil.set(player, "entityRewrite", entityMap); + + System.out.println("VERSION:" + ((net.md_5.bungee.UserConnection) player).getPendingConnection().getVersion()); } } } } - } diff --git a/bungee/src/main/java/us/myles/ViaVersion/bungee/service/ProtocolDetectorService.java b/bungee/src/main/java/us/myles/ViaVersion/bungee/service/ProtocolDetectorService.java index ddfba7505..d8113b12f 100644 --- a/bungee/src/main/java/us/myles/ViaVersion/bungee/service/ProtocolDetectorService.java +++ b/bungee/src/main/java/us/myles/ViaVersion/bungee/service/ProtocolDetectorService.java @@ -29,13 +29,20 @@ public class ProtocolDetectorService implements Runnable { @Override public void run() { for (final Map.Entry lists : plugin.getProxy().getServers().entrySet()) { - lists.getValue().ping(new Callback() { - @Override - public void done(ServerPing serverPing, Throwable throwable) { - if (throwable == null) - protocolIds.put(lists.getKey(), serverPing.getVersion().getProtocol()); - } - }); + updateProtocolInfo(lists.getKey(), lists.getValue()); } } + + private void updateProtocolInfo(final String key, ServerInfo value) { + value.ping(new Callback() { + @Override + public void done(ServerPing serverPing, Throwable throwable) { + if (throwable == null) + protocolIds.put(key, serverPing.getVersion().getProtocol()); + else + throwable.printStackTrace(); + } + }); + } + }