Send Player to Server on Login

Discussion in 'BungeeCord Plugin Development' started by CP02A, Aug 11, 2018.

  1. So I'm trying to make a plugin that detects the version a player joins with and then connects it to a specific server depending on it. What I have right now is this:
    Code (Java):

        int ver;

        @EventHandler
        public void onJoin(LoginEvent e) {
            System.out.println(e.getConnection().getVersion());
            ver = e.getConnection().getVersion();
        }
       
        @EventHandler
        public void onJoin(ServerConnectEvent e) {
            if(ver==316)
                e.getPlayer().connect((ServerInfo) ProxyServer.getInstance().getServerInfo("1.11.2"));
        }
     
    It detects the version just fine, but when it tries to send the player to the specific server, I get an Error Message:
    Code (YAML):

    18:11:35 [INFO] [/127.0.0.1:52451] <-> InitialHandler has connected
    18:11:36 [INFO] 316
    (The error gets sent a few times)
    18:11:36 [WARNING] Error dispatching event ServerConnectEvent(player=PlayerName, target=BungeeServerInfo(name=1.11.2, address=localhost/127.0.0.1:25567, restricted=false), cancelled=false, reason=PLUGIN) to listener [email protected]
    java.lang.StackOverflowError
        at java.base/java.lang.ClassLoader.defineClass1(Native Method)
        at java.base/java.lang.ClassLoader.defineClass(Unknown Source)
        at java.base/java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(Unknown Source)
        at java.base/jdk.internal.loader.BuiltinClassLoader.access$400(Unknown Source)
        at java.base/jdk.internal.loader.BuiltinClassLoader$4.run(Unknown Source)
        at java.base/jdk.internal.loader.BuiltinClassLoader$4.run(Unknown Source)
        at java.base/java.security.AccessController.doPrivileged(Native Method)
        at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(Unknown Source)
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(Unknown Source)
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source)
        at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
        at java.base/java.lang.ClassLoader.defineClass1(Native Method)
        at java.base/java.lang.ClassLoader.defineClass(Unknown Source)
        at java.base/java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(Unknown Source)
        at java.base/jdk.internal.loader.BuiltinClassLoader.access$400(Unknown Source)
        at java.base/jdk.internal.loader.BuiltinClassLoader$4.run(Unknown Source)
        at java.base/jdk.internal.loader.BuiltinClassLoader$4.run(Unknown Source)
        at java.base/java.security.AccessController.doPrivileged(Native Method)
        at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(Unknown Source)
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(Unknown Source)
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source)
        at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
        at com.google.gson.internal.LinkedTreeMap$EntrySet.iterator(LinkedTreeMap.java:566)
        at com.google.gson.internal.bind.TypeAdapters$29.write(TypeAdapters.java:774)
        at com.google.gson.internal.bind.TypeAdapters$29.write(TypeAdapters.java:714)
        at com.google.gson.internal.Streams.write(Streams.java:72)
        at com.google.gson.internal.bind.TreeTypeAdapter.write(TreeTypeAdapter.java:82)
        at com.google.gson.Gson.toJson(Gson.java:669)
        at com.google.gson.Gson.toJson(Gson.java:648)
        at com.google.gson.Gson.toJson(Gson.java:603)
        at com.google.gson.Gson.toJson(Gson.java:583)
        at net.md_5.bungee.chat.ComponentSerializer.toString(ComponentSerializer.java:61)
        at net.md_5.bungee.UserConnection.sendMessage(UserConnection.java:464)
        at net.md_5.bungee.UserConnection.sendMessage(UserConnection.java:438)
        at net.md_5.bungee.UserConnection.sendMessage(UserConnection.java:423)
        at net.md_5.bungee.UserConnection.connect(UserConnection.java:314)
        at net.md_5.bungee.UserConnection.connect(UserConnection.java:271)
        at net.md_5.bungee.UserConnection.connect(UserConnection.java:212)
        at net.md_5.bungee.UserConnection.connect(UserConnection.java:194)
        at com.github.cp02a.playercoordinator.PlayerLoginEvent.onJoin(PlayerLoginEvent.java:22)
        at jdk.internal.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    (at this point the next lines just repeat a couple thousand times)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.base/java.lang.reflect.Method.invoke(Unknown Source)
        at net.md_5.bungee.event.EventHandlerMethod.invoke(EventHandlerMethod.java:19)
        at net.md_5.bungee.event.EventBus.post(EventBus.java:46)
        at net.md_5.bungee.api.plugin.PluginManager.callEvent(PluginManager.java:377)
        at net.md_5.bungee.UserConnection.connect(UserConnection.java:281)
        at net.md_5.bungee.UserConnection.connect(UserConnection.java:271)
        at net.md_5.bungee.UserConnection.connect(UserConnection.java:212)
        at net.md_5.bungee.UserConnection.connect(UserConnection.java:194)
        at com.github.cp02a.playercoordinator.PlayerLoginEvent.onJoin(PlayerLoginEvent.java:22)
        at jdk.internal.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    18:11:36 [WARNING] Event ServerConnectEvent(player=PlayerName, target=BungeeServerInfo(name=1.11.2, address=localhost/127.0.0.1:25567, restricted=false), cancelled=false, reason=PLUGIN) took 356,120,974ns to process!
    18:11:36 [WARNING] Event ServerConnectEvent(player=PlayerName, target=BungeeServerInfo(name=1.12.2, address=localhost/127.0.0.1:25566, restricted=false), cancelled=false, reason=JOIN_PROXY) took 356,294,461ns to process!
    18:11:36 [INFO] [PlayerName] <-> ServerConnector [1.12.2] has connected
    18:11:36 [INFO] [PlayerName] disconnected with
    : Ā§cKicked whilst connecting to 1.12.2: Ā§fOutdated client! Please use 1.12.2
    18:11:36 [INFO] [PlayerName] <-> ServerConnector [1.12.2] has disconnected
     
    So I thought that maybe the player wasn't connected yet or something, so I tried another Event.
    Code (Java):

        int ver;

        @EventHandler
        public void onJoin(LoginEvent e) {
            System.out.println(e.getConnection().getVersion());
            ver = e.getConnection().getVersion();
        }
       
        @EventHandler
        public void onJoin(PostLoginEvent e) {
            if(ver==316)
                e.getPlayer().connect((ServerInfo) ProxyServer.getInstance().getServerInfo("1.11.2"));
        }
     
    Then I get no Error Message, but:
    Code (YAML):

    05:13:38 [INFO] [/127.0.0.1:58892] <-> InitialHandler has connected
    05:13:38 [INFO] 316
    05:13:39 [INFO] [PlayerName] <-> ServerConnector [1.11.2] has connected
    05:13:39 [INFO] [PlayerName] <-> ServerConnector [1.12.2] has connected
    05:13:39 [INFO] [PlayerName] disconnected with
    : Kicked whilst connecting to 1.12.2: Outdated client! Please use 1.12.2
    05:13:39 [INFO] [PlayerName] <-> ServerConnector [1.11.2] has disconnected
    05:13:39 [INFO] [PlayerName] <-> ServerConnector [1.12.2] has disconnected
    05:13:39 [INFO] [PlayerName] -> UpstreamBridge has disconnected
     
     
  2. Try a custom ReconnectHandler :)

    Code (Java):
    ReconnectHandler reconnectHandler = new MyReconnectHandler();
    getProxy().setReconnectHandler(reconnectHandler);
     
  3. I put this into the MyReconnectHandler class
    Code (Java):

    public class MyReconnectHandler implements ReconnectHandler {
        @Override
        public ServerInfo getServer(ProxiedPlayer player) {
            System.out.println(player.getPendingConnection().getVersion() + "");
            if(player.getPendingConnection().getVersion()==316)
                return (ServerInfo) ProxyServer.getInstance().getServerInfo("1.11.2");
            return (ServerInfo) ProxyServer.getInstance().getServerInfo("1.12.2");
        }

        @Override
        public void close() {
            // TODO Auto-generated method stub
         
        }

        @Override
        public void save() {
            // TODO Auto-generated method stub
         
        }

        @Override
        public void setServer(ProxiedPlayer player) {
            // TODO Auto-generated method stub
         
        }
    }
     
    And got this when I ran it:
    Code (Text):

    8:01:46 [INFO] [/127.0.0.1:50360] <-> InitialHandler has connected
    08:01:46 [INFO] 316
    08:01:46 [INFO] [PlayerName] <-> ServerConnector [1.11.2] has connected
    08:01:46 [INFO] [PlayerName] <-> ServerConnector [1.12.2] has connected
    08:01:46 [INFO] [PlayerName] <-> ServerConnector [1.11.2] has disconnected
    08:01:46 [INFO] [PlayerName] disconnected with: Kicked whilst connecting to 1.12.2: Outdated client! Please use 1.12.2
    08:01:46 [INFO] [PlayerName] <-> ServerConnector [1.12.2] has disconnected
    08:01:47 [INFO] [PlayerName] -> UpstreamBridge has disconnected
     

    I also deleted the old code
     
    #3 CP02A, Aug 11, 2018
    Last edited: Aug 11, 2018
  4. Is there a reason you decided against ViaVersion / ViaBackwards?
     

Share This Page